C Programming language Note 2017/3/25

Source: Internet
Author: User

1.3 For statement

The basic format for the For statement is as follows:

for (initializing part; condition part; increasing step size section)

{

Loop body

}

A For statement is a looping statement that is a generalization of a while statement, but a more intuitive operation for the for statement. The parentheses after the for has a total of 3 sections, separated by semicolons. Braces can also be used when the loop body part is a single statement.

See below for temperature conversion procedures:

#include <stdio.h>

/* Print Fahrenheit-Celsius temperature table */

Main ()

{

int Fahr;

for (FAHR=0;FAHR<=300;FAHR=FAHR+20)

printf ("%3d%6.1f\n", Fahr, (5.0/9.0) * (fahr-32));

}

1.4 Symbolic constants

It is not a good habit to use similar constants such as 300,20 often in programs. First, they can hardly provide any information to the reader; second, when the program is large, these numbers make it difficult to modify the program.

Therefore, in C, the usual method is to give these numbers the meaning of the name. #define指令可以把符号名 (or symbolic constant) is defined as a specific string:

Replace text #define Name

After the definition,

1. All names that appear in the program that are defined in # define will be replaced with corresponding replacement text;

2. The naming method is the same as the ordinary variable name, and it is a sequence of letters and numbers that begin with a letter;

3. The replacement text can be any sequence of characters, not limited to numbers.

So the above temperature conversion program can be rewritten as:

#include <stdio.h>

/* Print Fahrenheit-Celsius temperature table */

#define LOWER 0/* Lower limit for table */

#define UPPER 300 *//table Upper Limit */

#define STEP 20/* step */

Main ()

{

int Fahr;

for (Fahr=lower;fahr<=upper;fahr=fahr+step)

printf ("%3d%6.1f\n", Fahr, (5.0/9.0) * (fahr-32));

}

Attention:

1. #define指令行的末尾没有分号;

2. Lower and upper are symbolic constants, not variables, and do not need to be declared;

3. To distinguish between variable names spelled in lowercase letters, symbolic constants are usually spelled in uppercase letters.

1.5 character input/output

The C language standard library provides a function to read/write one character at a time, the simplest of which is the GetChar and Putchar two functions.

1. Call the GetChar function to read the next input character from the text stream and return it as the result value. That is, after executing the statement C=getchar (), the variable C will contain the next character in the input stream.

2. Call the Putchar function, which will print a character, usually displayed on the screen. That is, after executing Putchar (c), the contents of the integer variable C are printed as characters.

C Programming language Note 2017/3/25

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.