Basic C language tutorial (my C journey started) [2]

Source: Internet
Author: User

3. C program structure

A c program consists of more than one function and must have a main function. In addition, C Programs generally have some preprocessing commands. For example, the # include command. Of course, the # include command is not required. A function consists of a function header and a function body. The function header consists of the return value, function name, and parameter list (which can be void. The function body starts from {and ends. There can be a series of statements in the function body. Each statement ends with a semicolon. For example:

Preprocessing command --> # include <stdio. h>

Function header --> int main (void)
Function body start --> --〉{
Variable declaration statement --> int I;
Value assignment statement --> I = 1;

Function call statement --> printf ("% d \ n", I );

Return Statement --> return 0;
Function body end --> --〉}

In short, the structure of a simple standard C program is as follows:

# Include <stdio. h>/* is optional. If the function declared in stdio. h is not used */

Int main (void)
{
A series of statements

Return 0;
}

4. The second C program

First, take a look at the following code and compare it with our first C program to see what new knowledge points this program has added.

/* Meter2centimeters. c -- two meters in centimeters */

# Include<Stdio. h>

Int main (void)
{
Int meters, centimeters;
Meters = 2;

Centimeters = 100 * meters;/* 100 multiplied by meters */
Printf ("% d-meter is equal to % d centimeters. \ n", meters, centimeters );
Printf ("Yes, I said % d centimeters! \ N ", 100 * meters );

Return 0;
}

Next we will carefully analyze this small program.

1.Document
The comments in the Code header indicate the file name and the role of the program.Document. Writing documents is a good habit and can help us understand the role of programs. In a program composed of multiple source files, the document is more important. Since we have all written very simple programs so far, you may not think of the function of writing documents, or even think it is a waste of time. But we should develop the habit of writing documents, which will be very helpful for us to write complicated programs in the future. For complex programs, we should write the documents as detailed as possible. You can refer to the following format:
/*************************************** ************
* File name: meter2centimeters. c
* Author: Antigloss at http://cpp.ga-la.com
* Written on: 05-9-9
* Last modification Date: 05-9-9
* Function: two meters in centimeters
* Algorithm: 1 meter = 100 cm
**************************************** **********/

2.Multiple declarations
Int meters, centimeters;
The above statement declares two variables. We can declare any number of variables in a declaration statement, as longComma. For example, the following statement declares four variables.
Int a, B, c, d;

3.Multiplication
In C, * is a multiplication operator, so centimeters = 100 * meters; Means to multiply the value of the variable meters by 100 (2 in this example ), then, assign the result of multiplication to the variable centimeters.

4.Printf function
Printf ("% d-meter is equal to % d centimeters. \ n", meters, centimeters );
Placeholders in the preceding statement% DIt appears twice. The first % d is replaced by the meters value, while the second % d is replaced by the centimeters value. Note: Each variable is separated by a comma (,); "% d-meter is equal to % d centimeters. the double quotation marks in \ n are mandatory and must be English double quotation marks. They cannot be Chinese Double quotation marks.

Printf ("Yes, I said % d centimeters! \ N ", 100 * meters );
The preceding statement indicates that not only variables but alsoExpression(100 * meters is a multiplication expression ).


5. How to compile Functions

First, check the following code.

/* My_func.c -- this program uses a UDF */
# Include<Stdio. h>

Void butler (void);/* ISO/ansi c function prototype */

Int main (void)/* function header */
{/* Function body start */
Printf ("I will summon the butler function. \ n ");
Butler ();/* call the butler function */
Printf ("Yes. Bring me some tea and writeable CD-ROMS. \ n ");

Return 0;
}/* Function body end */

Void butler (void)/* butler function */
{
Printf ("You rang, sir? \ N ");
}

In the above Code, butler appears three times. For the first timeFunction prototype; The second isFunction call statement; The third time isFunction Definition. The following describes the three appearance of butler in detail.

1.The function prototype isDeclare a functionIs a concept added to the C89 standard. The old compiler may not support function prototypes. The function prototype is used to tell the compiler that we will use a specific function, which specifies the function attribute. For example, in the butler function prototype, the first void indicates that the butler function has no return value. The second void indicates that the butler has no parameters, that is, no parameters are received. After writing the butler function prototype in front of the main function, when the main function calls butler, the compiler can detect whether the butler function call statement is correct. If the main function does not have a butler function prototype before it, we cannot call the butler function in the main function unless we put the function definition before the main function. However, putting the function definition before the main function is a bad programming style, because it will take us a lot of time to find the location of the main function. Imagine that if we have written dozens of functions, if each function definition is placed before the main function, the main function is still easy to find; however, if we accidentally put some functions behind the main function, that is, the main function is placed in the middle of a bunch of functions, it will be hard to find!
C language supported before the C89 standard was introducedFunction DeclarationHowever, we can only specifyReturn Value TypeBut cannot listParameter List. For example:
Void butler ();
Before C89, the function was declared as described above. Both the C89 and C99 standards support this declaration method, but they also point out that this declaration method will sooner or later beElimination! Therefore, instead of using this declaration method, we should use a function prototype.

2.In the main function, the function of the statement butler (); is to call the butler function. Because the butler parameter list is empty (void), no parameters or butler (void) must be included in the brackets after the function name (butler );.

3.The butler function is defined in the same way as the main function.Function HeaderAndFunction body. The function header is almost identical to the function prototype, but a semicolon is missing. Note that a function definition is just a definition. It does not determine when a function is executed or whether the function is executed. When the main function calls the butler function, and when the butler function is executed; if the main function does not call the butler function, the butler function will not be executed.

All C Programs are executed from the main function. Whether the main function is in any position, in the middle, or at the end of the source file, in short, the C program must start to execute the main function. Writing the main function at the beginning of the source file is a good programming habit, because it allows the reader to quickly figure out the program structure.

So far, we have a brief understanding of the function. In the subsequent tutorials, we will learn functions more systematically.

6. keywords and reserved identifiers

KeywordsIt is a reserved word and cannot be usedIdentifier(Such as the variable name), such as: int double; is incorrect because double is a keyword and cannot be a variable name. Using keywords for variable names is a typeSyntax Error, Cannot be compiled! The following table lists all the keywords in the C language. The red keywords are newly added to the ISO C99 standard.

Auto enumRestrictUnsigned
Break extern return void
Case float short volatile
Char for signed while
Const goto sizeof_ Bool
Continue if static_ Complex
DefaultInlineStruct_ Imaginary
Do int switch
Double long typedef
Else register union

The identifier used by the C language and the identifier reserved by the C LanguageReserved identifier. The retained identifier includes the following dashes (_) Start identifier (such_ LINE __) AndStandard LibraryFunction name defined inPrintf).
We should not use reserved identifiers for custom variables or functions. It is not a syntax error to use reserved identifiers for custom variables or functions. You can compile them because the reserved identifiers are legal identifiers that matchIdentifier naming rules. However, because these identifiers have been used or retained by the C language, using the retained identifiers for User-Defined variables or functions may cause unexpected problems.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.