C language Programming "add by Computer"

Source: Internet
Author: User

Previous section: C language programming output with color

Through previous studies, we learned that the word "printf" is used to make a computer speak, and we can use the word "printf" to say what the computer wants to say. After learning to "talk", we look at how to make the computer to do mathematical operations, palms we first let the computer Do "addition", first calculate 1+2=? it.

Think back to our childhood when the father and mother how to teach us to calculate 1+2?

Mother said, "The left hand gives you an apple, the right hand to give you an apple, now a total of several apples?" "We had a quick thought in our heads and blurted out the" three apples ". That's right! We use the brain first remember to do a few apples in the left hand, in the brain to remember the right hand has a few apples, the time when MOM asked us a total of several, our brains carried out very fast calculation, will just remember the two numbers to add, to get the results, and finally to say the calculated results. Let's take a closer look at some of the following steps.

1) Memorize the number of left-handed apples with the brain

2) have the brain remember the number of right-hand apples

3) Our brains add up to two numbers.

4) Get Results

5) Finally output the result

In this, we have a total of the brain:

1) Two inputs: Record the number of apples in the left and right hand, respectively

2) Stores 3 values: The number of apples in the left and right hand and the added value

3) Calculated once: add

4) Once output: outputs the added result

So how do we get the computer to do the addition to you? It is also necessary to do the above steps.

First, let's figure out how to make a computer remember a number just like our brains.

In fact, the brain of the computer is like a "skyscraper", there are a lot of a "small house", the computer will need to remember the number in the "Small house" inside, a "small house" can only put a number, so that the computer can remember a lot of numbers. Let's take a look at how to do it.

The "=" assignment symbol acts like a hand and sends the number to a small box.

int a,b,c;

This sentence, on behalf of the computer "skyscraper" to apply for three names are called A, B and C three small houses. (Note: There is a space between int and a, separated by commas between A and B and C, and a semicolon at the end indicates the ending.) )

Next, we let "small house a" and "small House B" separately to record two numbers 1 and 2, as follows:

a=1;b=2;

Note: here is a "=" number, which is not the "equals" number, he called the "Give" number (also known as the assignment number), he is similar to an arrow, meaning to the "=" number to the right of the "=" number to the left. For example, the number 1 to a, so the computer will know that "small house a" is stored in the number 1.

Then, the number of "small house a" and "small House B" are added, and the results are then placed in the "small house C".

c=a+b;

This formula is divided into two steps. The first step is to a+b the second part of the A+B value assigned to the "=" to the right of C.

At this point, it is almost complete, we summarize

int a,b,c;a=1;b=2;c=a+b;

Do many children's shoes think that they are all finished now? You forget one of the most important steps: Output the answer!

Let's recall how we should let the computer output the results.

Yes, use the printf statement. So how to put the "small house C" stored in the number of output it? According to the knowledge we learned in the last section, we just put the content to be output in double quotation marks, as follows:

printf("c");

What do you think the computer will output at a time?

Yes, ruthless output of a C.

What about the value of output C?

Then we need someone else to play.

When it's time to be

%d

%d is actually a "debt collection" or it can also be said to be "beggar". His full-time job even asked someone to "Ask for money"! So how do we use it?

printf("%d",c);

Place%d between double quotation marks, place "small house C" behind double quotes, and separate them with commas.

Then printf found the double quotation mark inside is a "debt collection", printf knew, at this time need to output a specific value, and no longer a symbol. printf will ask for a specific value for the "small house C" behind the double quotes.

Well, finally add the C language Code framework, the complete code of the computer to do the addition is as follows:

#include#includeint main(){int a,b,c;a=1;b=2;c=a+b;printf("%d",c);return 0;}

C language Programming "add by Computer"

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.