The C Programming Language - Introduction

The task of this chapter is to introduce the basic elements of the C language through actual programs. As for the specific details, rules and some exceptions, we will not discuss them here for the time being.

This chapter is the framework for what is explained in detail in subsequent chapters


1. Getting Started

    The program needs to go through several stages of compilation, linking, loading, and running before it can actually run.

    A C program, no matter its size, consists of functions and variables. Functions contain statements that specify the computation to be performed; variables are used to store values ​​used during computation.

    Normally, there are no restrictions on how functions can be named, but main is a special function name - every program executes from the beginning of the main function, which means that every program must contain a main function somewhere.

    One way to exchange data between functions is for the calling function to provide a list of values ​​(called parameters) to the called function. A pair of parentheses following the function name encloses the parameter list.

    Statements within a function are enclosed in a pair of curly braces {}.

    When you call a function, you just use the function name followed by the parameter list enclosed in parentheses.

    A sequence of characters enclosed in double quotes is called a string or string constant.

    In the parameters of the printf function, only \n can be used to represent a newline, and the newline of the program text cannot be used instead of \n

    The printf function never wraps, so we can call the function multiple times to get a long output line in stages

    Escape character sequences provide a general and extensible mechanism for representing untyped or invisible characters.

   

2. Variables and Arithmetic Expressions

    Comments can be used wherever spaces, tabs, or newlines are allowed in the program

    In C language, all variables must be declared before use. Declarations are usually placed at the beginning of a function, before any executable statements. A declaration is used to describe a property of a variable, which consists of a type name and a list of variables.

    The size of primitive data type objects depends on the specific machine, but there is a general size

    There are also arrays, structures, and unions of primitive data types, pointers to these types, and functions that return values ​​of these types

    An assignment statement is an executable statement

    The loop body of a while statement can be one or more statements enclosed in curly braces, or a single statement without curly braces

    Although C compilers don't care about the appearance of a program, proper indentation and a programming style that preserves proper whitespace are very important to the legibility of a program. It is recommended to write only one statement per line, with a space character on either side of the operator. The style of curly braces, choose any one, stick to it

    Integer division in C will perform truncation, and any fractional parts in the result will be discarded

    Each % in the first parameter of the printf function corresponds to the second, third... parameters, and they must match in number and type, otherwise an incorrect result will occur

    Integer Arithmetic and Floating Point Arithmetic

    An integer operation is performed if all operands of an arithmetic operator are integers. However, if an arithmetic operator has a floating-point operand and an integer operand, the integer operand will be converted to floating-point before starting the operation

    Arithmetic operations, assignment operations, and relational operations all perform type promotion before the operation.

    Format specifiers can omit width and precision (both can be omitted)

    The printf function also supports the following format specifications: %o for octal; %x for hexadecimal; %c for character; %s for string; %% for the percent sign itself


1.3 for statement

    There are several ways to write a program for a particular task

    Anywhere a variable value of a type is allowed, more complex expressions of that type can be used

    Like the while statement, the body of the for loop statement can be a single statement or a group of statements enclosed in curly braces. The initialization part, the condition part and the increment part can be any expression

   

1.4 Symbolic constants

    Using "magic numbers" in a program is not a good practice, they provide little information to someone who will read the program later, and make program modification more difficult

    The #define directive can define a symbolic name (that is, a symbolic constant) as a specific string. The form is as follows: #define name replacement text After this definition, all occurrences in the program of names defined in #define (neither enclosed in quotation marks nor part of other names) will be replaced with the corresponding replacement text. The replacement text can be any sequence of characters, not just numbers.

    Symbolic constant names are usually spelled in uppercase letters. No semicolon at the end of the #define directive line

   

1.5 Character input/output

    The input/output model provided by the standard library is very simple. No matter where the text is input from and output to, its input/output is handled as a stream of characters. A text stream is a sequence of characters consisting of multiple lines of characters, each of which consists of zero or more characters, with a newline at the end of the line. The standard library is responsible for making every input/output conform to this model.

    The standard library provides functions to read/write one character at a time, the simplest of which are getchar and putchar

    Each time getchar is called, it reads the next input character from the text stream and returns it as the result value. This character is usually entered via the keyboard. Characters can also be entered from a file.

    Each time putchar(c) is called, it prints the contents of the integer variable c as characters, usually on the screen.

    No matter how a character is represented on the keyboard, screen, or anywhere else, it is stored in bit mode inside the machine. The char type is specially used to store this type of data, of course, any integer type can also be used to store character data.

    In the absence of input, the getchar function will return a special value that is different from any actual character. This value is called EOF. Therefore, the variable used to store the return value of the getchar function should be large enough to store the end-of-file character EOF in addition to any possible characters, so it is recommended to declare the variable as int

    EOF is defined in the header file <stdio.h> and is an integer. It doesn't matter what its exact value is, as long as it's not the same as any value of type char. The use of symbolic constants here ensures that the program does not need to depend on any specific value corresponding to it

    An assignment operation is an expression and has a value

    The conversion specifier %ld tells the printf function that its corresponding parameter is of type long

    The operands of the auto-increment or auto-decrement operators can be integer, floating-point, character and pointer types, and the operation effect on the pointer type needs to be considered separately

    For float and double types, the printf function uses %f for description

    One of the advantages of while and for statements is that the condition is tested before the body of the loop is executed. In the presence of boundary conditions, while and for statements help ensure that the program performs reasonable operations

    The characters in single quotation marks represent an integer value, which is equal to the corresponding value of the character in the machine character set, which is called a character constant, which is just another way of writing small integers. Using character constants is better than using numbers directly because the meaning is clearer and independent of a specific character set

    Escape character sequences used in string constants are also legal character constants. '\n' is a single character that represents an integer in an expression; '\n' is a string constant containing only one character constant

    If the magic numbers in the program are all in the form of symbolic constants, it is relatively easy to make a lot of changes to the program

    In expressions that have both value and assignment functions, assignment associative order is right-to-left

    && has a higher priority than ||, by &&

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325382168&siteId=291194637