ios-(5) Variable memory analysis, scanf function

Source: Internet
Author: User

I. Memory analysis of variables

1. Byte and Address

* In order to better understand the variables in memory to store the details, first to understand the memory of the "byte" and "address."

* Memory in bytes

* Different types of bytes are not the same, the larger the data, the more bytes required Frost

2. Variable Storage

* The number of bytes consumed is related to the type, but also to the compiler environment

    

  16-bit compiler 32-bit compiler 64-bit compiler
char 1 1 1< /td>
2 4 4
float 4 4
double 8 8

     * Variable Instance

     int B = 10;

int a = 20;

# memory is addressed by large to small, prioritizing memory addresses to large bytes to variables. The memory address of B is larger than A;

# each variable has an address: the address of the first byte is the address of the variable;

* View memory address

int A;

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

Second, scanf function

1. Introduction

This is a function declared in stdio.h, so you must add #include<stdio.h> before use: when calling 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 address corresponding variable;

2. Simple usage

int age;

scanf ("%d", &age);

* When the scanf function is called, it 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, scanf function Hey, assign the value you entered to the age variable.

3. Other usage

* Receive 3 values with the scanf function, separated by a middle row between each value

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

3%d is separated by an underscore-so we must underline the price every time we enter an integer--for example, enter 10-14-20, otherwise there will be a problem when assigning a value to a variable.

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.