Elementary C language (1) - Getting to know C language for the first time

Sorry, it took so long after posting the first article, mainly because after posting the last one, it entered the final week, and I have been reviewing, so I didn’t find a suitable time to update. Now it’s vacation, summer vacation let Let's study hard together! According to my plan, I will start to learn C language, mainly to update the study notes. If there is anything wrong, everyone is welcome to criticize and correct. As the saying goes: " The best time to plant a tree is ten years ago, followed by now. " Let us grasp the present and create the future together!

1. What is C language

C language is a general-purpose computer programming language , widely used in the underlying software development. The design goal of the C language is to provide a programming language that can be compiled in an easy way, handle low-level memory, generate a small amount of machine code, and can run without any operating environment support.
Although the C language provides many low-level processing functions, it still maintains a good cross-platform feature. A C language program written in a standard specification can be compiled on many computer platforms, even including some embedded processors (single-chip or called MCU) and supercomputers and other operating platforms.
In the 1980s, in order to avoid differences in the C language grammar used by various developers, the US National Bureau of Standards formulated
a complete set of American National Standard grammar for the C language, called ANSIC , as the initial standard of the C language. . [1] Currently on December 8, 2011
, the C11 standard released by the International Organization for Standardization (ISO) and the International Electrotechnical Commission (IEC) is the third official
standard latest standard of the C language, which is better It supports Chinese character function names and Chinese character identifiers, 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++ and Java. Its compilers mainly include Clang, GCC , WIN-TC, SUBLIME, MSVC , Turbo C, etc.

2. The first C language program

The first C language program: print "Hello World" on the screen

#include <stdio.h>//头文件
//main - 主函数(程序的入口,写的C语言代码都是从main函数的第一行开始执行的)
int main()//int-整型
{
    
    
	printf("Hello World!");
	//printf是库函数 - C语言标准库中提供的一个现成的函数,可以直接使用,功能是在屏幕上打印信息
	//库函数的实现需要包括库函数-printf需要用到的头文件是:stdio.h
	return 0;
}//{}-函数体

Note : 1. The main function is a must, and there is only one, do not write mian
2. All characters in the C language must be in English (especially semicolons; and quotation marks '' '')

3. Data type

Common data types:
char - character data type
short - short integer
int - integer
long - long integer
long long - longer integer
float - single precision floating point number (floating point number means decimal)
double - double precision points

Next, let's go through a piece of code to understand the size of each data type

int main()
{
    
    
	//%d-以十进制的形式打印整数
	printf("%d\n", sizeof(char));
	printf("%d\n", sizeof(short));
	printf("%d\n", sizeof(int));
	printf("%d\n", sizeof(long));
	printf("%d\n", sizeof(long long));
	printf("%d\n", sizeof(float));
	printf("%d\n", sizeof(double));
	return 0;
}

insert image description here
So, what do the numbers 1, 2, 4... here mean?
Here, we need to introduce some common units in computers: bit (bit), byte (byte), KB, MB, GB, TB, PB, etc. (8bit = 1 byte 1 KB = 1024 byte...) The unit corresponding to the number shown in the above figure is byte (bit)
C language standard: sizeof(long long)>=sizeof(long)>=sizeof(int) >sizeof(short)>sizeof(char)

Four. Variables, constants

In our daily life, there are some variable quantities (such as: height, weight, salary...) Similarly, there are also some constant quantities (such as: ID number, pi...) In C language, these constant The quantity that changes is called a constant, and the quantity that changes is called a variable.

1. How to define variables

The basic format of defining a variable is: data type variable name.
The example is as follows:

    int age = 20;
	float weight = 50.3f;//此处如果是double型,则不需要加f
	char ch = 'a';

While the above code defines the variable, it also assigns a value to the variable, which makes the program easier.

2. Variable naming

The naming of variables is also particular, the rules are as follows:
(1). It can only be composed of letters, numbers and underscores (_).
(2). It cannot start with a number
(3). The length cannot exceed 63 characters
(4). Variables Names are case-sensitive
(5). Keywords cannot be used in variable names

After reading this, you must be wondering what is a keyword?
A keyword is a character string with a specific meaning defined by the C language, and is usually called a reserved word. The user-defined identifier should not be the same as the keyword. There are 32 keywords in the C language. According to the function of the keyword, it can be It is divided into four categories: data type keywords, control statement keywords, storage type keywords and other keywords.

1. Data type keywords (12):
(1) char: declare character variable or function
(2) double: declare double precision variable or function
(3) enum: declare enumeration type
(4) float: declare floating point Type variable or function
(5) int: Declare an integer variable or function
(6) long: Declare a long integer variable or function
(7) short: Declare a short integer variable or function
(8) signed: Declare a signed type variable or Function
(9) struct: Declare a structure variable or function
(10) union: Declare a joint data type
(11) unsigned: Declare an unsigned type variable or function
(12) void: Declare a function with no return value or no parameter, declare no type Pointer (basically these three functions)

