5
头图

Hello everyone, I am a programmer cxuan! Learn the basics of C language with everyone today!

Preface

C language is a abstract, process-oriented language. The C language is widely used in the bottom layer of 161947745192e6. The C language occupies an irreplaceable role in the computer system. It can be said that the C language is the basis of programming, that is, No matter what language you learn, you should put the C language in the first place to learn . The following picture better illustrates the importance of the C language

<img src="https://s1.ax1x.com/2020/09/10/wG83gx.png" alt="01" border="0" style="zoom:50%;" >

As you can see, C language is a low-level language, a system-level language, and the operating system is written in C language, such as Windows, Linux, and UNIX. If other languages have a glamorous appearance, then the C language is the soul, always so simple and unpretentious.

link to the original text: C language basics, come here!

C language features

So, since the C language is so important, what is it worth learning? We shouldn’t just learn it because it’s important. What we care more about is what we can learn and what we can gain after learning.

C language design

The C language was designed in 1972 by Dennis Ritch and Ken Thompson of Bell Labs when developing the UNIX operating system. C language is a popular language that perfectly integrates computer science theory and engineering practice theory, enabling users to complete modular programming and design.

Computer Science Theory: referred to as CS, is a discipline that systematically studies the theoretical basis of information and calculations and how they are implemented and applied in computer systems.

C language is efficient

C language is an efficient language. It is designed to give full play to the advantages of computers. Therefore, C language programs run very fast, and C language can reasonably use memory to obtain maximum operating speed.

C language is portable

C language is a portable language, which means that C language programs written on one computer can be easily run on another computer, which greatly reduces the workload of program migration .

C language features

  • C language is a concise language, because C language design is closer to the bottom layer, so it does not require many features that are only available in high-level languages such as Java and C#, and the programming requirements are not very strict.
  • C language has structured control statements. C language is a structured language. The control statements it provides have structured features, such as for loops, if...else judgment statements, and switch statements.
  • C language has a wealth of data types, including not only the traditional character type, integer type, floating point type, array type and other data types, but also data types that other programming languages do not have, such as pointers.
  • C language can directly read and write memory addresses, so the main functions of assembly language can be realized and the hardware can be directly operated.
  • The C language is fast, and the generated target code is highly efficient.

Let us use a simple example to illustrate the C language

Entry-level C language program

Let's take a look at a very simple C language program. I think the tool doesn't matter if you use it smoothly.

The first C language program

#include <stdio.h>

int main(int argc, const char * argv[]) {
    printf("Hello, World!\n");
  
      printf("my Name is cxuan \n")
    
    printf("number = %d \n", number);
    
    return 0;
}

You may not know what this code means, but don't worry, let's run it and see the result.

This program outputs Hello,World! and My Name is cxuan Below we explain the meaning of each line of code.

First, the first line of #include <stdio.h> , this line of code contains another file, this line tells the compiler to stdio.h the content of 0619477451968c in the current program. stdio.h is a standard part of the C compiler software package, which can provide keyboard input and display output.

What is the C standard package? C is a general-purpose, procedural, imperative computer programming language developed by Dennis M in 1972. The C standard library is a set of C language built-in functions, constants and header files, such as <stdio.h>, <stdlib.h>, <math.h>, etc. This library will be used as a reference manual for C programmers.

We will introduce stdio.h later, and now you know what it is.

The following line of code in stdio.h is the function main

A C program can contain one or more functions. Functions are the root of the C language, just as methods are the basic structure of Java. main() represents a function name, and int represents that the main function returns an integer. void indicates that main() does not take any parameters. We will explain these in detail later, just remember that int and void are part of the standard ANSI C definition of main() (if you use a compiler before ANSI C, please ignore void).

Then it is /*A simple C language program*/ means a comment. The comment /**/ , and the content of the comment is between the two symbols. These symbols can improve the readability of the program.

Note: The comment is only to help the programmer understand the meaning of the code, the compiler will ignore the comment

Below is { , which is the left curly brace, which represents the beginning of the function body, and the last right curly brace } represents the end of the function body. { } middle of 06194774519821 is the place where the code is written, also called the code block.

int number means that a variable named number will be used, and number is int integer type of 06194774519844.

number = 11 represents the variable that assigns the value 11 to the number.

printf(Hello,world!\n); means to call a function. This statement uses the printf() Hello,world on the screen. The printf() function is one of the C standard library functions, which can output the results of the program to the display. The code \n means wrap, that is, start a new line and move the cursor to the next line.

