"C Primer Plus notes" chapter II C language Overview

Source: Internet
Author: User

The second chapter mainly introduces C language by analyzing a simple example.

First, the details of the program content

1 #include <stdio.h>

This statement serves as the equivalent of typing the full contents of the file stdio.h in the file where the line is located. is actually a cut-and-paste operation.

An example of a #include语句是C预处理器指令 (preprocessor directive). The stdio.h file is provided as part of all C compilation packages and contains information about input and output functions for use by the compiler. This name represents the standard input and output header file.

Most importantly, the header file contains information that the compiler needs to use when establishing the final executable program, such as explaining the function name and how the function is used. Note that the actual code of the function is contained in a library file of precompiled code, not in the header file.

Header files are used to guide the compiler to properly combine programs.

Finally, #include并不是C语言语句. # indicates that this line is a statement processed by the C preprocessor.

1 int Main (void)

In general, the C program starts with the main () function. Here the parentheses () indicate that main () is a function.

It is not recommended to use

1 Main ()

Or

1 void Main ()

The first of these C99 standards is not allowed, and the second does not currently have any criteria to consider accepting it.

Stick with the standard form so that the program moves from one compiler to another without problems.

Ii. annotations, curly braces, program bodies and code blocks

The part that is generally contained between/**/is the program comment. Sometimes the//symbol is used, but the comment is limited to one line.

When programming, try to use both annotation methods to avoid a single annotation style.

In general, all C functions use curly braces to denote the start and end of a function. Only curly braces {} can play this role.

III. Declaration

1 int num;

1. Declare that there is a variable named num in the function;

2.int description num is an integer;

The compiler uses this information to assign the variable num a suitable storage space in memory.

In the C language, all variables must be defined before they are used. Declaring variables is considered to be a good programming technique.

C99 follows the C + + convention, allowing the declaration to be placed anywhere within the code block.

Name of the variable: the characters available are lowercase letters, uppercase letters, numbers, and underscores (_), and the first character must be a letter or an underscore.

It is important to note that the operating system and C libraries usually use one or two underscore names, so try to avoid this usage when programming. So, it is suggested that the first character is a letter.

The names of C languages are case-sensitive.

Benefits of declaring variables:

1. Make the program more readable by putting all the variables together and assigning meaningful names to the variables;

2. Declaring variables will have a preliminary plan for the architecture of the entire program;

3. Declaring variables can avoid the occurrence of incorrect spelling of variable names in the program;

4. If you do not declare a variable, you cannot compile the C program.

It is strongly recommended that you put all the variable declaration groupings together.

Four, return statement

1 return 0;

in int main (void), int indicates that the return value of the main () function should be an integer. The C standard requires main () to do this: the C language function with the return value uses a return statement, which needs to include the keyword return followed by the value to be returned followed by a semicolon.

After reading to Chapter 11, add the content here.

Five, the skills of readability

1. Select a meaningful variable name;

2. Use annotations;

3. Use a blank line to separate multiple parts of the face of a function;

4. Try each statement one line;

Six, multiple functions

Examples are as follows:

1 /*even a simple example, you have to knock it out .*/2 /*Program Listing 2.3*/3#include <stdio.h>4 voidButlervoid) 5 intMainvoid)6 {7printf"I'll summon the butler function.\n");8 Butler ();9printf"yes.bring me some tea.\n");Ten      return 0; One } A  - voidButlervoid) - { theprintf"You rang,sir?\n"); -}

Butler () appeared three times in the program.

The first time is in the prototype, which notifies the compiler of the function to be used. A prototype is a declarative way to tell the compiler that a C program is using a special function that indicates the properties of the function.

1 void Butler (void);

The first void indicates that the Butler () function does not return a value, and the second void indicates that the Butler () function has no arguments.

The second time occurs in the main () function as a function call.

The third time is at the end of the definition of the function, that is, the source code of the function itself;

Seven, commissioning

Syntax error: C's syntax error is to place the correct C symbol in the wrong place.

How do I detect a syntax error in my program?

1. Browse the source code of the program before compiling, looking for obvious error;

2. Review the errors found by the compiler.

Note: Errors can also occur with the compiler. A real syntax error at a location can cause the compiler to mistakenly think that it found another error, and the location where the error was found is one line behind the real error.

Semantic error: A mistake in meaning. Generally, the variable name is inconsistent with the variable that is actually intended to be represented.

Method of finding Semantic Errors 1: Step-by-step execution of the program and get the results compared to the actual desired results. Of course, this method is cumbersome, but it is very good to find errors in the program. Naturally, for programs that need to iterate 10,000 times ... Please do not try it easily, but you can choose to trace some of the code for analysis;

Method 2: Add print to the key point of the program to monitor the operation of the program.

It is important to learn how to use the debugger in the compiler.

Viii. keywords and reserved identifiers

    • Auto/break/case/const/continue/default/do/double/else/enum/extern/float/for/goto/if/inline/int
    • long/register/restrict/return/short/signed/sizeof/static/struct/switch/typedef/union/unsigned/void/volatifle/ While/_bool/_complex/_imaginary

Ix. Key Concepts

1. The compiler is responsible for the completion of the written program into machine language work;

2. Carefully write the code, not slack.

Another, review questions and programming exercises are OK, not in this table.

"C Primer Plus notes" chapter II C language Overview

Related Article

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.