The first week of self-study summary, the first week of self-study

Source: Internet
Author: User

The first week of self-study summary, the first week of self-study

In this week, we started to review the basic knowledge we had just learned.

1. Create the first simple C Language

/* This is first program! */(/* Annotator, content output is not displayed)

# Include "stdio. h" // preprocessing command

Int main () // main function, which must be unique

{

Printf ("My first program! "); // Standard input/output functions

Printf ("\ nHello wrld! "); // \ N, escape sequence, line feed

Return 0

}

Note: When you get started with the C language, be sure to pay attention to punctuation marks. The most common mistake is the incorrect write or incorrect Chinese and English Punctuation Marks.

For example, output the following code to see what will happen.

# Include <stdio. h>

Int main ()

{

Printf ("Hi there! \ N \ nThis progarm is a bit ");

Printf ("longer than the others .");

Printf ("\ nBut really it's only more text. \ n \ a"); // \

Printf ("Hey wait a minute !! What was that ?? \ N ");

Printf ("\ t1. \ tA brid? \ N ");

Printf ("\ t2. \ tA plane \ n ");

Printf ("\ t3. \ tA control character? \ N ");

Printf ("\ n \ t \ B \ bAnd how will this look when it prints out? \ N ");

Return 0;

}

II. C Language Input and Output

Input: raw data is sent to the computer through the input device

Output: sends the computing results stored in the memory to the output device.

CThe language itself does not provide input and output statements. The input and output operations are implemented by function calls. To complete this operation, CThe language compilation system provides input and output functions.

 First, we will learn the standard input and output functions:

2.1.Standard Output Functions

(1) Name: standard output function (Header file stdio. h).

(2Function: outputs some data to a standard device in a certain format.

(3) Format:

 Form 1: printf("Format control string "[,Output list]);

 

 Form 2: printf ("a = % d B = % d",, B );

 

2.2.Standard Input Function

1) Name: format input function (header file stdio. h).

(2Function: receives data from the standard input device in the specified format.

(3) Format: scanf ("Format control string", Address list );

For example, the input diameter is used to calculate the perimeter of a round table.

# Include "stdio. h"

Int main ()

{

Flot radius = 0.0f; // defines the radius. Adding f to the surface is the initial value of the float type. Without f, It is the double type.

Float diamrte = 0.0f; // define the diameter

Float circumference = 0.0f; // defines the area.

Float pi = 3.14159265f; // circumference Rate

Printf ("input the diamrter of the table:"); // the system prompts the input information.

Scanf ("% f", & diameter); // &, // addressing Operator

Radius = diameter/2.0f; // calculate the radius

Circumference = 2.0f * pi * radius; // calculate the perimeter

Area = pi * radius; // calculated area

Printf ("\ nThe circumference is %. 2f", circumference); //. 2f, retain two decimal places

Printf ("\ nThe area is %. 2f \ n", area ):

Return 0;

}

3. Select Structure

If statement

Format:

(1) if (logical expression) // condition judgment

{

Execution Block

}

(2) if (logical expression)

{

Execution Block

}

Else if (logical expression)

{

Execution Block

}

Else if (logical expression)

{

Execution Block

}

......

(3) if (logical expression)

{

Execution Block

}

Else

{

Execution Block

}

For example, enter a number ranging from 1 to 10 to determine the size of the number.

# Include "stdio. h"

Void main ()

{

Int number = 0;

Printf ("Enter an integer between 0 and 10 :");

Sanf ("% d", & number );

If (number> 10)

Printf ("warning !! ");

Else if (number> 5)

Printf ("The number % d you entered is greater than 5 \ n", number );

Else if (number <6)

Printf ("The number % d you entered is smaller than 6 \ n", number );

}

Switch statement

Format

Switch (integer expression)

{

Case constant_expression_1:

Statements_1;

Break;

.....

Case constant expression_n:

Break;

Default:

Break;

}

The siwitch statement allows you to select an action from a group of actions based on the results of an integer expression.

Example:

# Include "stdio. h"
Void main ()
{
Int number;
Printf ("enter a number less than 100 :");
Scanf ("% d", & number );
If (number> 100)
Printf ("input error !! ");
Else
Switch (number)
{
Case 35:
Printf ("Incredible! You actually won the first prize! ");
Break;
Case 97:
Printf ("You are lucky to have won the second prize! ");
Break;
Case 78:
Printf ("Congratulations, you have won the third prize! ");
Break;
Default:
Printf ("sorry, no award! ");
Break;
}
}

Iv. Loop Structure

For Loop

Format: for (expression 1; expression 2; expression 3)

{

Loop body

}

There are three expressions in brackets. The first expression is executed only once at the beginning of the loop.

It declares the loop variable count and initializes it to 1. The first expression is a loop condition to determine whether to loop,

It must be a logical expression. The third expression is used to conveniently end the loop by changing the value of the loop variable.

Example: Draw a box

# Include "stdio. h"

Void main ()

{

Int count;

Printf ("\ n ****************");

For (count = 1; count <= 8; ++ count)

Printf ("\ n **");

Printf ("\ n ****************** \ n ");

}

While loop:

While (expression)

{Statement1;

Statement2 ;}

In this loop statement, a group of statements are repeatedly executed as long as a logical expression is equal to true.

Do-while loop

Different from the first two loops, the test loop ends at the end of the loop to determine whether to continue. Therefore, the loop will be executed at least once.

Do

{

/* Statements for the loop body */

}

While (expression );

Learning progress bar:

 

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.