2. Control statement keywords (12):
A Loop statement: (1) for: a loop statement (which can be understood but cannot be expressed in words) (2) do: the loop body of the loop statement (3) while: the loop of the loop statement Condition (4) break: Jump out of the current loop (5) continue: End the current loop and start the next round of loop
B Conditional statement: (1)if: Conditional statement (2)else: Conditional statement negates the branch (used in conjunction with if) (3 ) goto: unconditional jump statement
C switch statement: (1) switch: used for switch statement (2) case: switch statement branch (3) default: "other" branch in switch statement D
return: subroutine return statement (can With parameters, also see without parameters)

3. Storage type keywords (4):
(1) auto: generally not used to declare automatic variables (2) extern: declared variables are declared in other files (also can be regarded as reference variables) (3) register: declared Accumulator variable (4) static: declare static variable

4. Other keywords (4): (1) const: declare read-only variables (2) sizeof: calculate the length of the data type (3) typedef: used to alias the data type (of course there are other functions) (4) volatile: Indicates that variables can be implicitly changed during program execution.

Although there are only 32 keywords above, in order to facilitate readers to read your code, try to make your variable names meaningful.

3. Classification of variables

Variables are divided into local variables and global variables
Global variables : Variables defined outside {} are global variables
Local variables : Variables defined inside {} are local variables
Examples are as follows:

#include <stdio.h>
int b = 20;//全局变量
int a = 100;
int main()
{
    
    
	int a = 10;//局部变量
	printf("%d\n", a);
	printf("%d\n", b);
	return 0;
}

insert image description here

Note: When local variables and global variables are used in the same place, follow the principle of local priority.

4. The use of variables

Give an example: write a code to add two numbers and output the result.

int main()
{
    
    
	int a = 0;
	int b = 0;
	int sum = 0;
	//输入两个值
	//printf为输出函数,而scanf为输入函数
	scanf("%d %d", &a, &b);
	sum = a + b;
	printf("%d\n", sum);
	return 0;
}

insert image description here
Note : In the VS compiler , in order to prevent errors due to the scanf function, you can add #define _CRT_SECURE_NO_WARNINGS 1 before the header file (put it on the first line of the .c file)

5. The life cycle and scope of variables

Scope (scope) is a programming concept. Generally speaking, the name used in a piece of program code is not always valid/available, and the code scope that limits the availability of this name is the scope of this name.
1. The scope of a local variable is the local scope where the variable is located. (It is inside {})
2. The scope of global variables is the entire project. (Can be used throughout the project)

#include <stdio.h>
int c = 100;//全局变量
int main()
{
    
    
	int b = 10;//局部变量
	{
    
    
		int a = 30;//局部变量
		printf("%d\n", a);//a的作用域在与它最邻近的{}内
	}
	printf("%d\n", b);
	printf("%d\n", c);
	return 0;
}

Example of global variable vs. local variable scope

The life cycle of a variable refers to a period of time between the creation of the variable and the destruction of the variable.
1 The life cycle of a local variable is: the life cycle of entering the scope begins, and the life cycle of the out of scope ends.
2. The life cycle of global variables is: the life cycle of the entire program.

6. Constants

The definition forms of constants and variables in C language are different.
The constants in C language are divided into the following types:
1. Literal constants (values ​​written directly)
such as: 100, 3.12, 5.2, 'a'... 2. The basic format of
const modified constant variable
definition is: const data type variable name

#include <stdio.h>
int main()
{
    
    
	const int a = 10;//a为const修饰的常变量,此时a的值不能再被修改,但a在本质上还是一个变量
	printf("a = %d\n", a);
	return 0;
}

3. The identifier constant defined by #define
The basic format of the definition is: #define symbol constant value

#include <stdio.h>
#define M 100//注意:这里没有分号;
int main()
{
    
    
	printf("%d\n", M);
	int a = M;
	printf("%d\n", a);
	return 0;
}

insert image description here

4. Enumeration constants (enumeration - list one by one)

#include <stdio.h>
enum Color//自定义类型
//enum是枚举的关键字
{
    
    
	//枚举常量
	Blue, //0
	Red,  //1
	Green //2
};

int main()
{
    
    
	enum Color c = Red;
	printf("%d\n", Blue);
	printf("%d\n", Red);
	printf("%d\n", Green);
	return 0;
}

insert image description here
It is not difficult to see that the value of the enumeration constant is in order from top to bottom, starting from 0 and gradually +1

This is the end of today's study, and I will continue to follow the relevant study notes of the new C language later, come to like, comment and bookmark!

Guess you like

Origin blog.csdn.net/qq_73121173/article/details/131335393