[C language - first understanding of C language (1)]


foreword

The content of the C language series will be divided into several articles. The purpose is to quickly go through most of the content in the C language and have a preliminary and comprehensive understanding of the C language . The content mainly includes:

  • type of data
  • variable constant
  • String escape character comment
  • select statement
  • loop statement
  • function
  • array
  • operator
  • common keywords
  • define defines constants and macros
  • pointer
  • structure

When the update of the C language series for beginners ends, the content learning of the basic stage of the C language will officially begin.


1. The first C language program

For beginners, learn a programming language: first, you must have a computer, and then install the necessary software. The win system is VS, and I installed VS2017. For us beginners, we should first focus on the language itself, and then go deep into data structures and algorithms.
Getting started shouldn't spend time struggling with what programming tool to use. After installing the tool, write the first C language program, copy it manually according to the program, press ctrl+s to save the program, press ctrl+F5 to run the program, and output Hello World!.

#include<stdio.h>
//第一个程序 
int main()
{
    
    
	printf("Hello World!\n");//\n表示换行
	return 0;
}

2. Data type

char //字符数据类型
short //短整型
int //整形
long //长整型
long long //更长的整形
float //单精度浮点数
double //双精度浮点数

sizeof can read the bytes occupied by the data 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));
	printf("%d\n", sizeof(long double));
return 0;
}

The running result is as follows: insert image description here
the common char character data type occupies one byte, and the int integer occupies 4 bytes.
A bit, that is, a bit, is the smallest unit in a computer and is represented as a binary, 0 or 1. byte, byte, 1byte = 8bit, that is, 1 byte is represented by 8 binary, such as 11111111.
Commonly used units are 1KB=1024byte, 1MB=1024KB, 1GB=1024MB, 1TB=1024GB, 1PB=1024TB.

3. Variables, constants

3.1 How to define variables

In the C language, constants are used to represent constant values, such as gender, ID number, and so on. Use variables to represent changing values, such as age, weight, salary, etc.
code show as below:

int age = 50;//年龄
float weight = 45.5f;//体重
char ch = 'w';//字符

In the first line of code above:
int is a keyword, indicating an integer type;
age is a variable name, indicating age;
50 is a numerical value, indicating age;
the middle = is an equal sign, indicating assignment, assign 150 on the right side of the equation to the left side The
last line ends with a semicolon, indicating the end of the line; the
general variable name must have actual expressive meaning, do not define it as a, b, which seems very casual, and develop a good habit, of course, usually yourself Learning can be casual.

3.2 Classification of variables

Local variables and global variables
All variables: defined in front of the main function main, such as global.
Local variables: inside the main function, such as a.
Global variables can be reassigned within the main function, and the output becomes 2022.
When a local variable and a global variable have the same name, the local variable takes precedence.

int global = 2021;//global是全局变量,

int main()
{
    
    //主函数内部定义的变量都是局部变量 
	int a = 100;//局部变量
	int global = 2022;//局部变量可以重新赋值

	printf("%d\n", global);//

	return 0;
}

3.3 Use of variables

scanf is input, a function that accepts input from the console, and is used frequently later.
printf is output, which outputs the object to be monitored to the console, which is often used in debugging programs later.
Write code in strict accordance with the format to avoid errors.

int main()
{
    
     
	int num1 = 0;
	int num2 = 0;
	int sum = 0;

	printf("输入两个数:  ");
	//输入函数,后面会经常用到的 ,严格按格式编写 
	scanf("%d %d", &num1, &num2);

	sum = num1 + num2;
	
	printf("%d\n", sum);//老习惯了

	return 0;
}

3.4 Scope and Lifecycle of Variables

Scope:

  1. The scope of a local variable is the local scope in which the variable resides.
  2. The scope of global variables is the entire project.

life cycle:

  1. The life cycle of a local variable is: the life cycle of entering the scope begins, and the life cycle out of the scope ends.
  2. The life cycle of global variables is: the life cycle of the entire program.
int a = 100;

void test()
{
    
    
	printf("test:: %d\n", a);
}

int main()
{
    
    
	test();
	printf("main:: %d\n", a);

	return 0;
}

The above a is a global variable, and the scope is within the entire project, and the source file embodied in the entire code works. Both the main function and the custom function can output normally. The output is as follows:
insert image description here
In the following code, a is a local variable, and its job field is in the parentheses. If the parentheses are out, it will not work, and the printf function will display undefined.
insert image description here

3.5 Constants

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

  • literal constant
  • const-modified constant
  • Identifier constants defined by #define
  • enum constant
enum Sex
{
    
    //都是枚举常量
	MALE,//注意结尾是逗号,不是分号
	FEMALE,
	SECRET //没有符号
};

int main()
{
    
    
	//字面常量
	3.14;//字面常量
	1000;//字面常量

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

	//#define的标识符常量
	#define MAX 100//一般宏定义的常量都要大写

	//枚举常量默认从0开始,依次向下加1
	printf("%d\n", MALE);//输出为0
	printf("%d\n", FEMALE);//输出为1
	printf("%d\n", SECRET);//输出为2
	
	return 0;
}

Summarize

The C language series for the first time is written based on the content of the C language video content of station B. It is also to practice typing speed and programming feel according to the video files. The content layout is completely in accordance with the blackboard.

Writing a blog by yourself is also convenient for you to consolidate and learn, record your learning progress, and change the content if you have new experience in subsequent review. I am a mechanical engineering major. 51 single-chip microcomputer and STM32 series single-chip microcomputer are all programmed in C language. Learning C language well and combining software and hardware will lay a solid foundation for future employment, and at the same time can potentially broaden employment opportunities.

I have learned about branches and loops from the video, but typing is very slow, which will also affect the update speed. If there are any mistakes in the content, please feel free to comment and correct me. After all, I am a beginner in C language, thank you very much.

Guess you like

Origin blog.csdn.net/taibudong1991/article/details/123604546