C Language---Program general form, data type, constant volume, operator, expression, formatted input and output

Source: Internet
Author: User
Tags arithmetic operators

1. General form of the procedure

(1) Notes

① Category: Single-line comment (//): Comment one line. Multiline Comment (/**/): Within this interval, it is a multi-line comment that can be wrapped.

② function: Hint code function, hint idea does not write the consequence of comment: Come out mix always to also.

③ Single-line comment cannot wrap

(2) #include #import

① import file, the system provides class library file contains some function functions, if you want to use in a file, you must import the required files.

②stdio Standard Input Output

(3) Main function, also known as the main function, is the entry position of the program running

(4) Function code

(5) return

emphasizing coding specifications, code hints, errors, and warnings

2. Data type

In writing a computer program, we will give the data to the computer processing, then in C language how this data stored.

For example, to store a person's height and age, first of all these data types, first understand what type of data the C language contains.

(1) Data type classification

① the base data type. Integer, floating-point, character-type

② the constructed type. Arrays, enumerations

③ pointer type.

④ the empty type. void

3. Basic data types

Char character type contains: ' A~z ' a~z ' 0~9 ' stored characters

int shaping types include: 1 2 3 4 5 6 7 8 9 0 etc. store integer

Float Float type contains: Number 1.2 2.5 3.6 Store Decimal

Short shorter integer contains: Number 1 2 3 4 5 6 7 8 9 etc. store integer

Long integer contains: Number 1 2 3 4 5 6 7 8 9 etc. store integer

Double-precision floating-point type contains: Number 1.1 2.5 3.9 and so on storing decimals

(1) Data storage units

① the storage unit. Bytes byte, most commonly used storage unit, BITS bits, minimum storage unit.

② 1 bytes = 8 bits (bits) = 2 bits (hex bit)

(2) different data types storage space, storage space determines the data type can store the range of data

①char 1 Short 2 int 4 long 8 float 4 Double 8

The storage space of the ② data type is not uniform with respect to the operating system. But there is a principle: short <= int <= long <= long Long

The ③sizeof operator calculates that each data type occupies storage space.

④ storage space determines the range of data stored. Do not memorize, you can check the information, the most commonly used to remember. Overflow: The value range of the data type is exceeded

(3) The integral type distinguishes between signed and unsigned, unsigned identifiers unsigned.

(4) Distinguish positive/negative: integer corresponding to the binary number, the highest bit 1 is positive, the highest bit 0 is negative.

(5) How to store integers: complement. Positive complement: Self, negative complement: absolute value, negation, plus 1.

(6) How the floating-point number is stored: exponent. Standard exponential form: integer is 0, first decimal number is not 0.

printf ("%lu\n", sizeof (char)); 1

printf ("%lu\n", sizeof (short)); 2

printf ("%lu\n", sizeof (int)); 4

printf ("%lu\n", sizeof (long)); 8

printf ("%lu\n", sizeof (float)); 4

printf ("%lu\n", sizeof (double)); 8

3. Variables, constants

(1) Constants

① amount that cannot be changed during program operation

② Constant classification

A Integer constant: 10

b Real-type constants: 1.5

③ Character constant:

A normal character (refer to the ASCII character descriptor.) Example: ' A ')

The B escape character, which belongs to the action character, cannot be printed. For example: \ n)

④ string constant: "China"

(2) Variables

① The amount that can be modified during a program run

The ② variable represents a storage area for storing data, which is equivalent to a container. The stored content is the value of the variable

③ variables must be defined in three parts before use: type, variable name, initial value

④ Variable name requirements:

Can only consist of numbers, underscores, letters, and cannot start with a number

Cannot use system reserved word int, float, etc.

You cannot reuse a defined variable name

See the name of the idea, the hump named Law Teachername,studentname

⑤ identifiers: Valid characters used to name variables, types, only numbers, letters, underscores

Define a variable three steps even if the initial value is 0, you must also write

Emphasis on encoding specification (space use), syntax (only English characters are used)

int a = 10; (type int variable name a initial value 10)

float B = 1.5;

char c = ' a ';

int m = 0, n = 100; (one row defines multiple variables)

Practice defining several variables

Defines a variable age, which stores ages

int age = 0;

Define a variable C_score, store C language test scores, define a variable oc_score, store OC language test scores

float C_score = 94.5, Oc_score = 89;

Define a variable gender, store gender

char gender = ' F ';

modifying variables

Age = 18;

C_score = 100;

Oc_score = 90;

The code has hints and prompts for content. How to display code hints ESC, confirm code return or tab

Declaring variables does not set initial values, and generally requires defining variables, and special cases are explained

Declare only the problem: the storage area is reused, and if you define the variable without setting the initial value, the result can be 0, possibly the previous value (the plate brush is not brushed)

Variable names must conform to the grammatical requirements, but also to be known. What is good code, people and computers can read

Exercise: Define variables store the name, age, and gender of a teacher, define variables to store a student's name, age, C-language score, gender (Purpose: syntax and rules for defining variables)

*/

#pragma the mark-----------operator------------

/*

Operator

1, the operator contains many kinds: monocular, binocular, trinocular. Single, double, or triple represents the number of variables or constants involved in the operation

2, the priority is different, do not memorize, learn to query the table, remember the most commonly used

3. This lesson: assignment operators, arithmetic operators, compound operators

4, learn to use () to mark the priority of operations, benefits: Avoid errors, code readability is strong

*/

/*

Assignment operator Binocular operator

The value to the left of the lvalue equals argument can only be a variable

Right-valued constants, variables, expressions

int a = 0;

int b = A;

Exercise: Exchange values for two variables example: two cups exchange water, need to use a third cup temporarily installed

int a1 = 10;

int a2 = 20;

int temp = 0; Temporary

Copy the A1 data to Temp

temp = A1;

Copy the A2 data into the A1

a1 = A2;

Copy the data from temp to A2

a2 = temp;

Arithmetic operator Binocular operator +-*/

