C language variables and expressions

Let's write a program for each day of the current month is displayed, then this is illustrated by the program code, so that we have some understanding of the variables and arithmetic expressions:

#include <stdio.h> // include the header file

/ * This is a show in January

Each day of the program * /

main()

{

int month, max; // define two variables, and the number of days in May

int day; // variable Day

month = 1; // make variable month is 1,

max = 31; // make the value of the variable month 31,

day = 1; // make variable month is 1,

When circulating while (day <= max) // variables day less than the variable max

{

printf ( "% d% 2d \ n", month, day); / / display the month and day

day = day + 1; // variable value plus 1 day

}

}

This code contains a lot of inside knowledge to learn the C language, we come one by one to see:

First, the comment symbol // and / * * /

// double slash behind the content will be ignored by the compiler out, that is to say // back (to the right) the contents of the code will not work in the same row, so we are always here to write about the Bank code comments, the code used to remind the reader of the code here is what to do. Or some temporary unused code commented, rendered inoperative.

/ * / Divided into two parts, the front part / rear part * /. They come in pairs, which represents the code between the symbols will be ignored by the compiler, no longer work. It differs from the double slash is, / * * / can cross lines, allows this symbol commented code among a plurality of rows; // can only comments to the current row behind (right side) code.

Second, the use of variable

Variable, by definition, a variable amount. There are many types of variables, such as:

char character

short short integer

int int

long long integer

float Single-precision floating-point

double Double-precision floating point

Here we only need to know the type and function of these variables can be, on the range and use of different types of variables we will learn in later chapters.

Want to use variables, you must first define a variable of type in the code, we can use a letter or word to indicate a value, and this value can vary. E.g:

int month, max;

int day;

The above statement indicates that defines three int type variables named month The, called a max, the other is called day. Noteworthy is the definition of a variable we can define multiple variables after declaring variable types (int month, max; indicating that they are variable of type int), you can declare a variable for each individual type (int day;). After defining the variables, we can use this variable, the variable assignment such as this, is to let the value of this variable into our designated value:

month = 1;

max = 31;

day = 1;

This means that the value of the variable to make changes to 1 month; let the value of the variable max becomes 31; let the value of the variable to 1 day. Wherein the = assignment operator, which indicates the contents of the left side to the right side of an assignment.

Third, circulation and conditional expressions

While the code is a keyword that represents a circulatory function. Cycle is repeatedly performed braces {} in the content, the termination condition is determined by the cycle performed in parentheses on the right side while the value of the conditional expression (), for example while statement following codes:

while(day <= max)

{

}

It represents the value of the variable is less than or equal day max value of the variable, the loop executes the code in {}, when the value is greater than the value max of day, end of the cycle, the program will continue down. Here we see a conditional expression day <= max, this expression represents a condition when the result of the expression of this relationship is true returns 1, is false returns 0 when the result of the conditional expression of this relationship. As for the contents of the statement in parentheses () {} while executing the loop represented proviso, if the result is true (value 1, to be exact greater than 1) cycle is executed, if the result is false (value 0 ) the end of the cycle. Relational expressions are the following:

== equal

! = Not equal

<Less than

<= Less than or equal

more than the

= Greater than or equal

Fourth, the arithmetic expression

Careful readers will find: Because the day is 1, and the max value of 31, so the results day <= max would have been 1, while it would have been endless cycle continues. So we need to set a condition so that the loop variables to modify day execution value, let the value after performing a time greater than max, the results of such day <= max becomes the 0, then the end of the loop will execute while the following code. We look at the value of the process variable on the day of:

while (day <= max)

{

day = day + 1;

}

Wherein the body of the while we write {} in the day = day + 1; this statement, which is an arithmetic expression that represents the value of adding 1 to the variable of the day, day and assigned to the variable result, in other words this statement is used to make variable day 1 plus the value of the original foundation. Since the initial value of day 1, max value of 31, the while statement is performed after each cycle of day value plus one pair, so that after the execution cycle becomes 31 times a day values ​​32, 32 when the value of the day when so , day <= max result of the conditional expression is false (value 0) then the loop ends.

Fifth, according to the output format

In the previous section we simply tells the printf function call methods on, let's look at it the other uses:

while (day <= max)

{

printf("%d %2d\n", month, day);

day = day + 1;

}

We call the printf function, which will display a string of text on our standard output device, in the previous section we give it a pass called "Hello World! \ N" string, so it shows up ! Hello World in this section, we need it to be able to display some dynamic text messages, its function is defined as follows:

int printf (char *format, …);

int left represents the function will return after performing an execution result, which is an integer number, indicating that it shows the total number of characters. printf library function names that end, followed by a pair of small brackets () parameters passed, the parameter table first char * format indicates that this is a string (to be exact should be a character pointer, you can Whether it will be carefully talked about this in subsequent content) storage format display allows incoming back ... represents a number of parameters, the number is arbitrary. The Code:

printf("%d %2d\n", month, day);

Printf function performs display showing some content string to be displayed here% d represents an integer, the integer value of the source of the first parameter is the value of the variable month, then there is a space after% d, then a display space character, then the next display% 2d represents an integer, the source integer value is the second value of the parameter variable day. So what's the difference between% d and% 2d is it? Printf format definition display of% nd represents an integer, n represents the integer-bit bytes occupied, and align right. For example: day 1 value, using the result is displayed% 2d occupies two bytes, and right justified "1" (note quotes first character is a space, the second character is 1).

Finally, we look at the results of running a full program of beginning of this section:

1 1

1 2

1 3

1 4

1 5

// intermediate content slightly

1 31

Published 261 original articles · won praise 4 · Views 4257

Guess you like

Origin blog.csdn.net/it_xiangqiang/article/details/105206476