An overview of C language and programming

Chapter 1 Overview of C Language and Programming




Introduction to C Language

C language is a compiled programming language, which is developed on the basis of B language. Its kernel was originally written in assembly language. Assembly language is a machine-oriented language, and the quality of the generated code is high; but its readability and portability are poor, and its description of problems is far less close to the human expression habits than high-level languages. The original development purpose of C language is to write operating system and other system programs. It has some characteristics of assembly language and the characteristics of high-level language at the same time. Its roots can be traced back to Algol 60.
In 1963, the University of Cambridge in the United Kingdom launched the CPL (Combined Programming Language) language on the basis of Algol 60. It is closer to the hardware, but the scale is large and difficult to achieve.
In 1967, Martin Richards of Cambridge University in the United Kingdom simplified the CPL language and developed the BCPL (Basic Combined Programming Language) language.
In 1970, Ken Thompson of Bell Labs in the United States further simplified the BCPL language, designed a simpler and hardware-like B language, and used the B language to write the UNIX operating system in the DEC PDP-7 computer.
In 1973, Dennis Ritchie of Bell Labs in the United States designed the C language on the basis of the B language, and wrote the UNIX operating system in the C language for the first time, which was applied on the DEC PDP-11 computer.
In the late 1970s, C language gradually became the standard language for developing UNIX operating system; with the popularity of UNIX operating system, C language has also been rapidly promoted and applied. Later, C language was transplanted to the operating systems of large computers, workstations and other models, and gradually became a general language for compiling various operating systems and complex system software.

Baidu Encyclopedia Link: Click here to get there quickly


The following is the text of this article

First, the main features of the C language

1. C is a structured language

C language is provided to users in the form of functions, and is equipped with structured control statements. These functions can be easily called and the program can realize modular design.

2. The language is concise and compact, easy to use and flexible.

The C language has only 32 keywords and 9 control statements, and the writing form of the program is also very free.

3.C language allows direct access to physical addresses, can perform bit operations, can implement most of the functions of assembly language, and can directly operate hardware

C language can directly access memory physical addresses and hardware registers, and directly express operations on binary bits. Its program is concise, and the compiled program is small in size.

4. Rich in data types, with various data structures in modern languages.

The C language has rich data types. In addition to the basic data types: integer (int), real type (float and double), and character type (char), it also has various structural types and introduces the concept of pointers.

5. Operators are extremely rich.

There are 34 operators in C language. Parentheses, assignments, and forced type conversions all appear in the form of operators, making C language extremely powerful in performance and processing, and many algorithms are easier to implement.

6. The portability of C language programs is good (compared with assembly language).

Programs written in C language can be run on various types of computers or operating systems without modification or with minor modifications.

7. The target code generated by C language is of high quality and the program execution efficiency is high.

Code quality refers to the running speed and storage space size of the target program generated after the C program is compiled. In general, the higher the speed and the less storage space it takes, the better the code quality.

8. The syntax of C language is flexible and the restrictions are not very strict.

Although C language is also a strongly typed language, its syntax is more flexible, allowing programmers to have a greater degree of freedom.

Second, the C language program structure

1. The structure of C language program and its main features

[Example 1.1] Write a program to display "Hello World!" on the computer screen.

#include <stdio.h>//库函数
int main()//main( )是C语言程序中的主函数
{
   /* 我的第一个 C 程序 */
   printf("Hello, World! \n");//“printf (…… );//是C编译系统提供的标准函数库中的输出函数
   return 0;
}//“{” 、“}”是main函数体的标识符

[Example 1.2] The function returns the larger of the two numbers

/* 函数返回两个数中较大的那个数 */
#include<stdio.h>
int max(int num1, int num2) 
{
    
    
   /* 局部变量声明 */
   int result;
   if (num1 > num2)// “if...else...”是条件控制语句
      result = num1;
   else
      result = num2;
   return result; 
}
int main()
{
    
    
	int a,b;/*变量声明*/
	scanf("%d%d",&a,&b);//格式化输入
	/*scanf是C编译系统的标准输入函数,从键盘上接收输入的数据;
	scanf 圆括号中的“%d”是格式控制符,表示输入的数据是十进制整数;
	“&n”是地址列表,表示从键盘接收的十进制整数存入变量n的内存地址&n中。*/
	printf("%d",max(a,b));
	return 0;
}

From [Example 1.1] and [Example 1.2], we can see the structure and characteristics of C language programs:

  1. Function is the basic unit of C language program structure

C language is a structured programming language, which is a kind of thinking. It is just a set of text structure description system for the convenience of writing programs and cross-compiler compiling programs. The points to be paid attention to when writing programs should also be changed according to the compiler and target platform. Although the C language has a general standard, it will also There are differences depending on the compiler and target environment. All functions in C language are independent of each other, and there is only a calling relationship between them.

  1. C program has only one main function

