C series (1)----Basic concepts of C language

After a few months of learning the C language, Shi Lu intends to give a more detailed description of the content of the C language in the form of a blog . There are more than 20 blogs in this series (tentative). Update one in 2-3 days . The relationship between the chapters is still relatively large, and there will be a phenomenon of interspersed before and after. I hope that the friends who are learning C or the big guys who have attacked other technologies can give Shi Lu. a lot of support! The difficulty factor of this blog is low , but don't worry that the follow-up content will be more substantial and exciting! Not much to say, this is the beginning of today's explanation ---- the basic concepts of C language .

content

simple C program

compile and link

Notes

variables and assignments

Error-prone but highly graspable identifiers

keywords


simple C program

world.c

#include <stdio.h>


int main(void)
{    
    printf("Hello world!");
    return 0;
}

This is a simple C program that looks simple but what it contains is not simple (compile and link).

#include <stdio.h> is essential , it contains the relevant information of the C language standard input/output library. The first line of code in the main function is what you want to express. The printf function comes from the standard input/output library (not the main content of this article and will be covered later) and can produce formatted output. The code \n is an escape character that tells the printf function to wrap the line after the message is displayed. The second line of code returns 0; indicating that the program will return 0 to the operating system when it terminates. (return 0 can also be written as exit(0), these two are completely equivalent)

Some programmers may have such doubts in their hearts : I ignore return 0 in the process of typing code; the program can still run, then return 0; it should not be necessary. Indeed, the return statement is not required; without the return statement, the program would terminate as well. In c89, the value returned to the operating system is undefined. In c99, if the return type in the main function declaration is int, the program returns 0 to the operating system; otherwise, the program returns an indeterminate value .

compile and link

The above mentioned simple C program is not simple, in fact it is because of compiling and linking . Junior programmers may not think about how the machine works when typing code. First, a file containing the program world.c needs to be generated . Of course, the name of the file is irrelevant, but compilers usually require the file to have the extension .c .

For a C program, its conversion into a machine-executable form usually includes three steps, namely preprocessing, compilation, and linking .

Preprocessing: First the program will be sent to the preprocessor. The preprocessor executes commands starting with #. The preprocessor is somewhat similar to compiling i, it can add content to the program, and it can also modify the program.

Compilation: Similar to a human translator, this is just the translation of the spoken program into machine language.
Linking: The object code produced by the compiler and other required additional code (mainly library functions) are integrated together, so that a fully executable program is finally produced.

Notes

Let's first talk about the style of C89 /* */. The symbol /* marks the start of a comment, and the symbol */ marks the end of a comment. E.g:

/* I am iron man. */

Comments can appear anywhere in a program. Of course, comments don't just eliminate code, they also explain the code you've written. These types of comments are also called "airfoil comments" .

The C89 style of comments is still potentially dangerous. Forgetting to terminate a comment can cause the compiler to ignore important parts. See the following code:

printf("I ");  /* balabala.....
printf("am ");
printf("iron "); /* balabala */
printf("man.");

This piece of code finally shows that the result is I man. . Because the closing tagline was omitted from the first comment, the compiler ignored the middle two statements.

C99 provides another kind of annotation, which is also used by various compilers today, starting with //:

//I am iron man.

Comments in this style are automatically terminated at the end of the line. If you want to uncomment any line, just add // before that line of code. This type of annotation is a nice avoidance of what happens in C89.

variables and assignments

Most programs often need to perform a series of calculations before producing output, so there needs to be a way to temporarily store data during program execution. These storage units are called variables. This blog only talks about int type variables and float type variables.

Int variables can store integers such as 0, 44, -2555. But the number of storage is limited, the largest integer is usually 2147483647.

Float variables can store much larger values ​​than int variables. Moreover, float type variables can be stored with decimal places , such as 66.666f. (when assigning a constant to a float type variable), but float type variables are slower than int type in arithmetic operations; more importantly, float type variables are all The stored value is often an approximation of the actual value.

Variables get their values ​​by assignment. E.g:

a = 2;
b = 3;
c = 4;

Assign 2 3 4 to abc respectively.

Variables must be declared before being assigned or otherwise used. That is to say, the following example does not work.

a = 2;
int a;

Error-prone but highly graspable identifiers

When writing programs, variables, functions, macros, and other entities need to be named, and these names are called identifiers. Many novice programmers do not pay attention to the details of the book or do not hear the key points in the course of reading the book, and they will repeatedly encounter obstacles in the usual practice of identifier-based problems.

In the c language, identifiers can contain letters, numbers and underscores (but keywords cannot be used as identifiers, such as int short inline keywords), but must start with a letter or underscore. See the following example:

 Options A and B all start with a letter, option D starts with an underscore, but option C starts with a number, so C is wrong. This kind of question is actually very simple, as long as you master the method, you can solve one question by one question.

But the C language is case- sensitive , that is, the C language distinguishes between uppercase and lowercase letters in identifiers. For example, the following identifiers are different:

shilv_
Shilv_
shIlv_

Different capitalization has different meanings.

Some programmers may have such doubts in their minds: Is there a limit to the length of the identifier? The answer is yes, but not entirely ! This has something to do with the compiler and is not discussed in detail here.

keywords

This blog mainly introduces the int type and float type, and the remaining keywords will be shared with you in future blogs.

Thank you for reading this. If you think this blog is helpful to you, please leave your likes and favorites! In the next few months, Shi Lu. will accompany you to climb the mountain of C language!
 

Guess you like

Origin blog.csdn.net/qq_64263760/article/details/122728667