Getting started with iOS development☞C language Series 2, ios

Source: Internet
Author: User
Tags variable scope

Getting started with iOS development☞C language Series 2, ios
Constant

C language has rich data types. during development, constants or variables are usually used to represent the data.

"Volume" Indicates data. Constant indicates some fixed data, that is, data that cannot be changed.

10.1; // double

5.2f; // float with single precision

'A'; // constant of the struct type

'AB'; // Error

'Lil'; // incorrect writing because one Chinese Character occupies three bytes

'\ N'; // constant of the escape type (Escape Character, indicating a single character)

  

Method for defining constants:

Const int a = 100;

Variable

Variable: represents a memory space used to store frequently-changing data. (The variable name is the name of the space. The variable type determines the memory space occupied by the variable)

Variable name: it is the basis for operating data (storing data.

Initialization: clears some junk data by assigning an initial value (0/NULL.

Use of variables: Save value and value.

Int a; // defines a variable, which is not initialized.

The possibility of a value: 1) system data; 2) data left by the previous program; 3) garbage count.

Scope of the variable: (scope of use of the variable)

Local variables (internal variables): variables defined within the function or within the code block.

※Scope: starts from the location where the local variable is defined, and ends with the code block where it is located or the "}" of the function body.

※Note: within a block, you can define variables with the same name as the block. The block is temporarily blocked (the block does not work.

# Include <stdio. h>

 

Int main (){

Int age = 20;

// The code block represents a space, which is in parallel with the age space.

{// Code block starts

Int a = 5;

Printf ("a = % d \ n", a); // a = 5

Int age = 25; // variables with the same name as those outside the code block can be defined inside the code block.

Printf ("age = % d \ n", age); // age = 25;

} // The End Of The code block (all the spaces inside the code block will be released)

Printf ("age = % d \ n", age); // age = 20;

Return 0;

}

Variable name: The name must comply with the identifier syntax requirements.

  • It must start with a letter or underscore
  • Contains letters, underscores, or numbers
  • Case Sensitive
  • Cannot conflict with keywords
  • In theory, the identifier length is not limited, but it will be truncated by the compiler if it is too long.

Variable Declaration (Definition): variables in the same scope cannot be repeatedly defined!

Syntax format: variable name of the variable type;

For example, int age;

Float result;

Double x, y, z;

Variable type: indicates the type of constants that can be stored in the defined variable.

Variable name: it is a identifier used to identify the storage area that we open up in the memory for future use.

Variable scope: Starting from the defined line until the end of its braces

// 1. Define a variable (that is, to open a space in the memory, the size of the space is related to the defined type, and the space name is the variable name)

Int age; // This line of code opens up a 4-byte storage area in the memory, called age; this storage area is used to store int-type data

// 2. Initialization: the first value assignment of a variable is called initialization.

Age = 22; // This line of code is to put 22 in the storage area named age.

  • Undeclared variables cannot be used. variables must be declared only once before they are used.
  • The variable is used to save the temporary value generated during the program execution. It can be assigned multiple times, but only the last value is saved.

 

Variable Storage
  • Storage space occupied by variables (number of bytes): it is related to the variable type and the compiler environment.
  • The address of the first byte of the variable storage unit is the address of the variable (detailed address/first address)
  • Any variables are stored in binary format in the memory:

The binary form of a negative number is actually to reverse the binary form of its positive number and Add 1.

1. Different data types occupy different storage space:

  

2. Different data types:

  

  

How to store variables in the memory? (Why are there types of variables)

  • As long as variables are defined, the system will open up a storage space to store data for our variables.
  • The more defined the variable, the larger the memory address (starting from the largest byte address)
  • Memory addressing ranges from large to small, from high to high, and from low to low.
  • The address of the variable is the smallest byte address of the storage space occupied by the variable (that is, the first address: & variable name)

The minimum storage unit in a computer is byte, and each byte has an address.

    

// Extended: Get the data stored in each byte

Char * p = & value;

For (int I = 0; I <sizeof (value); I ++ ){

Printf ("% I \ n", * (p + I); // retrieve the data stored in each byte (88 2 0 0)

}

Note: Here, address (int pointer) + 1 is equivalent to char pointer + 4

  

// When defining a variable, there is nothing in the variable that is unknown.

Int score; // junk Value

 

// Print % characters in the printf function to print % characters

Printf ("5% % 2 = % d \ n", 5% 2); // result: 5% 2 = 1

 

 

Values of Integer Variables a and B:

For example, a = 10, B = 11; after exchange: a = 11, B = 10. Two methods are used:

  1. Use third-party Variables

Int temp;

Temp =;

A = B;

B = temp;

  1. Do not use third-party Variables

A = B-;

B = B-;

A = B +;

Printf function:

The printf function is called the format output function. The last letter of the keyword, f, is the meaning of "format ".

The general form of printf function calls is: (The format control string is used to specify the output format)

Printf ("format control string", output table column );

The general format of the format string is: (the items in square brackets [] are optional)

% [Flag] [minimum output width] [. Precision] [length] type.

// The printf function can be used to calculate the length of a string.

Int length = printf ("iOS development \ n ");

Printf ("% I bytes \ n", length); // 10 (1 + 1 + 1 + 3 + 3 + 1)

1. Type (format Controller)

  

2. Flag

The following table lists the four types of Logo characters:-, +, #, and space:

  

  • Specified Bit Width:
    1. % 0nd: between % and d, 0n, n indicates the width of the output number. If not, fill the left side with 0 (emphasis)
    2. % Nd: n between % and d. n indicates the width of the output number. If not, fill the left side with spaces.
    3. %-Nd: between % and d-n, n indicates the width of the output number. If not, fill the right side with spaces.
  • Specified number of digits:

% M. nf: between % and f, there can be m. n, m indicates the width of the output number, m indicates the number of digits after the decimal point, if not enough, fill the left with spaces

# Include <stdio. h>

 

Int main (){

Float m = 3.141592f;

Printf ("m = % 8.4f \ n", m); // fill in spaces on the left by default

// %-M. nf output occupies m places, where decimal places occupy n places. If the value width is less than m, fill in spaces at the right end.

Printf ("m = %-8.4f \ n", m); //-indicates the right side of the space Complement

// % *. * Special usage:

Printf ("m = % *. * f \ n", 6, 2, m); // m = 3.14

Printf ("%. * s \ n", 4, "abcdkkkkkkk"); // abcd

Return 0;

}

% G: automatically selects an output with a shorter value in the f format or an eformat, and does not output meaningless zero values.

% P: Output address (pointer)

/**

* % F retains 6 decimal places by default

* Specify the number of decimal places to be retained: %. nf, where n is the number of decimal places to be retained. f is used to output the real type.

*/

Float values = 3.14;

Printf ("% f \ n", value); // 3.140000

Printf ("%. 2f \ n", value); // 3.14

 

/**

* Effective digits of float and double:

* Float indicates a maximum of 7 valid data bits. Double represents a maximum of 16 valid data bits.

*

* Float type highlights:

* 1. Add f/F to the end of the decimal number and specify it as the float type.

* 2. 6 decimal places are reserved by default.

* 3. The precision is a 7-digit valid number (the first digit on the left is not zero, and the first seven digits except the decimal point are valid, and the first seven digits are junk data)

*

1bit (symbol bit) 8 bits (index bit) 23 bits (tail bit)

The precision is determined by the number of digits of the ending number.

Float: 2 ^ 23 = 8388608, a total of seven digits, which means a maximum of seven valid digits are allowed,

But it is absolutely guaranteed to be 6 bits, that is, the float precision is 6 ~ 7 valid digits;

*/

Float values = 3.1415926525;

Printf ("% f \ n", value); // 3.141593 (6 bits are reserved by default)

// The float valid number is 7 bits, and the excess bits will display junk data (inaccurate)

Printf ("%. 10f \ n", value); // 3.1415927410

 

// If you want to complete the output, you must save the data as the double type, because the valid bits of the double type are 15 bits.

Double value = 3.1415926525;

Printf ("%. 10lf \ n", value );

// How to increase the forced lattice (when the number of decimal places is retained)

// Specify the number of decimal places to retain, you can use the * placeholder, and assign a value to the specific reserved decimal places.

Float values = 3.1415926;

Printf ("%. * f \ n", 5, value );

& Is an address operator in C. It can be used to obtain the address of a variable.

& You can get the address of the variable, for example: & num

* You can find the variable based on the address data, for example, * (& num)

Scanf Functions

Scanf is a blocking function.

The program stops at the place where the scanf function appears and does not run the subsequent code until the data is received.

// Use scanf to receive user input from the keyboard

Scanf ("% d", & a); // Note: The detailed address of the scanf function variable must be told to store user input data.

// There can be a number n between % and d, which indicates the width of the received data

Scanf ("% 2d", & );

Scanf functions:

  1. The data entered by the keyboard must match the formatted string. If the data does not match, the scanf function is automatically terminated.
  2. When scanf receives multiple data, a separator (for example,) is added to prevent errors :,)

(Separators except space, carriage return, and Tab can be % c, because space, carriage return, and Tab are characters)

  1. \ N cannot be written at the end of the formatted string of scanf.

    

    

// \ N indicates line feed

// How do I tell the scanf function that the input is complete? Press enter (\ n)

// Because the carriage return is the scanf Terminator, \ n cannot be written at the end of the scanf formatted string. (If it is added to the end, the input will never end, that is, the scanf function will not end)

// If \ n is accidentally placed at the end of the scanf formatted string, it can also be broken. input as is (input \ n) or enter an unmatched type (for example, input)

Scanf ("% I \ n", & num1, & num2 );

 

Implementation principle of scanf Functions

How scanf functions run:

The system puts user input content into the input buffer.

The scanf function extracts the content one by one from the input buffer and assigns the value to the format operator. If the types are inconsistent, the original data is not modified.

  

  

When entering data, scanf must be in the same format as the formatted string.

  1. Scanf ("% d-% d", & a, & B, & c );

Note: The Middle delimiter of the scanf placeholder may be arbitrary. It may not be a hyphen (-), a comma, a space, an asterisk (*), a pound sign (#), or even an English letter.

  1. Scanf ("% d", & a, & B, & c );

3% d are separated by spaces. After each integer is entered, a separator must be entered. The separator can be space, tab, or press Enter.

Do not add \ n at the end of the scanf string, for example, scanf ("% d \ n", & a); this will cause the scanf function to end.

Interview Questions

Int;

Is there a value in a? What is it? Junk Value

// Determine whether a year is a leap year

// Can be divisible by 4, but (and) cannot be divisible by 100, or can be divisible by 400

Int year; // Save the year

Printf ("enter a year :");

Scanf ("% d", & year );

If (year % 4 = 0 & amp; year % 100! = 0) | year % 400 = 0 ){

Printf ("is a leap year \ n ");

} Else {

Printf ("not a leap year ");

}

 

※Variable analysis question: (Note the scope of the 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.