Then the next line of printf() is the same as the above line, we won't say more. The last line of printf() is a bit interesting, you will find that there is a %d , which means that the output string is outputted using plastic.

The last line of the code block is return 0 , which can be regarded as the end of the main function, and the last line is the code block } , which represents the end of the program.

Okay, now that we have finished writing our first C language program, do you have a deeper understanding of C? Certainly not. . . Where is this, keep learning.

Now, we can summarize it into several elements of C language program, as shown in the figure below

<img src="https://s1.ax1x.com/2020/09/10/wG8D2t.png" alt="03" border="0">

C language execution flow

The reason why a C language program has become a high-level language is that it can read and understand people's thoughts. However, in order to be able to run the hello.c program in the system, each C statement must be converted into a series of low-level machine language instructions by other programs. These instructions are packaged as executable object program and stored in a binary disk file. The target program is also called an executable target file.

In UNIX systems, the conversion from source files to object files is performed by the compiler.

gcc -o hello hello.c

The gcc compiler driver reads hello.c from the source file and translates it into an executable file hello . This translation process can be represented by the following figure

<img src="https://s1.ax1x.com/2020/09/10/wG80PA.png" alt="04" border="0">

This is a complete hello world program execution process, which involves several core components: preprocessor, compiler, assembler, linker , let’s break them one by one below.

  • Preprocessing phase, the preprocessor will modify the source C program # #include <stdio.h> The command will tell the preprocessor to read stdio.h and insert it into the program as text. Then I got another C program hello.i , this program usually ends .i
  • Then is the compilation phase (Compilation phase), the compiler will hello.i into the text hello.s , which includes a assembly language program (assembly-language program).
  • After the compilation is completed, the assembly phase (Assembly phase), at this step, the assembler as will translate hello.s into machine instructions, and package these instructions into relocatable object program and place it in hello. c file. The 17 bytes it contains is the instruction code of the function main. If we open hello.o in a text editor, we will see a bunch of garbled codes.
  • The last one is the linking phase. Our hello program will call the printf function, which is part of the C standard library provided by the C compiler. The printf function is located in a printf.o , which is a separate pre-compiled object file, and this file must be linked with our hello.o. The linker (ld) will handle this merge operation. The result is that the hello file, which is an executable object file (or called an executable file), is ready to be loaded into memory and executed by the system.

You need to understand what the build system does

For the simple hello program above, we can rely on the compilation system to provide a correct and effective machine code. However, for the programmers we talked about above, there are several major features of the compiler you need to know

  • Optimizing program performance. Modern compilers are an efficient tool for generating good code. For programmers, you don't need to understand what the compiler does in order to write high-quality code. However, in order to write efficient C language programs, we need to understand some basic machine code and the process by which the compiler converts different C statements into machine code.
  • Understanding link-time errors. In our experience, some very complex errors are mostly caused by the linking phase, especially when you want to build large software projects.
  • Avoiding security holes. In recent years, buffer overflow vulnerabilities are the main culprit for network and Internet services, so it is necessary for us to avoid this problem.

System hardware composition

In order to understand what happens when the hello program is running, we need to first have an understanding of the hardware of the system. Below is a model of Intel system products, let’s explain it

<img src="https://s1.ax1x.com/2020/09/10/wG8UVe.png" alt="05" border="0">

  • bus (Buses): What runs in the entire system is a collection of electrical pipes called buses. These buses transfer byte information back and forth between components. Usually the bus is designed to transfer fixed-length byte blocks, that is, words. The number of bytes in a word (word length) is a basic system parameter, which varies from system to system. Most words are now 4 bytes (32 bits) or 8 bytes (64 bits).

