C Language Programming-1th chapter programming and C language

Source: Internet
Author: User

A program is a set of instructions that a computer can recognize and execute.

Each instruction enables the computer to perform a specific operation. A specific sequence of instructions used to accomplish certain functions.

The essence of the computer is the machine of the program, the procedure and the instruction are the most basic concept in the computer system.

The binary code that can be directly recognized and accepted by a computer is called machine instruction, and the set of machine instructions is the machine language of the computer (machines language). In order to overcome the difficulties of hard-to-remember machine language difficult to write difficult to read difficult to modify the shortcomings of the use of difficult to promote, people created a symbolic language (symbolic language), it uses some English letters and numbers to denote an instruction. Symbolic language is also known as symbolic assembly (symbolic Assembler language) or assembly language (Assembeler language).

Assembly language, also known as low-level language (language), is now used in high-level languages (language).

The computer does not directly recognize high-level language programs, it is necessary to use a software called compiler to translate programs written in high-level languages (called source programs, sources program) into machine instructions (called Target programs, object program), Then let the computer execute the machine instruction program, and finally get the result. A statement in a high-level language often corresponds to a gold bar machine directive.

C: System Description Language, C + +: Large language support for object-oriented programming, Visual Basic: Support for object-oriented programming language, Java: language suitable for the web

C language is characterized by a variety of data types (such as characters, values, arrays, structures and pointers, etc.).

The C language is a versatile, powerful, flexible, procedural (procedural) programming language that can be used both to write applications and to write system software.

C language has some of the following key features:

(1) The language is simple, compact, easy to use and flexible. C language A total of 37 keywords, 9 kinds of control statements, the program is written in a free form, mainly in lowercase letters, compressed all the unnecessary ingredients. In fact, C is a very small kernel language, including very few hardware-related components, C does not directly provide input and output statements and statements about file operations and dynamic memory management statements, etc. (these operations are implemented by the library functions provided by the compilation system), and C's compilation system is quite concise.

(2) the operator is rich. Including 34 kinds of operators, C language, the parentheses, assignment and coercion type conversion are treated as operators, so that the C language is extremely rich in the operation type, expression type diversification.

(3) rich data types. The types of data available in C include: shaping, floating point, character, data type, pointer type, struct type, and common body type, and C 99 also expands complex floating-point types, super-tiny integer (Long Long), and Boolean (bool). Especially pointer type data, The use of very flexible and diverse, can be used to achieve a variety of complex data structures (such as linked lists, trees, stacks, etc.) operations.

(4) A structured control statement (such as a if...else statement, a while statement, a do...while language sentence, a switch statement, and a for statement). The function is used as the module unit of the program, which facilitates the modularization of the program. C language is a fully modular and structured language.

(5) Grammar restrictions are not very strict, the program design freedom is large. For example, an array subscript is not checked, and the program writer guarantees that the program is correct. The use of variable types is flexible, such as shaping and character data as well as logical data. General high-level language grammar check is relatively strict, Can check for almost all grammatical errors, while the C language allows writers to have greater degrees of freedom, thus easing the grammar check.

(6) C language allows direct access to the physical address, can perform bit operation, can realize most of the functions of assembly language, can directly operate on the hardware. Therefore, C language has the function of high-level language, and has many functions of low-level language, which could be used to write system software. This duality of C language makes it both a successful system description language and a universal programming language.

(7) program written in C language portability is good. Because C's compilation system is fairly concise, it is easy to migrate to a new system. and the C compiler system runs on the new system, you can directly compile the "Standard link library" Most of the features, do not need to modify the source code, Because the standard link library is written in the portable C language. Therefore, the C language can be used in almost all computer systems.

(8) High quality of generated target code and high program execution efficiency.

One of the main uses of C at present is to write "Embedded system programs".

Example 1.1 requires that the following line of information be printed on the screen.

This was a C program.

Problem-solving idea: Output the above text in the main function with the printf function intact.

 #include <stdio.h> //  this is Compile preprocessing directives  int  Main () / /  define main function  {//< /span> function start flag  printf ( " this is a C program.\n  " ); //     Outputs the specified line of information  return                             0 ; //                                         Returns the function value 0 } When the function is finished executing //  end of function flag  

The 5th line of the procedure "return 0;" The function is to return the integer 0 as the function value before the end of the main function, returning to the calling function.