// +

int B1 = 10;

int b2 = B1 + 20;

int B3 = B1 + b2;

// -

int C1 = 50;

int c2 = c1-10;

int c3 = C1-c2-10;

// *

/Division

1. The divisor cannot be 0

int D1 = 10/0;

2, division sign both sides are integers, the result is also an integer, omit the fractional part

int d2 = 3/2;

printf ("%d\n", D2);

3, division sign both sides are floating-point type, the result is floating-point type

float D3 = 4.2/2.0;

printf ("%f\n", D3);

4, Division sign on both sides of a floating-point type, the result is floating-point type

float d4 = 3/2.0;

float d5 = 3.0/2;

% modulo operator gets remainder

1. The modulo operator must both be integers

int e1 =-5% 3; 5 to 3 modulo, that is, 5 in addition to 3 remainder

printf ("e1 =%d", E1);

*/

2. Remainder < divisor

3. Equation A% b a = b * n + remainder

/*

+ + self-increment operation single-mesh operator

Can only be used for variables

1. + + at the back of the variable

(1) Equivalent i = i + 1

int i = 10;

i++; After statement execution, i = 11

(2) First to participate in the operation, after self-increment

int j = 10;

int k = + j + +;  Equivalent to k = + j; j = j + 1

printf ("%d", k);

2. + + in front of variables

(1) Equivalent i = i + 1

int x = 10;

x + +;

(2) Self-increment first, then participate in operation

int y = 10;

int z = + ++y; equals y = y + 1; z = + y

printf ("%d", z);

*/

/*

Compound operators

// +=

A+=b equivalent to a = a + b

int a = 10;

A + = 5;

printf ("%d", a);

2,-=

*/

#pragma mark-----------expressions and statements------------

/*

1, the expression consists of: constants, variables, operators. The expression has a return value.

2. Statement: the smallest unit of execution of a program, ending

*/

/*

int a = 0;

A = 1;

A = 1 * 5

*/

#pragma mark-----------output function------------

/*

1. Convert the content that needs to be output to a string, and the output is displayed in the console

2. Output content: Normal string, convert value of variable to string

3. function format printf (format string, expression 1, expression 2 ...)

*/

/*

Output normal string format strings using "Output content", no expression Required

Use newline character \ n, line break only implements function, does not display

printf ("Hello world!\n");

printf ("Easy Hui Yun \ n");

The value format string for an output variable consists of a normal character and a conversion character

Different types of variables have corresponding conversion characters

Use the converter placeholder where you want to display the contents of the variable

int age = 18;

printf ("I am 18 years old this year");

printf ("I'm%d this year \ n", age);

int number = 10;

printf ("My school number is:%d\n", No.);

printf ("I am%d this year, school number:%d\n", age, numbers);

float C_score = 95.5;

printf ("C-language score:%f\n", C_score);

char gender = ' F ';

printf ("Gender:%c\n", gender);

Output Format Control

Set the character width

1, the actual length is greater than the set length, according to the actual length display

2, the actual length is less than the set length, can fill the space%4d

3, the actual length is less than the set length, can fill 0%04d

int a = 100;

printf ("%2d\n", a); Fill Space

printf ("%05d\n", a); Complement 0

Left justified-and 0 cannot appear at the same time, 0 is ignored

printf ("%-4d\n", a);

printf ("%-04d\n", a);

Set decimal point to display 6 digits after default

float B = 9.54;

printf ("%f\n", b);

printf ("%.1f\n", b); Rounded

printf ("%2.2f\n", b);

How to output escape characters

printf ("%%\n");

printf ("100%%\n");

FLOAT score = 95.5;

printf ("%.1f%%\n", score);

Supplementary char type is the integer number of a byte when stored, and the characters and numbers are equivalent according to the ASCII code table.

Format characters can be used with each other when output

char x = ' a ';

printf ("Character x =%c\n", x);

printf ("Character x =%d\n", x);

int y = 97;

printf ("Integer y =%c\n", y);

printf ("Integer y =%d\n", y);

Can be used with each other when defined

char m = 98;

int n = ' B ';

printf ("Character m =%c\n", m);

printf ("integral type N =%d\n", n);

*/

#pragma mark-----------Input function------------

/*

1. Store the contents of the console input (a string) in a variable of a data type. Contrary to the printf function.

2. scanf format string, variable address

3. Use: Define variables first

*/

/*

The conversion must be consistent with the variable type or the data store will have problems

int number = 0; Define variables to store the input content

scanf ("%d\n", &number);

printf ("number =%d", number);

Enter float conversion%f,double conversion character%LF

float f1 = 0;

scanf ("%f", &F1);

printf ("F1 =%f", F1);

Double F2 = 0;

scanf ("%lf", &F2);

printf ("F2 =%f", f2);

*/

/*

The content in the format string is maintained with the console input

int a1 = 0;

int a2 = 0;

scanf ("%d,%d", &A1, &A2);

printf ("a1 =%d, a2 =%d", a1, A2);

*/

/*

Recommendation: The format string contains only the conversion character and does not contain other characters, especially \ n. The function of the carriage return is to end the input, but it will match \ n to cause problems with the input operation.

If you think of a hint, you can output a hint before you enter it.

int age = 0;

printf ("Please enter Age:");

scanf ("%d", &age);

printf ("Ages:%d", age);

*/

#pragma mark-----------added------------

/*

Automatic type conversion

Float A = 3 + 1.5;

Forcing type conversions

int b = (int) A;

C Language---Program general form, data type, constant volume, operator, expression, formatted input and output

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.