<img src="https://s1.ax1x.com/2020/09/10/wG8tbD.png" alt="06" border="0">

  • I/O Devices (I/O Devices): Input/Output devices are the connection between the system and the outside world. There are four types of I/O devices in the above picture: keyboard and mouse for user input, monitor for user output, and a disk drive for long-term storage of data and programs. At the beginning, the executable program is saved on the disk.

    Each I/O device connected to the I/O bus is called a controller or a adapter. The main difference between the controller and the adapter is the packaging method. The controller is the chipset on the I/O device itself or the main printed circuit of the system (usually called the motherboard). The adapter is a card that plugs into the motherboard slot. Regardless of the organizational form, their ultimate goal is to exchange information with each other.

  • Main Memory, the main memory is a temporary storage device, not a permanent storage, and the disk is a permanent storage device. The main memory not only saves the program, but also saves the data processed by the processor to execute the process. In terms of physical composition, the main memory is a collection of DRAM(dynamic random access memory) dynamic random storage. Logically speaking, memory is a linear byte array with its unique address number, starting from 0. Generally speaking, each machine instruction composing a program is composed of a different number of bytes, and the size of the data item corresponding to the C program variable changes according to the type. For example, on a Linux x86-64 machine, short data requires 2 bytes, int and float require 4 bytes, and long and double require 8 bytes.
  • processor, CPU(central processing unit) or simple processor, is an engine that interprets (and executes) the instructions stored in the main memory. The core size of the processor is a word storage device (or register), called program counter (PC). At any moment, the PC points to a certain machine language instruction in the main memory (that is, the address containing the instruction).

    From the time the system is powered on, until the system is powered off, the processor has been continuously executing the instruction pointed to by the program counter, and then updated the program counter to point to the next instruction. The processor operates according to the instruction model defined by its instruction set architecture. In this model, instructions are executed in a strict order, and executing an instruction involves performing a series of steps. The processor reads the instruction from the memory pointed to by the program counter, interprets the bits in the instruction, performs some simple operations indicated by the instruction, and then updates the program counter to point to the next instruction. Instructions may be continuous or discontinuous (for example, jmp instructions will not be read sequentially)

    Here are a few steps that the CPU may perform simple operations

  • Load: Copy a byte or a word from the main memory to the memory, overwriting the previous content of the register
  • Store: Copy the byte or word in the register to a certain location in the main memory, thereby overwriting the previous content of the location
  • Operation (Operate): Copy the contents of the two registers to ALU(Arithmetic logic unit) . Perform arithmetic operations on two words, store the result in a register, and rewrite the previous contents of the register.
Arithmetic logic unit (ALU) is a combined digital electronic circuit that performs arithmetic and bitwise operations on digital binary numbers.
  • jump: extract a word from the instruction, copy this word to the program counter (PC), overwrite the original value

Analyze the execution process of the hello program

Earlier we briefly introduced the composition and operation of the computer's hardware. Now we formally introduce what happens when running the sample program. We will describe it from a macro perspective, and will not involve all the technical details.

At the beginning, the shell program executes its instructions, waiting for the user to type a command. When we input the ./hello on the keyboard, the shell program reads the characters into the register one by one, and then puts them into the memory, as shown in the figure below

<img src="https://s1.ax1x.com/2020/09/10/wG8d5d.png" alt="07" border="0">

When we hit the enter key on the keyboard, the shell program knows that we have finished inputting the command. Then the shell executes a series of instructions to load the executable hello file. These instructions copy the code and data in the target file from the disk to the main memory.

Using DMA(Direct Memory Access) technology can directly copy the data in the disk to the memory, as follows

<img src="https://s1.ax1x.com/2020/09/10/wG8aUH.png" alt="08" border="0">

Once the code and data in the hello in the target file are loaded into the main memory, the processor starts to execute the machine language instructions in the main program of the hello program. These instructions hello,world\n string from the main memory to the register file, and then copy from the register to the display device, and finally display it on the screen. As follows

<img src="https://s1.ax1x.com/2020/09/10/wG8YDO.png" alt="09" border="0">

Cache is the key

We have introduced the execution process of a hello program above. The system spends a lot of time moving information from one place to another. The machine instructions of the hello program are initially stored on the disk. When the program is loaded, they will be copied to the main memory When the CPU starts to run, the instructions are copied from the memory to the CPU. Similarly, the string data hello,world \n was originally on the disk, it was copied to the memory, and then output to the display device. From the programmer's point of view, most of this copying is overhead, which slows down the efficiency of the program. Therefore, for system design, one of the most important tasks is to make the program run faster and faster.

Due to the laws of physics, larger storage devices are slower than smaller storage devices. As the processing efficiency of registers and memory is increasing, system designers have adopted smaller and faster storage devices for this difference, called cache memory (cache memory, referred to as cache cache). As a temporary staging area, it stores information that may be needed in the near future. As shown below

<img src="https://s1.ax1x.com/2020/09/10/wG8JKK.png" alt="10" border="0">

