# include & ltstdio.h & gt
int main()
{
printf("Hello World!" );
Returns 0;
}
2. Data type
Data type:
1. Basic data type:
1. 1. Integer: int 4 bytes.
1.2. Character type: char 1 byte.
1.3. Real number type (floating point type)
1.3. 1. Single precision type: floating point 4 bytes.
1.3.2.Double type: double 8 bytes.
2. Structure type:
2. 1. enumeration type
2.2. Array types
2.3. Structure type
2.4.*** User Type
3. Pointer type:
4. Empty type:
3. Print format statements
%d: decimal integer;
%c: single character;
%s: string;
%f:6 decimal places;
# include & ltstdio.h & gt
int main()
{
int age = 18;
Floating height =1.85;
char unit = ' m
Printf ("Xiaoming is %d years old this year \n", age);
Printf ("Xiao Ming's height %f%c\n", height, unit);
Printf ("Xiaoming learns IT technology online in massive open network courses \ n");
Returns 0;
}
Learning C++ well is a stepping stone to enter a big factory! If I had this class, my C++ wouldn't be like this.
Has it expired?
4. Constant
The quantity whose value is constant becomes a constant;
Define a character constant (note that there is no following; )
# include & ltstdio.h & gt
#define POCKETMONEY 10 // defines constants and constant values.
int main()
{
Printf ("Xiaoming got %d pocket money again today \n", Pocketmoney.
Returns 0;
}
5. Operators
5. 1. Arithmetic operators:+,-,*,/,%,++,-; Before++/-,calculate first, then take the value. ++/,value first and then operation;
5.2. Assignment operator:
5.3. Relational operators;
5.4. Logical operators;
5.5. Tripartite operators:
The expression 1? Expression 2: Expression 3;
6. Calculation of the number of daffodils
Output all three-digit daffodil numbers.
The so-called "daffodil number" refers to a three-digit number, and the cubic sum of its digits is equal to this number. For example, 153 is the daffodil number,11+555+333.
# include & ltstdio.h & gt
int main()
{
//Define three digits num, one digit sd, ten digits td and one hundred digits hd.
int num,sd,td,HD;
//Cycle through all three numbers
for(num = 100; num & lt 1000 ; num++)
{
//Get the hundredth digit of the three-digit num
HD = num/ 100;
//Get the tenth digit of the three-digit num.
TD = num/ 10% 10;
//get the number on num bit of three digits.
SD = num % 10;
//What are the conditions for the number of daffodils?
if(num = = HD * HD * HD+TD * TD * TD+SD * SD * SD)
{
Printf ("daffodil number: %d\n", num);
}
}
Returns 0;
}
7. Print regular triangle *
# include & ltstdio.h & gt
int main()
{
int i,j,k;
for(I = 1; I<5; i++)
{
/* Observe the number of spaces in each line and complete the loop condition */
for(j = I; j & lt5 ; j++)
{
printf(" "); //output spaces
}
/* Observe the number of * in each line and complete the loop condition */
for(k = 0; k & lt2 * I- 1; k++)
{
printf(" * "); //The * sign for each line of output
}
printf(" \ n "); //Line breaks in the loop every time
}
Returns 0;
}
8. the infamous goto statement
Rarely used
# include & ltstdio.h & gt
int main()
{
int sum = 0;
int I;
for(I = 1; I<= 10; i++)
{
printf("%d\n ",I);
if(i==3){
Go to loop; //Execute the goto statement when the condition is met.
}
}
//execute goto
Loop: printf ("End of for loop ..."); //Please select the appropriate location to add the identifier.
Returns 0;
}
9. Formal participation of real parameters
Formal parameters: formal parameters are parameters used when defining the function name and body, and are used to receive the parameters passed in when calling the function;
Argument: Argument is the parameter of the function passed when calling.
The formal parameters and arguments of a function have the following characteristics:
Parameter allocates memory cells only when called, and releases the allocated memory cells at the end of the call. Therefore, formal parameters are only valid inside the function. After the function call ends and returns to the tonic function, the parameter variable can no longer be used.
Parameters can be constants, variables, expressions, functions, etc. No matter what kind of quantity, there must be definite values when making function calls, so that these values can be passed to formal parameters. Therefore, it is necessary to use assignment and other methods in advance to get the definite value of real parameters.
When passing parameters, the number, type and order of actual parameters and formal parameters should be strictly consistent, otherwise there will be an error of type mismatch.
10. Comments on function return value
Note: void functions can have execution code blocks, but they cannot have return values. In addition, if there is a return statement in the void function, you can only end the function. Its format is: return;
1 1. recursion
# include & ltstdio.h & gt
Int getPeachNumber(int n) // Define n here, otherwise the compiler will report an error!
{
int num
If (n== 10)
{
Returns1;
}
other
{
num =(getPeachNumber(n+ 1)+ 1)* 2;
Printf ("%d peaches remaining on day %d \n ",n, num);
}
Quantity returned;
}
int main()
{
int num = getPeachNumber( 1);
Printf ("The monkey picked %d peaches on the first day. \n ",No.);
Returns 0;
}
12. Variable storage category!
12. 1. Lifecycle Split Storage Mode
According to the lifecycle of variables, C language can be divided into static storage and dynamic storage.
Static storage mode: refers to the mode of allocating fixed storage space when the program is running. Static storage area stores variables that exist in the whole program execution process, such as global variables.
Dynamic storage mode: refers to the mode of dynamically allocating storage space according to needs during the running of the program. Variables stored in dynamic storage area are established and released according to the needs of program operation, which usually include: function parameters; Automatic variable; Site protection and return address when calling function.
12.2. storage type division
There are four types of storage in C language: automatic, static, register and external.
The variable defined by the keyword auto is an automatic variable, and auto can be omitted. If auto is not written, it is implicitly defined as "automatic storage category" and belongs to dynamic storage mode.
Static is a static variable. If it is defined inside a function, it is called a static local variable. If defined outside a function, it is called a static external variable.
Note: static local variables belong to the category of static storage, and the storage units allocated in the static storage area will not be released during the whole running period of the program; Static local variables are given initial values at compile time, that is, they are given initial values only once; If no initial value is assigned when defining local variables, the initial value will be automatically assigned to 0 (for numeric variables) or null characters (for character variables) at compile time for static local variables.
In order to improve efficiency, C language allows the values of local variables to be placed in registers in CPU, which are called "register variables" and declared with the keyword register.
Note: Only local automatic variables and formal parameters can be used as register variables; The number of registers in a computer system is limited, so it is impossible to define any number of register variables. Local static variables cannot be defined as register variables.
Variables declared with extern are external variables, and the meaning of external variables is that functions can call variables defined after functions.
# includ & ltstdio.h & gt
//Source WeChat official account: C language and CPP programming
int main()
{
//Define external local variables
External integer x;
Returns 0;
}
int x = 100;
13. Internal function external function!
Functions in C language that cannot be called by other source files are called internal functions, which are defined by static keywords, so they are also called static functions, in the form of:
Static [data type] function name ([parameter])
Static here is a restriction on the scope of the function and can only be used in the source file, so it is no problem for internal functions with the same function name to appear in different files.
In C language, functions that can be called by other source files are called external functions, which are defined by the extern keyword in the following form:
External [data type] function name ([parameter])
C language stipulates that when the scope of a function is not specified, the system will think it is an external function by default, so extern can also be omitted when defining an external function. Extern 14. Array Array: a continuous memory space with a fixed size and a consistent data type can be omitted. Declaration of array: data type array name [length n]
Data type array name [length n]= {element 1, element 2, element 3, ...};
Data type array name [] = {element 1, element 2, element 3, ...};
Number class type array name [length n]; Array name [0] = element1; Array name [1] = element 2; ..... Note: 1 and the subscripts of the array all start with 0; 2. When initializing the array, the number of elements in the array cannot be greater than the declared array length; 3. If the first initialization method is adopted, when the number of elements is less than the length of the array, the redundant array elements are initialized to 0; 4. When the array is not initialized after declaration, the initialization elements of array elements of static and extern types are 0, and the initialization values of array elements of auto type are uncertain.
15. Array traversal
# include & ltstdio.h & gt
int main()
{
int arr[] = {0, 1,2,3,4,5,6,7,8,9 };
int I;
for(I = 0; I< 10; i++)
{
printf("%d\n ",arr[I]);
}
Returns 0;
}
Bubble sorting of arrays
The idea of bubble sorting: compare adjacent elements with each other, and put the larger numbers behind until all the numbers are sorted.
Strings and arrays
In C language, there is no way to directly define the data type of substring. You need to use an array to define the required string in the following format:
char string name[length]= " string content ";
Character string name [length] = {'string 1',' string 2', ...,' string n',' \ 0'};
note:
The length in [] can be omitted;
In the second way, the last element must be' \0', indicating the end;
The second way can't write Chinese! ; When outputting a string, use: printf("%s ",character array name); Or puts (character array name);
16. String function
Strlen(s): get the length s of the string;
Strcmp(s 1, s2): compare strings; When comparing, the string will be converted into ASCII code and then compared. The return result is 0, which means that the ASCII code values of s 1 and s2 are equal. The return result is 1, indicating that s 1 is an ASCII code greater than s2, and the return result is-1, indicating that s 1 is an ACSII code less than s2.
Strcpy(s 1, s2): string copy; S2 will replace the contents in s 1;
Strcat(s 1, s2) splices S2 after s 1; Note: the length of s 1 should be enough!
Atoi(s 1) converts a string into an integer!
17. Multidimensional array
Data type array name [constant expression 1]...[ constant expression n];
The initialization of multidimensional arrays is similar to that of one-dimensional arrays, and there are two kinds:
Data type array name [constant expression 1][ constant expression 2]...[ constant expression n] = {{ value 1, ..., value n}, {value 1, ... value n}, ..., {value 1.
Data type array name [constant expression 1][ constant expression 2]...[ constant expression n]; Array name [subscript 1][ subscript 2]...[ subscript n] = value;
Multidimensional array initialization should pay attention to the following matters:
When using the first initialization, the array declaration must specify the dimension of the column. Because the system will allocate space according to the total number of elements in the array, when the total number of elements and the dimension of the column are known, the dimension of the trip will be calculated directly;
When using the second initialization, the array declaration must specify the dimensions of rows and columns.
18. Traversal of multidimensional arrays
Use nested loops
Note: Each one-dimensional subscript of a multidimensional array cannot be out of bounds!
19. Structure
Arrays allow you to define variables that can store the same type of data items. Structure is another user-defined data type available in C programming, which allows you to store different types of data items.
Structure is used to represent a record. Suppose you want to track the dynamics of books in the library, you may need to track the following properties of each book:
title
author
subject
Book ID
Define structure
In order to define the structure, you must use the struct statement. The struct statement defines a new data type with multiple members. The format of the struct statement is as follows:
Structural tag {
Member list
Member list
Membership list?
...
} Variable-list;
A tag is a structural tag.
Member-list is a standard variable definition, such as int I;; Or other valid variable definitions.
Variable list structural variables are defined at the end of the structure. Before the last semicolon, one or more structural variables can be specified. The following is the way to declare the book structure:
Structural book
{
Charles? Title [50];
Charles? Author [50];
Charles? Theme [100];
int? book _ id
} book;
Under normal circumstances, there must be at least two tags, a member list and a variable list. Here are some examples:
//This declaration declares a structure with three members, namely, an integer A, a character B and a double precision C..
//Declare the structure variable s 1 at the same time.
//This structure is not marked.
Structure
{
int a;
char b;
Double c;
} s 1;
//This declaration declares a structure with three members, namely, an integer A, a character B and a double precision C..
//The tag name of the structure is SIMPLE, and no variables are declared.
simple structure
{
int a;
char b;
Double c;
};
//structure marked as SIMPLE, and variables t 1, t2 and t3 are declared.
struct SIMPLE t 1,t2[20],* t3
//You can also create new types with typedef.
Typedef structure
{
int a;
char b;
Double c;
} Simple2
//You can now declare new structural variables with Simple2 as the type.
Simple2 u 1,u2[20],* u3
Access structure members
To access members of a structure, we use the member access operator (. ). The member access operator is the period between the name of the structural variable and the structural member we want to access. You can use the struct keyword to define variables of structural type. The following example demonstrates the use of the structure:
# include & ltstdio.h & gt
# include & ltstring.h & gt
//Source WeChat official account: C language and CPP programming
Structural book
{
Charles? Title [50];
Charles? Author [50];
Charles? Theme [100];
int? book _ id
};
int main()
{
Structure book book1; /* Declared book 1, book type */
Structure book volume 2; /* Declare Book2, which belongs to Books type */
/* Book 1 details */
Strcpy( Book 1.title, C Programming);
Strcpy( Book 1.author, "Nuha Ali");
Strcpy( Book 1.subject, C Programming Tutorial);
book 1 . book _ id = 6495407;
/* Details of Volume II */
Strcpy( Book2.title, "Telecom Billing");
strcpy( Book2.author,“Zara Ali”);
Strcpy( Book2.subject, Telecom Billing Course);
Book2.book _ id = 6495700
/* output books 1 information */
printf( "Book 1 title : %s\n ",Book 1 . title);
printf( "Book 1 author : %s\n ",Book 1 . author);
printf(" Book 1 subject:% s \ n ",Book 1 . subject);
printf(" Book 1 Book _ id:% d \ n ",Book 1 . Book _ id);
/* output the second volume of information */
Printf ("Second Book Title: %s\n", Book2.title);
Printf ("Second book by %s\n", Book2. author);
printf( "Book 2 subject : %s\n ",book2 . subject);
printf( "Book 2 book_id : %d\n ",Book 2 . Book _ id);
Returns 0;
}
Learning C++ well is a stepping stone to enter a big factory! If I had this class, my C++ wouldn't be like this.
Has it expired?
20.*** verbs
* * * Object is a special data type, which allows you to store different data types in the same memory location. You can define a * * * topic with multiple members, but only one member can have a value at any time. * * * The body provides an efficient way to use the same memory location.
# include & ltstdio.h & gt
# include & ltstring.h & gt
Joint data
{
int I;
Floating f;
Charles? str[20];
};
int main()
{
Union Data data;
Printf ("Memory size occupied by data: %d\n", sizeof(data)););
Returns 0;
}
2 1. pointer
# include & ltstdio.h & gt
int main()
{
int? var = 20? /* Declaration of actual variables */
int? * ip/* Declaration of pointer variable */
ip = & ampvar? /* Store the address of the variable in the pointer variable */
Printf ("address of var variable: %p\n",&var? );
/* Address stored in pointer variable */
Printf ("address stored in IP variable: %p\n", IP);
/* Use pointer to access value */
Printf ("* value of IP variable: %d\n", * IP);
Returns 0;
}
22. File reading and writing
Write file
# include & ltstdio.h & gt
int main()
{
FILE * fp = NULL
fp = fopen("/tmp/test.txt "," w+");
Fprintf(fp, "This is a test of fprintf ... \ n");
Fputs ("This is a test of fputs ... \n", FP);
fclose(FP);
}
Read file
# include & ltstdio.h & gt
int main()
{
FILE * fp = NULL
char buff[255];
fp = fopen("/tmp/test.txt "," r ");
fscanf(fp," %s ",buff);
printf(" 1: %s\n ",buff);
fgets(buff,255,(FILE *)FP);
printf("2: %s\n ",buff);
fgets(buff,255,(FILE *)FP);
printf("3: %s\n ",buff);
fclose(FP);
}
C Language and C++ Learning Route
23. Classification algorithm
Ten classic sorting algorithms (dynamic demonstration+code)
24. Search algorithm
Nine search algorithms
Interview knowledge
C Language and C++ Interview Knowledge Summary
Word processing
A comprehensive summary of string operations
27.? Interpretation of general standard library of C language
Interpretation of general standard library of C language
28. The most commonly used greedy algorithm in C language.
In this way, the most commonly used greedy algorithm in C language was conquered.
29. Common C language memory errors and countermeasures
Common memory errors in C language and their countermeasures
30.c language realizes the object-oriented principle.
C language realizes the object-oriented principle.
3 1.C language /C++ memory management
Can you still understand C language /C++ memory management after reading this article?
32. Talk about C language pointer again
Let's talk about the pointer again: Big Brother will clear the cloud of the C pointer for you.
Callback function of function pointer in C language
Detailed explanation of C language pointer (welfare at the end of the article)
33.c language preprocessing command
Detailed explanation of long text: C language preprocessing command
34. Efficient programming and code optimization of C language
Efficient Programming and Code Optimization of C Language
35.c language structure
The structure of C language has been conquered in this way! Worth collecting!
36. Detailed explanation of original code, complement and complement
Detailed explanation of original code, complement and complement
37.c language macro definition
On the Use of Macro Definition in C Language
38.* * * Use union, enumeration and big and small end modes in C language.
Union, Enumeration and Big-Small Mode of * * in C Language