[Xcode C-2] constants, variables, scanf, printf and various operators

Source: Internet
Author: User

I. Constants

(1) integer constant: 1

(2) floating-point constants (with decimal places): The default value is double type, for example, 5.3. The float type is followed by an F, for example, 5.3f.

(3) character constants: single quotes, 'A' or '1', which can only be one byte character.

(4) string constants: Double quotation marks, "hello", etc.


Ii. Variables

Use int, double, float, and Char to define variables. After defining a variable, the first value assignment is also called initialization. Variables must be used after initialization.

(1) The scope of a variable starts from the definition and ends with return}. It is actually valid in a function. If this function is used, it is invalid unless it is a global variable.

(2) variables need to be defined before use, because the code is executed from top to bottom.

(3) formatted output, % I, % d, % C, % F, % 04i, %. 2f, and other placeholders.

There is no difference between % I and % d. % I is an old-fashioned writing method, and % d is a new writing method.

%. 2f the number after the decimal point indicates that the number of decimal places is retained.

% 4I indicates the width of the number of digits. If the number is 1, the result is 1. The first three digits are retained.

% 04i fills the gap with 0, that is, the number is 1, and the result is 0001. This frame animation can be used in iOS development. The serial number is, png0000002.png0000003.png, and so on.

%-4I is the right alignment, the number is 1, the output result is 1, and three digits are retained.

% 4I. If the number is more than 12345 digits, it will not be constrained. All outputs will be 12345.


Iii. scanf notes

Int main (INT argc, const char * argv []) {// use scanf and printf to accept two numbers entered by the user. After adding them, the output result is int number1; int number2; printf ("plz input your first number:"); // & indicates the address. scanf requires the user to enter scanf ("% d", & number1 ); printf ("then second number:"); scanf ("% d", & number2); int sum = number1 + number2; printf ("the sum is % d \ n ", sum); // scanf can accept multiple inputs at a time, but the user input format must be the same as the defined one. // as shown in the following sentence, the two numbers must also be separated by commas. // we can use any symbol as the separator, not necessarily a comma. // scanf ("% d, % D ", & number1, & number2); // If 1234 is input, the program intercepts only the first three digits, namely 123, and finally outputs nnumber1 as 123. // Scanf ("% 3d", & number1); // In scanf, use the carriage return as the end input signal, so \ n cannot be used, for example, scanf ("% d \ n", & number1); // put the input content in the input buffer. When you press the Enter key, enter another enter key in the buffer/n, at this time, scanf takes a value from the input buffer. If the format does not match, the end value is used. // When the value ends, there is still a data and/N in the input buffer. Therefore, when the next scanf occurs, the program does not require us to input anything, but directly assign values to the value in the input buffer. // For the following program, we enter a. The first unmatched format is a random number in Number3, because the input buffer contains a and \ n, therefore, to destroy the child, you must enter a value for the Child Program, but directly assign the value to the C ++ program in the input buffer. If it does not match, it is estimated that it will continue like this. Int Number3; scanf ("% d", & Number3); printf ("% d \ n", Number3); char ACC; scanf ("% C", & exp ); printf ("% C \ n", ACC); Return 0 ;}

Iv. Operator number

(1) In addition to +,-, *,/, there are also % worker modulo operation remainder. The remainder of the modulo operation is generally used to obtain a random number in a range ~ % 16 is used for the range between 15.

Int main (INT argc, const char * argv []) {// automatic type conversion, warning int Number3 = 10.8; // forced type conversion int number4 = (INT) 10.8; // type increase: convert 8 to double before adding 10.8. Otherwise, this conversion is automatically completed by the computer. // Rule: convert small data to Big Data Type Double number5 = 10.8 + 8; // What type of data is involved in the budget, and what type of data is returned, for example, the following result is 0, but it is not the function of the previous Int. If it is replaced with double, it is still 0 int number6 = 1/2; return 0 ;}


(2) The assignment operator is =, or + =,-=, and so on.


(3) There are two forms of auto-increment and auto-increment: A ++, ++ A. The former is the first operation, then auto-increment, and the latter is the opposite. Therefore, B = A ++ and B = ++ a, B = A of the former, and B = a + 1 of the latter.

In complex cases, A = 10, B = (a ++) + (A ++), B is 21, and A is 12. The first a ++ operation is performed first and then auto-increment. Therefore, the value is 10 and the auto-increment result is 11. When a ++ is followed, A is 11, the first operation adds 11 to the previous 10, which is equal to 21. Then add one to 12.


(4) sizeof () is an operator used to calculate the memory size of data and data types. For example, int num1 = sizeof (10) or Int num1 = sizeof 10, or define float a = 10.5 first, then int num1 = sizeof (A) or Int num1 = sizeof. The parentheses cannot be omitted when calculating the data type, that is, int num1 = sizeof (float ).


(5) comma OPERATOR: outputs the final calculation result, for example, int B = (a = 5, a ++, A * 6); and the result is 36.


(6) Relational operators: The returned results are true 1, false 0, and>, >=, <, <= are given priority over = ,! =. If all operations are in the same level, they are calculated from left to right. The relational operator priority is less than + 1 */This arithmetic operator.


(7) logic and operator: And or are respectively &, |, And ,!. When the core needs to pay attention to & if the front is false, the latter will not be computed. | If the preceding value is true, the subsequent values are not computed. The operation is omitted because the result is known.

[Xcode C-2] constants, variables, scanf, printf and various operators

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.