In the figure, we have marked the location of the cache. The L1 cache capacity in the cache can reach tens of thousands of bytes, and the access speed is almost as fast as the register file. L2 cache with larger capacity is connected to the CPU through a special bus. Although the L2 cache is 5 times slower than the L1 cache, it is still 5-10 times faster than the memory. L1 and L2 are implemented using a static random access memory (SRAM) hardware technology. The latest and more powerful systems even have three levels of cache: L1, L2, and L3. The system can obtain a large memory, and the access speed is also faster. The reason is that the locality principle of the cache is used.

Again: the details of the entry program

Now, let's explore entry-level program, from the shallower to the deeper to understand the characteristics of the C language.

include<stdio.h>

As we mentioned above, #include<stdio.h> is the content to be processed before the program is compiled, which is called compilation pre-command.

Preprocessing commands are processed before compilation. The preprocessing program generally starts with the number #

All C compiler software packages provide stdio.h files. This file contains the input and output functions used by the compiler, such as println() information. The meaning of the file name is standard input/output header file. Usually, the collection of information at the top of the C program is called the header.

The first standard of C was published by ANSI. Although this document was later adopted by the International Organization for Standardization (ISO) and the revised version issued by ISO was also adopted by ANSI, the name ANSI C (rather than ISO C) is still widely used. Some software developers use ISO C , and some use Standard C .

C standard library

In addition to <sdtio.h>, the C standard library also includes the following header files

<img src="https://s1.ax1x.com/2020/09/10/wG88v6.png" alt="11" border="0" style="zoom:50%;" >

<assert.h>

A keyword named assert is provided, which is used to verify the hypothesis made by the program and output a diagnostic message if the hypothesis is false.

<ctype.h>

The ctype.h header file of the C standard library provides some functions that can be used to test and map characters.

These characters accept int as a parameter, and its value must be EOF or an unsigned character

EOF is a computer term, an abbreviation of End Of File, which means that the data source has no more data to read in the operating system. The data source is usually called a file or a stream. Usually, the presence of this character at the end of the text indicates the end of the data.

<errno.h>

errno.h header file of the C standard library errno , which is set through system calls. These library functions indicate what has gone wrong.

<float.h>

float.h header file of the C standard library contains a set of platform-dependent constants related to floating-point values.

<limits.h>

limits.h The header file determines the various attributes of various variable types. The macros defined in this header file restrict the values of various variable types (such as char, int, and long).

<locale.h>

locale.h The header file defines the settings of a specific region, such as date format and currency symbol

<math.h>

math.h The header file defines various mathematical functions and a macro. All functions available in this library have a double , and all return results of type double

<setjmp.h>

setjmp.h header file defines macro setjmp() , function longjmp() and variable type jmp_buf , this variable type will bypass the normal function call and return rules.

<signal.h>

signal.h header file defines a variable type , two function calls and some macros to handle different signals reported during program execution.

<stdarg.h>

stdarg.h The header file defines a variable type va_list and three macros. These three macros can be used to obtain parameters in a function when the number of parameters is unknown (that is, the number of parameters is variable).

<stddef.h>

stddef.h The header file defines various variable types and macros. Most of these definitions also appear in other header files.

<stdlib.h>

header file defines four variable types, some macros and various general utility functions.

<string.h>

string .h The header file defines a variable type, a macro, and various functions for manipulating character arrays.

<time.h>

time.h The header file defines four variable types, two macros and various functions for operating date and time.

main() function

The main function sounds like a mischievous kid deliberately named the main method to tell others that he is the center of the world. But this is not the case, and the main() method is indeed the center of the world.

The C language program must start execution from the main() function, except for the main() function, you can name other functions at will. Usually, () after main represents some incoming information. In our example above, no information is passed because the input in parentheses is void.

In addition to the above kind of writing, there are two ways to represent the main method, one is void main(){} and the other is int main(int argc, char* argv[]) {}

  • void main() declares a construction method with uncertain parameters
  • int main(int argc, char* argv[]) {} where argc is a non-negative value, representing the number of parameters passed from the environment of the running program to the program. It is a pointer to the first element of the argc + 1 pointer array, the last of which is null, and the previous one (if any) points to a string representing the parameter passed from the host environment to the program. If argv[0] is not a null pointer (or equivalently, if argc> 0), it points to a string representing the program name, and if the program name cannot be used in the host environment, the string is empty.

Annotation

