"C Language Elementary" Chapter 1 - Getting to Know C Language (1)


foreword

Getting to know C language for the first time is to understand the basic knowledge of C language, and understand the knowledge of each block of C language from a global concept.


The first C language program

#include<stdio.h>
int main()
{
    
    
	printf("hello world!\n");
	return 0;
}
//解释:
//main函数是程序的入口
//一个工程中main函数有且仅有一个

I believe that many veterans have seen or even written such a piece of code when they first learned this C language. Many friends may know that the English word print means printing and output. It is obvious that it is printed out in this program. hello world!Then, of course, the old iron is puzzled or curious about other code segments, such as #include<stdio.h>what does it mean? int mainWhat is it? returnWhy is it behind 0? Well, as long as you maintain this curiosity, then I will take you into the gate of C language, and slowly enter the ocean of knowledge with me!


Understanding Data Types

We have studied mathematics and know that there are integers, decimals, letters, etc. in mathematics, so how to express them in C language?

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

The existence of a data type is to define a variable, describe the characteristics of the variable and the size of the occupied space.

char ch = 'A';
int age = 18;
float hight = 180;

Here we take it int age = 18;as an example, which means to open up a int(整形)space of type , we name this space age, and assign a value to this space 18.

Knowing the data types in C language, you must not know their size.
Now let's learn the first operator in C language: sizeof
sizeof:是一个单目操作符,用来求类型、变量所占内存空间的大小,单位为字节

#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 shown in the figure below:
insert image description here

Well, it is said that the knowledge of computers is comprehensive. At this time, you should still want to know what a byte is? Then I use the form below to tell you~

unit conversion
bit the smallest
byte (byte) 1 byte=8 bit
KB 1KB=1024 byte
MB 1MB=1024KB
TB 1TB=1024GB
PB 1PB=1024TB

variable, constant

1. Straight to the point:

Variable: It is the amount that will change, which means that the value of this step of the program is a, and the next step becomes b.
Constant: Median value of life is constant (ID card, pi, gender...)

2. How to define variables:

Type name variable name = initial value;
Example:int age = 18;

3. Classification of variables (according to scope of use):

  • global variable
  • local variable
#include<stdio.h>
int age = 18;//全局变量
int main()
{
    
    
	int age = 10;//局部变量
	int sum = 0;//局部变量
	return 0;
}

4. The scope of variables

Conceptually, the scope is the scope of application of the variable
局部变量的作用域:局部变量所在的局部范围;
全局变量的作用域:整个工程;

5. Life cycle

Refers to: the time period between the creation of a variable and the destruction of the variable.

  • 局部变量The life cycle is: enter the scope life cycle start, out of the scope life cycle end.
  • 全局变量The life cycle is: the life cycle of the entire program.

6. Definition of constants

  • literal constant
  • const modified constant variable
  • Identifier constants defined by #define
  • enumeration constant

strings, escape characters, comments

1. String

"hello world"

A string of characters surrounded by double quotes is called a string, whereas a string of characters surrounded by single quotes is called a character, don't get confused.
content.
example:

'A'

注:字符串的结束标志是一个 ‘\0’ 的转义字符。在计算字符串长度的时候 ‘\0’ 是结束标志,不算作字符串。

2. Escape characters

|

escape character paraphrase
\? Used when writing multiple question marks in a row, preventing them from being parsed into triplets
\’ Used to represent character constants'
\" Used to denote double quotes inside a string
\\ Used to denote a backslash, preventing it from being interpreted as an escape sequence
\a warning character, beep
\b backspace
\f Feed character
\n new line
\r carriage return
\t horizontal tab
\v vertical tab
\ddd ddd represents 1-3 octal numbers. For example: \130 means character X
\xdd dd means 2 hexadecimal digits. For example: \x30 means character 0

3. Notes

Used to describe the text information of the code, so that people can understand the function of the code

Two styles:
1. /*xxxxxxx*/Indicates multi-line comments, and comments cannot be nested.
example:

/*#include<stdio.h>
int age = 18;//
int main()
{
	int age = 10;
	int sum = 0;
	return 0;
}*/

2. //xxxxxxx, you can comment one line or multiple lines.
example:

//#include<stdio.h>
//int age = 18;//
//int main()
//{
    
    
	//int age = 10;
	//int sum = 0;
	//return 0;
//}

Comment function:
Unnecessary code can be deleted or commented;
some code is difficult to understand, you can comment the text


This is the end of the first chapter of the first introduction to C language. If you have any ideas, see the comment area~

Guess you like

Origin blog.csdn.net/hsjsiwkwm/article/details/130901581