C language exploration: Part 1 Lesson 4 Chapter 3: DISPLAY variable content in the world of variables, Chapter 3 variables

Source: Internet
Author: User

C language exploration: Part 1 Lesson 4 Chapter 3: DISPLAY variable content in the world of variables, Chapter 3 variables




Introduction


1. Course outline

2. Part 1 Lesson 4 Chapter 3: DISPLAY variable content in the world of Variables

3. Lesson 5: basic operations


Course outline


Our courses are divided into four parts. Each part has exercise questions after completion and answers will be published. Three games will also be written in C language.


Basic knowledge of C Programming


  • What is programming?

  • To do their best, you must first sharpen your tools.

  • Your first program

  • World of Variables

  • Basic operations

  • Conditional expressions

  • Loop statement

  • Practice: The first C games

  • Function

  • Exercise questions

  • Exercise: Perfect the first C games



C Advanced Technology


  • Modular programming

  • Attack pointer, C trump card

  • Array

  • String

  • Preprocessing

  • Create your own variable type

  • File read/write

  • Dynamic Allocation

  • Practice: "hanging villain" games

  • Secure text input

  • Exercise questions

  • Exercise: Explain the pointer in your own language



Develop 2D games with C language-based SDL Library


  • Install SDL

  • Create a window and canvas

  • Show images

  • Event Processing

  • Practice: "Super Mary pushes boxes" games

  • Time usage

  • Use SDL_ttf to edit text

  • Use FMOD to control sound

  • Practice: Visual Sound line

  • Exercise questions



Data Structure


  • Linked List

  • Heap, stack, and queue

  • Hash table

  • Exercise questions



Part 1 Lesson 4 Chapter 3: DISPLAY variable content in the world of Variables


The course "World of variables" has a lot of content. After the last two chapters, "Memory of the World of variables" and "variable Declaration of the world of variables", today, let's take a look at the last chapter "DISPLAY variable content of the world of variables".


In the last few lessons, we have learned how to use the printf function to display content on the screen. At that time, however, it only showed some simple text, such as "Hello World", "how are you", and "have you eaten.

Next we will learn how to use the printf function to display the variable content.


Using printf to display the variable content is similar, except that a special symbol is used to insert the variable content. For example:


Printf ("You have % d dogs ");


The special symbol here is actually "%" with a letter (in the above example, It is d). This letter indicates the type to be displayed, and d indicates the integer to be displayed. The following table lists common letters and corresponding variable types:


Format

Type

"%d"

int

"%ld"

long

"%f"

float

"%f"

double


We can see that the symbols used to display float (single-precision floating point number) and double (double-precision floating point number) are the same (because they are all floating point numbers in the end ).


When appropriate, we will introduce other symbols. It is enough to remember these symbols.


We are almost finished. We specify an integer at the exact position, but we have not specified which number to display. Therefore, the above Code is incomplete. You must tell the printf function the variables we want to display.


In fact, it is also very simple. We only need to write a comma after the double quotation marks, and write the name of the variable we want to display after the comma, as shown below:


Printf ("You have % d dogs", numberOfDogs );


When the program runs, the printf function will replace % with the value of the variable numberOfDogs.


Let's test it in a complete program.


# Include <stdio. h>
# Include <stdlib. h>

Int main (int argc, char * argv [])
{
Int numberOfDogs = 5; // you have five dogs at the beginning.
 
Printf ("You have % d dogs \ n", numberOfDogs );
Printf ("***** ran a dog ***** \ n ");
NumberOfDogs = 4; // just ran a dog and only 4
Printf ("ah, you only have % d dogs \ n", numberOfDogs );
 
Return 0;
}


Run the above program and the screen will display


You have five dogs, *** running a dog ***. You have only four dogs left.


Easy!


Show multiple variables

You can use a printf function to display the values of multiple variables.


As follows:


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


Run and Output


You have five dogs and four cats.



Extract input from the program


From now on, variables will become more and more important to us and become more and more interesting. The last point in this chapter is how to extract user input values and store them in variables.

To allow users to enter data in the console, we need to use another function: scanf.


This function is similar to the printf function in usage. One is responsible for input and the other is responsible for output.


A scanf example is provided:


