[IOS development Basics] [C Language] 01, ios development 01

Source: Internet
Author: User
Tags variable scope

[IOS development Basics] [C Language] 01, ios development 01

I. Keywords

The C language provides 32 symbols with special meanings.

Generally, all keywords in Xcode are purple-brown, and all keywords are in lower case. Such as int and return.

Ii. identifier

Identifiers are some symbols and names customized by programmers in the program. Keywords are provided by default in C language, and identifiers are customized by programmers. Such as the function name, used to differentiate various transactions.

Naming rules:

  • It can only contain 26 lower-case letters, 0-9 digits, and underscores.
  • Strictly case sensitive.
  • It cannot start with a number.
  • You cannot use keywords as identifiers.

Naming rules:

(1) try to give a meaningful name.

(2) Multiple words can be identified by camper (the first letter at the beginning of the second word is capitalized) or underlined.

3. Notes

(1) annotations are used to explain the meaning of the code and improve the readability of the program. The commented-out code does not participate in compilation. In development tools, annotations are generally bean paste green.

(2) Annotated symbols

Single line comment //

Multi-line comment /**/

(3) Comments: Check the code and troubleshoot.

(4) annotation nesting

① Single-row comments can be nested with single-row comments and multi-row comments

②. Multi-line comments can be nested with single-line comments

③ Multi-line comments cannot be nested with multi-line comments

(5) Note: printf ("// abc"); here // is a part of the string rather than a comment in double quotation marks.

Iv. Data

Image text and so on are all data, which are stored in the computer with 0 and 1.

(1) Classification

Data can be classified into static data and dynamic data.

①. Static Data: some permanent data is usually stored on the hard disk and stored as files on the hard disk. After the computer is restarted, it still exists. As long as you do not actively delete or the hard disk has no bad data, it will always be there.

②. Dynamic Data: temporary data dynamically generated during the program running is generally stored in the memory, and the memory storage space is generally small. After the computer is disabled, the data will be cleared. If the software or computer is disabled, the temporary data is cleared.

③ Static and dynamic data can be converted.

④. Note: Why not store dynamic data on the hard disk? Because the memory access speed is N times faster than the hard disk. The software is installed on the hard disk and loaded into the memory during running. Programmers should be more concerned with memory data.

(2) Data size

①. All data is composed of 0 and 1.

②. Data size, static data occupies the hard disk space, and dynamic data occupies the memory space.

③. The larger the data, the more values 0 and 1 are contained,

④. The unit of data storage is bit and byte. A 1 or 0 is a bit, that is, 1 bit.

⑤ Data is stored in bytes in a computer. 1 Byte = 8 bits (1 Byte = 8 bits ).

⑥ 1 KB = 1024 B, 1 MB = 1024 KB, 1 GB = 1024 MB, 1 TB = 1024 GB

(3) Data Types in C Language

1. Basic data type

1) INTEGER (int % d)

2) character type (char % c)

3) float % d

①. Float)

②. Double Floating Point Type)

2. pointer type void *

3. void

 

4. construction type

1) array a []

2) struct

3) enum Enumeration

4) union

Note: There is no boolean type in C language, which is usually expressed by 0 and 1.

5. Constants

(1) Definition

Constant, indicating some fixed data

(2) Classification

①. Integer constants such as 6, 27, 109, 256,-10, 0,-289

②. Float constants such as 5.43,-2.3, 0.0, 5.43f,-2.3f, and 0.0f

③. Character constants such as '6', 'A', 'F', '+', and '$' (Chinese characters such as 'male' cannot be used ')

④. String constants such as "6", "male", "Wow haha", "abcd", and "my_car4"

Vi. Variables

(1) Definition

If the value of a data item is uncertain or needs to be changed frequently, it is represented by a variable.

(2) defining variables

Purpose: To use variables, you must first define them. Allocate a storage space to the variable in the memory to facilitate data storage in the future. If multiple variables are defined, different buckets are allocated for multiple variables.

Format: variable name of the variable type;

Example: int a; char c; int num1, num2;

Note: The variable name is an identifier and must comply with the naming rules of the identifier.

Remember: variables should be defined for saving as long as the data is uncertain. In a 64-bit compiler, the int type occupies 4 bytes, 4*8 = 32bit in total, and the char type occupies 1 byte.

Q: Why are many types of variables defined?

A: different types of variables occupy different sizes of storage space, because the memory is extremely limited, allocating appropriate space to store them can save space.

(3) Use of Variables

Initialization: 1.int a; a = 10; 2.int a = 10;

Modify: you can modify the value of a variable and assign values multiple times. Each assignment will overwrite the original value.

Output: Use placeholders to output variables. Placeholders of various types are as follows:

Int % d or % I

Float/double % f (6 bits are output by default, which can be controlled by. 2f)

Long % ld

Long % lld

Char % c

String % s

Unsigned long % zd

(4) memory ANALYSIS OF VARIABLES

1. bytes and address

①. The memory is in bytes, and the computer usually uses hexadecimal (0x) to indicate the address.

        

②. Different types occupy different bytes. The larger the data, the more bytes required.

2. Storage of Variables

1. The number of bytes occupied is related to the type and the compiler environment.

        

②. Variable instance

Int B = 10;

Int a = 20;

From large to small addressing, the memory address is preferentially allocated to the variable in bytes with a large memory address. B's memory address is larger than a's

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

③. View the memory address:

Int;

Printf ("the address of a is % p \ n", & );

(5) usage of Variables

Variable scope: starts from the row defined by the variable until the end of the code block. Code block {} can be used to improve performance and immediately recycle unused variables.

Return: exit the function and clear the data in the memory.

We recommend that you use rectangular boxes and excel to analyze the memory.

(6) Exercise

Exercise: exchange the values of Integer Variables a and B in multiple ways:

Method 1: use intermediate Variables

1 # include <stdio. h> 2 // swap the values of two variables 3 4 int main () 5 6 {7 8 int a = 10; 9 10 int B = 11; 11 12 printf ("before the exchange a = % d, B = % d \ n", a, B ); 13 14 // define an intermediate variable to complete the exchange between two variables 15 int temp; 16 17 temp = a; 18 19 a = B; 20 21 B = temp; 22 23 printf ("after swapping a = % d, B = % d \ n", a, B); 24 25 return 0; 26 27}

Method 2: Do not use intermediate Variables

1 # include <stdio. h> 2 3 int main () 4 5 {6 7 int a = 10; 8 9 int B = 11; 10 11 printf ("before switching a = % d, B = % d \ n ", a, B); 12 13 a = B-a; 14 15 B = B-a; 16 17 a = a + B; 18 19 printf ("after swapping a = % d, B = % d \ n", a, B); 20 21 return 0; 22 23}

Method 3: Use bitwise OR

1 # include <stdio. h> 2 3 int main () 4 5 {6 7 int a = 10; 8 9 int B = 11; 10 11 printf ("before switching a = % d, B = % d \ n ", a, B); 12 13 a = B ^ a; 14 15 B = a ^ B; 16 17 a = a ^ B; 18 19 printf ("after swapping a = % d, B = % d \ n", a, B); 20 21 return 0; 22 23}

 

 

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.