Mad Man C-fully understand expressions

Source: Internet
Author: User

/*
Programming for 1 + 3 + 5 + 7 + 9 + 11.
*/
# Include <stdio. h>
# Include <stdlib. h>

Int main (void)
{
Printf ("1 + 3 + 5 + 7 + 9 + 11 = ");
Printf ("% d", 1 + 3 + 5 + 7 + 9 + 11 );

System ("PAUSE ");
Return 0;

}
The main purposes of this question are as follows:

1. Master the method for writing Integer constants. This is not difficult.

2. Understand the meaning of expressions in C. It is necessary to say a few more words.

In the code, the expression "1 + 3 + 5 + 7 + 9 + 11" has two meanings: first, it is "command" computer calculation "1 + 3 + 5 + 7 + 9 + 11". Second, this expression itself also represents the calculation result.

The C language is an Imperative language that specifies a series of executable operations and describes the computing process in the order of operations, learning C language means learning to use a C language "command" computer. If you don't realize this, you won't be able to understand the nature of C programming. Therefore, when learning the C language, pay special attention to the "commands" that can be sent to the computer using the C language ".

The expression "1 + 3 + 5 + 7 + 9 + 11" requires the computer to perform a series of addition operations. Operators in C language are the actions that require the computer to execute. The most basic commands sent to the computer are represented by operators. In algebra, computation is performed by human, while in code expressions, computation is performed by computers, which is essentially different.

Expressions all have a common function: Evaluate a value. Even if an expression does not ask for a value, it is called a void value (void is a type without a value ). And the expression itself represents the value it obtains.

Therefore, another layer of the expression "1 + 3 + 5 + 7 + 9 + 11" means that it is the value obtained by the computer. Many beginners do not understand this. They often habitually store this value in a variable, so that they can write the code

/*
Programming for 1 + 3 + 5 + 7 + 9 + 11.
*/
# Include <stdio. h>
# Include <stdlib. h>

Int main (void)
{
Int he; // no need to define this variable

He = 1 + 3 + 5 + 7 + 9 + 11; // here there is an additional assignment operation
Printf ("1 + 3 + 5 + 7 + 9 + 11 = ");
Printf ("% d", he );

System ("PAUSE ");
Return 0;

}

It is not difficult to think carefully and find that the he variable is unnecessary at all. Since there is no need, why should we set such a variable? In addition, compared with the previous code, this code has an assignment operation.

The Code should be concise. If there is no reason, you should not write anything unnecessary.

Summary:

Operator indicates the computation performed by the command computer;
The expression itself has a value, which can be used directly. This value does not need to be assigned to the variable for further use. Such code is more direct and concise.

Related Article

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.