C 99 recommends that the main function be specified as int (integer), which requires the function to bring back an integer value. In the main function, set a "return 0" at the end of the execution; Statement, when the main function ends normally, the value of the function is 0, and when an exception or error occurs during the execution of the main function, the function value is an integer that is not 0. This function value is returned to the operating system that called the main function. The programmer can check the return value of the main function with the action instruction. This determines whether the main function has been executed properly, and then decides what to do later. If you do not write "return 0;" In the program statement, some C-compiling system will automatically add this statement in the target program, so, the main function at the end of normal, you can also make the function value of 0. In order to make the program specification and portability, the program that wants to write will all designate the main function as int, and add a "return 0" at the end of the main function. Statement.

Each C language program must have a main function. The function body is enclosed in curly braces {}. In this case, printf is the output function from the C compiler's library of functions. \ n is a newline character, that is, after the output, the cursor position on the display moves to the beginning of the next line. This cursor position is called the current position of the output That is, the next output character appears at this position. Each statement ends with a semicolon that indicates the end of the statement.

When using the input and output functions in the library, the compilation system asks the program to provide information about the function (such as the declaration of these input and output functions and the definition of macros, the definition of global quantities, etc.). Stdio.h is a file name provided by the system, Stdio is "standard input & Output "abbreviation, file suffix. h means header file, because these files are placed at the beginning of each file module of the program. The information about the input and output functions has been placed in the stdio.h file. Now, use the # include directive to transfer this information into the .

In the program 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 precompiled, so the comment section does not produce the target code at compile time, and the annotation does not work for the run.

(1) A single-line comment with//start. (2) Start with/* and end with a block comment.

The//and/* in the string are not the beginning of the comment, but rather as part of the string.

Example 1.2 The sum of two integers

Problem solving: Set 3 variables, A and B are used to hold two integers, sum is used for storing and counting. The result of the addition is passed to sum with the assignment operator "=".

#include <stdio.h>int  main () {    int  a,b,sum;    A=123;    b=456;    Sum=a+b;    printf ("sum is%d\n", sum);     return 0 ;}

Line 8th output, this printf function parentheses have two parameters, one is the contents of the double apostrophe sum is%d\n, it is the output format string, the function is to output the character and output the user wants to output format. Where%d is the specified output format, d means "decimal integer" Form output. The 2nd parameter sum in parentheses, which represents the value to output the variable sum. When you execute the printf function, the value of the sum variable (expressed as a decimal integer) is substituted for%d in the double-apostrophe. The value of sum is now 579, so the decimal integer 579 replaces%d at the time of output.

The return value of the main function is 0 due to the normal operation and end of this program.

Example 1.3 asks for the larger of two integers.

#include <stdio.h>#include<stdlib.h>//Main functionintMain ()//defining the main function{//The main function body begins    intMaxintXinty);//declaration of the called function Max    intA,b,c;//Defining Variables A,b,cscanf"%d,%d", &a,&b);//Enter values for variables A and BC=max (A, b);//Call the Max function and assign the resulting value to Cprintf"max=%d\n", c);//value of output C    return 0;//returns a function value of 0}//end of main function body//Max function for the larger of two integersintMaxintXintY//define the Max function, the function value is shaping, formal parameters x and y are shaping{    intZ//in the Declarations section of the Max function, define the variable z used in this function as shaping    if(x>y) z=x;//If X>y is established, assign the value of x to the variable z    ElseZ=y;//Otherwise (that is, y>x), assign the value of y to the variable Z.    return(z);//return the value of Z as the Max function value to the location where the Max function is called}

The program needs to declare the called function Max, in the main function to call the Max function, and Max is defined in the main function, the program is compiled from top to bottom execution, when the above call Max, the compilation system does not know what Max is, Therefore, it cannot be processed as a function call. In order for the compilation system to recognize the MAX function, use "int max" (int x,int y) before calling the Max function. The "declaration" of the Max function, the so-called declaration, is to tell the compiler what the system Max is, and about its information.

scanf the parentheses are followed by two parts: one is the contents of the double apostrophe, which specifies the format in which the input data is entered. %d "means a decimal integer. Second, the input data is ready to be put there, that is, to assign that variable.

In the C language, "&" is the address character, &a means "the address of variable a", &b is "the address of variable B". Execute the SCANF function, read two integers from the keyboard, send to the address of the variable A and B, and assign the two integers to variables a and B, respectively.

C=max (A, A, b); When you call Max, the A and the actual arguments for the Max function are passed to the form parameter x and y in the Max function, and then the function body of the MAX function is executed, and the z in the Max function is given a value, return (z) 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, instead of Max (A, A, b), and then assign the value to the variable C.

Note: Both functions in this example have a return statement. Two functions are defined as integers, all have function values, and you need to use the return statement as a function to specify the return value. However, the return statement in the main function specifies a value of 0, and the return value of the Max function is the maximum value that is obtained. Only the return statement can be used as the value of the function and return to the location where it was called. The statement executed in the function does not automatically return the value to the call, and the Reture statement must be used to specify the half value as the function value. You can also write "return 0" at the end of all functions without adding an analysis. .