In the program, the use of /**/ means comments. Comments are of no practical use to the program, but they are very useful to programmers. They can help us understand the program and allow others to understand the program you wrote. , In our development work, we are very disgusted with people who do not write comments, which shows that comments are very important.

<img src="/Users/mr.l/Library/Application Support/typora-user-images/image-20200830071857009.png" alt="image-20200830071857009" style="zoom:50%;" />

The advantage of C language comments is that it can be placed anywhere, even if the code is on the same line. Longer comments can be expressed in multiple lines. We use /**/ to indicate multi-line comments, while // only indicates single-line comments. The following are the representations of several annotations

// 这是一个单行注释

/* 多行注释用一行表示 */

/*
     多行注释用多行表示
          多行注释用多行表示
               多行注释用多行表示
                    多行注释用多行表示

*/

Function body

After the header file and the main method is the function body (comments are generally not counted), the function body is the execution body of the function, where you write a lot of code.

Variable declaration

In our entry-level code, we declared a number , its type is int, this line of code is called declaration, declaration is one of the most important features of the C language. This declaration accomplishes two things: defines a variable named number, and defines the specific type of number.

int is a keyword in C language, which represents a basic C language data type. Keywords are used for language definitions. You cannot use keywords as variables for definition.

number in the example is a identifier, which is the name of a variable, function or other entity.

Variable assignment

In the introductory example program, we declare a number variable and assign it a value of 11. Assignment is one of the basic operations of the C language. The meaning of this line of code is to assign the value 1 to the variable number. When executing int number, the compiler reserves space for the variable number in the computer memory, and then stores the value in the previously reserved location when executing this line of assignment expression statement. You can assign different values to number, which is why number is called variable.

<img src="https://s1.ax1x.com/2020/09/10/wG8181.png" alt="12" border="0">

printf function

In the introductory example program, there are three lines of printf(), which is a standard function of the C language. The content in parentheses is passed from the main function to the printf function. There are two types of parameters: actual argument and formal parameters. The content in the parentheses of the printf function we mentioned above are all actual parameters.

return statement

In the introductory example program, the return statement is the last statement. int main(void) indicates that the main() function should return an integer. A C function with a return value must have a return statement, and a program without a return value is also recommended to keep the return keyword. This is a good habit or a unified coding style.

semicolon

In the C language, the end of each line must ; , which means the end of a statement, if you forget or omit the semicolon, the compiler will prompt an error.

Keyword

The following are the keywords in the C language. There are 32 keywords in the C language, 0619477451edc, which are divided according to their different functions.

Data type keywords

There are mainly 12 keywords for data types, namely

  • char : Declare a character variable or function
  • double : Declare double-precision variables or functions
  • float : Declare floating-point variables or functions
  • int : Declare integer variables or functions
  • long : Declare a long integer variable or function
  • short : Declare a short integer variable or function
  • signed : Declare a signed variable or function
  • _Bool : declare boolean type
  • _Complex : Declare plural
  • _Imaginary : Declare imaginary number
  • unsigned : Declare an unsigned variable or function
  • void : Declare a function with no return value or no parameters, and declare an untyped pointer
Control statement keywords

There are also 12 keywords that control the loop of sentences, namely

Loop statement

  • for : for loop, the most used
  • do : The precondition of the loop statement loop body
  • while : Loop condition of loop statement
  • break : Jump out of the current loop
  • continue : End the current cycle and start the next cycle

Conditional statement

  • if : Judgment condition of conditional statement
  • else : Negative branch of conditional statement, used in conjunction with if
  • goto : Unconditional jump statement

switch statement

  • switch : used for switch statements
  • case : Another branch of the switch statement
  • default : other branches in the switch statement

return statement

retur : Subroutine return statement (with or without parameters)

Storage type keywords
  • auto : The declaration of automatic variables is generally not used
  • extern : The declared variable is being declared in another file (can also be regarded as a reference variable)
  • register : declare register variables
  • static : Declare static variables
Other keywords
  • const : Declare read-only variables
  • sizeof : Calculate the length of the data type
  • typedef : used to alias the data type
  • volatile : Explain that variables can be changed implicitly during program execution

postscript

In this article, we first introduced the characteristics of the C language, why the C language is so popular, and the importance of the C language. After that, we start with a C language entry program. We talked about the basic components of the C language. The C language is in the hardware How the above works, the compilation process and execution process of the C language, etc. After this, we have further explained the composition characteristics of the introductory example program.

