Author Xie Enming, public number “programmer union”. Please indicate the source of reprint. Original: www.jianshu.com/p/497355a6b…

The whole series of C language Exploration

Content abstract


  1. Use printf to display variable content
  2. Use scanf to extract input from the program
  3. conclusion
  4. Part 1 Lesson 7 Notice

1. Use printf to display variable contents


Variables related to the content of a little more, after a class C language exploration trip | lesson 5: the first part of the world of the variable (2), the variable declaration, today we are going to learn variables of the last points: show variable content.

In the last few lessons, we’ve seen how to use the printf function to display content on the screen. But back then it was just simple words like “Hello World”, “how are you”, “have you eaten yet”, etc.

Let’s learn how to use the printf function to display variable content.

Using printf to display the contents of a variable is similar, except that we use a special symbol where we want to insert the contents of a variable, for example:

printf("You have % D dogs.");
Copy the code

The special symbol here is actually % plus a letter (d in the above example) that indicates what type to display, and d indicates an integer to display. The following table lists some commonly used letters and their corresponding variable types:

format type
%d int
%ld long
%f float
%f double

You can see that the symbols used to display float and double are the same (because they’re both floats after all).

We’ll introduce other symbols in due course, but these are the only ones we need to remember for the time being.

We’re almost done. We specify exactly where we want to display an integer, but we haven’t specified which number to display. So the above code is not complete, and you have to tell the printf function what variable we want to display.

After the comma, write the name of the variable we want to display, as follows:

printf("You have % D dogs.", numberOfDogs);
Copy the code

When the program runs, the printf function replaces %d with the value of the numberOfDogs variable.

Let’s test it with a complete program:

#include <stdio.h>
#include <stdlib.h>int main(int argc, char *argv[]) { int numberOfDogs = 5; // You start with 5 dogsprintf("You have %d dogs \n", numberOfDogs);
    printf("**** Ran away a dog ****\n"); numberOfDogs= 4; // There are only four dogs leftprintf("Jeez, you only have % D dogs \n", numberOfDogs);

    return 0;
}
Copy the code

Run the above program and the screen will display:

You have five dogs **** one dog ran away **** Oh, you only have four dogs leftCopy the code

It’s simple!

Display multiple variables


You can also display the values of multiple variables with a single printf function

The following

int main(int argc, char *argv[])
{
    int numberOfDogs= 5, numberOfCats= 6;
    printf("You have %d dogs, and % D cats \n", numberOfDogs, numberOfCats);

    return 0;
}
Copy the code

To run or output:

You have five dogs and four catsCopy the code

2. Use scanf to extract input from the program


From now on, variables are going to be more and more important and interesting to us.

Let’s move on to the last point of the lesson: how to extract the value entered by the user and store it in a variable.

To enable the user to type on the console, we need another function: scanf.

This function is a pair of printf functions, and it’s used in a similar way. Scanf is the input, printf is the output.

To give an example of scanf’s use:

int numberOfDogs = 0;
scanf("%d", &numberOfDogs);
Copy the code

It’s a little bit like printf, but you notice numberOfDogs has an ampersand in front of it. Why is it that printf doesn’t need this symbol in front of the variable name but scanf does?

The answer is: we don’t have to go into the meaning of this symbol, just know that scanf is used like this, and we will study it together in the next class. Trust me, if we had to talk, we wouldn’t be done for today.

There is another difference between the scanf and printf functions:

For float and double, the substitution in printf is %f, but in scanf it is different. In scanf, float is %f and double is %lf.

Such as:

double sum = 0;
scanf("%lf", &sum);
Copy the code

Here is a complete example of how to extract user input using scanf, store it in a variable, and print the value of the variable using printf:

int main(int argc, char *argv[]) { int sum = 0; // Initialize the money to zeroprintf("How much money have you got? ");
    scanf("%d", &sum); // Request the user to enter the amount of moneyprintf("You've got the % D's and give them up quickly! \n", sum);
        
    return 0;
}
Copy the code

Run, the program will first say, “How much money do you have?” , and the cursor stops behind, waiting for user input; After the user enters the data, press Enter, and the printf sentence is displayed as follows:

How much money do you have? 12000 you have 12000 yuan, that still don't hurry to hand over!Copy the code

Okay, so you get the general idea. Thanks to the scanf function, our program can now interact with the user!

Note also that scanf is caprine. Although we specify an integer type, scanf does not fail if the user enters any other type:

  • For example, if the user types in 5600.45, which is a floating-point number, not an integer, scanf will read it in anyway, but the sum variable will become 5600 (leaving out the decimal part and keeping only the integer part).

  • Similarly, if the user typed in some strange character, such as g^b@&*, the variable sum would remain the same as its initial value (0, because we initialized sum to 0, otherwise it would be arbitrary).

3. Summary


Now that the World of Variables series is over, let’s review some important points:

  1. Our computers have several different types of memory, in order from fastest to slowest: registers, cache, memory, and hard disk.

  2. Data in the hard drive does not disappear when the computer is shut down. The other three types of storage are temporary storage.

  3. In order to store information, our computer needs to store data in memory, generally temporary data is stored in memory, of course, may also be stored in registers or cache, we don’t have to worry about, the computer will allocate.

  4. In our source code, variables refer to data that is temporarily held in memory (mainly) and whose value changes during program execution.

  5. We also have constant variables (read-only variables, not constants) whose value does not change during program execution.

  6. There are many types of variables, and each type takes up different amounts of memory. In general, int is the preferred declaration of integers and double is the preferred declaration of floating-point numbers.

  7. The scanf function allows the user to enter data, and the printf function outputs data.

4. Part 1 Lesson 7 Notice


That’s all for today’s lesson, come on!

The next lesson: the first part of C language to explore trip | lesson 7: operation that something


I am Xie Enming, the operator of the public account “Programmer Union”, and the moOCs elite lecturer Oscar, a lifelong learner. Love life, like swimming, a little cooking. Life motto: “Run straight for the pole”