C language basic expansion (2) and C language basic Expansion

Source: Internet
Author: User

C language basic expansion (2) and C language basic Expansion

Basic expansion of scanf () functions:

 

The C function library contains multiple input functions. The most common function is scanf (). It can read input in various formats.

Scanf () can convert the input string into the desired forms, such as integers, floating-point numbers, and strings. It is the inverse process of printf. Like printf (), both have control strings and parameter lists. The difference is that scanf ()The parameter list is a pointer to a variable..

#include <stdio.h>int main(){    int age;    float height;    char address[30];        printf("enter your age\n");    scanf("%d",&age);    printf("enter your height\n");    scanf("%f",&height);    printf("enter your address\n");    scanf("%s",address);        printf("age : %d height: %f  address: %s\n",age,height,address);}

The above program uses scanf () to complete a simple prompt input interaction function.

  

    int age;    float height;    char address[30];        scanf("%d",&age);    scanf("%f",&height);    scanf("%s",address);

We can see that the parameters of the third scanf are not the same as those of the first two parameters. The first two parameters use the get address operator, and the third one does not. However, they are all pointers to the corresponding variables. For pointers to the int and float types, use the variable name to get the address, that is, the above & age and & height; For arrays, the array name is the pointer to it, as shown in the preceding address.

   Scanf () requires that the pointer of the variable be passed.Because our input is stored in these variables, that is, in the functionChange the value of these variables before passing a pointer to modify the value of the parameter variable.In this case, it seems like a function is used to exchange two numbers of value parameters to pass pointers. If the pointer to the corresponding variable is not passed,The program may crash.

 

From the scanf () perspective:

Suppose we use a % d specifier to read an integer.

Scanf () starts to read one input character each time, andSkip blank characters(Space, tab, line break)Until the first non-blank character is encounteredIt tries to read an integer, so if the input character is a number or a symbol (+ and-), save the character and continue reading the next character,Until it encounters a non-Numeric CharacterThe conclusion is that it has been read to the end of the integer. Then scanf () puts the non-numeric character back into the input. This means that when the program starts reading the input for the next time, it willThe non-numeric character that was previously input..

Finally, scanf () calculates the corresponding value of the read input and stores the value in the corresponding variable.

  

Return Value of scanf:

Scanf () function returnsNumber of successfully read ProjectsIf no project is read (when the expected number is read and the input is a flying Number character ),Returns 0.. When the end of the file is detectedReturn EOF(End of file), EOF is in the file

Special value defined in stdio. h. Generally, # define is-1.

The return value of scanf () can include the following applications:

  

// Process the input as a number. Otherwise, exit the loop while (1 = scanf ("% d", & num) {// do something ...} // skip the input integer while (1 = scanf ("% d", & num ));

 

 

 

Reference: C primer plus

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.