A C language program must have one and only one main function, main(), which is the entry point of the program. Everything has a beginning. After the C language process is successfully created, the main thread will be created. The main thread calls the convention to start the runtime library, and the runtime library starts to call the agreed main function, and then starts to execute the user's code. If another auxiliary thread starts up in the middle, it will make a function agreement for the auxiliary thread, telling it which function to start executing after it starts up. To sum up, the main function is just a convention for the execution of a thread, and each thread will agree on the name of the function to start execution.

  1. The writing format of C language programs is relatively free

Every statement in C language must end with ";". The writing style of C statements is relatively free. One or more statements can be written on a line, and a statement can also be written on multiple lines (add "\" statement connector at the end of the line). In actual writing, you should pay attention to the writing format of the program, which should be easy to read and understand.

  1. The C language itself has no input/output statements

C language itself does not provide input and output statements, and the operations of input and output are realized by functions. The standard function library of C language provides some input and output functions, such as: printf function and scanf function. However, the printf and scanf functions are not keywords of the C language, they are just the names of the functions, and they cannot be mistaken for the "input and output statements provided by the C language.

  1. C language can be compiled with preprocessor commands

Lines starting with "#" are called macro definitions or file inclusions, and are compilation preprocessing commands in C language, without the ";" sign at the end. Each compilation command needs to be on a separate line.

  1. Identifiers in C language are case sensitive

The keywords reserved by the system consist of lowercase letters. User-defined variable names, function names and other identifiers are generally composed of lowercase letters, but cannot occupy keywords reserved by the system.

  1. The use of declaration statement in C language

Various quantities (identifiers) used in C language programs must be defined and used, and sometimes variable reference descriptions and function reference descriptions are added.

  1. The use of annotation information in C language

The format of comment information in C language is: /* comment content*/ (multi-line comment) or // comment content (single-line comment). Comments only increase the readability of the program, but are not executed by the computer.

2. Identifiers in C language

Variable names, function names, labels, etc. used in a program are collectively referred to as identifiers. Except for the function names of library functions, which are defined by the system, the rest are user-defined. The C language stipulates that an identifier can only be a string consisting of letters (A~Z, a~z), numbers (0~9) and underscores, and the first character of an identifier must be a letter or an underscore.

The following identifiers are legal:

abc,x,_abc,BOOK_3,flag1

The following identifiers are illegal:

6s starts with a digit
S&T illegal character & -7z
starts with minus sign
kingboy-2 illegal character - (minus sign)

When using identifiers, you must also pay attention to the following points:
①Standard C does not limit the length of identifiers, but it is limited by various versions of the C language compilation system, and is also limited by specific machines. For example, a certain version C stipulates that the first eight bits of an identifier are valid, and when the first eight bits of two identifiers are the same, they are considered to be the same identifier.
②In identifiers, there is a difference between upper and lower case. For example HELLO and hello are two different identifiers.
③ Although the identifier can be arbitrarily defined by the programmer, the identifier is a symbol used to identify a certain quantity. The naming should have the corresponding meaning as much as possible to facilitate reading and understanding; it is generally expressed in English words, and try to "see the name and know the meaning." ".

3. Keywords in C language

A keyword is a character string with a specific meaning specified by the C language, which is also called a reserved word. User-defined identifiers cannot be the same as keywords.

Data type keywords (12) Control statement keywords (12) Storage type keywords (4) Other types of keywords (4)
char break auto const
double case external sizeof
enum continue register typedef
float default static volatile
int do
long else
short for
signed goto
struct if
union return
unsigned While
void switch

Three, the steps of C language program implementation

insert image description here
A C language program needs to go through four steps from writing to running on the computer: editing, compiling, linking and running.
①Edit. Write a C language source program and edit it on the computer, generate a source program *.c with a suffix of .c, and save it to disk.
② Compile (Compile). Use the C language compiler to compile the *.c source program generated in the previous step.
③ Connect. Compile the generated target program *.obj, connect and assemble the target program *.obj with system functions and library functions referenced by header files, etc., and finally generate an executable program *.exe with the suffix .exe.
④Run. The *.exe program generated in the previous step can be executed by the computer, and the result of the operation is obtained, and the output is displayed.


Summarize

Tip: Here's a summary of the article: The
above is what I'm going to talk about today. This article only briefly introduces the C language and programming overview. This article is updated for a long time, please pay attention so as not to miss the news.

Guess you like

Origin blog.csdn.net/qq_42866708/article/details/112392885
Recommended