[C Language] A true beginner’s introduction to C language grammar

Preface

This article records the author's understanding of C language and is suitable for people who have no foundation in C language. I think that when you first learn C language, you should understand these contents first, so that the subsequent knowledge will not be difficult to learn. If there are typos or misunderstanding of the content, please correct me.

1. The first C language program – print Hello world!

The entire program to print Hello world is as follows:

#include <stdio.h>

int main()
{
    
    
	printf("Hello world!\n");
	return 0;
}

I will start with Notes on Chinese and English symbols, main function< a i=4> and printf function explains the program to print Hello world!.

1.1. Chinese and English symbols

All symbols must be in English. If you use Chinese symbols in the program, an error will be reported. For example, the Chinese () and the English () look different.
In addition to the double quotes in the printf function, Chinese symbols can be used.

1.2. main function (main function)

As long as it is a complete C language code, there must be a main function.fixed formatas follows:

int main()
{
    
    
	//程序代码部分:这一行包含了整个程序的具体实现
	return 0;
}

int: int is the abbreviation of integer (integer), which means mainFunction return value typeIs an integer (integer type).

main: The main function isProgram entryAnd main functionThere is and only one

The parameters are placed in parentheses () after the main function. If nothing is placed, there are no parameters.

Inside a pair of curly brackets { } isfunction body, because this is the function body of the main function, it is called the main function body. The program code part in the main function body is the core of the main function body, which contains the specific implementation of the program.
Everything

Two // indicate that the following content isComment, the comments following // will be ignored when the program is run.

Statements in the function body must be preceded byindentation(Four spaces are equivalent to a Tab key).

retrun 0;: return 0 means the program ends normally and the return value is 0. This 0 corresponds to int, return is followed by an integer, and the return value type of the function is int.Note that there is a semicolon after return 0;

Semicolon ; indicates the end of a statement. In C language, each statement must end withEnding with semicolon, otherwise the compiler will report an error.

1.3. printf function

Let me start with an analogy: If you want to write with a pen, but you don’t have a pen, and you can’t make a pen yourself, you can only borrow a pen from your deskmate. There is a pen box on the desk of my deskmate, which contains many kinds of stationery. If you want to borrow a pen from your deskmate, you have to ask first and then take the pen from his pencil box to use.
The printf function is like the pen I want to use, but I can’t write the printf function myself (or I am too lazy to write the printf function myself...just kidding), so I have to ask a person named stdio.h file, please say hello before using the content in the file. This can explain why the first line of the program needs to be #include <stdio.h>,In order to call the printf function that exists in the C language itself

#include <stdio.h>: means including a file called stdio.h, std – standard (standard), i – input (input), o – output (output), .h represents the header file.printf is a function in the stdio.h file

printf(" "); : short for print function, meaningOutput function, specially used to print data. printf is a library function - that is, the function provided to us by the C language itself.
The function should be followed immediatelybrackets ( ), the brackets contain a pair ofDouble quotes " ", you can freely enter the text, numbers or symbols you want the program to output in double quotes (note that the numbers here only represent characters, how to output integers and decimals will be mentioned in 3.6). Lastsemicolondo not forget . The semicolon marks the end of this line of statements.

1.4. Supplement: Hello world! The following ‘\n’

’ \n ’ is a newline character, meaningnewline, is one of the escape characters. Adding a slash \ changes the original meaning to a lowercase 'n' character.

2. Data type

Data types and variables and constants are best understood together.

There are different types of data in life, such as the name on the ID card, ID number, date of birth, address, etc. The name is expressed in words, and the date of birth is expressed in numbers. If a computer wants to know different types of data, it must classify the data. for exampleCharacter ‘0’ and number 0They are different. The number 0 is stored in the computer as 0, while the character '0’ is stored in the computer as 48. If there is no data type to distinguish the data, the integer 48 and the character '0' will be confused.
Everything

2.1. Classification of data types

Data types are divided into C languageBuilt-in data typesandCustom type, the custom type will be mentioned in the structure, so the main focus here is the built-in data type.
everything

2.2. Common data types among built-in types

char is a character type. Every word that can be typed on the keyboard is a character, such as: ‘y’, ‘&’, ‘3’, ‘=’. A Chinese character occupies 2 characters.The essence of a character is the ASCII value of the character. The ASCII code value is an integer. Therefore, when classifying character types, they can be divided into integer families.. Note that characters must be enclosed in single quotes. Without single quotes, they are not characters! ! !

int is an integer type. Integer type is integer type, including positive integers, negative integers, and the number 0. However, integers of type int have a range (about -2.1 billion to 2.1 billion. Of course, every data type has a range, and a piece of memory space is inherently limited).
short is a short integer. The range is smaller than the int type.
long is a long integer. The range is larger than the int type.

float is a single precision floating point type. The floating point number type is the decimal type.
double is a double precision floating point type. Higher precision than float type.

Question: Why are integers divided into long integers and short integers, and floating point types also divided into single precision and double precision?
Answer: In order to allocate memory space reasonably, for example, the space occupied by storing 1 of type int must be smaller than that of storing 1 of type long, which saves a certain amount of space.

2.2.1 Memory space occupied by each data type

