当前位置:网站首页>First knowledge of C language -- constants and variables
First knowledge of C language -- constants and variables
2022-07-27 05:37:00 【yin_ Yin】
List of articles
Some values in life are constant ( such as : PI , Gender , Id card number , Blood type and so on )
Some values are variable ( such as : Age , weight , Salary ).
Constant value ,C The concept of constant is used in language to express , Become value C In languages, variables are used to represent .
1. How to define variables
grammar : data type Variable name = The value to initialize
int age = 150;
float weight = 45.5f;
char ch = 'w';
2. Variable naming requirements
1. Only letters ( Including uppercase and lowercase )、 Numbers and underscores ( _ ) form .
2. Cannot start with a number .
3. The length cannot exceed 63 Characters .
4. Variable names are case sensitive .
5. Variable names cannot use keywords .
#include <stdio.h>
int main()
{
int d_9 = 8;
//int 6_p = 7;
int a = 3;
int A = 4;
printf("a=%d A=%d", a, A);
//int if = 9;
return 0;
}


3. Classification of variables
Variables are divided into Local and global variables , We can think of it this way :
Global variables are defined in { } External variables , If the global variable is not initialized, the default value is 0.

Local variables are defined in { } External variables .

Then think about a problem , Local and global variables can The same name Do you ?
The answer is yes !
Look at this piece of code :
#include <stdio.h>
int global = 2019;// Global variables
int main()
{
int local = 2018;// local variable
// As defined below global Will there be a problem ?
int global = 2020;// local variable
printf("global = %d\n", global);
return 0; }

however , We don't recommend writing the names of local variables and global variables the same .
4. The scope and life cycle of variables
First of all, we need to know what the scope is ?
Scope (scope) It's a programming concept , Generally speaking , The names used in a program are not always valid / The scope of available code that limits the availability of the name is the scope of the name .
1. The scope of a local variable is the local scope of the variable .
2. The scope of global variables is the whole project .
Look at a piece of code :
void test()
{
int i = 9;
printf("%d\n", p);
}
#include <stdio.h>
int p = 8;
int main()
{
test();
printf("%d\n", i);
printf("%d\n", p);
return 0;
}


Then let's discuss it next , What is life cycle ?
The life cycle of a variable is the period between the creation of a variable and its destruction
1. The life cycle of a local variable is : Enter the scope lifecycle begins , Out of scope life cycle ends .
2. The life cycle of global variables is : The whole life cycle of the program .

5. Constant
C There are differences in the form of definitions between constants and variables in languages .
C Constants in a language can be divided into the following categories :
Literal constants
const Modified constant variable
#define Defined identifier constant
Enumeration constants
1. First, we introduce literal constants :
It's like 3.14,100,‘a’ , “abcdf”. These are literal constants
int main()
{
3.14;
5;
300;
's';
"adsfis";// These are literal constants
"hello world";
return 0;
}
2. const Modified constant variable
const The modified constant is C In language, it is only restricted at the grammatical level that this variable cannot be changed directly , But it's essentially a variable , So it's called a constant variable .
for instance :
#include <stdio.h>
int main()
{
const int i = 9;
i = 0;
return 0;
}

3.#define Defined identifier constant
grammar :#define Constant names value
//#define Defined identifier constant
#define max 100
int main()
{
max = 99;
int arr[max]={
0};
return 0;
}

4. Enumeration constants
Enumeration constants are constants that can be enumerated one by one , Enumeration keywords are required to define enumeration types enum.
enum Is a data type in a computer programming language . Enumeration type : In practical terms , Some variables are limited in a limited range . for example , There are only seven days in a week , There are only twelve months in a year , A class has six courses a week and so on . If we describe these quantities as integers , Character type or other types are obviously inappropriate . So ,C Language provides a language called “ enumeration ” The type of . stay “ enumeration ” All possible values are listed in the definition of type , Described as the “ enumeration ” The variable value of type cannot exceed the defined range . It should be noted that , Enumeration type is a basic data type , Not a construction type , Because it can no longer be broken down into any basic type .
for instance , For example, gender , For gender , Men and women , We can list them one by one , Code up :
#include <stdio.h>
int main()
{
enum Sex //enum Enumerate keywords
{
MALE,
FEMALE,
SECRET // Possible values of gender enumeration types
};
printf("%d\n", MALE);
printf("%d\n", FEMALE);
printf("%d\n", SECRET);
enum Sex s = MALE;
// notes : The default of enumeration constants is from 0 Start , Increase in descending order 1 Of
return 0;
}

That's right C Introduction of constants and variables in language !
边栏推荐
猜你喜欢
随机推荐
Redis publish subscribe mode
Pytorch installation new pit
分享一道关于变量的选择题(内含全局变量、局部变量、变量的作用域、生命周期知识点)
努力进化中_我的第一篇csdn博客
Cenos7更新MariaDB
Introduction to C language pointer
块,行内块元素之间存在间隙
初始C语言——关键字static的作用
初识C语言——初识指针
C语言中堆内存介绍和管理
初识C语言——字符串+转义字符+注释
js基础练习题
elment-ui使用方法
Student management system
Li Hongyi machine learning team learning punch in activity day01 --- introduction to machine learning
Looking at the PK of alphago and Li Shishi from a deep perspective
Localdatetime and zoneddatetime
如何将Excel表格中的多列内容合并到一列
Redis persistence
Source code of document type full-text retrieval knowledge base management system









