C Programming Language (2nd edition • New version) Chapter 1th Introduction

Source: Internet
Author: User
Tags arithmetic operators integer division

Examples and introduction to basic concepts.

1.1 Getting Started

Example 1: Print Hello World

(UNIX: Build hello.c; compile executable file a.out by command cc hello.c; execute a.out to print out.) Different OS compile, load, and run rules vary. )

Each program must contain a main function in a location, and each program executes from the start point of Main, and main (PS: can have two parameter argc,argv) (PS:?). The main output of C can be no int,c++ must be int, or it may be related to the compiler; function call (library function or self-coding function)

"Hello world\n" is a string constant that can be used as an input parameter for printf (no wrap);

\ n, \ t, \b, \ ", \ \ are both escape characters (add 3 octal digits after exercise ps:\; plus some characters are undefined);

1.2 Variables and arithmetic expressions

Note : Include between, cannot nest, compile ignores (Ps:ansi C is called C89; C99 Standard Add line comment//)

declaration : type name + variable table; All variables in C must be declared before use;

The loop body is a single statement without parentheses;

Recommendation: One statement per line, with spaces on both sides of the operator;

printf () formatted output , = represents three digits wide and right-aligned;

Floating-point printing width and precision :%6 represents at least 6 characters justifies;%.2 means printing two decimal places (not limited in width),%3.0 indicates that the floating-point number is at least 3 characters justifies and does not have a fractional part;%6.1 similar; all right-aligned

The operands of the arithmetic operators are integer, and if there is a floating-point type the integral type is converted to floating-point type; 5/9=0, 5.0/9 The result is floating-point number: C, and many languages, integer division performs the rounding (discarding fractional parts);

recommendation: A floating-point constant takes an integer, and a decimal point is added when writing. such as 9.0

1.3 For Loop

A general rule in C: An expression of that type can always be used where a value of a type variable is used, such as a%6.1-matched floating-point value, such as three statements in a for-post bracket;

1.4 Symbolic constants

Replace text #define Name

The name form is the same as the variable, it is generally recommended to use uppercase and variable distinction, the replacement text can be any sequence of characters, no semicolon at the end, the name does not define after the name (not in quotation marks; not part of other names) is replaced;

1.5 character input/output

The I/O model of the standard library: Regardless of where the text is I/O, it is treated as a character stream; Provides functions that read/write one character at a time, such as GetChar and Putchar:

C=getchar ();//reads the next input character from the text stream (usually from the keyboard) and returns it; Int C stores the character type and is large enough; returns EOF (a symbol constant defined in, different from an integer of any char type) indicates the end of the file;

A similar assignment statement is an expression with a value that can be put into other expressions such as (C=getchar ())!=eof;

Putchar (c);//The integer variable c is printed as a character (usually on-screen display)

Putchar can be called alternately with printf, and the output is the same as the call order;

++n, more concise and usually more efficient;%ld is used to output long type, if long is not enough to declare n as double, output%.0f;

  Note: to consider special cases ( boundary conditions ), such as the initial decision of the loop is false, the correct result should still be output;

Character constant: single quote character, the value is equal to the corresponding value in the machine character set, the essence is the small integer number of another writing; for example, the ' a ' value in ASCII is zero, ' \ n ' (is a single character) is 10 but using ' A ' can be independent of the character set, which is better than using 65;

Assignment statement a=b=c=1; equivalent to A= (b= (c=1)), that is, the assignment is from right to left;

&& Ratio | | Priority is higher, and the expressions they join are evaluated from left to right, and the final result is the termination evaluation, which is sometimes necessary;

1.6 Arrays

int ndigit[10] contains ndigit[0] to ndigit[9] 10 elements; [] but any integral expression;

1.7 Functions

The function defines the general form:

Returns a value type function name (0 or more parameter declarations)

{

Declarations section

Statement section

}

Return any expression, without expression, control to the caller, and the keynote function to ignore the return value;

General Main returns 0 for normal termination, not 0 for exceptions or errors, and for the program to perform environment return status;

function Prototype:the declaration statement before main, must be consistent with the definition, the parameter name can be ignored, the compiler detects the number of arguments in the function call and the type is wrong;

1.8 parameter-Value call

c The value passed to the invocation parameter exists in the temporary variable (local/private copy) rather than in the original variable, which differs from Fortran or Pascal; c The function can not directly modify the value of the variable in the key function, can only modify the value of the private copy, you need to modify the pointer; array name as a parameter, do not copy the array, the address of the starting element is passed in, you can access or modify the value of the array element by subscript;

1.9 Character Array

The default return type of the function is int, which can be omitted; The original C language function prototype does not write input parameters;

Call character array:

int getline (char s[], int lim)

void copy (char to[], char from[])

In C language, string constants are stored with the ' + ' flag at the end, and the%s format specification requires this form

1.10 External variables and scopes

The function's local variable (that is, the automatic variable) cannot be accessed directly by other functions and disappears when the function is executed (special case:static storage class Local variables are not changed between multiple function calls); An invalid value exists when an automatic variable is not assigned a value;

external variables: defined outside of all functions, can be defined only once, can be accessed at the global scope, the function can exchange data without using the parameter table, during the execution of the program has been in existence;

Declare variable types, and the compiler assigns them a storage unit;

Before the function uses an external variable, (I understand) if the external variable definition appears after the function or in other source files, you need to use extern (preceded by int, etc.) declaration;

The extern declarations of variables and functions are usually placed in a separate header file and include at the beginning of each source file;

A declaration such as copy () is considered an old-fashioned declaration, and the parameter table will not be checked; ANSI c specifies that an empty parameter table must be declared explicitly: void copy (void);

"definition" and "declaration": the former creates a variable or allocates a storage unit, the latter merely describes the nature of the variable and does not allocate a storage unit;

Recommendation: do not misuse external variables, will make the data relationship confusion, and reduce the generality of some functions

C Programming Language (2nd edition • New version) Chapter 1th Introduction

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.