First introduction to C language in C language training (2)

1. Constants and variables

All exercises focus on the foundation. Only by laying a solid foundation can you go far. Constants and variables are one of the foundations of the C language. Invariant values ​​are represented by the concept of constants in C language, and variable values ​​are represented by variables in C language.
For example, some values ​​in life are constant (pi, gender, ID number, blood type, etc.) and
some values ​​are variable (such as age, weight, salary).

1.1 How to define variables

int age = 150;
float weight = 45.5f;
char ch = 'w';

1.2 Naming of variables

The name of the variable cannot be chosen casually, this is the only way for the program to find it. The following are variable naming rules.

  • Can only consist of letters (both uppercase and lowercase), numbers, and underscores (_).
  • Cannot start with a number.
  • The length cannot exceed 63 characters.
  • Variable names are case-sensitive.
  • Keywords cannot be used in variable names.

1.3 Classification of variables

  1. local variables
  2. global variables

1.4 The 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 range that limits the availability of the name is the scope of the name.

  1. The scope of a local variable is the local scope where the variable is located.
  2. The scope of global variables is the entire project.

life cycle

The life cycle of a variable refers to the period of time between the creation of the variable and its destruction.

  1. The life cycle of local variables is: the life cycle begins when entering the scope and ends when leaving the scope.
  2. The life cycle of global variables is: the life cycle of the entire program.

1.5 Constants

The definition forms of constants and variables in C language are different.
Constants in C language are divided into the following types:

  • literal constant
  • const modified constant variable
  • #define defined identifier constant
  • enum constants
#include <stdio.h>
//举例
enum Sex
{
    
    
	MALE,
	FEMALE,
	SECRET
};
//括号中的MALE,FEMALE,SECRET是枚举常量
int main()
{
    
    
	//字面常量演示
	3.14;//字面常量
	1000;//字面常量

	//const 修饰的常变量
	const float pai = 3.14f;
	//这里的pai是const修饰的常变量
	//pai = 5.14;//是不能直接修改的!

	//#define的标识符常量 演示
#define MAX 100
	printf("max = %d\n", MAX);

	//枚举常量演示
	printf("%d\n", MALE);
	printf("%d\n", FEMALE);
	printf("%d\n", SECRET);
	//注:枚举常量的默认是从0开始,依次向下递增1的
	return 0;
}

2. String + escape character + comment

2.1 Strings

"hello C语言.\n"

This string of characters enclosed by double quotes is called a string literal, or simply a string.

2.2 Escape characters

escape character Definition
\? Used when writing multiple question marks in a row to prevent them from being parsed into three-letter words
\’ used to represent character constants'
\" Used to represent double quotes inside a string
\\ Used to represent a backslash, preventing it from being interpreted as an escape sequence character
\a Warning character, buzzer
\b backspace character
\f Feed character
\n newline
\r Enter
\t horizontal tab
\v vertical flat tab
\ddd ddd represents 1-3 octal digits. Such as: 130 represents the character X
\xdd dd represents 2 hexadecimal characters, such as: x30 represents character 0

2.3 Notes

  1. If there is any unnecessary code in the code, you can delete it directly or comment it out.
  2. Some codes in the code are difficult to understand. You can add comments,
    such as:
#include <stdio.h>
int Add(int x, int y)
{
    
    
	return x + y;
}
/*C语言风格注释
int Sub(int x, int y)
{
    return x-y;
}
*/
int main()
{
    
    
	
	//调用Add函数,完成加法
	printf("%d\n", Add(1, 2));
	return 0;
}

Comments come in two flavors:

  • C-style comments/ xxxxxx /
    Defect: Cannot nest comments
  • C++ style comments //xxxxxxxx
    can comment on one line or multiple lines

Let’s learn some appetizers first, and I’ll give you more details next time

Guess you like

Origin blog.csdn.net/weixin_44752340/article/details/130100667