http://www.shucunwang.com/RunCode/c/
http://codepad.org/TIbKemXJ
http://www.manonggu.com/biancheng/
C Language Resource Collection in Chinese Version
https://github.com/jobbole/awesome-c-cn
【Raw】Compare C Language and Machine Language (English, for reference)
https://www.bilibili.com/video/av11237858
Yingsi Teaching Network (C language content mainly based on Turbo C)
http://www.winyes.com/
Bilibili C Language
https://www.bilibili.com/video/av9430200/?from=search&seid=8131884833781373784
Sogou Turbo C Chinese Version Download (Use the localized version in the run wizard, run the program in virtual machine mode)
http://xiazai.sogou.com/detail/34/16/902784868571182432.html?uID=440DBA3D4F0A900A00000000598FB273
C Language
Locked
Synonym C (computer language) generally refers to C language
This entry is reviewed by the "Science Popularization China" Encyclopedia Science Entry Writing and Application Project.
C language is a general-purpose computer programming language with wide applications. The design goal of C language is to provide a programming language that can be compiled easily, handle low-level memory, generate a small amount of machine code, and can run without any runtime environment support.
Although C language provides many low-level processing functions, it still maintains good cross-platform characteristics. A C language program written according to a standard specification can be compiled on many computer platforms, even including some embedded processors (microcontrollers or MCUs) and supercomputer operating platforms.
In the 1980s, in order to avoid differences in the C language syntax used by various developers, the American National Standards Institute formulated a complete international standard syntax for C language, called ANSI C, as the initial standard of C language.
Chinese Name
C Language
English Name
The C Programming Language
Category
Computer Programming Language
Founder
Dennis MacAlistair Ritchie
Founding Time
1972
Originated From
BCPL Language
Main Compilers
Clang, GCC, MSVC, Turbo C, etc.
Inspired Languages
B Language, Assembly, ALGOL68
Operating System
Multi-platform
Table of Contents
1. Development History
2. Programming and Development
▪ Compiler
▪ Integrated Development Environment (IDE)
▪ Hello World
3. Language Standards
▪ K&R C
▪ ANSI C / C89 Standard
▪ C99 Standard
▪ C11 Standard
4. Language Features
▪ Basic Characteristics
▪ Unique Features
▪ Advantages and Disadvantages
▪ New Features of C11
5. Language Components
▪ Basic Composition
▪ Keywords
▪ Syntax Structure
▪ Program Structure
▪ Function
▪ Operators
6. Classic Errors
Development History
The reason why C language is named C is that C language originated from the B language invented by Ken Thompson, and the B language originated from the BCPL language.
In 1967, Martin Richards from Cambridge University simplified the CPL language, thus producing the BCPL (Basic Combined Programming Language) language.
C Language Promotion Image C Language Promotion Image
In the 1960s, Ken Thompson, a researcher from Bell Laboratories of AT&T (AT&T Bell Laboratory) in the United States, was idle and itchy, wanting to play an electronic game he made himself, Space Travel. He found an idle machine, PDP-7, behind his boss. But this machine had no operating system, and the game had to use some functions of the operating system, so he started to develop an operating system for PDP-7. Later, this operating system was named - UNIX.
In 1970, Ken Thompson from Bell Laboratories in the United States, based on the BCPL language, designed a very simple and hardware-closer B language (taking the first letter of BCPL). And he wrote the first UNIX operating system in B language.
In 1971, Dennis M. Ritchie, who also loved Space Travel, joined Thompson's development project to cooperate in the development of UNIX in order to play the game earlier. His main job was to transform the B language to make it more mature.
In 1972, D.M. Ritchie from Bell Laboratories in the United States finally designed a new language based on the B language. He took the second letter of BCPL as the name of this language, which is the C language.
At the beginning of 1973, the main body of the C language was completed. Thompson and Ritchie couldn't wait to rewrite UNIX completely with it. At this time, the fun of programming made them completely forget that "Space Travel" and devoted themselves wholeheartedly to the development of UNIX and C language. With the development of UNIX, the C language itself is also constantly improving. Until today, various versions of UNIX kernels and surrounding tools still use C language as the most important development language, and many of them still inherit the code from Thompson and Ritchie.
During the development, they also considered porting UNIX to other types of computers. The strong portability (Portability) of the C language is manifested here. Both machine language and assembly language are not portable. A program developed for x86 cannot run on machines such as Alpha, SPARC, and ARM. The C language program can be used on processors of any architecture, as long as the processor of that architecture has a corresponding C language compiler and library, and then the C source code is compiled and linked into a target binary file to run.
In 1977, Dennis M. Ritchie published the C language compilation text "Portable C Language Compiler" that is not dependent on a specific machine system.
The C language continued to develop. In 1982, many knowledgeable people and the American National Standards Institute decided to establish a C standard committee to establish the standard of the C language in order to make this language develop healthily. The committee is composed of hardware manufacturers, compiler and other software tool manufacturers, software designers, consultants, academic people, C language authors, and application programmers. In 1989, ANSI released the first complete C language standard - ANSI X3.159-1989, referred to as "C89", but people are also used to calling it "ANSI C". C89 was adopted unchanged by the International Organization for Standardization (ISO) in 1990. The official name given by ISO is: ISO/IEC 9899, so ISO/IEC9899: 1990 is also usually referred to as "C90" for short. In 1999, after making some necessary corrections and improvements, ISO released a new C language standard, named ISO/IEC 9899: 1999, referred to as "C99". On December 8, 2011, ISO officially released a new standard, called ISO/IEC9899: 2011, referred to as "C11".
Programming and Development
Compiler
GCC, an open-source and free compiler developed by the GNU organization
MinGW, GCC under the Windows operating system
Clang, an open-source BSD-licensed compiler based on LLVM
Visual C++ :: cl.exe, the compiler built into Microsoft VC++
Integrated Development Environment (IDE)
CodeBlocks, an open-source and free C/C++ IDE
CodeLite, an open-source, cross-platform C/C++ integrated development environment
Orwell Dev-C++, a portable C/C++ IDE
C-Free
Light Table
Visual Stdio series
Hello World
The following is a simple program that prints the string "Hello, world!" on the standard output device (stdout). A similar program is usually the first program when learning a programming language:
1
2
3
4
5
6
7
#include <stdio.h>
int main(void)
{
printf("Hello, world!");
return 0;
}
Language Standards
K&R C
At first, there was no official standard for the C language. In 1978, it was officially published by Bell Laboratories of AT&T (AT&T). Brian Kernighan and Dennis Ritchie published a book called "The C Programming Language". This book is called K&R by C language developers and has been regarded as an informal standard description of the C language for many years. People call this version of the C language K&R C.
K&R C mainly introduces the following features:
Struct (struct) type
Long integer (long int) type
Unsigned integer (unsigned int) type
Change the operators =+ and =- to += and -=. Because =+ and =- will make the compiler not know whether the user wants to handle i = -10 or i =- 10, causing confusion in processing.
Even many years after the ANSI C standard was proposed, K&R C is still the most accurate requirement of many compilers, and many old compilers still run the K&R C standard.
ANSI C / C89 Standard
From the 1970s to the 1980s, the C language was widely used, from large mainframes to small microcomputers, and many different versions of the C language also derived.
In 1983, the American National Standards Institute (ANSI) established a committee X3J11 to formulate the C language standard.
In 1989, the American National Standards Institute (ANSI) passed the C language standard, which is called ANSI X3.159-1989 "Programming Language C". Because this standard was passed in 1989, it is generally referred to as the C89 standard. Some people also refer to it as ANSI C because this standard was released by the American National Standards Institute (ANSI).
In 1990, the International Organization for Standardization (ISO) and the International Electrotechnical Commission (IEC) set the C89 standard as the international standard of the C language, named ISO/IEC 9899:1990 - Programming languages -- C . Because this standard was released in 1990, some people simply call it the C90 standard. However, most people still call it the C89 standard because this standard is completely equivalent to the ANSI C89 standard.
In 1994, the International Organization for Standardization (ISO) and the International Electrotechnical Commission (IEC) released a revised version of the C89 standard, named ISO/IEC 9899:1990/Cor 1:1994 , some people simply call it the C94 standard.
In 1995, the International Organization for Standardization (ISO) and the International Electrotechnical Commission (IEC) released another revised version of the C89 standard, named ISO/IEC 9899:1990/Amd 1:1995 - C Integrity , some people simply call it the C95 standard.
C99 Standard
In January 1999, the International Organization for Standardization (ISO) and the International Electrotechnical Commission (IEC) released a new standard for the C language, named ISO/IEC 9899:1999 - Programming languages -- C , referred to as the C99 standard. This is the second official standard of the C language.
The features included in C99 are:
Increased restrictions on the compiler, such as each line of the source program requiring at least 4095 bytes of support, and variable names and function names requiring 63 bytes of support (extern requires 31 bytes of support).
Enhanced preprocessing functions. For example:
Macro supports taking variable arguments #define Macro(...) __VA_ARGS__
When using a macro, it is allowed to omit parameters, and the omitted parameters will be expanded into empty strings.
Supports single-line comments starting with // (this feature has actually been supported by many compilers of C89).
Added new keywords restrict, inline, _Complex, _Imaginary, _Bool
Supports types such as long long, long double _Complex, float _Complex, etc.
Supports variable-length arrays, that is, the array length can be determined at runtime, such as using variables as array lengths. Declare in the form of int a. However, considering efficiency and implementation, variable-length arrays cannot be used globally, or in structs and unions.
Variable declarations do not have to be placed at the beginning of the statement block. The for statement is recommended to be written as for(int i=0;i<100;++i), that is, i is only valid within the for statement block.
Allows the use of (type_name){xx,xx,xx} similar to the constructor form of C++ to construct anonymous structs.
Compound literals: When initializing a structure, it is allowed to assign values to specific elements, in the form:
struct test{int a,b;} foo = { .a = {1}, .a = 2 };
struct test{int a, b, c, d;} foo = { .a = 1, .c = 3, 4, .b = 5 }; // 3,4 are assignments to .c,.d
In the format string, \u is used to support unicode characters.
Supports the description of hexadecimal floating-point numbers.
The format strings of printf scanf have added support for the long long int type.
The internal data description of floating-point numbers supports the new standard, and the #pragma compiler directive can be used to specify it.
In addition to the existing __line__ __file__, __func__ is added to get the current function name.
Allows the compiler to simplify non-constant expressions.
Modified the definition of /% processing negative numbers, so that a clear result can be given. For example, in C89, -22 / 7 = -3, -22% 7 = -1, and also -22 / 7= -4, -22% 7 = 6. In C99, it is clearly -22 / 7 = -3, -22% 7 = -1, only one result.
Cancelled the regulation that the function return type is int by default.
Allows the last array in the struct definition to not specify its length, written as (flexible array member).
const const int i will be treated as const int i.
Added and modified some standard header files, such as <stdbool.h> defining bool, <inttypes.h> defining int of some standard lengths, <complex.h> defining complex numbers, <wctype.h> defining wide characters, <tgmath.h> similar to generic mathematical functions, <fenv.h> related to floating-point numbers. va_copy is added in <stdarg.h> for copying ... parameters. struct tmx is added, and struct tm is extended.
Input and output support corresponding to wide characters and long integers, etc.
Most features of C99 are supported by GCC and other commercial compilers.
C11 Standard
On December 8, 2011, the International Organization for Standardization (ISO) and the International Electrotechnical Commission (IEC) released a new standard for the C language again, named ISO/IEC 9899:2011 - Information technology -- Programming languages -- C , referred to as the C11 standard, formerly known as C1X. This is the third official standard of the C language and also the latest standard of the C language.
The new standard improves compatibility with C++ and adds some new features. These new features include:
Standardization of alignment processing (including _Alignas identifier, alignof operator, aligned_alloc function, and <stdalign.h> header file.
_Noreturn function mark, similar to gcc's __attribute__((noreturn)).
_Generic keyword.
Multithreading support, including:
_Thread_local storage type identifier, <threads.h> header file, which contains functions for thread creation and management.
_Atomic type modifier and <stdatomic.h> header file.
Enhanced Unicode support. Based on the C Unicode technical report ISO/IEC TR 19769:2004, enhanced support for Unicode. Including adding char16_t and char32_t data types for UTF-16/UTF-32 encoding, and providing the <uchar.h> header file containing Unicode string conversion functions.
The gets() function is deleted, and a new safer function gets_s() is used instead.
Added boundary check function interfaces and defined new safe functions, such as fopen_s(), strcat_s(), etc.
Added more floating-point processing macros.
Anonymous struct/union support. This has long existed in gcc, and C11 introduces it into the standard.
Static assertions (static assertions), _Static_assert(), which are processed after interpreting #if and #error.
New fopen() mode, ("…x"). Similar to O_CREAT|O_EXCL in POSIX, it is commonly used in file locks.
Added quick_exit() function as a third way to terminate the program. When exit() fails, the least cleaning work can be done.
Language Features
Basic Characteristics
C++ Language Code C++ Language Code
1. High-level language: It is a working unit that combines the basic structures and statements of high-level languages with the practicality of low-level languages.
2. Structured language: The prominent feature of structured language is the separation of code and data, that is, each part of the program is independent except for necessary information exchange. This structured method can make the program hierarchical, easy to use, maintain, and debug. The C language is provided to users in the form of functions, which can be easily called, and has various loops and conditional statements to control the program flow, so that the program is completely structured.
4. Code-level cross-platform: Due to the existence of the standard, almost the same C code can be used for multiple operating systems, such as Windows, DOS, UNIX, etc.; it is also suitable for multiple machine types. The C language is superior to other high-level languages for occasions that need to perform hardware operations.
5. Using pointers: You can directly perform operations close to the hardware, but the pointer operation of C is not protected, which also brings many unsafe factors. C++ has made improvements in this regard. While retaining pointer operations, it has enhanced security, which is supported by some users. However, because these improvements increase the complexity of the language, it is also criticized by another part. Java has learned from the lessons of C++, canceled pointer operations, and also canceled some controversial places in C++ improvements, achieving good results in security and suitability, but its own interpretation runs in the virtual machine, and the running efficiency is lower than C++/C. Generally speaking, C, C++, and java are regarded as languages of the same series, and they have long occupied the top three in the program usage list.
Unique Features
The C language is a procedural language with structured program design, variable scope (variable scope), and recursion function.
The C language passes parameters by value (pass by value), and also can pass pointers (a pointer passed by value).
Different variable types can be combined with structs.
There are only 32 reserved words (reserved keywords), which makes variable and function naming more flexible.
Some variable types can be converted, such as integer and character type variables.
Through pointers, the C language can easily perform low-level control of memory.
The preprocessor makes the compilation of the C language more flexible.
Advantages and Disadvantages
Advantages
1. Concise, compact, flexible and convenient
The C language has only 32 keywords and 9 control statements. The program writing form is free and case-sensitive. Combine the basic structures and statements of high-level languages with the practicality of low-level languages. The C language can operate on bits, bytes, and addresses like assembly language, and these three are the most basic working units of the computer.
C Language's Hello World Program C Language's Hello World Program
2. Rich operators
The operators of the C language cover a wide range, with a total of 34 operators. The C language treats parentheses, assignment, forced type conversion, etc. as operators. Thereby making the operation types of the C language extremely rich and the expression types diversified. Flexibly using various operators can realize operations that are difficult to implement in other high-level languages.
3. Rich data types
The data types of the C language include: integer type, real type, character type, array type, pointer type, structure type, union type, etc. It can be used to implement operations of various complex data structures. And the pointer concept is introduced, which makes the program more efficient.
4. Flexible and practical expression methods
The C language provides a variety of operators and methods for the value of expressions. The expression of the problem can be obtained through various ways, and its program design is more active and flexible. Its syntax restrictions are not too strict, and the program design freedom is large. For example, integer types, character types, and logical types can be used universally.
5. Allowing direct access to physical addresses and operating hardware
Because the C language allows direct access to physical addresses and can directly operate on hardware, it has both the functions of high-level languages and many functions of low-level languages. It can operate on bits (bit), bytes, and addresses like assembly language, and these three are the most basic working units of the computer, which can be used to write system software.
6. High quality of generated object code and high efficiency of program execution
The C language describes the problem more quickly than assembly language, with less workload and better readability, easy to debug, modify and port, and the code quality is equivalent to that of assembly language. The C language is generally only 10% to 20% less efficient than the object code generated by the assembly program.
7. Good portability
The C language compilation programs on different machines have 86% of the code in common, so the C language compilation program is easy to port. A program written in C language in one environment can be ported to run in a completely different environment with little or no modification.
8. Strong expressive power
The C language has rich data structures and operators. It contains various data structures, such as integer types, array types, pointer types, and union types, to implement operations of various data structures. The C language has 34 operators, with a wide range. Flexibly using various operators can implement extremely difficult operations.
The C language can directly access the physical address of hardware and can perform bit (bit) operations. It has many advantages of both high-level languages and low-level languages.
It can be used to write both system software and application software, and has become a general-purpose programming language.
In addition, the C language has powerful graphics functions and supports multiple displays and drivers. And it has powerful calculation functions and logical judgment functions.
Disadvantages
1. The main disadvantage of the C language is reflected in the encapsulation of data. This makes the C language have great defects in data security, which is also a big difference between C and C++.
2. The syntax restrictions of the C language are not very strict, and the type constraints on variables are not strict, which affects the security of the program, and does not check for array subscript out of bounds, etc. From an application perspective, the C language is more difficult to master than other high-level languages. That is to say, for people using the C language, more proficient programming is required.
New Features of C11
1. Standardization of alignment processing (including _Alignas identifier, alignof operator, aligned_alloc function, and <stdalign.h> header file).
2. _Noreturn function mark, similar to gcc's __attribute__(noreturn).
3. _Generic keyword.
4. Multithreading support, including: _Thread_local storage type identifier, <threads.h>; header file, which contains functions for thread creation and management.
5. Enhanced Unicode support, based on the C Unicode technical report ISO/IEC TR 19769:2004, enhanced support for Unicode. Including adding char16_t and char32_t data types for UTF-16/UTF-32 encoding, and providing the <uchar.h> header file containing Unicode string conversion functions.
6. The gets() function is deleted, and a new safer function gets_s() is used instead.
7. Added boundary check function interfaces and defined new safe functions, such as fopen_s(), strcat_s(), etc.
8. Added more floating-point processing macros.
9. Anonymous struct/union support, which has long existed in gcc, and C11 introduces it into the standard.
10. Static assertions (Static assertions), _Static_assert(), which are processed after interpreting #if and #error.
11. New fopen() mode, ("…x"), similar to O_CREAT|O_EXCL in POSIX, which is commonly used in file locks.
12. Added quick_exit() function as a third way to terminate the program. When exit() fails, the least cleaning work can be done.
13. _Atomic type modifier and <stdatomic.h> header file.
Language Components
Basic Composition
Data Types
The data types of C include: integer type, character type, real type or floating type (single-precision and double-precision), enumeration type, array type, structure type, union type, pointer type, and void type.
Constants and Variables
The value of a constant cannot be changed, and the symbolic constant name is usually in uppercase.
A variable is a quantity whose name is an identifier and whose value can be changed. The identifier is a sequence composed of letters or underscores followed by a string of letters, numbers, or underscores. Please note that the first character must be a letter or underscore, otherwise it is an illegal variable name. Variables are allocated corresponding storage units at compile time.
Array
If a variable name is followed by a bracket with a number, this declaration is an array declaration. A string is also an array. They end with the ASCII NULL. It should be particularly noted that the index value in the square brackets starts from 0.
Pointer
If a * is used in front of a variable declaration, it indicates that this is a pointer variable. In other words, the variable stores an address, and * (specifically referring to the unary operator * here, the same below. There is also a binary operator * in the C language) is the content fetch operator, which means fetching the content stored in this memory address. Pointers are one of the main features of the C language that distinguish it from other contemporary high-level languages.
Pointers can not only be the address of a variable, but also the address of an array, array elements, and functions. Through pointers as formal parameters, more than one return value can be obtained during the function call process, which is different from return (z) which can only get one return value.
Pointers are a double-edged sword. Many operations can be naturally expressed through pointers, but incorrect or excessive use of pointers will bring a large number of potential errors to the program.
String
The string of the C language is actually a char array ending with the '\0' character. Using a character type does not require referencing a library, but using a string requires some functions in the C standard library to operate on the string. They are different from character arrays. Using these functions requires referencing the header file <string.h>.
File Input/Output
In the C language, input and output are implemented through a set of functions in the standard library. In ANSI C, these functions are defined in the header file <stdio.h>;
Standard Input/Output
There are three standard input/outputs that are pre-defined by the standard I/O library:
stdin standard input
stdout standard output
stderr input and output error
Operation
The operation of the C language is very flexible and has very rich functions. The number of operation types is far more than that of other programming languages. In terms of expressions, it is more concise than other programming languages. For example, self-increment, self-decrement, comma operation, and ternary operation make the expression simpler, but beginners often find this expression difficult to read. The key reason is that they do not fully understand the operator and the operation order. When a variety of different operations form an operation expression, that is, when multiple operators appear in an operation expression, the priority order and associative rules of the operations are very important. In learning, it is reasonable to classify them, find the differences from the operations learned in mathematics, and then it is not difficult to remember these operations. Some operators will be firmly remembered after understanding, and will be handy in the future, while some can be temporarily forgotten and remembered when used.
First, it is necessary to clarify that the operators are classified according to different priorities. The operators in "C Programming" can be divided into 15 levels of priorities, from high to low, with priorities 1 ~ 15. Except for levels 2.13 and 14, which are combined from right to left, the others are combined from left to right, which determines the operation order of operators at the same level.
Keywords
Keywords are also called reserved words, that is, words that have been used by the C language itself and cannot be used for other purposes. For example, keywords cannot be used as identifiers such as variable names and function names
There are a total of 32 keywords defined by the ISO standard for the C language:
auto double int struct break else long switch
case enum register typedef char extern return union
const float short unsigned continue for signed void
default goto sizeof volatile do if while static inline
restrict _Bool _Complex _Imaginary _Generic
Basic Data Types
void: Declare that the function has no return value or no parameters, declare a pointer of no type, and explicitly discard the operation result. (Added in C89 standard)
char: Character type data, which belongs to a kind of integer data. (Introduced during K&R period)
int: Integer data, indicating the range usually specified by the compiler's memory bytes. (Introduced during K&R period)
float: Single-precision floating-point data, which belongs to a kind of floating-point data. (Introduced during K&R period)
double: Double-precision floating-point data, which belongs to a kind of floating-point data. (Introduced during K&R period)
_Bool: Boolean type (added in C99 standard)
_Complex: Basic type of complex number (added in C99 standard)
_Imaginary: Imaginary number, similar to the basic type of complex number, a pure imaginary number without a real part (added in C99 standard)
_Generic: Provide an interface entry for overloading (added in C11 standard)
Type Modifier Keywords
short: Modify int, short integer data, and the modified int can be omitted. (Introduced during K&R period)
long: Modify int, long integer data, and the modified int can be omitted. (Introduced during K&R period)
long long: Modify int, super long integer data, and the modified int can be omitted. (Added in C99 standard)
signed: Modify integer data, signed data type. (Added in C89 standard)
unsigned: Modify integer data, unsigned data type. (Introduced during K&R period)
restrict: Used to limit and restrict pointers, and indicate that the pointer is the only and initial way to access a data object. (Added in C99 standard)
Complex Type Keywords
struct: Structure declaration. (Introduced during K&R period)
union: Union declaration. (Introduced during K&R period)
enum: Enumeration declaration. (Added in C89 standard)
typedef: Declare type alias. (Introduced during K&R period)
sizeof: Get the size of a specific type or specific type variable. (Introduced during K&R period)
inline: Inline function is used to replace macro definition and will be expanded in any place where it is called. (Added in C99 standard)
Storage Level Keywords
auto: Specify as an automatic variable, which is automatically allocated and released by the compiler. Usually allocated on the stack. Opposite to static. When the variable is not specified, it is automatically auto by default. (Introduced during K&R period)
static: Specify as a static variable, allocated in the static variable area. When modifying a function, specify that the function scope is internal to the file. (Introduced during K&R period)
register: Specify as a register variable, and suggest that the compiler store the variable in the register for use. It can also modify function parameters and suggest that the compiler pass parameters through the register instead of the stack. (Introduced during K&R period)
extern: Specify the corresponding variable as an external variable, that is, indicate that the definition of the variable or function is in another file, and prompt the compiler to find its definition in other modules when encountering this variable and function. (Introduced during K&R period)
const: Specify that the variable cannot be changed by the current thread (but may be changed by the system or other threads). (Added in C89 standard)
volatile: Specify that the value of the variable may be changed by the system or other threads, and force the compiler to always obtain the value of the variable from the memory, preventing the compiler from optimizing the variable into a register variable. (Added in C89 standard)
Flow Control Keywords
Jump Structure
return: Used in the function body to return a specific value (if it is of void type, it does not return a function value). (Introduced during K&R period)
continue: End the current loop and start the next loop. (Introduced during K&R period)
break: Jump out of the current loop or switch structure. (Introduced during K&R period)
goto: Unconditional jump statement. (Introduced during K&R period)
Branch Structure
if: Conditional statement, no semicolon needs to be placed later. (Introduced during K&R period)
else: Conditional statement negative branch (used with if). (Introduced during K&R period)
switch: Switch statement (multiple branch statement). (Introduced during K&R period)
case: Branch mark in the switch statement, used with switch. (Introduced during K&R period)
default: "Other" branch in the switch statement, optional. (Introduced during K&R period)
Compilation
#define precompilation macro
#if expression #else if expression #else #endif conditional compilation
#ifdef macro #else #endif conditional compilation
#ifndef macro #else #endif conditional compilation and conditional compilation
Syntax Structure
Sequential Structure
The sequential structure program design is the simplest. As long as the corresponding statements are written in the order of solving the problem, its execution order is from top to bottom, and it is executed in sequence.
For example: a = 3, b = 5, now exchange the values of a and b. This problem is like exchanging water in two cups. Of course, a third cup is needed. Suppose the third cup is c, then the correct program is: c = a; a = b; b = c; the execution result is a = 5, b = c = 3. If the order is changed, written as: a = b; c = a; b = c; then the execution result becomes a = b = c = 5, which cannot achieve the expected purpose. Beginners are most likely to make this kind of mistake. The sequential structure can be used independently to form a simple complete program. The common three-step program of input, calculation, and output is a sequential structure. For example, calculate the area of a circle. The statement order of its program is to input the radius r of the circle, calculate s = 3.14159*r*r, and output the area s of the circle. However, in most cases, the sequential structure is part of the program, and forms a complex program with other structures. For example, the compound statement in the branch structure, the loop body in the loop structure, etc.
Selection Structure
The sequential structure program can solve problems such as calculation and output, but cannot make judgments and selections. For problems that need to make judgments and selections first, the selection structure should be used. The execution of the selection structure is based on certain conditions to select the execution path, rather than strictly following the physical order of the statements. The program design method of the selection structure is to construct a suitable branch condition and analyze the program flow. Select the appropriate selection statement according to different program flows. The selection structure is suitable for calculations with logical or relational comparison and other condition judgments. When designing such a program, it is often necessary to draw its program flow chart first, and then write the source program according to the program flow, which separates the program design analysis from the language, making the problem simple and easy to understand. The program flow chart is the program execution flow chart drawn according to the problem analysis.
Loop Structure
The loop structure can reduce the workload of repeatedly writing source programs, and is used to describe the problem of repeatedly executing a certain algorithm. This is the program structure that can best exert the characteristics of the computer. The C language provides four loops, namely goto loop, while loop, do while loop, and for loop. The four loops can be used to handle the same problem. Generally, they can be replaced with each other, but generally, the goto loop is not advocated, because forcibly changing the order of the program often brings unpredictable errors to the operation of the program.
Special attention should be paid to including statements that tend to end in the loop body (that is, the change of the loop variable value), otherwise it may become an infinite loop, which is a common mistake of beginners.
The similarities and differences of the three loops: When using the while and do...while loops, the initialization operation of the loop variable should be before the loop body, while the for loop is generally carried out in statement 1; the while loop and the for loop both judge the expression first, and then execute the loop body, while the do...while loop executes the loop body first and then judges the expression, that is to say, the loop body of do...while is executed at least once, while the while loop and for may not be executed even once. In addition, it should also be noted that these three loops can all use the break statement to jump out of the loop, and use the continue statement to end this loop, while the loop composed of the goto statement and if cannot be controlled by the break and continue statements.
The sequential structure, branch structure, and loop structure are not isolated from each other. There can be branches and sequential structures in the loop, and there can be loops and sequential structures in the branch. In fact, no matter which structure, they can be generally regarded as a statement. In the actual programming process, these three structures are often combined with each other to implement various algorithms and design corresponding programs. However, if the problem to be programmed is large, the written program is often very long and has repeated structures, resulting in poor readability and difficult understanding. The method to solve this problem is to design the C program into a modular structure.
Specific content:
for loop
The for loop structure is the most characteristic loop statement in the C language, and it is the most flexible and convenient to use. Its general form is:
for (expression 1; expression 2; expression 3) loop body statement. (Where; cannot be omitted)
Expression
Expression 1 is the initial value expression, used to assign an initial value to the loop variable before the loop starts.
Expression 2 is the loop control logic expression, which controls the condition for loop execution and determines the number of loops.
Expression 3 is the loop control variable modification expression, which makes the for loop tend to end.
The loop body statement is the statement that is repeatedly executed when the loop control condition is established.
However, in the entire for loop process, expression 1 is calculated only once, and expressions 2 and 3 may be calculated multiple times or not at all. The loop body may be executed multiple times or not at all.
First execute expression 2, then execute the loop structure, and finally expression 3, and this cycle continues.
The for loop statement is the most powerful statement in the C language, and can even replace other loop statements to a certain extent.
do
do loop structure, the execution order of do 1 while⑵; is 1->2->1... loop, and 2 is the loop condition.
while
while loop structure, the execution order of while(1) 2; is 1->2->1... loop, and 1 is the loop condition
The above loop statements continue the loop when the loop condition expression is true, and jump out of the loop when it is false.
Program Structure
The modular program structure of the C language is implemented by functions, that is, divide a large problem into several modules, each module is written as a C function, and then the main function calls the function and the function calls the function to implement the C program writing of a large problem. Therefore, it is often said: C program = main function + sub-function. Therefore, special attention should be paid to understanding and application of the definition, call, and value return of functions, and consolidated through debugging on the computer.
Judgment Statement (Selection Structure):
if statement: "If" statement; if-else statement: "If... (then)... else..." statement; switch statement: "Switch" statement; switch-case: "Switch-case" statement.
Loop Statement (Loop Structure):
while statement: "When..." statement; do-while statement: "Do... when... (time)" statement; for statement: conditional statement (that is, "(do)... for... " statement).
Jump Statement (Loop Structure: Whether to Loop):
goto statement: "Steering" statement, also called "jump" statement; break statement: "Interrupt" (loop) statement, that is, end the entire loop; continue statement: "Continue" statement (end this loop, continue the next loop); return statement: "Return" statement.
It needs to be explained that:
1. A C language source program can be composed of one or more source files.
2. Each source file can be composed of one or more functions.
3. A source program, no matter how many files it is composed of, has one and only one main function, that is, the main function. It is the entry of the entire program.
4. There can be preprocessing commands (including include commands, ifdef, ifndef commands, define commands) in the source program, and the preprocessing commands should usually be placed at the front of the source file or source program.
5. Each statement and each statement must end with a semicolon. But preprocessing commands, function headers, and curly braces "}" should not be followed by semicolons. (The "}" after the declaration of struct, union, and enumeration type should be followed by ";".)
6. There must be at least one space between identifiers and keywords to show the interval. If there are obvious interval symbols, no more spaces need to be added to separate.
Writing Rules
1. One statement or one statement occupies one line.
2. The part enclosed in {} usually represents a certain level of structure of the program. {} is generally aligned with the first letter of this structure statement and occupies a line alone.
3. The statements or instructions of the lower level can be written with several more spaces than the statements or instructions of the higher level. In order to look more clear and increase the readability of the program. When programming, you should try to follow these rules to develop a good programming style.
Function
A C program is composed of a set of external objects of variables or functions. A function is a self-contained execution code segment that completes certain related functions. We can regard a function as a "black box". As long as you send data in, you can get the result, and the external program does not know how the function works internally. All the external program knows is what is input to the function and what is output by the function. Functions provide a means of programming, making it easy to read, write, understand, debug, modify, and maintain.
The number of functions in a C program is actually unlimited. If there is any restriction, it is that there must be at least one function in a C program, and there must be one and only one function named main, which is called the main function, and the entire program starts from this main function.
The C language encourages and advocates that people divide a big problem into sub-problems, and compile a function for each sub-problem. Therefore, a C language program is generally composed of a large number of small functions rather than a small number of large functions, that is, "small functions constitute a large program". The advantage of this is that each part is fully independent and has a single task. Therefore, these fully independent small modules can also be used as a fixed specification of small "components" to form a new large program.
Over the years of development of the C language, a huge number of systems and programs developed with the C language have been accumulated. At the same time, a lot of directly usable library functions have also been accumulated.
ANSI C provides standard C language library functions.
Beginners of the C language prefer Turbo C 2.0, which provides more than 400 runtime functions, each of which completes a specific function, and users can call them freely. These functions are generally divided into input and output functions, mathematical functions, string and memory functions, functions related to BIOS and DOS, character screen and graphics functions, process control functions, directory functions, etc.
The Windows SDK provided by the Windows system contains thousands of functions related to Windows application development.
Other operating systems, such as Linux, also provide a large number of functions for application program developers to call.
As a programmer, you should try to be familiar with the functions of the target platform library. In this way, you can develop application programs for a specific platform with ease. For example, as a developer of Windows applications, you should try to be familiar with the Windows SDK; as a developer of Linux applications, you should try to be familiar with the Linux system calls and POSIX function specifications.
Operators
It is particularly special that the bit right shift (>>) operator can be arithmetic (the highest significant bit is filled on the left end) or logical (0 is filled on the left end) shift. For example, shifting 11100011 right by 3 bits, the arithmetic right shift becomes 11111100, and the logical right shift becomes 00011100. Because arithmetic bit right shift is more suitable for handling negative integers, almost all compilers are arithmetic bit right shift.
The priority of operators is roughly from high to low: unary operators, arithmetic operators, relational operators, logical operators, conditional operators, assignment operators (=), and comma operators.
()、 、 -> 、 .、!、 ++、 -- Parentheses, square brackets, pointer, member, logical not, self-increment, self-decrement
++ 、 -- 、 * 、 & 、 ~ 、! Unary operators
+、 - 、 sizeof、(cast)
* 、 / 、% Arithmetic operators
+ 、 - Arithmetic operators
<< 、 >> Bit operators
< 、 <= 、 > 、 >= Relational operators
== 、!= Relational operators
& Bitwise AND
^ Bitwise XOR
| Bitwise OR
&& Logical AND
|| Logical OR
? 、: Conditional operator
/= 、%= 、 &= 、 |= 、 ^= Assignment operator
= 、 += 、 -= 、 *= 、
, Sequence operator
Classic Errors
The usage of void main() is not formulated by any standard. The C language standard syntax is int main. Any implementation must support int main(void) { /* ... */ } and int main(int argc, char* argv) { /* ... */ }.
Operations like a+=a++ or (i++)+(i++)+(i++) belong to undefined behavior. It is not that this behavior has not been defined in the C language, it has long been concluded. Its result depends on the compiler implementation. Do not write such code!
References
1. Stephen Prata.C Primer Plus.United States: People's Post and Telecommunications Press, 2005: 1-3
2. Su Xiaohong, Sun Zhigang, Chen Huipeng.Practical C Language Tutorial.Beijing: Electronic Industry Press, 2013: 8-8
3. Development Process of C Language .C Language Chinese Network.2012-04-28
4. American National Standards Institute (ANSI - American National Standards Institute) .Standard Information Network
5. ISO/IEC 9899:1990 - Programming languages -- C .International Organization for Standardization (ISO - International Organization for Standardization)
6. ISO/IEC 9899:1990/Cor 1:1994 .International Organization for Standardization (ISO - International Organization for Standardization)
7. ISO/IEC 9899:1990/Amd 1:1995 - C Integrity .International Organization for Standardization (ISO - International Organization for Standardization)
8. ISO/IEC 9899:1999 - Programming languages -- C .International Organization for Standardization (ISO - International Organization for Standardization)
9. ISO/IEC 9899:2011 - Information technology -- Programming languages -- C .International Organization for Standardization (ISO - International Organization for Standardization)
10. Related Introduction to C Language Programming Tutorial .Spark Tutorial Network.2012-03-22
[ Last edited by zzz19760225 on 2018-7-31 at 21:29 ]
http://codepad.org/TIbKemXJ
http://www.manonggu.com/biancheng/
C Language Resource Collection in Chinese Version
https://github.com/jobbole/awesome-c-cn
【Raw】Compare C Language and Machine Language (English, for reference)
https://www.bilibili.com/video/av11237858
Yingsi Teaching Network (C language content mainly based on Turbo C)
http://www.winyes.com/
Bilibili C Language
https://www.bilibili.com/video/av9430200/?from=search&seid=8131884833781373784
Sogou Turbo C Chinese Version Download (Use the localized version in the run wizard, run the program in virtual machine mode)
http://xiazai.sogou.com/detail/34/16/902784868571182432.html?uID=440DBA3D4F0A900A00000000598FB273
C Language
Locked
Synonym C (computer language) generally refers to C language
This entry is reviewed by the "Science Popularization China" Encyclopedia Science Entry Writing and Application Project.
C language is a general-purpose computer programming language with wide applications. The design goal of C language is to provide a programming language that can be compiled easily, handle low-level memory, generate a small amount of machine code, and can run without any runtime environment support.
Although C language provides many low-level processing functions, it still maintains good cross-platform characteristics. A C language program written according to a standard specification can be compiled on many computer platforms, even including some embedded processors (microcontrollers or MCUs) and supercomputer operating platforms.
In the 1980s, in order to avoid differences in the C language syntax used by various developers, the American National Standards Institute formulated a complete international standard syntax for C language, called ANSI C, as the initial standard of C language.
Chinese Name
C Language
English Name
The C Programming Language
Category
Computer Programming Language
Founder
Dennis MacAlistair Ritchie
Founding Time
1972
Originated From
BCPL Language
Main Compilers
Clang, GCC, MSVC, Turbo C, etc.
Inspired Languages
B Language, Assembly, ALGOL68
Operating System
Multi-platform
Table of Contents
1. Development History
2. Programming and Development
▪ Compiler
▪ Integrated Development Environment (IDE)
▪ Hello World
3. Language Standards
▪ K&R C
▪ ANSI C / C89 Standard
▪ C99 Standard
▪ C11 Standard
4. Language Features
▪ Basic Characteristics
▪ Unique Features
▪ Advantages and Disadvantages
▪ New Features of C11
5. Language Components
▪ Basic Composition
▪ Keywords
▪ Syntax Structure
▪ Program Structure
▪ Function
▪ Operators
6. Classic Errors
Development History
The reason why C language is named C is that C language originated from the B language invented by Ken Thompson, and the B language originated from the BCPL language.
In 1967, Martin Richards from Cambridge University simplified the CPL language, thus producing the BCPL (Basic Combined Programming Language) language.
C Language Promotion Image C Language Promotion Image
In the 1960s, Ken Thompson, a researcher from Bell Laboratories of AT&T (AT&T Bell Laboratory) in the United States, was idle and itchy, wanting to play an electronic game he made himself, Space Travel. He found an idle machine, PDP-7, behind his boss. But this machine had no operating system, and the game had to use some functions of the operating system, so he started to develop an operating system for PDP-7. Later, this operating system was named - UNIX.
In 1970, Ken Thompson from Bell Laboratories in the United States, based on the BCPL language, designed a very simple and hardware-closer B language (taking the first letter of BCPL). And he wrote the first UNIX operating system in B language.
In 1971, Dennis M. Ritchie, who also loved Space Travel, joined Thompson's development project to cooperate in the development of UNIX in order to play the game earlier. His main job was to transform the B language to make it more mature.
In 1972, D.M. Ritchie from Bell Laboratories in the United States finally designed a new language based on the B language. He took the second letter of BCPL as the name of this language, which is the C language.
At the beginning of 1973, the main body of the C language was completed. Thompson and Ritchie couldn't wait to rewrite UNIX completely with it. At this time, the fun of programming made them completely forget that "Space Travel" and devoted themselves wholeheartedly to the development of UNIX and C language. With the development of UNIX, the C language itself is also constantly improving. Until today, various versions of UNIX kernels and surrounding tools still use C language as the most important development language, and many of them still inherit the code from Thompson and Ritchie.
During the development, they also considered porting UNIX to other types of computers. The strong portability (Portability) of the C language is manifested here. Both machine language and assembly language are not portable. A program developed for x86 cannot run on machines such as Alpha, SPARC, and ARM. The C language program can be used on processors of any architecture, as long as the processor of that architecture has a corresponding C language compiler and library, and then the C source code is compiled and linked into a target binary file to run.
In 1977, Dennis M. Ritchie published the C language compilation text "Portable C Language Compiler" that is not dependent on a specific machine system.
The C language continued to develop. In 1982, many knowledgeable people and the American National Standards Institute decided to establish a C standard committee to establish the standard of the C language in order to make this language develop healthily. The committee is composed of hardware manufacturers, compiler and other software tool manufacturers, software designers, consultants, academic people, C language authors, and application programmers. In 1989, ANSI released the first complete C language standard - ANSI X3.159-1989, referred to as "C89", but people are also used to calling it "ANSI C". C89 was adopted unchanged by the International Organization for Standardization (ISO) in 1990. The official name given by ISO is: ISO/IEC 9899, so ISO/IEC9899: 1990 is also usually referred to as "C90" for short. In 1999, after making some necessary corrections and improvements, ISO released a new C language standard, named ISO/IEC 9899: 1999, referred to as "C99". On December 8, 2011, ISO officially released a new standard, called ISO/IEC9899: 2011, referred to as "C11".
Programming and Development
Compiler
GCC, an open-source and free compiler developed by the GNU organization
MinGW, GCC under the Windows operating system
Clang, an open-source BSD-licensed compiler based on LLVM
Visual C++ :: cl.exe, the compiler built into Microsoft VC++
Integrated Development Environment (IDE)
CodeBlocks, an open-source and free C/C++ IDE
CodeLite, an open-source, cross-platform C/C++ integrated development environment
Orwell Dev-C++, a portable C/C++ IDE
C-Free
Light Table
Visual Stdio series
Hello World
The following is a simple program that prints the string "Hello, world!" on the standard output device (stdout). A similar program is usually the first program when learning a programming language:
1
2
3
4
5
6
7
#include <stdio.h>
int main(void)
{
printf("Hello, world!");
return 0;
}
Language Standards
K&R C
At first, there was no official standard for the C language. In 1978, it was officially published by Bell Laboratories of AT&T (AT&T). Brian Kernighan and Dennis Ritchie published a book called "The C Programming Language". This book is called K&R by C language developers and has been regarded as an informal standard description of the C language for many years. People call this version of the C language K&R C.
K&R C mainly introduces the following features:
Struct (struct) type
Long integer (long int) type
Unsigned integer (unsigned int) type
Change the operators =+ and =- to += and -=. Because =+ and =- will make the compiler not know whether the user wants to handle i = -10 or i =- 10, causing confusion in processing.
Even many years after the ANSI C standard was proposed, K&R C is still the most accurate requirement of many compilers, and many old compilers still run the K&R C standard.
ANSI C / C89 Standard
From the 1970s to the 1980s, the C language was widely used, from large mainframes to small microcomputers, and many different versions of the C language also derived.
In 1983, the American National Standards Institute (ANSI) established a committee X3J11 to formulate the C language standard.
In 1989, the American National Standards Institute (ANSI) passed the C language standard, which is called ANSI X3.159-1989 "Programming Language C". Because this standard was passed in 1989, it is generally referred to as the C89 standard. Some people also refer to it as ANSI C because this standard was released by the American National Standards Institute (ANSI).
In 1990, the International Organization for Standardization (ISO) and the International Electrotechnical Commission (IEC) set the C89 standard as the international standard of the C language, named ISO/IEC 9899:1990 - Programming languages -- C . Because this standard was released in 1990, some people simply call it the C90 standard. However, most people still call it the C89 standard because this standard is completely equivalent to the ANSI C89 standard.
In 1994, the International Organization for Standardization (ISO) and the International Electrotechnical Commission (IEC) released a revised version of the C89 standard, named ISO/IEC 9899:1990/Cor 1:1994 , some people simply call it the C94 standard.
In 1995, the International Organization for Standardization (ISO) and the International Electrotechnical Commission (IEC) released another revised version of the C89 standard, named ISO/IEC 9899:1990/Amd 1:1995 - C Integrity , some people simply call it the C95 standard.
C99 Standard
In January 1999, the International Organization for Standardization (ISO) and the International Electrotechnical Commission (IEC) released a new standard for the C language, named ISO/IEC 9899:1999 - Programming languages -- C , referred to as the C99 standard. This is the second official standard of the C language.
The features included in C99 are:
Increased restrictions on the compiler, such as each line of the source program requiring at least 4095 bytes of support, and variable names and function names requiring 63 bytes of support (extern requires 31 bytes of support).
Enhanced preprocessing functions. For example:
Macro supports taking variable arguments #define Macro(...) __VA_ARGS__
When using a macro, it is allowed to omit parameters, and the omitted parameters will be expanded into empty strings.
Supports single-line comments starting with // (this feature has actually been supported by many compilers of C89).
Added new keywords restrict, inline, _Complex, _Imaginary, _Bool
Supports types such as long long, long double _Complex, float _Complex, etc.
Supports variable-length arrays, that is, the array length can be determined at runtime, such as using variables as array lengths. Declare in the form of int a. However, considering efficiency and implementation, variable-length arrays cannot be used globally, or in structs and unions.
Variable declarations do not have to be placed at the beginning of the statement block. The for statement is recommended to be written as for(int i=0;i<100;++i), that is, i is only valid within the for statement block.
Allows the use of (type_name){xx,xx,xx} similar to the constructor form of C++ to construct anonymous structs.
Compound literals: When initializing a structure, it is allowed to assign values to specific elements, in the form:
struct test{int a,b;} foo = { .a = {1}, .a = 2 };
struct test{int a, b, c, d;} foo = { .a = 1, .c = 3, 4, .b = 5 }; // 3,4 are assignments to .c,.d
In the format string, \u is used to support unicode characters.
Supports the description of hexadecimal floating-point numbers.
The format strings of printf scanf have added support for the long long int type.
The internal data description of floating-point numbers supports the new standard, and the #pragma compiler directive can be used to specify it.
In addition to the existing __line__ __file__, __func__ is added to get the current function name.
Allows the compiler to simplify non-constant expressions.
Modified the definition of /% processing negative numbers, so that a clear result can be given. For example, in C89, -22 / 7 = -3, -22% 7 = -1, and also -22 / 7= -4, -22% 7 = 6. In C99, it is clearly -22 / 7 = -3, -22% 7 = -1, only one result.
Cancelled the regulation that the function return type is int by default.
Allows the last array in the struct definition to not specify its length, written as (flexible array member).
const const int i will be treated as const int i.
Added and modified some standard header files, such as <stdbool.h> defining bool, <inttypes.h> defining int of some standard lengths, <complex.h> defining complex numbers, <wctype.h> defining wide characters, <tgmath.h> similar to generic mathematical functions, <fenv.h> related to floating-point numbers. va_copy is added in <stdarg.h> for copying ... parameters. struct tmx is added, and struct tm is extended.
Input and output support corresponding to wide characters and long integers, etc.
Most features of C99 are supported by GCC and other commercial compilers.
C11 Standard
On December 8, 2011, the International Organization for Standardization (ISO) and the International Electrotechnical Commission (IEC) released a new standard for the C language again, named ISO/IEC 9899:2011 - Information technology -- Programming languages -- C , referred to as the C11 standard, formerly known as C1X. This is the third official standard of the C language and also the latest standard of the C language.
The new standard improves compatibility with C++ and adds some new features. These new features include:
Standardization of alignment processing (including _Alignas identifier, alignof operator, aligned_alloc function, and <stdalign.h> header file.
_Noreturn function mark, similar to gcc's __attribute__((noreturn)).
_Generic keyword.
Multithreading support, including:
_Thread_local storage type identifier, <threads.h> header file, which contains functions for thread creation and management.
_Atomic type modifier and <stdatomic.h> header file.
Enhanced Unicode support. Based on the C Unicode technical report ISO/IEC TR 19769:2004, enhanced support for Unicode. Including adding char16_t and char32_t data types for UTF-16/UTF-32 encoding, and providing the <uchar.h> header file containing Unicode string conversion functions.
The gets() function is deleted, and a new safer function gets_s() is used instead.
Added boundary check function interfaces and defined new safe functions, such as fopen_s(), strcat_s(), etc.
Added more floating-point processing macros.
Anonymous struct/union support. This has long existed in gcc, and C11 introduces it into the standard.
Static assertions (static assertions), _Static_assert(), which are processed after interpreting #if and #error.
New fopen() mode, ("…x"). Similar to O_CREAT|O_EXCL in POSIX, it is commonly used in file locks.
Added quick_exit() function as a third way to terminate the program. When exit() fails, the least cleaning work can be done.
Language Features
Basic Characteristics
C++ Language Code C++ Language Code
1. High-level language: It is a working unit that combines the basic structures and statements of high-level languages with the practicality of low-level languages.
2. Structured language: The prominent feature of structured language is the separation of code and data, that is, each part of the program is independent except for necessary information exchange. This structured method can make the program hierarchical, easy to use, maintain, and debug. The C language is provided to users in the form of functions, which can be easily called, and has various loops and conditional statements to control the program flow, so that the program is completely structured.
4. Code-level cross-platform: Due to the existence of the standard, almost the same C code can be used for multiple operating systems, such as Windows, DOS, UNIX, etc.; it is also suitable for multiple machine types. The C language is superior to other high-level languages for occasions that need to perform hardware operations.
5. Using pointers: You can directly perform operations close to the hardware, but the pointer operation of C is not protected, which also brings many unsafe factors. C++ has made improvements in this regard. While retaining pointer operations, it has enhanced security, which is supported by some users. However, because these improvements increase the complexity of the language, it is also criticized by another part. Java has learned from the lessons of C++, canceled pointer operations, and also canceled some controversial places in C++ improvements, achieving good results in security and suitability, but its own interpretation runs in the virtual machine, and the running efficiency is lower than C++/C. Generally speaking, C, C++, and java are regarded as languages of the same series, and they have long occupied the top three in the program usage list.
Unique Features
The C language is a procedural language with structured program design, variable scope (variable scope), and recursion function.
The C language passes parameters by value (pass by value), and also can pass pointers (a pointer passed by value).
Different variable types can be combined with structs.
There are only 32 reserved words (reserved keywords), which makes variable and function naming more flexible.
Some variable types can be converted, such as integer and character type variables.
Through pointers, the C language can easily perform low-level control of memory.
The preprocessor makes the compilation of the C language more flexible.
Advantages and Disadvantages
Advantages
1. Concise, compact, flexible and convenient
The C language has only 32 keywords and 9 control statements. The program writing form is free and case-sensitive. Combine the basic structures and statements of high-level languages with the practicality of low-level languages. The C language can operate on bits, bytes, and addresses like assembly language, and these three are the most basic working units of the computer.
C Language's Hello World Program C Language's Hello World Program
2. Rich operators
The operators of the C language cover a wide range, with a total of 34 operators. The C language treats parentheses, assignment, forced type conversion, etc. as operators. Thereby making the operation types of the C language extremely rich and the expression types diversified. Flexibly using various operators can realize operations that are difficult to implement in other high-level languages.
3. Rich data types
The data types of the C language include: integer type, real type, character type, array type, pointer type, structure type, union type, etc. It can be used to implement operations of various complex data structures. And the pointer concept is introduced, which makes the program more efficient.
4. Flexible and practical expression methods
The C language provides a variety of operators and methods for the value of expressions. The expression of the problem can be obtained through various ways, and its program design is more active and flexible. Its syntax restrictions are not too strict, and the program design freedom is large. For example, integer types, character types, and logical types can be used universally.
5. Allowing direct access to physical addresses and operating hardware
Because the C language allows direct access to physical addresses and can directly operate on hardware, it has both the functions of high-level languages and many functions of low-level languages. It can operate on bits (bit), bytes, and addresses like assembly language, and these three are the most basic working units of the computer, which can be used to write system software.
6. High quality of generated object code and high efficiency of program execution
The C language describes the problem more quickly than assembly language, with less workload and better readability, easy to debug, modify and port, and the code quality is equivalent to that of assembly language. The C language is generally only 10% to 20% less efficient than the object code generated by the assembly program.
7. Good portability
The C language compilation programs on different machines have 86% of the code in common, so the C language compilation program is easy to port. A program written in C language in one environment can be ported to run in a completely different environment with little or no modification.
8. Strong expressive power
The C language has rich data structures and operators. It contains various data structures, such as integer types, array types, pointer types, and union types, to implement operations of various data structures. The C language has 34 operators, with a wide range. Flexibly using various operators can implement extremely difficult operations.
The C language can directly access the physical address of hardware and can perform bit (bit) operations. It has many advantages of both high-level languages and low-level languages.
It can be used to write both system software and application software, and has become a general-purpose programming language.
In addition, the C language has powerful graphics functions and supports multiple displays and drivers. And it has powerful calculation functions and logical judgment functions.
Disadvantages
1. The main disadvantage of the C language is reflected in the encapsulation of data. This makes the C language have great defects in data security, which is also a big difference between C and C++.
2. The syntax restrictions of the C language are not very strict, and the type constraints on variables are not strict, which affects the security of the program, and does not check for array subscript out of bounds, etc. From an application perspective, the C language is more difficult to master than other high-level languages. That is to say, for people using the C language, more proficient programming is required.
New Features of C11
1. Standardization of alignment processing (including _Alignas identifier, alignof operator, aligned_alloc function, and <stdalign.h> header file).
2. _Noreturn function mark, similar to gcc's __attribute__(noreturn).
3. _Generic keyword.
4. Multithreading support, including: _Thread_local storage type identifier, <threads.h>; header file, which contains functions for thread creation and management.
5. Enhanced Unicode support, based on the C Unicode technical report ISO/IEC TR 19769:2004, enhanced support for Unicode. Including adding char16_t and char32_t data types for UTF-16/UTF-32 encoding, and providing the <uchar.h> header file containing Unicode string conversion functions.
6. The gets() function is deleted, and a new safer function gets_s() is used instead.
7. Added boundary check function interfaces and defined new safe functions, such as fopen_s(), strcat_s(), etc.
8. Added more floating-point processing macros.
9. Anonymous struct/union support, which has long existed in gcc, and C11 introduces it into the standard.
10. Static assertions (Static assertions), _Static_assert(), which are processed after interpreting #if and #error.
11. New fopen() mode, ("…x"), similar to O_CREAT|O_EXCL in POSIX, which is commonly used in file locks.
12. Added quick_exit() function as a third way to terminate the program. When exit() fails, the least cleaning work can be done.
13. _Atomic type modifier and <stdatomic.h> header file.
Language Components
Basic Composition
Data Types
The data types of C include: integer type, character type, real type or floating type (single-precision and double-precision), enumeration type, array type, structure type, union type, pointer type, and void type.
Constants and Variables
The value of a constant cannot be changed, and the symbolic constant name is usually in uppercase.
A variable is a quantity whose name is an identifier and whose value can be changed. The identifier is a sequence composed of letters or underscores followed by a string of letters, numbers, or underscores. Please note that the first character must be a letter or underscore, otherwise it is an illegal variable name. Variables are allocated corresponding storage units at compile time.
Array
If a variable name is followed by a bracket with a number, this declaration is an array declaration. A string is also an array. They end with the ASCII NULL. It should be particularly noted that the index value in the square brackets starts from 0.
Pointer
If a * is used in front of a variable declaration, it indicates that this is a pointer variable. In other words, the variable stores an address, and * (specifically referring to the unary operator * here, the same below. There is also a binary operator * in the C language) is the content fetch operator, which means fetching the content stored in this memory address. Pointers are one of the main features of the C language that distinguish it from other contemporary high-level languages.
Pointers can not only be the address of a variable, but also the address of an array, array elements, and functions. Through pointers as formal parameters, more than one return value can be obtained during the function call process, which is different from return (z) which can only get one return value.
Pointers are a double-edged sword. Many operations can be naturally expressed through pointers, but incorrect or excessive use of pointers will bring a large number of potential errors to the program.
String
The string of the C language is actually a char array ending with the '\0' character. Using a character type does not require referencing a library, but using a string requires some functions in the C standard library to operate on the string. They are different from character arrays. Using these functions requires referencing the header file <string.h>.
File Input/Output
In the C language, input and output are implemented through a set of functions in the standard library. In ANSI C, these functions are defined in the header file <stdio.h>;
Standard Input/Output
There are three standard input/outputs that are pre-defined by the standard I/O library:
stdin standard input
stdout standard output
stderr input and output error
Operation
The operation of the C language is very flexible and has very rich functions. The number of operation types is far more than that of other programming languages. In terms of expressions, it is more concise than other programming languages. For example, self-increment, self-decrement, comma operation, and ternary operation make the expression simpler, but beginners often find this expression difficult to read. The key reason is that they do not fully understand the operator and the operation order. When a variety of different operations form an operation expression, that is, when multiple operators appear in an operation expression, the priority order and associative rules of the operations are very important. In learning, it is reasonable to classify them, find the differences from the operations learned in mathematics, and then it is not difficult to remember these operations. Some operators will be firmly remembered after understanding, and will be handy in the future, while some can be temporarily forgotten and remembered when used.
First, it is necessary to clarify that the operators are classified according to different priorities. The operators in "C Programming" can be divided into 15 levels of priorities, from high to low, with priorities 1 ~ 15. Except for levels 2.13 and 14, which are combined from right to left, the others are combined from left to right, which determines the operation order of operators at the same level.
Keywords
Keywords are also called reserved words, that is, words that have been used by the C language itself and cannot be used for other purposes. For example, keywords cannot be used as identifiers such as variable names and function names
There are a total of 32 keywords defined by the ISO standard for the C language:
auto double int struct break else long switch
case enum register typedef char extern return union
const float short unsigned continue for signed void
default goto sizeof volatile do if while static inline
restrict _Bool _Complex _Imaginary _Generic
Basic Data Types
void: Declare that the function has no return value or no parameters, declare a pointer of no type, and explicitly discard the operation result. (Added in C89 standard)
char: Character type data, which belongs to a kind of integer data. (Introduced during K&R period)
int: Integer data, indicating the range usually specified by the compiler's memory bytes. (Introduced during K&R period)
float: Single-precision floating-point data, which belongs to a kind of floating-point data. (Introduced during K&R period)
double: Double-precision floating-point data, which belongs to a kind of floating-point data. (Introduced during K&R period)
_Bool: Boolean type (added in C99 standard)
_Complex: Basic type of complex number (added in C99 standard)
_Imaginary: Imaginary number, similar to the basic type of complex number, a pure imaginary number without a real part (added in C99 standard)
_Generic: Provide an interface entry for overloading (added in C11 standard)
Type Modifier Keywords
short: Modify int, short integer data, and the modified int can be omitted. (Introduced during K&R period)
long: Modify int, long integer data, and the modified int can be omitted. (Introduced during K&R period)
long long: Modify int, super long integer data, and the modified int can be omitted. (Added in C99 standard)
signed: Modify integer data, signed data type. (Added in C89 standard)
unsigned: Modify integer data, unsigned data type. (Introduced during K&R period)
restrict: Used to limit and restrict pointers, and indicate that the pointer is the only and initial way to access a data object. (Added in C99 standard)
Complex Type Keywords
struct: Structure declaration. (Introduced during K&R period)
union: Union declaration. (Introduced during K&R period)
enum: Enumeration declaration. (Added in C89 standard)
typedef: Declare type alias. (Introduced during K&R period)
sizeof: Get the size of a specific type or specific type variable. (Introduced during K&R period)
inline: Inline function is used to replace macro definition and will be expanded in any place where it is called. (Added in C99 standard)
Storage Level Keywords
auto: Specify as an automatic variable, which is automatically allocated and released by the compiler. Usually allocated on the stack. Opposite to static. When the variable is not specified, it is automatically auto by default. (Introduced during K&R period)
static: Specify as a static variable, allocated in the static variable area. When modifying a function, specify that the function scope is internal to the file. (Introduced during K&R period)
register: Specify as a register variable, and suggest that the compiler store the variable in the register for use. It can also modify function parameters and suggest that the compiler pass parameters through the register instead of the stack. (Introduced during K&R period)
extern: Specify the corresponding variable as an external variable, that is, indicate that the definition of the variable or function is in another file, and prompt the compiler to find its definition in other modules when encountering this variable and function. (Introduced during K&R period)
const: Specify that the variable cannot be changed by the current thread (but may be changed by the system or other threads). (Added in C89 standard)
volatile: Specify that the value of the variable may be changed by the system or other threads, and force the compiler to always obtain the value of the variable from the memory, preventing the compiler from optimizing the variable into a register variable. (Added in C89 standard)
Flow Control Keywords
Jump Structure
return: Used in the function body to return a specific value (if it is of void type, it does not return a function value). (Introduced during K&R period)
continue: End the current loop and start the next loop. (Introduced during K&R period)
break: Jump out of the current loop or switch structure. (Introduced during K&R period)
goto: Unconditional jump statement. (Introduced during K&R period)
Branch Structure
if: Conditional statement, no semicolon needs to be placed later. (Introduced during K&R period)
else: Conditional statement negative branch (used with if). (Introduced during K&R period)
switch: Switch statement (multiple branch statement). (Introduced during K&R period)
case: Branch mark in the switch statement, used with switch. (Introduced during K&R period)
default: "Other" branch in the switch statement, optional. (Introduced during K&R period)
Compilation
#define precompilation macro
#if expression #else if expression #else #endif conditional compilation
#ifdef macro #else #endif conditional compilation
#ifndef macro #else #endif conditional compilation and conditional compilation
Syntax Structure
Sequential Structure
The sequential structure program design is the simplest. As long as the corresponding statements are written in the order of solving the problem, its execution order is from top to bottom, and it is executed in sequence.
For example: a = 3, b = 5, now exchange the values of a and b. This problem is like exchanging water in two cups. Of course, a third cup is needed. Suppose the third cup is c, then the correct program is: c = a; a = b; b = c; the execution result is a = 5, b = c = 3. If the order is changed, written as: a = b; c = a; b = c; then the execution result becomes a = b = c = 5, which cannot achieve the expected purpose. Beginners are most likely to make this kind of mistake. The sequential structure can be used independently to form a simple complete program. The common three-step program of input, calculation, and output is a sequential structure. For example, calculate the area of a circle. The statement order of its program is to input the radius r of the circle, calculate s = 3.14159*r*r, and output the area s of the circle. However, in most cases, the sequential structure is part of the program, and forms a complex program with other structures. For example, the compound statement in the branch structure, the loop body in the loop structure, etc.
Selection Structure
The sequential structure program can solve problems such as calculation and output, but cannot make judgments and selections. For problems that need to make judgments and selections first, the selection structure should be used. The execution of the selection structure is based on certain conditions to select the execution path, rather than strictly following the physical order of the statements. The program design method of the selection structure is to construct a suitable branch condition and analyze the program flow. Select the appropriate selection statement according to different program flows. The selection structure is suitable for calculations with logical or relational comparison and other condition judgments. When designing such a program, it is often necessary to draw its program flow chart first, and then write the source program according to the program flow, which separates the program design analysis from the language, making the problem simple and easy to understand. The program flow chart is the program execution flow chart drawn according to the problem analysis.
Loop Structure
The loop structure can reduce the workload of repeatedly writing source programs, and is used to describe the problem of repeatedly executing a certain algorithm. This is the program structure that can best exert the characteristics of the computer. The C language provides four loops, namely goto loop, while loop, do while loop, and for loop. The four loops can be used to handle the same problem. Generally, they can be replaced with each other, but generally, the goto loop is not advocated, because forcibly changing the order of the program often brings unpredictable errors to the operation of the program.
Special attention should be paid to including statements that tend to end in the loop body (that is, the change of the loop variable value), otherwise it may become an infinite loop, which is a common mistake of beginners.
The similarities and differences of the three loops: When using the while and do...while loops, the initialization operation of the loop variable should be before the loop body, while the for loop is generally carried out in statement 1; the while loop and the for loop both judge the expression first, and then execute the loop body, while the do...while loop executes the loop body first and then judges the expression, that is to say, the loop body of do...while is executed at least once, while the while loop and for may not be executed even once. In addition, it should also be noted that these three loops can all use the break statement to jump out of the loop, and use the continue statement to end this loop, while the loop composed of the goto statement and if cannot be controlled by the break and continue statements.
The sequential structure, branch structure, and loop structure are not isolated from each other. There can be branches and sequential structures in the loop, and there can be loops and sequential structures in the branch. In fact, no matter which structure, they can be generally regarded as a statement. In the actual programming process, these three structures are often combined with each other to implement various algorithms and design corresponding programs. However, if the problem to be programmed is large, the written program is often very long and has repeated structures, resulting in poor readability and difficult understanding. The method to solve this problem is to design the C program into a modular structure.
Specific content:
for loop
The for loop structure is the most characteristic loop statement in the C language, and it is the most flexible and convenient to use. Its general form is:
for (expression 1; expression 2; expression 3) loop body statement. (Where; cannot be omitted)
Expression
Expression 1 is the initial value expression, used to assign an initial value to the loop variable before the loop starts.
Expression 2 is the loop control logic expression, which controls the condition for loop execution and determines the number of loops.
Expression 3 is the loop control variable modification expression, which makes the for loop tend to end.
The loop body statement is the statement that is repeatedly executed when the loop control condition is established.
However, in the entire for loop process, expression 1 is calculated only once, and expressions 2 and 3 may be calculated multiple times or not at all. The loop body may be executed multiple times or not at all.
First execute expression 2, then execute the loop structure, and finally expression 3, and this cycle continues.
The for loop statement is the most powerful statement in the C language, and can even replace other loop statements to a certain extent.
do
do loop structure, the execution order of do 1 while⑵; is 1->2->1... loop, and 2 is the loop condition.
while
while loop structure, the execution order of while(1) 2; is 1->2->1... loop, and 1 is the loop condition
The above loop statements continue the loop when the loop condition expression is true, and jump out of the loop when it is false.
Program Structure
The modular program structure of the C language is implemented by functions, that is, divide a large problem into several modules, each module is written as a C function, and then the main function calls the function and the function calls the function to implement the C program writing of a large problem. Therefore, it is often said: C program = main function + sub-function. Therefore, special attention should be paid to understanding and application of the definition, call, and value return of functions, and consolidated through debugging on the computer.
Judgment Statement (Selection Structure):
if statement: "If" statement; if-else statement: "If... (then)... else..." statement; switch statement: "Switch" statement; switch-case: "Switch-case" statement.
Loop Statement (Loop Structure):
while statement: "When..." statement; do-while statement: "Do... when... (time)" statement; for statement: conditional statement (that is, "(do)... for... " statement).
Jump Statement (Loop Structure: Whether to Loop):
goto statement: "Steering" statement, also called "jump" statement; break statement: "Interrupt" (loop) statement, that is, end the entire loop; continue statement: "Continue" statement (end this loop, continue the next loop); return statement: "Return" statement.
It needs to be explained that:
1. A C language source program can be composed of one or more source files.
2. Each source file can be composed of one or more functions.
3. A source program, no matter how many files it is composed of, has one and only one main function, that is, the main function. It is the entry of the entire program.
4. There can be preprocessing commands (including include commands, ifdef, ifndef commands, define commands) in the source program, and the preprocessing commands should usually be placed at the front of the source file or source program.
5. Each statement and each statement must end with a semicolon. But preprocessing commands, function headers, and curly braces "}" should not be followed by semicolons. (The "}" after the declaration of struct, union, and enumeration type should be followed by ";".)
6. There must be at least one space between identifiers and keywords to show the interval. If there are obvious interval symbols, no more spaces need to be added to separate.
Writing Rules
1. One statement or one statement occupies one line.
2. The part enclosed in {} usually represents a certain level of structure of the program. {} is generally aligned with the first letter of this structure statement and occupies a line alone.
3. The statements or instructions of the lower level can be written with several more spaces than the statements or instructions of the higher level. In order to look more clear and increase the readability of the program. When programming, you should try to follow these rules to develop a good programming style.
Function
A C program is composed of a set of external objects of variables or functions. A function is a self-contained execution code segment that completes certain related functions. We can regard a function as a "black box". As long as you send data in, you can get the result, and the external program does not know how the function works internally. All the external program knows is what is input to the function and what is output by the function. Functions provide a means of programming, making it easy to read, write, understand, debug, modify, and maintain.
The number of functions in a C program is actually unlimited. If there is any restriction, it is that there must be at least one function in a C program, and there must be one and only one function named main, which is called the main function, and the entire program starts from this main function.
The C language encourages and advocates that people divide a big problem into sub-problems, and compile a function for each sub-problem. Therefore, a C language program is generally composed of a large number of small functions rather than a small number of large functions, that is, "small functions constitute a large program". The advantage of this is that each part is fully independent and has a single task. Therefore, these fully independent small modules can also be used as a fixed specification of small "components" to form a new large program.
Over the years of development of the C language, a huge number of systems and programs developed with the C language have been accumulated. At the same time, a lot of directly usable library functions have also been accumulated.
ANSI C provides standard C language library functions.
Beginners of the C language prefer Turbo C 2.0, which provides more than 400 runtime functions, each of which completes a specific function, and users can call them freely. These functions are generally divided into input and output functions, mathematical functions, string and memory functions, functions related to BIOS and DOS, character screen and graphics functions, process control functions, directory functions, etc.
The Windows SDK provided by the Windows system contains thousands of functions related to Windows application development.
Other operating systems, such as Linux, also provide a large number of functions for application program developers to call.
As a programmer, you should try to be familiar with the functions of the target platform library. In this way, you can develop application programs for a specific platform with ease. For example, as a developer of Windows applications, you should try to be familiar with the Windows SDK; as a developer of Linux applications, you should try to be familiar with the Linux system calls and POSIX function specifications.
Operators
It is particularly special that the bit right shift (>>) operator can be arithmetic (the highest significant bit is filled on the left end) or logical (0 is filled on the left end) shift. For example, shifting 11100011 right by 3 bits, the arithmetic right shift becomes 11111100, and the logical right shift becomes 00011100. Because arithmetic bit right shift is more suitable for handling negative integers, almost all compilers are arithmetic bit right shift.
The priority of operators is roughly from high to low: unary operators, arithmetic operators, relational operators, logical operators, conditional operators, assignment operators (=), and comma operators.
()、 、 -> 、 .、!、 ++、 -- Parentheses, square brackets, pointer, member, logical not, self-increment, self-decrement
++ 、 -- 、 * 、 & 、 ~ 、! Unary operators
+、 - 、 sizeof、(cast)
* 、 / 、% Arithmetic operators
+ 、 - Arithmetic operators
<< 、 >> Bit operators
< 、 <= 、 > 、 >= Relational operators
== 、!= Relational operators
& Bitwise AND
^ Bitwise XOR
| Bitwise OR
&& Logical AND
|| Logical OR
? 、: Conditional operator
/= 、%= 、 &= 、 |= 、 ^= Assignment operator
= 、 += 、 -= 、 *= 、
, Sequence operator
Classic Errors
The usage of void main() is not formulated by any standard. The C language standard syntax is int main. Any implementation must support int main(void) { /* ... */ } and int main(int argc, char* argv) { /* ... */ }.
Operations like a+=a++ or (i++)+(i++)+(i++) belong to undefined behavior. It is not that this behavior has not been defined in the C language, it has long been concluded. Its result depends on the compiler implementation. Do not write such code!
References
1. Stephen Prata.C Primer Plus.United States: People's Post and Telecommunications Press, 2005: 1-3
2. Su Xiaohong, Sun Zhigang, Chen Huipeng.Practical C Language Tutorial.Beijing: Electronic Industry Press, 2013: 8-8
3. Development Process of C Language .C Language Chinese Network.2012-04-28
4. American National Standards Institute (ANSI - American National Standards Institute) .Standard Information Network
5. ISO/IEC 9899:1990 - Programming languages -- C .International Organization for Standardization (ISO - International Organization for Standardization)
6. ISO/IEC 9899:1990/Cor 1:1994 .International Organization for Standardization (ISO - International Organization for Standardization)
7. ISO/IEC 9899:1990/Amd 1:1995 - C Integrity .International Organization for Standardization (ISO - International Organization for Standardization)
8. ISO/IEC 9899:1999 - Programming languages -- C .International Organization for Standardization (ISO - International Organization for Standardization)
9. ISO/IEC 9899:2011 - Information technology -- Programming languages -- C .International Organization for Standardization (ISO - International Organization for Standardization)
10. Related Introduction to C Language Programming Tutorial .Spark Tutorial Network.2012-03-22
[ Last edited by zzz19760225 on 2018-7-31 at 21:29 ]
1<词>,2,3/段\,4{节},5(章)。
