Preliminary understanding of C language: variables and constants, data types

Table of contents

Know the C language

 1. Data type

2. Variables: Quantities that will change (simple understanding).

Variable scope:

1. Local variables: The scope of a local variable is the local scope where the variable is located.

2. Global variables: The scope of global variables is the entire project.

Variable life cycle:

1. The life cycle of a local variable is from entering the scope to the end of the scope.

2. The life cycle of global variables is from the beginning to the end of the entire program.

3. Constants

1) Literal constants

2) Constant variable (const modification)

3) Identifier constants defined by #define

4) Enum constants


Know the C language


       C language is a computer programming language widely used in low-level development. For most programmers, C language is the first course in learning programming. C language provides many low-level processing functions, and has a good cross-platform. C language is now in the top three of the programming language rankings, which can be described as a classic. C language can be used as a system design language to write operating systems, and it can also be used as a programming language to write applications and programs. Some people describe "C creates everything", which shows the importance and greatness of C language.
 

 1. Data type

 The char type is a character type, for example: a, c, b, @, ! Such letters and symbols are character data types.

short, int, long, and longlong are all integers, and integers are integer types. The difference between them is that the range of executable integers is different

Float, double are floating-point types, such as decimals: 2.3, 3.5 numbers with decimal points are called floating-point numbers. The difference lies in the degree of precision.

The space occupied by different data types is also different. I can use the program to output the memory size occupied by each type.

#include<stdio.h>
int main(){
    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;
}

Use sizeof to calculate the memory size occupied by each type. The unit is byte (byte)

2. Variables: Quantities that will change (simple understanding).

Variable scope:

1. Local variables: The scope of a local variable is the local scope where the variable is located.

2. Global variables: The scope of global variables is the entire project.

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

Two a defined with the same name will not report an error, and the running results are as follows:

 

Variable life cycle:

1. The life cycle of a local variable is from entering the scope to the end of the scope.

2. The life cycle of global variables is from the beginning to the end of the entire program.

       Application of Variables: Adding Two Variables

#define _CRT_SECURE_NO_WARNINGS 
//直接使用scanf会报错
//1.可以最前面填#define _CRT_SECURE_NO_WARNINGS 
//2.将scanf改为scanf-s,但是其不为c语言的通用语言,只能在vs编译器中使用。
//3.可将文件中newc++file记事本打开形式加上#define _CRT_SECURE_NO_WARNINGS 
#include <stdio.h>//可以使用printf,scanf等
int main()
{

int num1 = 0;//初始化  因为若不初始化会导致结果不定

	int num2 = 0;

	scanf("%d %d", &num1, &num2);//scanf格式("输出控制符",存放位置);

	int sum = num1 + num2;

	printf("%d", sum);//printf格式("输出控制符",输出参数);

	return 0;
}

3. Constants

The types of constants are:

1) Literal constants

2) Constant variable (const modification)

3) Identifier constants defined by #define

4) Enum constants

#include<stdio.h>
#define M 3.141592//#define定义的标识符常量
enum sex {
	male,//枚举常量
	female,//枚举常量(可改变初值进而使下一个常量也发生变化)
	secret//枚举常量
};
int main()
{
	100;//字面常量
	printf("%d\n", male);
	printf("%d\n", female);
	printf("%d\n", secret);
	printf("%lf\n", M);
}

 Output result:

The identifier constant M here can be used instead of the number 3.141592 when used in later programs.

Enumeration constants start from 0 and increase backwards by default when there is no initial value. Change initial value:

 

 The change value starts and increases backwards in turn.

int main()
{  //30 3.14 'W' "abc"
    //若 int a=10;
      //a=20;  则最后结果为20
    //若const int a=10;
    //a=20;    则会报错后边不改变前边变量的值

    //const修饰的a,本质是变量,但不能直接修改,有常量属性
    const int a = 10;     
     printf("%d",a);
  return 0;
}

Guess you like

Origin blog.csdn.net/fcccfffg/article/details/131946774