C language programming-Chapter 1 program design and C language, C language programming Chapter 2
A program is a group of commands that computers can recognize and execute.
Each command enables the computer to execute specific operations. A specific command sequence is used to complete certain functions.
Computers are essentially computers of programs, and programs and commands are the most basic concepts in computer systems.
The binary code that can be directly recognized and accepted by a computer is called a machine instruction.Machine language(Machine language). In order to overcome the difficulties of machine language learning, hard to write, difficult to remember, difficult to check, difficult to modify, difficult to promote the use of shortcomings, people have createdSymbol Language(Symbolic language), which uses English letters and numbers to represent a command. symbolic language is also known as symbolic assembler language or assembeler language ).
Assembly languages are also calledLow-level language(Low level language ).Advanced Language(High level language ).
Computers cannot directly identify high-level language programs.Compile the programThe software converts a program (called the source program, source program) Written in advanced languages to a program (called the target program, object program) for machine instructions, and then allows the computer to execute the machine instruction program, the final result is displayed. A statement in advanced language usually corresponds to a golden machine command.
C: System Description Language. C ++: A large language that supports object-oriented programming. Visual Basic: a language that supports object-oriented programming. Java: A language suitable for the network.
C language features a variety of data types (such as characters, numbers, arrays, struct and pointers ).
C language is a widely used, powerful, and flexible procedural programming language. It can be used to compile application software and system software.
C language has the following main features:
(1) The language is concise, compact, easy to use, and flexible. C language has a total of 37 keywords and 9 control statements. The program is free to write and is mainly represented by lowercase letters, compressing all unnecessary components. in fact, C is a very small kernel language that only contains a very small number of hardware-related components, C language does not directly provide input and output statements, statements related to file operations, and statements for dynamic memory management (these operations are implemented by the library functions provided by the compilation system ), c's compilation system is quite concise.
(2) rich operators, including 34 types of operators. The C language treats parentheses, assignments, and forced type conversion as operators, so that the C language has rich operation types and diverse expression types.
(3) rich data types. the data types provided by C language include: integer, floating point, balanced, data type, pointer type, struct type, and shared body type. C 99 expands the complex floating point type, super tiny INTEGER (long) and Boolean (bool. in particular, pointer-type data is very flexible and diversified, and can be used for various complex data structures (such as linked lists, trees, stacks, and so on.
(4) Structured control statements (such as if... Else statement, while statement, do... While language sentence, switch statement and for statement). Use a function as the module unit of the program to facilitate program modularization. C language is a fully modular and structured language.
(5) The syntax is not very strict, and the degree of freedom in programming is large. for example, if you do not check whether the array subscript is out of bounds, the program writer ensures that the program is correct. variable types can be used flexibly. For example, integer data, complex data, and logical data can be used universally. generally, the syntax check of advanced languages is strict, and almost all syntax errors can be checked. The C language allows developers to have a great degree of freedom. Therefore, the syntax check is relaxed.
(6) the C language allows direct access to the physical address, supports bit operations, implements most of the functions of the Assembly Language, and can directly operate on the hardware. therefore, C language supports both advanced and low-level functions and can be used to compile system software. this dual nature of C makes it both a successful system description language and a general programming language.
(7) Good portability of programs written in C language. since C's compilation system is quite concise, it is easy to transplant it to a new system. C compilation system can directly compile most of the functions in the "standard link library" when running on the new system without modifying the source code, because the standard link library is written in portable C language. therefore, almost all computer systems can use C language.
(8) high quality of the generated target code and high efficiency of program execution.
Currently, C is mainly used to compile "embedded system programs ".
For example, 1.1, the following line of information must be output on the screen.
This is a C program.
Solution: Use the printf function to output the above text as is in the main function.
#include <stdio.h> // This is the compile preprocessing instruction
int main () // Define the main function
{// function start flag
printf ("This is a C program. \ n"); // Output the specified line of information
return 0; // Return function value 0 when function execution is completed
} // end of function flag
The "return 0;" clause of the program is used to return the integer 0 as the function value before the main function is executed.
C 99 specifies the main function as an int type (integer), which requires the function to return an integer. in the main function, set a "return 0;" statement at the end of the execution. When the main function ends normally, the resulting function value is 0, when an exception or error occurs during the execution of the main function, the function value is a non-0 integer. this function value is returned to the operating system that calls the main function. the programmer can use the Operation Command to check the return value of the main function to determine whether the main function has been properly executed and determine future operations accordingly. if the "return 0;" statement is not written in the program, some C compilation systems automatically add this statement to the target program. Therefore, when the main function ends normally, the function value can also be set to 0. to ensure program specification and portability, all programs you want to write will specify the main function as the int type, and add a "return 0;" statement at the end of the main function.
Every C language program must have a main function. the function body is enclosed by curly braces. in this example, printf is the output function in the function library provided by the C compiling system. \ n is a line break, that is, after the output, the cursor position on the display screen moves to the beginning of the next line. the cursor position is the current output position, that is, the next output character appears at this position. each statement ends with a semicolon, indicating that the statement ends.
When using the input and output functions in the function library, the compilation system requires the program to provide information about the function (for example, the Declaration of these input and output functions, macro definition, and global definition ). stdio. h is a file name provided by the system. stdio is the abbreviation of "standard input & output" and the file suffix. h indicates the header file, because these files are placed at the beginning of each file module of the program. information about the input and output functions has been stored in stdio. h file. now, use the # include command to call this information.
If you want to use the input and output functions in the standard function library, you should write the following line at the beginning of this file module:
# Include <stdio. h>
Each comment is replaced with a space when the program is pre-compiled. Therefore, the comments do not generate the target code during compilation, and the comments do not work for running.
(1) Comment on a single line starting with //. (2) Comment on a block starting with/* and ending.
// And/* in the string are not used as the start of the comment, but as part of the string.
Example 1.2 calculates the sum of two integers
Solution: set three variables. a and B are used to store two integers, sum is used to store the numbers, and the value-assignment operator "=" is used to send the sum result to sum.
#include<stdio.h>
int main()
{
int a,b,sum;
a=123;
b=456;
sum=a+b;
printf("sum is %d\n",sum);
return 0;
}
8th row output result. The printf function has two parameters in parentheses. One is the content of sum is % d \ n in the double marker, which is an output format string, the role is to output the characters and formats that the user wants to output. here, % d is the specified output format, and d represents output in the form of a "decimal integer. the sum parameter of the first parameter in parentheses indicates the sum value of the variable to be output. when executing the printf function, replace the value of the sum variable (expressed as a decimal integer) with % d in the double marker. the sum value is 579. Therefore, % d is replaced by a decimal integer of 579 in the output.
Because the program runs normally and ends, the return value of main function is 0.
In Example 1.3, the greater of the two integers is obtained.
#include <stdio.h>
#include <stdlib.h>
// Main function
int main () // Define the main function
{// The main function body starts
int max (int x, int y); // Declaration of the called function max
int a, b, c; // Define variables a, b, c
scanf ("% d,% d", & a, & b); // The values of the input variables a and b
c = max (a, b); // Call the max function and assign the obtained value to c
printf ("max =% d \ n", c); // output the value of c
return 0; // Return function value is 0
} // End of main function body
// Find the max function of the larger of two integers
int max (int x, int y) // define the max function, the function value is integer, and the formal parameters x and y are integer
{
int z; // max declaration part of the function, defines the variable z used in this function as an integer
if (x> y) z = x; // If x> y holds, assign the value of x to the variable z
else z = y; // Otherwise (i.e. y> x), assign the value of y to the variable z.
return (z); // Use the value of z as the value of the max function and return to the position where the max function was called
}
In the program, you need to declare the called function max. In the main function, you need to call the max function, while in the max definition, after the main function, the program compilation is executed from top to bottom, when max is called above, the compilation system cannot know what max is, so it cannot be processed as a function call. to enable the compilation system to recognize the max function, we need to use "int max (int x, int y);" to "declare" The max function before calling the max function, it is to tell the compiling system what max is and its related information.
Scanf contains two parts in the brackets. One is the content in the double marker, which specifies the format of the input data. "% d" indicates a decimal integer. second, the input data is prepared to be put there, that is, assigned to the variable.
In C language, "&" indicates the address, and "a indicates the address of variable a" and "B Indicates the address of variable B ". execute the scanf function, read two integers from the keyboard, send them to the address of the variables a and B, and then assign the two integers to the variables a and B respectively.
C = max (a, B); when calling max, use a and B as the actual parameters of the max function and pass them to the formal parameters x and y in the max function, respectively, then execute the function body of the max function, so that z in the max function gets a value, return (z) the function is to take the value of z as the value of the max function back to the location where the main function calls the max function, replace max (a, B), and then assign the value to the variable c.
Note: In this example, both functions have return statements. both functions are defined as integer and have function values. You must use the return statement as the function to specify the return value. however, the return value specified by the return Statement in the main function is generally 0, while the return value of the max function is the maximum value, only through the return statement can we take the obtained z value as the function value and return the location where it is called. the statement executed in the function does not automatically return the value to the call. You must use the reture statement to specify the half value as the function value. you can't help but write "return 0" at the end of all functions ".
1.4.2 structure of C language program
(1) A program consists of one or more source program files.
A source program file contains three parts: 1. specifies the preprocessing. for example, # include <stdio. h> command, that is, stdio. h. Read the content of the header file and place it in the # include command line to replace # include <stdio. h>. 2. global declaration. declare data outside the function. for example, place "int a, B, sum" in front of the main function as a global declaration. The variables declared outside the function are called global variables. if the variable is declared at the beginning of the Program (before the function is defined), it is valid within the entire source program file range. the variable declared in the function is a local variable, which is valid only within the function range. 3. function Definition. each function is used to implement certain functions. When calling these functions, the functions specified in the function definition are completed.
(2) functions are the main component of C Programs. almost all the work of a program is done by each function separately. A function is the basic unit of a C program, and each function is used to implement one or several specific functions. writing C programs is to write functions one by one.
A c program consists of one or more functions. It must contain one main function (and only one main function can be used ).
A program contains several source program files, and each source program file contains several functions. A source program file is a program module, which divides a program into several program modules.
During compilation, the source program file is used as the object.
A function called in a program can be a library function provided by the system (such as printf and scanf functions), or a function designed by the user based on your own. the number and functions of library functions provided by different compilation systems are not exactly the same.
(3) A function consists of two parts. 1. Function header. That is, the first line of the function, including the function name, function type, function attribute (formal parameter) Name, and parameter type.
# Include <stdio. h> # include <stdlib. h> int main () {int a, B, c, max; printf ("please input a, B, c: \ n"); scanf ("% d, % d, % d, ", & a, & B, & c); max = a; if (max <B) max = B; if (max <c) max = c; printf ("the largest number is % d \ n", max); return 0 ;}