Step into the first day of C Programming, step into the first day of C Programming
I want to develop a website after learning ruby, but ruby is a high-level language that hides many underlying things, so I am familiar with the C language first.
First, the file name of the c program ends with. c
C program format:
The first line # include <stdio. h>
# Is a pre-processing standard used to pre-process text, indicating that the line of code must be processed first and run before compiling the code
Include is a command that reads <> files
Stdio. h indicates the standard input/output header file (the header file is like a directory of a book, which can be easily checked by the reader.
After reading the required content, stdio. h declares the standard input/output function. If you want to use the standard input/output function, add this header.
File
The first C Language Program:
# Include <stdio. h>
Int main ()
{
Printf ("I like c language ");
Return 0;
}
The first line serves to stdio the header file before the compiler compiles. all content in h reads the first line of the current program, due to stdio. h declares all input and output functions, so the printf function can be called directly below.
Main () represents a function. The main function is translated into Chinese. In each c program, the primary function is compiled first. () is in a fixed format, and {} is also in a fixed format, the part is called a function block or function body.
The returned type must be specified in each program. int indicates that an integer is returned to the operating system after the program is executed.
Mian functions are essentially different from other functions: Generally, functions are called or activated by other functions, such as printf functions.
Is activated by the mian function, but the main function is automatically executed when the program starts.
Printf is the abbreviation of print format. I like c language is the information that the main function passes to the printf function. After receiving this information, the printf function outputs this information to the screen.
Return 0 indicates that the mian function returns a value of 0 to the operating system. Generally, after the function is executed, an execution result is returned. 0 indicates that the function is normal. If the value is not 0, an exception is returned.
Notes in C:
// Single line comment
/*... */Multi-line comment
Statement Terminator; as long as the row contains; it indicates that the statement ends. For example, the space in the statement is ignored.
For example, x = a + B; or x = a + B; are the same. We recommend that you use the first one, that is, assign the value of a + B to x.
# Include <stdio. h>
Int main ()
{
{
Printf ("I like c language ");
Printf ("I like c language ");
Return 0;
}
}
The {} contains a block, and the {} contains a function block. The {} block contains multiple statements (two printf statements ), exactly a compound statement
Expression: x = c = a + B Means to assign the value of a + B to c and then assign the value to x
# Include <stdio. h>
Int main ()
{
Int a = 1, B = 2, c = 3, x = 0;
X = c = a + B;
Printf ("% d", x );
Return 0;
}
% D indicates that the output is an integer corresponding to the following x. Run this program to get 3 results.
OPERATOR:
# Include <stdio. h>
Int main ()
{
Printf ("% d", 5% 2 );
Return 0;
}
5% 2 represents the modulo, that is, dividing 5 by 2 and taking the remainder of the modulo. Put the value in the previous "" and output it.
Computing priority:
Printf ("% d", 1 + 1*2 + 3 );
The preceding operation sequence: 1X2 + 1 + 3,
Printf ("% d", (1 + 1) * (2 + 3 ));
The preceding operation sequence is as follows: (1 + 1) the value is multiplied by (2 + 3) the value, which is the same as that used for primary calculation.
= Value assignment operator in C Language
# Include <stdio. h>
Int main ()
{
Int x = 0;
X + = 1;
Printf ("% d", x );
Return 0;
}
X + = 1: Add 1 to the value of x, and then assign this value to x. The opposite is x-= 1.
Similarly, x * = 2, x/= 2, x % = 2, x % = 1 + 2 (first evaluate the value of 1 + 2)
Auto-increment: x ++ adds the value of x to 1.
Auto-subtract: x -- subtract 1 from x
# Include <stdio. h>
Int main ()
{
Int x = 2;
Printf ("% d", x --);
Return 0;
}
The above program will first output the value of x to the screen, and then subtract 1. If you want to get the result after the subtraction of 1, you can use the following method
# Include <stdio. h>
Int main ()
{
Int x = 2;
Printf ("% d \ n", x --);
Printf ("% d \ n", x );
Return 0;
}
\ N indicates line feed
The above program can also be written as follows:
# Include <stdio. h>
Int main ()
{
Int x = 2;
X --;
Printf ("% d", x );
Return 0;
}
It seems a bit wrong to say so much. How can I execute the above program files? That's what I think.
In linux, I use vim to edit files. For example, the command for compiling a file by using vim 1.c gcc: gcc 1.c.. the out file executes this file :. /. out
You can also use gcc 1.c-o hello to customize the file name.