C Language section fifth scanf function

Source: Internet
Author: User

    1. Memory Analysis of variables
    1. Bytes and Addresses

To better understand the storage details of variables in memory, first recognize the "bytes" and "addresses" in memory.

    1. Memory in "bytes"

0x is the hexadecimal, not too tangled, can understand the numbers between the big who is small on the line

    1. The bytes occupied by different types are not the same, and the larger the data, the more the number of bytes required

    1. Storage of variables
    2. The number of bytes consumed is related to the type, but also to the compiler environment

    1. Variable instance

int B = ten;

int a = ;

    • Memory is addressed by large to small, giving precedence to variables with large bytes of memory addresses. B has a larger memory address than a
    • Each variable has an address: the address of the first byte is the address of the variable.
    1. To view memory addresses:

int A;

printf ("A 's address is:%p\n", &a);

    1. Attention

Do not attempt to use the value of a variable before it is initialized

int A;

printf ("A 's value is:%d\n", a);

The above wording is not recommended .

    1. scanf function
    1. Brief introduction

This is also a function declared in stdio.h , so #include <stdio.h> must be added before use . When calling the scanf function, you need to pass in the address of the variable as a parameter, andthescanf function waits for the standard input device (such as the keyboard) to enter data. and assigns the input data to the variable that corresponds to the address.

    1. Simple usage

int age ;

scanf("%d", &age);

  • scanffunction when, it waits for the user's keyboard input and does not execute the code backwards. scanfThe first1A parameter is"%d", stating that the user is required toTenEnter an integer in the form of a binary. It is important to note thatThe First 2 arguments of scanf are not the age variable , but the address &ageof the age variable, & is an address operator in C that can be used to get the address of a variable
  • After the input is complete, hit the ENTER key to tell the scanf function that we have already entered , andthescanf function assigns the input value to the Age Variable

    1. Other usage
    2. Use the scanf function to receive 3 numeric values , separated by an underscore between each value

scanf ("%d-%d-%d", &a, &b, &c);

3 %d is separated by an underscore- so we have to add an underscore after each integer input -suchas input, or when assigning a value to the variable will be a problem

    • Note : The delimiter between the values is arbitrary , not necessarily with an underscore --It can be a comma, a space, an asterisk *, a pound sign # , and so on , even the English alphabet.

// comma ,

scanf ("%d,%d,%d", &a, &b, &c); // Input Format: 10,14,20

// Well number #

scanf ("%d#%d#%d", &a, &b, &c); // Input Format: 10#14#20

// Letters x

scanf ("%dx%dx%d", &a, &b, &c); // Input Format: 10x14x20

    1. Receive 3 values with the scanf function , separated by a space between each value

scanf ("%d%d%d", &a, &b, &c);

3 %d is separated by a space , we must enter a delimiter after each input integer, the delimiter can be a space ,tab, Enter

    1. Attention

Do not include \ n in the first parameter of scanf, such as scanf ("%d\n", &a); This causes the SCANF function to not end

    1. Exercises

Prompts the user to enter two integer n, and then calculates and outputs two integers and

Content Source: Preach intelligence podcast Li Mingjie teacher Content

C Language section fifth scanf function

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.