C Language Study Note 2

Source: Internet
Author: User
Tags microsoft c

Before writing notes, I saw some content in advanced C language books. There are many textbooks for C language learning. I don't necessarily like this, or what I record every day may not be in line with everyone's wish, but it is just a record. You can also take a look.

Features of C Language

C is a structured language. It has a clear hierarchy, which facilitates the organization of programs in a modular manner and facilitates debugging and maintenance. The C language has strong performance and processing capabilities. It not only has rich operators and data types, but also facilitates various complex data structures. It can also directly access the physical address of the memory for bit-level operations. Since C language implements hardware programming, C language integrates the functions of advanced and low-level languages. It can be used for system software development and application software development. In addition, C language features high efficiency and portability. Therefore, it is widely transplanted to various types of computers, thus forming multiple versions of C language.

C language version

Currently, the most popular C languages are as follows:
Microsoft C, or MS C
Borland Turbo C, or Turbo C
· AT & T C
These C language versions not only implement the ansi c standard, but also expand them to make them more convenient and perfect.

Object-Oriented Programming Language

On the basis of C, C ++ was launched by Bjarne Strou-strup of Bell Laboratory in 1983. C ++ further expands and improves the C language and becomes an object-oriented programming language. The latest popular C ++ versions include Borland C ++ 4.5, Symantec C ++ 6.1, and Microsoft VisualC ++ 2.0. C ++ puts forward some more in-depth concepts. The object-oriented concepts supported by C ++ can easily map the problem space directly to the program space, it provides programmers with a different way of thinking and programming than traditional structural programming. This increases the complexity of the entire language and makes it difficult to grasp it.

C and C ++

However, C is the basis of C ++, and C ++ and C are compatible in many aspects. Therefore, if you have mastered the C language, and then learn C ++, you can use a familiar syntax to learn the object-oriented language, so as to get twice the result with half the effort.

What software is used to learn C language?

Some people often use Turboc 2.0.

This software is an application software in the DOS operating system, but it is very small, less than 1 M, it runs very conveniently and quickly, but because it is not a windows application, graphical operations are not available, and Chinese characters are not supported.

In addition, VC 6.0 is an application in windows, which is easy to use. However, I feel that there are too many functions, not only for the C language, but also for the large size and slow operation.

Another is the C language learning and compilation system, which is a specialized software for learning. It is no problem to download and install the software and use it for learning and practice. Most functions are available without registration.

C source program structure features

To illustrate the features of the C language source program structure, first look at the following programs. These steps are simplified to difficult, and show the characteristics of the C language source program in the composition structure. Although the relevant content has not yet been introduced, you can learn from these examples the basic parts of a C source program and the writing format.
Main ()
{
Printf ("www. www.www.www.vcok.com, hello! \ N ");
}
Main is the name of the main function, indicating that it is a main function. Each C source program must have only one main function ). Function call statement. The printf function sends the output content to the monitor for display. The printf function is a standard function defined by the system and can be directly called in a program.

# Include "Stdio. h"
# Include "Math. h"
Main ()
{
Double x, s;
Printf ("input number: \ n ");
Scanf ("% lf", & x );
S = sin (x );
Printf ("sine of % lf is % lf \ n", x, s );
}

Comment on each line

Include is a file that contains the command extension. h, also known as a header file or a header file.
Define two real variables to be used by subsequent programs
Display prompt information
Obtain a real number x from the keyboard
Returns the sine of x and assigns it to the variable s.
Show Program operation results
The main function ends.

The function of the program is to input a number x from the keyboard, calculate the sine of x, and then output the result. The two lines before main () are called preprocessing commands (see below ). There are several other types of pre-processing commands. The include command is called the file inclusion command, which means to include the specified file in the angle brackets "" or quotation marks <> to this program, become part of this program. The contained file is generally provided by the system, and its extension is. h. Therefore, it is also called a header file or a header file. The header file of C language includes the function prototype of each standard library function. Therefore, when calling a library function in a program, it must contain the header file of the function prototype. In this example, three library functions are used: Input Function scanf, sine function sin, and output function printf. Sin is a mathematical function. Its header file is math. h. Therefore, before the main function of the program, use the include command to include math. h. Scanf and printf are standard input and output functions. The header file is stdio. h. Before the main function, the include command also contains the stdio. h file.

It should be noted that the C language requires that the scanf and printf functions can be omitted to include commands for their header files. Therefore, in this example, you can also delete the include command # include of the second line. . Similarly, the printf function is used in Example 1.1, and the include command is also omitted.

In the example, the main function body is divided into two parts: the description part and the execution part. Description refers to the type description of the variable. No variables are used in this example. C language requires that all variables used in the source program must be described first and then used. Otherwise, errors may occur. This is a feature of the compiled high-level programming language, which is different from the interpreted BASIC language. Description is an important part of the C source program structure. In this example, two variables x and s are used to represent the input independent variables and sin function values. Because the sin function requires that the two quantities must be of double Precision Floating Point type, the type specifier double is used to describe these two variables. The execution part of the last four actions is called the execution statement part to complete the functions of the program. The first line of the execution part is the output statement. Call the printf function to output the prompt string on the display. Ask the operator to enter the value of the independent variable x. The second input statement calls the scanf function, receives the input number on the keyboard, and stores it in variable x. The third line is to call the sin function and send the function value to the variable s. The fourth line is to use the printf function to output the value of the variable s, that is, the sine value of x. The program ends.

