Don't gossip. Start with a mind map.
As shown in the figure, it is still the first part of getting to know C language. This time only introduces the characteristics of C language, such as data type, constant variable, string escape, character annotation, selection loop statement, function and array.
Next, please join me in a rough discussion of the connotation.
Characteristics of C language itself
This is the definition of C language:
C language is a general computer programming language, which is widely used in the bottom development. The design goal of C language is to provide a programming language that can compile and process low-level memory in a simple way, generate a small amount of machine code and run without any running environment support.
Although C language provides many low-level processing functions, it still maintains good cross platform characteristics. C language programs written in a standard specification can be compiled on many computer platforms, and even include some embedded processors (single chip microcomputer or MCU) and supercomputer and other operating platforms.
In the 1980s, in order to avoid differences in C language syntax used by various developers, the U.S. National Bureau of standards formulated a complete set of American national standard syntax for C language, called ANSI C, as the original standard of C language. [1] At present, the C11 standard issued by the international organization for Standardization (ISO) and the International Electrotechnical Commission (IEC) on December 8, 2011 is the third official standard of C language and the latest standard of C language. The standard better supports Chinese function names and Chinese identifier, and realizes Chinese character programming to a certain extent.
C language is a process oriented computer programming language, which is different from object-oriented programming languages such as C + +, Java and so on.
Its compilers mainly include Clang, GCC, WIN-TC, SUBLIME, MSVC, Turbo C, etc.
But its characteristics are summarized as follows: 1 Bottom development, 2 International standard ANSI C, 3 Process oriented
#include <stdio.h> //stdio standard input & output headfile //int - integer - type of integer //Main function //The main function is the entry of the program //There is and only one uniqueness int main() { //Library function - print function - output function printf("hello world!\n"); printf("hello world!\n"); printf("hello world!\n"); return 0; } //This way of writing clearly tells you that the main function does not need to pass parameters int main(void) { } //This kind of writing is useful, but it's not the point int mian(int argc, char* argv[]) { }
data type
C language mainly has six data types, namely character char, short integer, short integer, integer int, long integer, single precision floating-point float and double precision floating-point double.
These six types have their own character length, that is, the space occupied by their variables. The minimum char is only 1 byte, short is 2, int is 4, and long is generally 4 or 8. Anyway, as long as the byte length of long is greater than or equal to int. The two floating-point types are the most special, which are 4 and 8 respectively. Obviously, the precision of double precision is higher.
int main() { //How to calculate the space occupied by variables created by a type //sizeof(); printf("%d\n", sizeof(char));//1 byte printf("%d\n", sizeof(short));//2 bytes printf("%d\n", sizeof(int));//4 bytes printf("%d\n", sizeof(long));//4 bytes printf("%d\n", sizeof(long long));//8 bytes printf("%d\n", sizeof(float));//4 bytes printf("%d\n", sizeof(double));//8 bytes return 0; }
constant variables
Variable classification
Obviously, there are two types of variables and constants. Variables are divided into two categories: local variables and global variables.
As the name suggests, local variables are defined in a pair of braces. Obviously, we usually create local variables in the main function, and global variables are variables defined outside the braces. When the two have the same name, local variables take precedence. Of course, who writes this kind of code.
Use tips
Use scanf function in the program, if the program is scanf ('% d% d');% Whether there are spaces between d% d or not, variables should be separated by spaces when entering. If there are commas between them, they should be separated by commas.
Lifecycle scope
If the scope and life cycle of these two variables are discussed, the scope of global variables is the whole project file, if you want to use them in other fields For use in the c file, you need to declare that the life cycle is the life cycle of the whole project, that is, the life cycle of the main function. The scope of a local variable is within the braces {} of its definition. When entering the scope, the life begins and when leaving, it ends.
//Verify the scope of local variables int main() { int b = 100;//Arbitrary position of main function { int a = 10;//Local variable scope: the scope of the local variable printf("a=%d\n", a); printf("b=%d\n", b); } //printf("a=%d\n", a); printf("b=%d\n", b); return 0; } //Verify that the scope of the global variable is the whole project int a = 200; void test() { printf("test:%d\n", a); } int main() { test(); printf("main:%d\n", a);//Global variables can be used arbitrarily return 0; } //Use of global variables in other source files //Go to test1 C file viewing int g_val = 110; //Verify that the life cycle of the local variable is in scope to out scope int main() { { int a = 100; printf("%d\n", a); } printf("%d\n", a);//Out of scope is the end of the life cycle return 0; } //The life cycle of the global variable is the whole project, that is, the life cycle of the main function int main() { printf("%d\n", g_val); return 0; }
Constant classification and its characteristics
Constants are roughly divided into four types: 1 literal constants, 2const modified constant variables, 3#define defined identifier constants, and 4 enumeration constants.
First, a literal constant is a number written casually. For example, 3.14 is a literal constant.
Second, const modified constant variables, that is, add const modification when creating variables, such as const int # a=0; In this way, variable a has constant attributes and cannot be modified. However, it is worth noting that the term constant variable is still a variable and cannot be used to define an array.
//const modified variable int main() { //local variable int a = 200; //A property that cannot be changed const int a = 110; a = 100; printf("%d\n", a); //const decorated variables cannot be defined as arrays //(constant variable) int n = 110; int arr[n] = { 0 };//array return 0; }
Third, #define defined constants, written as #define N 10, are true constants that can be used to define arrays. It is also immutable (constants cannot be modified of course).
#define PAI 314 int main() { int a = PAI; printf("a=%d\n", a); //MAX = 300;//# The variable defined by define cannot be modified //Can be used to define arrays int arr[PAI] = { 0 }; return 0; }
The fourth is enumerating constants. The specific use methods are like enum sex {mal, FEMALE, SECRET}; In this way, we get the enumeration constants, which have initial values, respectively 0, 1, 2, and so on. Of course, we can also assign values by ourselves.
//Enumeration constant SEX example //You can list the values, gender and three primary colors one by one enum Sex { //Possible values of enumeration MALE=3,//Assign initial value FEMALE=8, SECRET//One backward is 9 }; int main() { //FEMALE = 99;//ERR enumeration variable value cannot be modified enum Sex a=MALE; enum Sex s = FEMALE; printf("%d\n", MALE); printf("%d\n", FEMALE); printf("%d\n", SECRET); return 0; }
String + escape character + comment
character string
Enclosed in single quotation marks, such as' w 'and' r 'are characters, and "yyx" and "abcdef" enclosed in double quotation marks are strings. The string usually ends with '\ 0', which is implied at the end of the string. Since '\ 0' is the end of string flag, it is certainly not counted as the string content when calculating the string length.
//Character, string int main() { //'a', 'X' - character //"ABC" "123" - string //"abcdef"; // string literal //① char ch1[] = "abcdef";//Initialize character array //a ,b ,c ,d ,e ,f ,\0 //%s - print string //strlen - print the length of the string, (stop in case of \ 0, not in itself) \ 0 is the end flag of the string printf("%s\n", ch1); //abcdef printf("%d\n", strlen(ch1));//6 //② char ch2[] = { 'a','b','c','d','e','f' }; //a ,b ,c ,e ,f printf("%s\n", ch2); //abcdef scald printf("%d\n", strlen(ch2));//22, not found \ 0 //③ char ch3[] = { 'a','b','c','d','e','f','\0' }; //a ,b ,c ,d ,e ,f ,\0 printf("%s\n", ch3); //abcdef printf("%d\n", strlen(ch3));//6 return 0; }
Escape character
There are '\', '\', '\ \' to prevent single quotation marks, double quotation marks and backslashes from being transferred. Warning, beeping '\ a', backspace '\ b', feed '\ f', line feed '\ n', carriage return '\ r', horizontal tab '\ t', vertical tab '\ v', octal digit '\ ddd', hexadecimal digit '\ xdd'.
#include <stdio.h> int main() { printf("c:\code\test.c\n");//c:code est.c printf("c:\\code\test.c\n");//c:\code est.c //A single \ is an escape sequence character, but there is no escape character of \ c, so the system automatically ignores it\ //If you want this \, you must match two \ \, to prevent it from being escaped return 0; } // \? //Three letter word // ??) --> ] ??( --> [ //\' \" int main() { printf("%c\n", 'a'); printf("%c\n", 'b'); printf("%c\n",'\'');//Add \ 'before' to simply treat it as a character ' printf("%s\n", "\"");// \" return 0; } //\a \b \f \v \t \n \r //\ddd octal digit \ xdd hexadecimal digit int main() { printf("%c\n", '\165'); //Print the character represented by the octal digit 065 converted to the decimal digit 53 (ASCII code) printf("%d\n", '\165'); //Print octal digit 065 to decimal digit 53 printf("%c\n", '\x15'); printf("%d\n", '\x15');//21 return 0; }
Two notes
C + + style / / xxxxx, which can only be commented on one line. The style of C language / * xxxxx * /, and comments cannot be nested.
Select loop statement
Select a statement to branch. The specific situation is if (condition) {statement} else (condition) {statement}; Loop statement, with while loop, do While loop and for loop.
int main() { int i = 0; printf("Add bit\n"); printf("Do you want to type the code well (1)/0)?\n"); scanf("%d", &i); if (i == 1) { printf("good offer\n"); } else { printf("pill\n"); } return 0; } int main() { int line = 0; printf("Add bit\n"); while (line<20000) { printf("knock%d Code\n",line); line++; } printf("The cow is bad and gets a good one offer\n"); return 0; }
function
Function's main parameters should correspond to type of the return value
int ADD(int num1, int num2) { scanf("%d %d", &num1, &num2); int sum = num1 + num2; return sum; } int main() { int num1 = 0; int num2 = 0; input //scanf("%d %d", &num1, &num2); Add up //int sum = num1+num2; output //printf("sum=%d\n", sum); //int output = ADD(num1,num2); //printf("%d\n", output); printf("%d\n", ADD(num1, num2)); return 0; }
Array
A definition is a set of elements of the same type. The use of the array is based on the subscript, starting from 0.
int main() { //Array - a collection of identical elements int arr[10] = { 1,2,3,4,5,6,7,8,9,10 }; int i = 0; for (i = 0; i < 10; i++) { printf("%3d", arr[i]); } printf("\n"); for (i = 9; i >= 1; i--) { printf("%3d", arr[i]); } return 0; }