First knowledge of C language (1)

What is C language
   1. The first C language program
   2. Data types
   3. Variables, constants
   4. Strings + escape characters + comments
   5. Selection statements
   6. Loop statements
   7. Functions
   8. Arrays
   9. Operators
 10 .common keywords
 11.define defines constants and macros
 12. pointers
 13. structures


What is the C language?
C language is a general-purpose computer programming language, which is widely used in low-level development. The design goal of the C language is to provide a programming language that can be
compiled in an easy way, handle low-level memory, generate a small amount of machine code, and can run without any operating environment support
.
Although the C language provides many low-level processing functions, it still maintains a good cross-platform feature. A
C language program written in a standard specification can be compiled on many computer platforms, even including some embedded processors (single-chip or called MCU) and supercomputers
and other operating platforms.
In the 1980s, in order to avoid differences in the C language grammar used by various developers, the US National Bureau of Standards
formulated , called ANSI C, as the original C language standard. [1] Currently on December 8, 2011
, the C11 standard released by the International Organization for Standardization (ISO) and the International Electrotechnical Commission (IEC) is the third official
standard latest standard of the C language, which is better It supports Chinese character function names and Chinese character identifiers, and realizes Chinese
character programming to a certain extent.
C language is a process-oriented computer programming language, which is different from object-oriented programming languages ​​such as C++ and Java.
Its compilers mainly include Clang, GCC, WIN-TC, SUBLIME, MSVC, Turbo C, etc.


 1. The first C language program

Next, let's write the first C language program. printf is a library function. Its function is to print data information-output on the screen.
Library functions are functions provided in the standard library. These functions are ready-made and can be used directly. However, using The library function needs to include the corresponding header file, and the header file stdio.h required by the input/output function.

#define _CRT_SECURE_NO_WARNINGS 1

#include <stdio.h>

//main - 主函数
//main函数是程序的入口
//不管你的代码有多少行,都是从main函数的第一行开始执行的
//在一个工程中,可以有多个.c文件,但是main有且仅有一个

//推荐写法
int main() 
{
	printf("hello C\n");
	return 0;
}

2. Data type

char
short
int
long
long long
float
double
// character data type
// short integer
// integer
// long integer
// longer integer //
single precision floating point number
// double precision floating point number

Note: There are so many types, in fact, to express various values ​​in life more abundantly.

qwc is a character type, an integer is an integer, and a decimal is a floating point type. 

 Next, in order to better understand the data types, we use sizeof() to find their sizes. The unit is byte, and the conversion of each unit is also shown in the figure below.


3. Variables, constants

Some values ​​in life are constant (such as: pi, gender, ID number, blood type, etc.) and
some values ​​are variable (such as: age, weight, salary).

Unchanged value, represented by the concept of constant in C language, becomes value represented by variable in C language.


3.1 Method of defining variables

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

3.2 Classification of variables


1. Local variables
2. Global variables

#include <stdio.h>
int global = 2019;//全局变量
int main()
{
int local = 2018;//局部变量
//下面定义的global会不会有问题?
int global = 2020;//局部变量
printf("global = %d\n", global);
return 0;
}

There is actually nothing wrong with the definition of the local variable global variable above!
When a local variable has the same name as a global variable, the local variable takes precedence. 

3.3 Use of variables 

This code can complete the addition of two integers.

#include <stdio.h>
int main()
{
int num1 = 0;
int num2 = 0;
int sum = 0;
printf("输入两个操作数:>");
scanf("%d %d", &num1, &num2);
sum = num1 + num2;
printf("sum = %d\n", sum);
return 0;
}

Scanf is the input function, and printf is the output function, both of which are library functions. These two functions are usually used in pairs. Some compilers do not support the scanf function, and may report an error saying that it is not safe. We can write #define _CRT_SECURE_NO_WARNINGS 1 at the top of the source file.

 3.4 The scope and life cycle of variables

scope

Scope (scope) is a programming concept, generally speaking, the names used in a piece of program code are not always valid/usable.
The scope of 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 in which the variable resides.
2. The scope of global variables is the entire project.
life cycle

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

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

int g = 2023;
int main()
{
	int a = 10;
	printf("g1 = %d\n", g);
	printf("a = %d\n", a);
	{
		int b = 100;
		printf("b = %d\n", b);
		printf("g2 = %d\n", g);
	}
	//printf("b = %d\n", b);
	printf("g3 = %d\n", g);

	return 0;
}

b is a local variable, and it will be destroyed when it leaves the scope where it is located, so the compiler will report an error. 

 But g is a global variable, its scope and life cycle are the entire project, so the compiler will not report an error. 

 When we place variables in another source file, we only need to declare them with extern, which is to declare external symbols.

 3.5 Constants

The definition forms of constants and variables in C language are different.
The constants in C language are divided into the following types:
  literal constant
  const modified constant variable
  #define defined identifier constant
  enumeration constant

#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;
}

4. String + escape character + comment

4.1 Strings

"holle C.\n"

This string of characters enclosed by double quotes (Double Quote) is called a string literal (String Literal), or simply a string.
Note: The end of the string is a \0 escape character. When calculating the length of the string, \0 is the end mark, and it is not counted as the content of the string.
 

#include <stdio.h>
//下面代码,打印结果是什么?为什么?(突出'\0'的重要性)
int main()
{
char arr1[] = "bit";
char arr2[] = {'b', 'i', 't'};
char arr3[] = {'b', 'i', 't', '\0'};
printf("%s\n", arr1);
printf("%s\n", arr2);
printf("%s\n", arr3);
return 0;
}
#include <stdio.h>
int main()
{
printf("c:\code\test.c\n");
return 0;
}

Why is this? Because arr2 does not have \0, the compiler cannot find it, so it keeps searching and searching, and it crashes, so \0 is very important. 

 4.2 Escape characters

Let's say we want to print a directory on the screen: c:\code\test.c
How should we write the code?

#include <stdio.h>
int main()
{
printf("c:\code\test.c\n");
return 0;
}

In fact the result is as follows:

Here I have to mention the escape character. The escape character, as the name suggests, is to change the meaning.
Let's look at some escape characters.

escape character paraphrase
\? Used when writing multiple question marks in a row, preventing them from being parsed into three-letter words
\' 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. Such as: \130 X
\xdd dd means 2 hexadecimal digits. Such as: \x30 0

Let's write a code to deepen our understanding of escape characters. 

#include <stdio.h>
int main()
{
//问题1:在屏幕上打印一个单引号',怎么做?
//问题2:在屏幕上打印一个字符串,字符串的内容是一个双引号“,怎么做?
printf("%c\n", '\'');
printf("%s\n", "\"");
return 0;
}

Next, let's look at a classic question. What do you think the result is?

#include <stdio.h>
int main()
{
printf("%d\n", strlen("abcdef"));
// \62被解析成一个转义字符
printf("%d\n", strlen("c:\test\628\test.c"));
return 0;
}

 The first one is because of encountering \0, so there are 6. This is very simple, but what does the second one mean? In fact, it is like this, c|:|\t|e|s|t|\62|8|\t|e|s|t|.|c|, please count if there are 14, why not 13 Well, because 8 is beyond the range of octal, it is counted as one alone.


This is the end of today's sharing, thank you for reading, and see you next time.

Guess you like

Origin blog.csdn.net/2301_79035870/article/details/132134377