Printf ("input number: \ n ");
Scanf ("% lf", 'c10f10 & x );
S = sin (x );
Printf ("sine of % lf is % lf \ n", 'c10f10x, s );
When running this program, the system first displays the input number on the display screen, which is completed in the first line of the execution part. At the prompt, enter a number on the keyboard, for example, 5, press enter, and then the calculation result is displayed on the screen.

Input and Output Functions

The input and output functions scanf and printf are used in the first two examples. In chapter 3, we will introduce them in detail. Here we will briefly introduce their formats for use below. Scanf and printf are called format input functions and format output functions respectively. It indicates that the input and output values are in the specified format. Therefore, the parameter tables of these two functions are composed of the following two parts: "format control string". The parameter table format control string is a string and must be enclosed in double quotation marks, it indicates the data type of input output. For more information about the formats and representations, see Chapter 3. In the printf function, you can also display non-format control characters in the format control string. Then, the original text is printed on the display screen. The parameter table lists the input and output quantities. Use commas (,) to separate multiple values. For example:
Printf ("sine of % lf is % lf \ n", x, s );
Here, % lf is a formatted character, which indicates processing by double-precision floating point number. It appears twice in the format string, corresponding to the two variables x and s. Other non-formatted characters are output on the screen as they are
Int max (int a, int B );
Main ()
{
Int x, y, z;
Printf ("input two numbers: \ n ");
Scanf ("% d", & x, & y );
Z = max (x, y );
Printf ("maxmum = % d", z );
}
Int max (int a, int B)
{
If (a> B) return;
Else return B;
}
This function is used to input two integers and output a large number.
/* Function Description */
/* Main function */
/* Variable Description */
/* Enter the x and y values */
/* Call the max function */
/* Output */
/* Define the max function */
/* Return the result to the main function */
In the above example, the function of the program is to input two integers by the user. After the program is executed, a large number is output. This program consists of two functions: the main function and the max function. There is a parallel relationship between functions. Other functions can be called from the main function. The function of the max function is to compare two numbers, and then return a large number to the main function. The max function is a user-defined function. Therefore, the description (the third line of the program) should be given in the main function ). It can be seen that in the description section of the program, there can be not only a description of variables, but also a description of functions. The function details are described in Chapter 5. After each line of the program, the content enclosed by/* and */is the comments part, and the program does not execute the comments part.

In the above example, the execution process of the program is: first, the prompt string is displayed on the screen. Please enter two numbers. After the carriage return, the scanf function statement receives the two numbers and sends them to the variables x and y, then call the max function and transmit the values of x and y to the parameters a and B of the max function. Compare the size of a and B in the max function, return the major variable z to the main function, and output the value of z on the screen.

C source program structure features

1. a c language source program can be composed of one or more source files.

2. Each source file can be composed of one or more functions.

3. No matter how many files a source program is composed of, there is one and only one main function, that is, the main function.

4. The source program may contain preprocessing commands (the include command is only one of them). Preprocessing commands should usually be placed at the beginning of the source file or source program.

5. Each description and statement must end with a semicolon. However, the pre-processing command, function header and curly braces "}" cannot be followed by a plus sign.

6. identifier. At least one space must be added between keywords to indicate the interval. If there is an obvious delimiter, there is no space to separate it.

Rules to be followed when writing a program

From the perspective of clear writing, easy reading, understanding, and maintenance, you should follow the following rules when writing a program:

1. One description or one statement occupies one row.

2. The section enclosed by {} usually represents a certain level of the program. {} Is generally aligned with the first letter of the structure statement and occupies a single row.

3. a lower-level statement or description can be written after being indented to a higher-level statement or description. This makes the program clearer and more readable. In programming, we should strive to follow these rules to develop a good programming style.

C Character Set

Character is the most basic element of a language. The C character set consists of letters, numbers, spaces, punctuation marks, and special characters. You can also use Chinese characters or other graphical symbols in character constants, string constants, and comments.
1. lowercase letters ~ A total of 26 z, uppercase letters ~ Z: 26 in total

2. numbers 0 ~ 9 A total of 10

3. Blank characters, space characters, tabs, line breaks, and so on are collectively referred to as blank characters. A blank character only plays a role in character constants and string constants. When it appears elsewhere, it only serves as an interval, and the Compilation Program ignores them. Therefore, the use of blank characters in the program does not affect the compilation of the program, but the use of blank characters in appropriate places in the program will increase the program's clarity and readability.

4. punctuation and special characters

I will learn about it today. I can go to this C language getting started tutorial every day and continue to learn the following content.

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.