Everything

Question: Why are the memory spaces of long and long long types both greater than or equal to 4?
Answer: C language only specifies memory space: long long type >= long type >= int type, different memory spaces will be allocated under different compilers and operating systems.

Explanation formemory space:
Everything

Computer unit conversion:
Everything

3. Variables and constants

3.1. What are variables?

Variables: Variables in C language are different from variables in mathematical functions.A variable is a memory space used to store data, as for what type of data is stored, it depends on the data type of the variable.

Here's an example using code:

int main()
{
    
    
	int a;//定义一个名字为a的变量,变量a的数据类型是int类型,也就是说变量a能存储一个整型数据。
	return 0;
}

Drawing to explain int a; this line of code:
Everything

3.2. Naming rules and suggestions for variables

Naming rules:

  • Can only consist of uppercase and lowercase English letters or underscores _ or numbers
  • cannot start with a number
  • case sensitive
  • Variable name cannot be the same as Keyword

Example of variable naming:

int main()
{
    
    
	int char;//error,char是关键字
	long Char;//OK,因为大小写区分
	
	float 2B;//error,因为以数字开头了
	double _2B;//OK
	
	char !1@E$;//error,符号除了下划线 _ 都不能出现在变量名上
	
	return 0;
}

Suggestions for naming variables:
The name should be meaningful. For example, if you want to store an age, you can name the variable age: when defining a variable.

int main()
{
    
    
	int age;//定义一个整形变量,变量名是age
	return 0;
}

3.3. What are constants?

constant:A constant is an unchanging quantity, a kind of data. For example, the character ‘a’, the integer 123, and the decimal 6.66. For example: if the variable is a glasses case, then the constant is a pair of glasses that can be placed in the glasses case.

3.4. How to let a variable store a piece of data (constant)

Take the above variable name age as an example:

int main()
{
    
    
	int age;//定义一个整形变量,变量名是age
	age = 18;//把常量18赋值给变量age,此时的变量age储存的是18
	return 0;
}

You can also define a variable and assign a value to the variable at the same time. The upper and lower codes are equivalent.

int main()
{
    
    
	int age = 18;//定义整型变量age的同时把18赋值给age,此时的变量age储存的是18
	return 0;
}

3.5. Assignment operator ’ = ’

The equal sign ’ = ' in C language does not mean that both sides are equal in the mathematical sense, but the meaning of assignment, that is, assigning data (the right side of the equal sign) to a variable (the left side of the equal sign).
equal signleftMust be onevariable,
equalityrightcan be adata (constant) means that the data on the right side of the equal sign is assigned to the variable on the left. If the variable has stored a data before the assignment, the original data will be replaced after the assignment.
equal signrightIt can also be another kindvariable, indicating that the data stored in the variable on the right is assigned to the variable on the left.

Example:

int main()
{
    
    
	int a = 10;
	int b = 20;
	//在下面a=b执行之前,变量a存储的是10,变量b存储的是20
	a = b; 
	//a=b执行之后,变量a里的10被替换成20,变量b里的数据还是20不变
	return 0;
}

Illustration:
Everything

3.6. How to output the data in the variable?

#include<stdio.h>
int main()
{
    
    
	char alphabet = 'a';//定义了一个名为alphabet的字符变量,并把字符'a'赋值给变量alphabet.
	int number = 123;

	printf("%c\n", alphabet);
	printf("%d\n", number);
	printf("%c %d",alphabet,number);
    return 0;
}

Program execution result:
Everything
Compared with printf(""Hello world!");, an extra comma and variable name are added at the end, and there are %c %d in the double quotes. Format (detailed introduction in 3.6.1), different formats will output different results. For example, the following example:

#include<stdio.h>
int main()
{
    
    
	char alphabet = 'a';//定义了一个名为alphabet的字符变量,并把字符'a'赋值给变量alphabet.
	printf("%c\n",alphabet);
	printf("%d\n",alphabet);
	return 0;
}

Program running results:
Everything

%c means that the variable is output in the form of characters, and %d means that the variable is output in the form of an integer.
Obviously, the data in the alphabet variable is output, but the output results are different. One is the character 'a', and the other is the integer 97, which is affected by the format control in double quotes.

3.6.1. Common format control of each data

Everything

3.7.scanf function

In the previous examples, the data written in the program was assigned to variables. Can't I type data on the keyboard while the program is running? There is a printf output function and a scanf input function. Similarly, before calling the scanf function, #include<stdio.h> is included at the top of the program.

example:

#include<stdio.h>
int main()
{
    
    
	int age;
	printf("请输入你的年龄:");
	scanf("%d",&age);
	printf("你的年龄是%d岁",age);
	return 0;
}

The program runs:
Everything
The cursor behind will keep flashing until you enter an integer on the keyboard. After inputting, press Enter and the program will continue to run:
Everything

The double quotes in the scanf function are format controls, because the integer variable age is defined, and the integer format control is used when inputting age.
In the scanf function, in addition to the variable after the comma, there must also be the address character & in front of the variable. You must know the address of the age variable so that the data you enter can be stored in the age variable.

Guess you like

Origin blog.csdn.net/weixin_73276255/article/details/131234573