05-c language scanf function

Source: Internet
Author: User

One, 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

2> the bytes occupied by different types are not the same, and the larger the data, the more the number of bytes required is 2. Storage of variables

The number of bytes consumed by 1> is related to the type, and also to the compiler environment

2> Variable Instance

int B = 10;

int a = 20;

L memory is addressed by large to small, prioritizing bytes with large memory addresses to variables. B has a larger memory address than a

L Each variable has an address: the address of the first byte is the address of the variable.

3> View memory Address:

int A;

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

4> Note

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 notation is not recommended for the second, scanf function 1. Brief introduction

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

int age;

scanf ("%d", &age);

The L scanf function waits for the user's keyboard input and does not execute the code backwards. The 1th parameter of scanf is "%d", stating that the user is required to enter an integer in 10 binary form. Note here that the 2nd argument of scanf is not the age variable, but the address of the age variable &age,& is an address operator in C that can be used to get the address of the variable

L After the input is finished, hit the ENTER key to tell the scanf function that we have entered, and the SCANF function assigns the input value to the age variable 3. Other usage

1> uses the SCANF function to receive 3 values, each of which is separated by an underscore-

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-such as 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, or even an English letter

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

Letter X

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

2> receives 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, carriage return 4. 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

05-c language 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.