The structure of 1.4.2 C language Program

(1) A program consists of one or more source program files.

In a source program file, you can include 3 parts: 1. Preprocessing is specified. For example, the #include<stdio.h> instruction is to read the contents of the Stdio.h header file and place it in the # include command line instead of the #include<stdio.h . 2. Global declaration. That is, the data declaration is outside the function. If you put "int a,b,sum" in front of the main function as a global declaration, a variable declared outside the function is called a global variable. If it is a variable declared at the beginning of the program (before the function is defined), Valid within the scope of the entire source program file. The variable declared in the function is a local variable and is valid only within the scope of the function. 3. function definition. Each function is used to implement certain functions, and when these functions are called, the function specified in the function definition is completed.

(2) The function is the main part of the C program. Almost all the work of the program is done by each function, and the function is the basic unit of the C program, each of which is used to implement one or several specific functions. Writing a C program is a function of writing individual functions.

A C language program consists of one or more functions, which must contain a main function (and only one main function).

A program contains several source program files, and each source program file contains several functions. A source program file is a program module, a program is divided into a number of program modules.

At compile time, the source program file is the object.

Functions that are called in a program can be system-provided library functions (such as printf and scanf functions) or functions that the user designs according to their own design. The number and function of library functions provided by different compiling systems are not identical.

(3) A function consists of two parts. 1. First of the functions. That is, the first line of the function, including the function name, function type, function attribute (formal parameter) name, parameter type.

A function name must be followed by a pair of parentheses, the parameter name of the function is written in parentheses, and its type. If the function has no arguments, you can write void in parentheses, or it can be an empty parenthesis.

such as int main (void) or int main ()

2. Function body. That is, the part of the curly brace below the first part of the function. If you include a multi-level curly brace in one of the functions, the outermost pair of curly braces is the range of the function body.

The function body generally consists of the following two parts:

The Declarations section includes: Defines the variables used in this function and declares the functions called by this function.

Execution part: consists of a number of statements that specify the operations that are performed in the function.

(4) The program always executes from the main function, regardless of where the main function is located throughout the program.

(5) The operation of the computer in the program is done by the C statement in the function. If assigned, the operation of input and output data is implemented by the corresponding C statement.

(6) There must be a semicolon at the end of each data declaration and statement.

(7) The C language itself does not provide input and output statements. The input and output operations are performed by functions such as library functions scanf and printf. c the input and output is "functional". Because the input and output operation involves the specific computer equipment, the input and output operation with the library function, you can make the C language itself small size, The compiler is simple, easy to implement on a variety of machines, the program is portable.

(8) The program should contain comments.

1.5 Steps and methods to run a C program

The computer does not directly recognize and execute instructions written in high-level languages, it must compile the program (also known as the compiler) to translate the C source program into a binary form of the target program, and then connect the target program with the system's function library and other target programs to form an executable target program.

(1) on-machine input and edit source program.

(2) compiling in the source program, first using the "preprocessor" provided by the C compiler system to pre-process the pre-processing instructions in the program.

The role of compilation first is to check the source program, to determine whether it has a syntax error, if any, issue an "error message", modify and recompile, until there is no syntax error, the source program is soft to the binary form of the target program (in C + + suffix. obj).

(3) connection processing. The compilation is based on the source program files, and a compilation can only get the target file (also known as the target module) corresponding to a source program file, which is only part of the whole program.

All of the compiled target modules must be connected together and connected to the library as a whole to generate a target program that can be executed by the computer, known as the executable (Executive program). In C + +, the suffix is. exe.

(4) Run the executable program and get the running result.

, where the solid line represents the operation flow, and the dashed lines represent the input and output of the file.

For example, after editing to get a source program file F.C, and then at the time of compiling the source program file F.C input, compiled to get the target program file F.obj, and then all the target module input computer, and the system provides library functions, etc. to connect to the executable target program F.exe, Finally, input the f.exe into the computer and make it run, and get the result.

1.6 Tasks of the programming

From identifying problems to finalizing tasks, you typically experience the following stages of work:

1) problem analysis. 2) design algorithm. 3) write the program. 4) Edit, compile, and connect the source program. 5) Run the program, analyze the results. 6) write the program documentation.


1. Write a C program, enter A,b,c three values, and output the largest of them.

#include <stdio.h>#include<stdlib.h>intMain () {intA,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;}

C Language Programming-1th chapter programming and C language

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.