The scanf function of Black Horse programmer--c Language

Source: Internet
Author: User

I. Memory analysis of variables

1. Byte and Address

1> memory in "bytes", oxffc1,oxffc2,oxffc3,oxffc4 .... are bytes, 0x represents hexadecimal

2> the bytes used by different types are not the same, the larger the data, the more the number of bytes required

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

#include <stdio.h>

int main ()
{
Memory addressing from large to small
int a=10;

int b=20;

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

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

return 0;
}

Memory is addressed by large to small, giving precedence to variables with large bytes of memory addresses. a the memory address is more than B Big

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 wording is not recommended.

Second, scanffunction

1. 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 input data and assigns the input data to the variable that corresponds to the address

2. Simple usage

int age;

scanf ("%d", &age);

The 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

After the input is complete, 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 uses

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 numbers 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. Note

Do not include \ nin the first parameter of scanf, such as scanf ("%d\n", &a); this will cause scanf function does not end

5. Exercises

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

Solution

Prompt user to enter 2 numbers

#include <stdio.h>

int main ()
{
Define 2 variables
int A, B;

Hint First number
printf ("Please enter the 1th number: \ n");

Receive the first number
scanf ("%d", &a);

Hint 2nd number
printf ("Please enter the 2nd number: \ n");

Receive 2nd number
scanf ("%d", b)

Compute AND and output
int sum = a + b;
printf ("%d +%d =%d\n", a,b,sum);

return 0;
}

The scanf function of Black Horse programmer--c Language

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.