C Language Basics Tutorial (my c tour started) [Two]_c language

Source: Internet
Author: User
Tags function definition function prototype reserved

3. Structure of the C program

The C program consists of more than one function and must have Main function. In addition, the C program generally has some preprocessing instructions. For example #include directives. Of course, it is not necessary to have #include instructions. Functions are composed of function headers and function bodies. The function header consists of a return value, a function name, and a list of arguments (which can be void) . The function body starts with {and ends with } . The function body can have a series of statements, each with a semicolon (; ) ended. For example:

Preprocessing Directives--〉 #include <stdio.h>

Function Header--〉int Main (void)
function Body Start--〉{
Variable declaration statement--〉int i;
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>/* not required. If you do not use the function declared in stdio.h, you do not need to * *

int main (void)
{
A series of statements

return 0;
}

4. The second C procedure

First, look at the code below and compare it to our first C program to see what new knowledge is added to the program.

/ * METER2CENTIMETERS.C--in centimeters to say two M /

#include <stdio.h>

int main (void)
{
int meters, centimeters;
meters = 2;

centimeters = meters; / * 100 times meters * *
printf ("%d-meter is equal to%d centimeters.\n", meters, centimeters);
printf ("Yes, I said%d centimeters!\n", * meters);

return 0;
}

Let's analyze this applet carefully.

1.Document
The comments on the head of this code indicate the role of the file name and the program, which we refer to asDocument。 Writing a document is a good habit to help us understand the role of the program. In programs made up of multiple source files, documents appear to be more important. Since what we've written so far is a very simple program, you may not find it useful to write a document, or even waste time. But we should get into the habit of writing documents, which is very helpful for us to write complex programs later. For complex programs, we should write the documents in more detail as much as possible. You can refer to the following format:
/***************************************************
* FileName: METER2CENTIMETERS.C
* Author: Antigloss athttp://cpp.ga-la.com
* Date of preparation: 05-9-9 0:00
* Last Modification Date: 05-9-9 0:00
* Function: two meters in centimeters
* algorithm: 1 m = 100 cm
**************************************************/

2. Multiple declarations
int meters, centimeters;
The statement above declares two variables. We can declare any number of variables in a declaration statement, as long as the variable names are separated by commas (,). For example, the following statement declares 4 variables.
int A, B, C, D;

3. multiplication operation
C,* is the multiplication operator, so centimeters = meters means: 100 times the value of the variable meters (2 in this case), and then assign the result of the multiplication to the variable centimeter S.

4. printf function
printf ("%d-meter is equal to%d centimeters.\n", meters, centimeters);
Placeholder %d appears two times above in this statement. The first%d is replaced by the value of the variable meters, while the second%d is replaced by the value of centimeters. Note: Each variable is separated by a comma (,), and the double quotation marks in "%d-meter is equal to%d centimeters.\n" are required and must be in English double quotes, not double quotes in Chinese.

printf ("Yes, I said%d centimeters!\n", * meters);
The above statement shows that the substitution of placeholders is not just a variable, but also an expression (meters is a multiplication expression).


5. How to write a function

First, look at the following code.

/ * MY_FUNC.C-This program uses a custom function.
#include <stdio.h>

void Butler (void); / * Iso/ansi C function prototype * *

int main (void)/ * Function Header /*
{/ * function Body Start * *
printf ("I'll summon the butler function.\n");
Butler (); / * Call 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 code above, the butler appears 3 times. The first is as a function prototype , the second is a function call statement , and the third is a function definition . The following three occurrences of Butler are described in detail below.

    1. function prototype is a way to declare a function , which is C89 The concept of standard addition, the old compiler may not support function prototypes. The function prototype is used to tell the compiler that we are going to use a particular function that indicates the properties of the function. For example, in a Butler function prototype, the first void indicates that the Butler function has no return value, and the second void indicates that Butler has no parameters, that is, no arguments are received. After the Butler function prototype is written in front of the main function, when the main function calls Butler, the compiler can detect that the Butler function call statement is correct. If there is no Butler function prototype in front of the main function, then we cannot call the Butler function in the main function unless we place the function definition before the main function. But putting the function definition before the main function is a bad programming style, because it will lead us to spend a lot of time looking for the location of the main function. Imagine if we wrote dozens of functions, and if each function definition was placed before the main function, the main function would be easy to find; but if we accidentally put some functions behind the main function, which means that the main function is placed in the middle of a bunch of functions, it's hard to look for! Before the introduction of the
    c89 Standard, the C language also supports the function declaration , but we can only specify the return value type of the function, and cannot list parameter list . For example:
            void Butler ();
Before C89, functions are declared as they are written above. C89 and C99 standards support this declarative approach, but they also point out that this kind of declarative approach will be eliminated sooner or later! So let's not use this declarative approach, but use a function prototype.

2. in the main function, the statement butler (); The function is to call the Butler function. Because the argument list for Butler is empty (void), there can be no arguments in the parentheses following the function name (Butler), nor can it be butler (void);.

3. The Butler function is defined in the same way as the main function, and is composed of the function head and function body . The function header and the function prototype are almost identical, except that a semicolon is missing. Note that the function definition is just a definition, and it does not determine when the function will execute or whether the function is executed. When the main function calls the Butler function, the Butler function is executed, and if the main function does not call the Butler function, then the Butler function is not executed.

All C programs are executed from the main function. Whether the main function is anywhere in the source file, in the middle or at the end, the C program must be executed from the main function. Writing the main function at the beginning of the source file is a good programming practice because it allows the reader to quickly figure out the structure of the program.

So far, we've got an overview of the functions. In the following tutorials, we will learn more about functions in more detail and in a more systematic way.

6. Keywords and reserved identifiers

keywords are reserved words and cannot be used to make identifiers (such as variable names), for example:int double; is wrong because double is a keyword and cannot be a variable name. Using the keyword to do variable name is a syntax error , can not be compiled! The following table lists all keywords in the C language, where the red is the New keyword for 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 that the C language has already used, and an identifier reserved by the C language, is called a reserved identifier . A reserved identifier includes an identifier (such as __line__) that starts with an underscore (_) and a function name (such as printf) that is defined in the standard library .
We should not use a reserved identifier to do the identifiers of our custom variables or functions. Identifiers that use reserved identifiers for custom variables or functions are not grammatical errors and can be compiled, because the reserved identifiers are valid identifiers that conform to the rules of identifiers . However, because these identifiers are already used or reserved by the C language, using a reserved identifier to do identifiers such as custom variables or functions can 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.