Int numberOfDogs = 0;
Scanf ("% d", & numberOfDogs );


Is it similar to printf, but we have noticed that there is an & Symbol in front of numberOfDogs. Why does scanf need this symbol before the variable name in printf?

The answer is: the role of this symbol does not need to be further explored. It is good to know that scanf is used in this way. We will study it together in the future. Believe me, if we have to talk about it, today's class will not end.


The functions of scanf and printf differ from the following:

For float and double, all their replacement symbols in printf are % f, but they are different in scanf. In scanf, float is % f, while double is % lf

For example:


Double sum = 0;
Scanf ("% lf", & sum );


The following is a complete example to demonstrate how to use scanf to extract user input, store it in a variable, and use the printf function to output the value of the variable:


Int main (int argc, char * argv [])
{
Int sum = 0; // initialize the amount of money to zero
 
Printf ("How much do you have? ");
Scanf ("% d", & sum); // request the user to enter the amount of money
Printf ("You have % d dollars. It's not easy! \ N ", sum );

Return 0;
}


Run, the program will first show "How much money do you have ?", Then, the cursor stops at the end and waits for the user to input the data. After the user inputs the data, press enter to display the printf statement, as shown below:


How much do you have? 12000

You have 12000 yuan, so it's not easy!


Well, you should have understood the general principle. Thanks to the scanf function, our program can interact with users!


It should also be noted that scanf is a "willful" place, that is, although the user input Integer type is specified in the above example, if the user inputs another type, scanf will not make an error:

  • For example, if you enter 5600.45, This is a floating point number, not an integer, but scanf will still read it, but the number stored in the sum variable will change to 5600 (the decimal part is removed, only the integer part is retained)

  • Similarly, if you enter some strange characters, such as g ^ B @ & *, the value of sum will not change, or its initial value (in the above example, It is 0, because we initialize sum to 0. If no Initialization is made, the sum value will be arbitrary)


Summary:

The "World of variables" lesson has finally ended. Let's review the important knowledge points:

  1. Our computer has several different storage types, from the fastest to the slowest sorting: registers, high-speed cache, memory, Hard Disk

  2. The data in the hard disk will not disappear as the computer shuts down. The other three types are temporary storage.

  3. In order to store information, our computer needs to store data in the memory. Generally, the temporary data is stored in the memory. Of course, it may also be stored in registers or cache. We don't have to worry about it, automatically assigned by Computer

  4. In our source code, the variable refers to the data temporarily stored in the memory (main) and the value will change during the program running.

  5. We also have the constant variable (read-only variable, not a constant), which will not change during the running of the program.

  6. There are many types of variables, and each type occupies a different amount of space in the memory. In general, the int type is the first choice for declaring integers, and the double type is the first choice for declaring floating point numbers.

  7. The scanf function allows you to input data. The printf function outputs data.



Part 1 Lesson 5 notice: basic operations


Today's class is here. Come on together.

In the next lesson, we will study chapter 5 to perform some operations.





Programmer Alliance public account* If you think this article is good, click "share to friends" or "send to friends" or "add to Favorites" in the upper-right corner of the screen"

* New friends should follow the "Programmer alliance" Search Public AccountProgrammerleleague

Little Editor: frogoscar

Mini-editor's QQ number: 379641629

Small mailbox: enmingx@gmail.com

(Most common with mailbox)


PS: A friend reported that reading articles on the mobile phone is too tired. In fact, they can be viewed on a browser webpage.

Method 1. click the "·" button in the upper-right corner of the screen, select "Copy link", paste the link to your browser, or send it to yourself by email, you can open it in your computer's browser.



Method 2. toutiao.comWww.toutiao.com, Search my self-Media "Programmer alliance", which has an article, you can also directly into this link: http://www.toutiao.com/m3750422747/



How do new friends view all articles:

Click "view public account" and then "view historical messages"





The "Programmer alliance" public account is designed for programmers and App designers. You love programming and sharing to push a wide range of programming-related knowledge, excellent software recommendations, and industry trends. Search for ProgrammerLeague and follow-up ~


Continuous attentionProgrammer AlliancePublic Account, more interesting, expected, and content with highlights are waiting for you!

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.