Learning C language notes: initial C language

Learning Content:

1. Operator - =;

2. Functions - main(), printf();

3. Write a simple C program;

4. Create an integer variable, assign it a value and display its value on the screen;

5. Newline character;

6. How to write comments in the program, create a program containing multiple functions, and find program errors;

7. What is a keyword.

Start with a simple example C program

The first.c program is as follows, which demonstrates some basic features of programming in C language.

#include<stdio.h>
int main(void)                           /*一个简单的C程序*/
{
    int num;                         /*定义一个名为num的变量*/
    num=1;                           /*为num赋值*/
    printf("I am a simple ");        /*使用printf()函数*/
    printf("computer.\n");
    printf("My favorite number is %d because it is first.\n",num);

    return 0;
}

From looking at the code of this program you would think that the program would print something to the screen, and you would be right! But just looking at the program may not know the specific content of the print, so run the program and check the results. First, use your familiar editor to create a file containing all the contents of first.c. Give the file a name with .c as the extension to meet the requirements of the current system for the file name. Then compile and run the program. If running correctly, the program's output should be:

I am a simple computer.                                                                                                

My favorite number is 1 because it is first.

What are \n and %d in the program? Next learn about the meaning of each line of code in the above program.

#include: preprocessor directive;

int main(): main() is always the first function to be called, and it is the statement that makes up the function;

function a(): function, a function is a building block that makes up a C program;

 6 statements in C language:

label statement; conformance statement; expression statement; selection statement; iteration statement; jump statement;

#include<stdio.n> includes another file

This line tells the compiler to include the contents of stdio.h in the current program. stdio.h is a standard part of the C compiler software, he provides keyboard input and screen output support. The function of #include<stdio.h> is equivalent to inputting all the content in the stdio.h file into the location of the line. In fact, this is a "paste-copy" operation. include files provide a convenient way to share information common to many programs.

The line #include is a C preprocessor directive. Usually, the C compiler will do some preparation work on the source code before compiling, that is, preprocessing.

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

Header files contain information that the compiler will use to create the final executable program. For example, a header file can define some constants, or specify function names and how to use them. However, the actual code for the function is in a library file of precompiled code. In short, header files help the compiler put your program together correctly. Which functions to use should include the corresponding header files. For example, to use the printf() function, the stdio.h header file must be included.

int main(void) function name

A C program contains one or more functions, which are the basic building blocks of a C program. There is a function called main() in the program first.c. The parentheses indicate that main() is a function name. int indicates that the main() function returns an integer, and void indicates that main() does not take any parameters. int and void are part of the standard ANSI C definition of main(). Generally, void can also be omitted.

A C program must start executing from the main() function. In addition to the main () function, you can name other functions, and the main () function must be the first function. What is the function of parentheses? Used to identify main() is a function.

/* A simple C program */ Comments

Comments are between the /* and */ symbols, these comments can improve the readability of the program. Note that comments are only to help readers understand the program, and will not participate in compilation and operation. There is another style of annotation:

//A simple C program

This kind of comment is limited to a single line, and the end of a line marks the end of the comment, so this style of comment only needs to indicate the // symbol at the beginning of the comment. 

{ function body begins

A left curly brace indicates the beginning of a function definition, and a right curly brace indicates the end of a function definition.

return 0; return statement

C functions can provide (or return) a number to the caller. For now, consider this line temporarily as a requirement to end the main() function.

The int in int main(void) indicates that the main() function should return an integer. The C standard requires main() to do this. C functions that return a value must have a return statement. The statement starts with the return keyword, followed by the value to be returned, and ends with a semicolon. If the return statement in the main() function is omitted, the program will return 0 when it runs to the outermost right curly brace. Therefore, the return statement at the end of the main() function can be omitted. However, don't miss it among other functions that return a value.

newline character

The \n character in the double quotes of the printf() function is not output, why is this? \n means newline. The \n combination represents a newline character. For printf(), it means "start a new line at the far left of the next line". That is, printing a newline has the same effect as pressing the Enter key on the keyboard. That being the case, why not just use the Enter key when typing printf() parameters? Because the editor may think this is a direct command, not an instruction stored in the source code. In other words, if the Enter key is pressed directly, the editor exits the current line and starts a new one. However, line breaks only affect the display format of program output.

A newline character is an escape sequence. Escape sequences are used to represent characters that are difficult to represent or type. For example, \t represents the Tab key, and \b represents the Backspace key (backspace key). Each escape sequence begins with a backslash character (\n).

What are data types? How to name variables?

C language can handle various types of data, such as integers, characters, and floating point numbers.

Variables can be named with lowercase letters, uppercase letters, numbers, and underscores. Also, the first character of the name must be a letter or an underscore, not a number. The name of the C language is case-sensitive, that is, the uppercase and lowercase of a letter are regarded as two different characters.

multiple functions

The printf() function is used in the above program, so how to add your own function to the program?

#include<stdio.h>
void butler(void);              /*ANSI/ISO C函数原型*/
int main(void)
{
    printf("I will summon the butler function.\n");
    butler();
    printf("Yes.Bring me some tea and writeable DVDs.\n");
    return 0;
}
void butler(void)                /*函数定义开始*/
{
    printf("You rang, sir?\n");
}

output: 

 In this program, the butler() function appears 3 times. The first time is the function prototype, telling the compiler to use the function in the program; the second time it appears in main() in the form of a function call; the last time it appears in the function definition, which is the source code of the function itself .

Keywords and Reserved Identifiers

Keywords are the vocabulary of the C language. They are special to C and cannot be used as identifiers (eg, variable names). Many keywords are used to specify different types, such as int. There are also keywords (eg, if) used to control the order in which statements in a program are executed. The following table is some keywords of C language.

If keywords are used inappropriately (for example, using a keyword as a variable name), the compiler will treat it as a syntax error. There are also some reserved identifiers, the C language has specified their use or reserves the right to use them, and if you use these identifiers to indicate other meanings, it will cause some problems. So although they are also valid names, do not cause syntax errors, and cannot be used casually. Reserved identifiers include those that begin with an underscore character and the names of standard library functions, such as printf().

Guess you like

Origin blog.csdn.net/weixin_51995147/article/details/128471142