Dark Horse Programmer--c Language Foundation (i)

Source: Internet
Author: User
Tags arithmetic operators

a simple C program

Preprocessing directives: Directives executed before compilation, usually beginning with # .

#include: The system comes with the file with <>, write their own files with "" .

. h called a header file, is used to declare a number of commonly used functions, if you want to use these functions, you must include this header file.

Main function: The entry point of the whole C program, a C program has only one main function,C program main function does not write the return value type does not error, the default return int type.

1 #include <stdio.h>23main ()4 {}
steps to develop and run a C program

Write a program--> compile--> link ( target code & C- language function library)- -> Generate executable machine code-- > running programs

Compile: translate the source program of C into the binary form of the computer can recognize the target file code, by the C compiler. The compilation succeeds in generating the target file with the same file name as the source program file with the extension ". obj". If there are multiple C source files in a project , a successful compilation generates multiple . obj target files.

Link: A process that combines all of the associated . obj target files with the system-provided C Library functions to generate the executable file, called a " link."

functionClassification of functions

1. main function (main function).

2. developer-defined functions

3. Library functions provided by C language.

Declaration and definition of functions

Because the C language is compiled from the top down, it is necessary to make a declaration before the main function when calling the function. A function declaration can be placed in an . h file, and the function definition is placed in a . C file.

Formal parameters and arguments for a function

When a function is defined, the variable defined in () after the function name becomes a formal parameter (formal argument), and the value passed in when the function is called is called an argument (the actual parameter).

Note: Can I #include. C file?

No. Because . c files are defined functions,#include. C files have the function of repeating the declaration of the problem, resulting in the link phase error.

Data type

empty type :void

Basic Data type :int(shaping),float(single-precision float),double(double-precision float),Char(character)

constructed type : array,struct(struct),Union(Common),enum(enum)

Pointer type

Variable

The value that is used during the storage calculation must first be defined and then used .

The variable type is defined to allocate the appropriate storage space for the variable to hold the data.

In C , when a local variable is declared, it can be used without initializing the assignment. However, this is not recommended because the system will assign a value to the variable, resulting in a garbage data. The variables should be initialized and reused before they are used.

Type modifier

Short

Short type

Long

Long type

Signed

Signed type

Unsigned

Non-symbolic

1. Short and long can provide different lengths of shaping number, can change the value range of shaping number,short value range is -32768~32767, Long is the value range is -2147483648~2147483647.

2. The storage length of the data will change as well.

3. In different compiler environments, the range of values and the length of occupancy is not the same,ANSI \ ISO has the following rules:

  • Short and int at least (2 bytes)
  • < Span lang= "en-US" > < Span lang= "en-US" > Long at least 32 bit (4 bytes)
  • < Span lang= "en-US" > < Span lang= "en-US" > short cannot be longer than Span lang= "en-US" >int,int cannot be longer than Long
  • < Span lang= "en-US" > < Span lang= "en-US" > < Span lang= "ZH-CN" > Char must be 8 bit (1 bytes)
Basic statements and Operations basic statements

Loop Statements : Do While , while, for

conditional Statements :if,if-else,switch

Goto Statement

Basic Operations

Arithmetic operators: +,-,*,/,%

Relational operators: <,<=,>,>=,= =,!=

  • < Span lang= "ZH-CN" > The result of the relational operation is " True " return 1, false "0
  • In C , any non- 0 value represents true , and only 0 is false.

logical operators: &&,| | ,!

Assignment operators: =, + =,-=,*=,/=,%=

The comma operator and the comma expression: An expression concatenated with a comma operator is called a comma expression; the value of the entire comma expression is the value of the last expression.

conditional operator and conditional expression (trinocular operator): expression 1 ? expression 2 : expression 3

sizeof : used to calculate the number of bytes of memory for a variable or a constant, a data type.

Attention:

1 sizeof ('A');

The size is then printed out as 4 bytes, because ' A ' will be converted to int and then taken to take up the number of bytes. Should be changed to:

1 Char ' A ' ; 2 sizeof (d);
Array address
    1. The memory in the computer is the storage space in bytes, and each byte in memory has a unique number. This number is called an address. Every program and data stored in memory has an address, that is, a function has its own memory address.
    2. When a variable is defined, the system allocates a storage unit with a unique address to store the variable. The address of the first byte of a variable storage unit is the address of the variable.
One-dimensional arrays
  1. Definition of one-dimensional array: type array name [ number of elements ].
    int a[];
  2. [] can only be placed after the name of the array.
  3. [] The number must be a fixed value, can be a constant, constant expression. You cannot use variables or variable expressions to represent the number of elements .
  4. When you define an array, the system follows the array type and numberallocate a contiguous amount of storage spaceto store array elements.
     1  //  2  int  a[5  ];  3   The first way:  4  printf ( " %d  , &a[0  ] ); 5   Second way:  6  printf ( " %d  , a); 

    The array name represents the address of the entire array, which is the starting address of the array, which is the address of the first 0 elements.

One-dimensional arrays and function parameters
      • The element of a one-dimensional array is a one-way value pass as a function argument, as a simple variable of the same type as an argument. The change of formal parameters does not affect the actual arguments.
      • If the name of a one-dimensional array is a function argument, the entire array is passed, that is, the shape parameter group is exactly the same as the real parameter group, pointing to the same piece of memory space. When modifying a parameter group, the real parameter group is also changed. The number of elements in the shape parameter group can be omitted.
Two-dimensional arrays
    1. Definition of a two-dimensional array: type array name [ number of rows ] [ number of columns ]
      1 int a[2[3];
    2. Storage of two-dimensional arrays:
    • You can think of a two-dimensional array as a collection of one- dimensional arrays, which means that two-dimensional arrays are a special one-dimensional array: its elements are one-dimensional arrays.
    • The order of the two-dimensional arrays is stored in the order of the rows.
      1 //View the address of a two-dimensional array2 inta[2][3] ;3 //The first way:4printf"%d", &a[0][0] );5 //The second way:6 //A[0] is also a group, so A[0] is the name of the array, which represents the address of the array.7printf"%d", a[0] );8 //The Third Way:9printf"%d", a);

Format character

%.2f

Keep Two decimal places

%4d

space that occupies 4 spaces

&

Take address character

Dark Horse Programmer--c Language Foundation (i)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.