If you think this article is good, welcome friends to walk in four consecutive times: , watching, comment, and share . Your Silian is my motivation for more writing.

Data in C

After we understand the introductory example program above, we will have a comprehensive understanding of the C language program. First of all, let's get to know the most basic variables and constants of the C language.

Variables and constants

Variables and constants are the two basic objects handled by programs.

Some data types have been set before the program is used, and there is no change in the whole process (the description in this paragraph is inaccurate, but for the sake of easy understanding, I will describe it for the time being), this kind of data is called constant (constant ). Another data type may change during program execution. This data type is called variable (variable). For example, int number is a variable, and 3.1415 is a constant, because once int number is declared, you can assign any value to it, and 3.1415 will not change once it is declared.

variable name

It is necessary to talk about the concept of variable names before talking about data types. A variable name is a sequence of letters and numbers, and the first character must be a letter. In the process of naming variable names, the underscore _ is regarded as a letter, and the underscore is generally used for variable names with longer names, which can improve the readability of the program. Variable names usually do not start with an underscore. In C, there is a difference between upper and lower case, that is, a and A are completely different variables. Generally, variable names use lowercase letters, and symbolic constants (defined by #define) are all uppercase. When choosing a variable name, try to describe the purpose of the variable literally, and avoid such abc meaningless variables.

It should also be noted that generally local variables use shorter variable names, and external variables use longer names.

type of data

Before understanding the data types, we need to understand these concepts bits, bytes and words .

Bits, bytes, and words are all descriptions of the computer storage unit . In the computer world, the smallest unit is bits (bit), a bit means a 0 or 1, generally when your friend asks your computer is xxx bit, the common ones are 32-bit or 64-bit, here is the bit It refers to the bit, and the bit is the Chinese name of bit, so the 32-bit or 64-bit here refers to the 32-bit or 64-bit. Byte is the basic storage unit. The basic storage unit says that it is stored in bytes in the computer. A byte is equal to 8 bits, that is, 1 byte = 8 bits. A word is a natural storage unit. In modern computers, a word is equal to 2 bytes.

There are many data types in C language, let's introduce them one by one below.

Integer

The integer type in C language is int , which can be a positive integer, a negative integer, or zero. The range of values is also different in computers with different digits. However, in 32-bit and 64-bit computers, the value range of int is 2^32, which is -2147483648 ~ +2147483647, and the value range of unsigned type is 0 ~ 4294967295.

Integers are stored as binary integers, divided into signed numbers and unsigned numbers . Signed numbers can store positive integers, negative integers, and zeros; unsigned numbers can only store positive integers and zeros.

You can use printf to print out the value of type int, as shown in the following code.

#include <stdio.h> 
int main(){
 int a = -5;
 printf("%d\n",a);
 
 unsigned int b = 6;
 printf("%d\n",b);
 
}

The C language also provides three auxiliary keywords to modify integer types, namely short, long and unsigned .

  • The short int type (or short for short) occupies less storage space than the int type and is suitable for scenarios with small values.
  • occupied by long int or long may be more than that of int type, which is suitable for scenarios with larger values.
  • Long long int or long long (added by C99) occupies more storage space than long, and is suitable for larger values, occupying at least 64 bits. Similar to int, long long is also a signed type.
  • Unsigned int or unsigned is only used for non-negative value scenarios. The value range of this type is different. For example, the 16-bit unsigned int represents the range from 0 to 65535 instead of -32768 to 32767.
  • In the C90 standard, unsigned long int or unsigned long and unsigned short int or unsigned short types were added, and unsigned long long int or unsigned long long was added in C99.
  • Add signed before any signed type to emphasize the intention of using signed types. For example, short, short int, signed short, and signed short int all represent one type.

For example, the above description can be declared with the following codes:

long int lia;
long la;
long long lla;
short int sib;
short sb;
unsigned int uic;
unsigned uc;
unsigned long uld;
unsigned short usd;
One thing to note here is that variables defined by unsigned can display negative values when formatted and output according to printf. Why? It's not that the value modified by unsigned can't be negative. That's because the variable modified by unsigned will be useful in calculations, and the output has no effect. This is also a pit that cxuan stepped on when it first started learning.

Our classmates who have studied Java were puzzled by these definitions at first. Why does a C language have so many definitions of data types? C language is really troublesome, I won't learn it!

Don't have this kind of thought. If you have this kind of thought, you must be protected like a child by JVM! I must correct your thoughts from now on, because Java is protected by JVM and many features are optimized, and C is like a child without an umbrella, it must deal with the world on its own!

When talking about short int and long int above, both have added a possibility. What, is it that short int and long int are different from int?

Here is a unique style of C language data types.

Why is it possible? This is due to the grammatical rules set by the C language to adapt to different machines. On early computers, both the int type and the short type occupies 16 bits, and the long type occupies 32 bits. In later computers, Both use 16-bit storage short type, 32-bit storage int type and long type. Nowadays, computers generally use 64-bit CPUs. In order to store 64-bit integers, the long long type is introduced. Therefore, the common settings on personal computers are that long long occupies 64 bits, long occupies 32 bits, short occupies 16 bits, and int occupies 16 bits or 32 bits.
char type

The char type is generally used to store characters, and the representation method is as follows

char a = 'x';
char b = 'y';

Char is called a character type, and can only be expressed with single quotes'' instead of double quotes "", which is the opposite of the representation of strings.

Although char represents characters, char actually stores integers instead of characters. Computers generally use ASCII to process characters. The standard ASCII code ranges from 0 to 127, which only needs to be represented by a 7-bit binary number. C language stipulates that char occupies 1 byte.

In fact, the integer type and the character type are similar, and their storage in the memory is essentially the same. When the compiler finds char, it will automatically convert it to an integer for storage. On the contrary, if you assign an English character to the int type, it will also be converted to an integer. Store, the following code

#include <stdio.h>

int main(){
 char a = 'x';
 int b;
 b = 'y';
 
 printf("%d\n%d\n",a,b);
}

Output

120

121

Therefore, int and char only have different storage ranges. Integer types can be 2 bytes, 4 bytes, or 8 bytes, while character types only occupy 1 byte.

Some C compilers implement char as a signed type, which means that char can represent a range of -128 ~ 127, while some compilers implement char as an unsigned type. In this case, char can represent a range of 0- 255. Signed char represents a signed type, and unsigned char represents an unsigned type.

_Bool type

The _Bool type is a new data type added to C99, used to represent Boolean values. That is, the logical values true and false. Before C99, it was represented by 1 and 0 in int. So _Bool is also a data type to some extent. To indicate 0 and 1, 1 bit is enough.

float, double, and long double

Integers are sufficient for most software development projects. floating-point numbers are often used in the financial and mathematical fields. Floating point numbers in C language have float, double and long double types. The floating-point number type can represent a larger range of numbers including decimals. Floating point numbers can represent decimals, and the range is relatively large. The representation of floating-point numbers is similar to scientific and technical methods. Here are some examples of scientific notation:

numberScientific notationExponential Notation
10000000001 * 10^91.0e9
4560004.56 * 10^54.56e5
372.853.7285 * 10 ^ 23.7285e2
0.00252.5 * 10 ^ -32.5e-3

C stipulates that the float type must be able to represent at least 6 significant digits, and the value range is at least 10^-37 ~ 10^+37. Normally, the storage of a floating-point number in the system takes up 32 bits.

Another floating-point type provided by C is double (double-precision type) . Generally speaking, double occupies 64 bits instead of 32 bits.

The third type provided by C is long double, which is used to meet higher precision requirements than the double type. However, C only guarantees that the long double type is at least the same as the double type.

The declaration of floating-point numbers is similar to that of integers. Here are some declarations of floating-point numbers.

#include <stdio.h>

int main(){

 float aboat = 2100.0;
 double abet = 2.14e9;
 long double dip = 5.32e-5;
 
 printf("%f\n", aboat);
 printf("%e\n", abet);
 printf("%Lf\n", dip);
 
}

The printf() function uses %f conversion instructions to print float and double type floating-point numbers in decimal notation, and use %e to print floating-point numbers in exponential notation. To print long double type, use %Lf conversion specification.

Regarding floating-point numbers, we also need to pay attention to the overflow and underflow.

Overflow refers to that because the number is too large, it exceeds the range that the current type can represent, as shown below

float toobig = 3.4E38 * 100.0f;printf("%e\n",toobig);

The output content is inf , which means that the result of toobig exceeds its defined range, the C language will assign a specific value representing infinity to toobig, and the printf display value is inf or infinity.

Underflow: because the value is too small, lower than the smallest value that the current type can represent, the computer has to shift the mantissa bit to the right, vacating the first binary bit, but at the same time, it loses the original end The number above the significant digit, this situation is called underflow. For example, the following code

float toosmall = 0.1234e-38/10;printf("%e\n", toosmall);
Complex and imaginary types

Many scientific and engineering calculations require the use of complex and imaginary numbers. The C99 standard supports complex and imaginary types. There are 3 types of complex numbers in the C language: float _Complex, double _Complex and long double _Complex .

There are three types of imaginary numbers provided by the C language: float _Imaginary, double _Imaginary and long double _Imaginary .

If you include the complex.h header file, you can use complex to replace _Complex, and imaginary to replace _Imaginary.

Other types

In addition to the types we introduced above, there are other types in the C language, such as arrays, pointers, structures, and unions. Although the C language does not have a string type, the C language can handle strings very well.

constant

In many cases, we need constants, whose values will not change during the execution of the entire program, such as 24 hours a day, the size of the maximum buffer, the maximum value of the sliding window, etc. These fixed values are called constants or literals .

Constants are also divided into many types, integer constants, floating-point constants, character constants, string constants, we will introduce them separately below

integer constant

Integer constants can be expressed in decimal, octal, or hexadecimal. The prefix specifies the radix: 0x or 0X means hexadecimal, 0 means octal, and without a prefix, it means decimal by default. An integer constant can also have a suffix. The suffix is a combination of U and L. U represents an unsigned integer (unsigned), and L represents a long integer (long).

330         /* 合法的 */315u        /* 合法的 */0xFeeL      /* 合法的 */048         /* 非法的:8 进制不能定义 8 */

Floating point constant

Floating-point constants consist of integer part, decimal point, decimal part and exponent part. You can use decimal form or exponential form to represent floating-point constants.

When expressed in decimal form, it must include an integer part, a decimal part, or both. When expressed in exponential form, the decimal point, exponent, or both must be included. Signed exponents are introduced with e or E.

3.14159       /* 合法的 */314159E-5L    /* 合法的 */510E          /* 非法的:不完整的指数 */210f          /* 非法的:没有小数或指数 */

Character constant

Character constants in C language use single quotation marks (ie, apostrophes) to enclose a character. For example,'a','x','D','?','$', etc. are all character constants. Note that'a' and'A' are different character constants.

In addition to the above forms of character constants, C also allows a special form of character constants, which is a sequence of characters starting with a "\". For example, the'\n' in the printf function, which has been encountered before, represents a newline character. This is a control character and cannot be displayed on the screen.

Commonly used special characters starting with "\" are

image-20211109055031470

The characters listed in the table are called "escape characters", which means to convert the characters after the backslash (\) into another meaning. For example, the "n" in'\n' does not represent the letter n but serves as a "line feed" character.

The last second line in the table uses ASCII code (octal number) to represent a character, for example,'\101' represents the character "A" with ASCII code (decimal number) of 65. '\012' (decimal ASCII code is 10) represents line feed.

It should be noted that'\0' or'\000' represents the control character with ASCII code 0, which is used in the string.

string constant

String constants are usually represented by "". A string is a collection of characters. A string contains characters similar to character constants: ordinary characters, escape sequences, and general characters.

Constant definition

In C language, there are two ways to define constants.

  1. Use #define preprocessor for preprocessing
  2. Use const keywords for processing

Below is the code for constant definition using #define preprocessor.

#include <stdio.h>#define LENGTH 5#define WIDTH 10int main(){  int area = LENGTH * WIDTH;  printf("area = %d\n", area); }

Similarly, we can also use the const keyword to define constants, as shown in the following code

#include <stdio.h>int main(){  const int LENGTH = 10; const int WIDTH = 5;  int area; area = LENGTH * WIDTH;  printf("area = %d\n", area); }
So what is the difference between these two constant definition methods?

Compiler processing is different

The #define preprocessor is used in the preprocessing stage, and the const modified constants are used in the compilation stage.

Type definition and check are different

Use #define without declaring data types, and without type checking, just definitions; while using const, you need to declare specific data types, and type checking will be performed during the compilation phase.

Article reference:

https://www.zhihu.com/question/19668080

I wrote four PDFs myself, very hardcore, the links are as follows

cxuan I worked hard to produce four PDFs.

Finally, I recommend my own Github , which contains a lot of hardcore articles, which will definitely help you.


程序员cxuan
4.7k 声望17k 粉丝