Basic C Language Learning 04

Source: Internet
Author: User
Tags arithmetic goto sleep function

=============================================================================
The knowledge points involved are: three structures of the program, conditional branching statements, cyclic statements, the and of natural numbers,
Ask for the number of daffodils between 100 and 999, the output of a isosceles triangle on the screen, the Loop Statement case-window movement
=============================================================================
Review:
(Note that constants are also of a type!) )
For example:

#define MAX 100 Description The definition of Max is a signed int
Const long A = 100U; Description The definition of a is an unsigned long
"A" string constant equivalent to ' a '
' A ' character constant, character constant can only be an ASCII character

Int4 a byte, 2 Word, 1 DWORD
The C language cannot directly write binary, substituting 8 and 16 (and the default decimal).

int a = 100;
int b = sizeof (a);
  The sizeof keyword is used to get the size that a data type occupies in memory (the memory size is not negative), in bytes:
sizeof is not a function, so it does not need to contain any header files
Actually, sizeof. The type of the return value is the size_t,size_t type is an unsigned integer under the 32-bit operating system.
In a 32-bit system, sizeof returns a value of unsigned int, with%u; under 64-bit systems, unsigned long, with%lu.
For example:
Under 32-bit systems:
int a = 100;
printf ("%u\n", sizeof (a));
Under 64-bit systems:
int a = 100;
printf ("%lu\n", sizeof (a));
-----------------------------------------------------------------------------
int A; equal to signed int A;
unsigned int A;

Long is 4 bytes under a 32-bit system, what is the 64-bit system? Windows is 4 bytes, and Linux is 8 bytes.

200f equivalent to 200.0
-----------------------------------------------------------------------------
int a = ' a ';//a = 97, can be stored
Char a = 500;//is not stored, overflow, Max put 127

Where is the difference between ' 2 ' and ' 2 '?
' 2 ' is the character 2dASCII
2 is an integer 2

Where is the difference between "100" and 100?
' 1 ' 0 ' 0 '
100 is an integer constant of type int
-----------------------------------------------------------------------------
int a = a;//the wrong wording!

char a = 97;
printf ("%d\n", a);//output result is 97
printf ("%c\n", a);//output is character a

Abc
BCD (This is actually a carriage return + line break)

Abc
BCD (line break only, no carriage return)

BCD (ABC is a BCD overlap, this is called carriage return)

The origin of the ENTER key:
Keyboard:
--------------------------------------------------Print head
------------------------
-         -
Enter
-         -
------------------------
Press ENTER on the typewriter and the print head will be reset

=============================================================================
Three kinds of structure of the program:
1, sequential structure
2, Branch structure
3, Cycle structure
=============================================================================
Conditional branching statements:
1, relational operators: in C language 0 for false, not 0 for true.
Relational operators have:< (less than), > (greater than), <= (less than equals), >= (greater than or equal), = = (equals),! = (Not equal)
-----------------------------------------------------------------|-----------------------------
The first four priority levels are the same as those of the last two

0 off
1 true
34 true
-34 true, not 0 is true

Conditional operator: (condition)? Value 1: Value 2
When the condition is true, the value of the expression is 1, and when the condition is not true, the value of the expression is 2.
Many times a question mark is used instead of a simple if statement.
Example: an example of an absolute value
int i =-8;
int x = (i < 0)? -I:I;
-----------------------------------------------------------------------------
2, logical operator:&& (logic and/and), | | (logical OR/or),! (Logical non-/not)
-----------------------------------------|---------------------
Binocular operator (both sides must have an expression called binocular) Monocular operator
-----------------------------------------------------------------------------
3, 1) Single-branch statement
If is a conditional branching statement: If the condition is true, the code in the code block is executed.
if (condition)
{
code block;
}
-----------------------------------------------------------------------------
2) Dual-branch statements
If else is a two-branch statement:
if (condition)
{
code block 1;
}
Else
{
code block 2;
}
-----------------------------------------------------------------------------
3) Multiple branch statements (multiple IF): else always paired with the most recent if (Special note)
if (condition 1)
{
code block 1;
}
else if (condition 2)
{
code block 2;
}
else if (condition 3)
{
code block 3;
}
......
Else
{
code block 3;
}

Examples are as follows: arithmetic
#include <stdio.h>

Int main ()
{
int A;
int b;
Char C;
printf ("Please input A:");
scanf ("%d", &a);
printf ("Please input B:");
scanf ("%d", &b);
printf ("Please input C:");  
GetChar ();//Call scanf when the user enters the ENTER key to eat
C = GetChar ();
  printf ("A =%d, B =%d, c =%c\n", A, B, c);//Print results test under
if (c = = ' + ')
{
printf ("%d\n", A + b);
  
Else if (c = = '-')
{
printf ("%d\n", A-B);
  
Else if (c = = ' * ')
{
printf ("%d\n", A * b);
  
Else if (c = = '/')
{
if (b! = 0)
printf ("%f\n", (double) A/b);
  else
printf ("error\n");
  
Else
{
printf ("Input error\n");
}
return 0;
}
-----------------------------------------------------------------------------
4) Multiple branch statements (    Multiple selections: Switch and break,default)
Switch (volume)
{
Case value 1:
Statement 1;
Break    
Case Value 2:
Statement 2;
Break
...

......

......
Case Value N:
Statement N;
Break
Default
Statement When all the case is not satisfied, execute the statement under Default.
}

Examples are as follows: arithmetic
#include <stdio.h>

int main ()
{
int A;
int b;
char c;
printf ("Please input A:");
scanf ("%d", &a);
printf ("Please input B:");
scanf ("%d", &b);
printf ("Please input C:");
GetChar ();//Call scanf when the user enters the ENTER key to eat
c = GetChar ();
printf ("A =%d, B =%d, c =%c\n", A, B, c);//Print results test
Switch (c)
{
Case ' + ':
printf ("%d\n", A + B);
Break
Case '-':
printf ("%d\n", A-B);
Break
Case ' * ':
printf ("%d\n", A * b);
Break
Case '/':
if (b! = 0)
printf ("%f\n", (double) A/b);
Else
printf ("error\n");
Break
Default
printf ("Input error\n");
}
return 0;
}

Note: Use if when the conditions are particularly complex, when the conditions are simple, but the branches are many, use switch.
-----------------------------------------------------------------------------
If, switch and? (question mark) are conditional branch statements;
Unconditional Branching statement: Goto

Goto end;//unconditionally jumps to the label end to execute the code
...
...
end://Marking

However, a large number of Goto is not advocated, unless using Goto can greatly simplify the amount of code.

=============================================================================
Loop statement:

while (condition)
{
code block;
}
-----------------------------------------------------------------------------
Continue statements
When the loop encounters the continue statement, it no longer executes continue the code below, but instead returns to the Loop start statement to continue the execution loop.
(That is, when the loop encounters continue, it goes directly to the place where the condition is to begin to be re-judged)
-----------------------------------------------------------------------------
Break statement
When the loop encounters a break statement, the loop terminates immediately and the loop ends.
-----------------------------------------------------------------------------
Do While

Do
{
Statement
}while (conditions);
-----------------------------------------------------------------------------
While is to determine the condition before deciding whether to loop
Does while looping before judging the condition

While the while may not loop at all, but does while looping at least once.

Attention:
while (A! = 10); The sentence corresponds to while (a! = 10)
{
;
}
The equivalent of a dead loop, nothing is done.
=============================================================================
Small exercise: The and of natural numbers.
The user can enter a positive integer arbitrarily, and the number of all natural numbers from 1 to the integer is calculated.
Example: Enter 5
1 + 2 + 3 + 4 + 5 = 15

The code under VS2017 is as follows:
#include <stdio.h>
#pragma warning (disable:4996)

int main ()
{
int sum = 0;//is used to hold variables that calculate the sum of the natural numbers
int a = 10;
scanf ("%d", &a);
while (a > 0)
{
sum + = A;
a--;
}
printf ("%d\n", sum);
return 0;
}
-----------------------------------------------------------------------------
Class Assignment: Finding the number of digits in an integer
Arbitrary input An integer, assuming the user entered 123, the program runs output hundred
If the user enters 2896, the program outputs thousands of
If the user enters 10000, the output

The code under VS2017 is as follows:
#include <stdio.h>
#pragma warning (disable:4996)

int main ()
{
int a = 0;
scanf ("%d", &a);
int sum = 0;//sum is a counter

while (a > 0) the% in//c language is the fetch operation,/is the rounding operation
{
sum++;
A/= 10;
}

Switch (SUM)
{
Case 1:
printf ("a \ n");
Break
Case 2:
printf ("10 \ n");
Break
Case 3:
printf ("Hundred \ \");
Break
}
return 0;
}
-----------------------------------------------------------------------------
Small exercise: Ask for the number of daffodils between 100 and 999.
What is the number of daffodils?
A number of hundred cubic + 10 bit cubic + bits of cubic = this number itself
For example: 371 is a number of daffodils
3 * 3 * 3 + 7 * 7 * 7 + 1 * 1 * 1 = 371

The code under VS2017 is as follows:
#include <stdio.h>

int main ()
{
int A;
for (a = n; a <; a++)
{
int a1;//Hundred
int a2;//10 bit
int a3;//Bits

A1 = a/100% 10;//a/100=5;5%10=5 Note: The precedence of/and% is the same as that of the binding from left to right
a2 = a/10% 10;//a/10=53;53%10=3
a3 = a/1% 10;//a/1=537;537%10=7

if ((A1 * A1 * A1 + A2 * A2 * A2 + A3 * a3 * a3) = = a)
printf ("%d\n", a);
}
return 0;
}
-----------------------------------------------------------------------------
99 Multiplication Table

The code under VS2017 is as follows:
#include <stdio.h>
int main ()
{
int A;
int b;
for (a = 1; a < a++)//outer loop, control line, i.e. outer loop determines how many rows
{
for (b = 1; b <= A; b++)//inner loop, control column, that is, inner loop determines each row has several columns, outer loop once per cycle, Inner loop loop 4 times
{
printf ("%d\t", A * b);
}
printf ("\ n");
}
return 0;
}
-----------------------------------------------------------------------------
Job: Output a isosceles triangle on the screen
-------*-------
------***------
-----*****-----
----*******----
---*********---
--***********--
-*************-
***************
You cannot print output isosceles triangle a line with printf
printf ("*\n");
printf ("***\n");
printf ("*****\n");

Analysis:
The relationship between the * and line numbers of each line is: line number * 2-1
The relationship between the * and minus signs of each line is: line number-1; line number-2; line number-3; Line number-line number

The code under VS2017 is as follows:
#include <stdio.h>
#pragma warning (disable:4996)

int main ()
{
int A;
int b;
int num = 0;
scanf ("%d", &num);
for (a = 1; a <= num; a++)//outer loop, control line, i.e. outer loop determines how many rows
{
for (b = 0; b < num-a; b++)
{
printf ("-");
}
for (b = 0; b < A * 2-1; b++)//inner loop, control column, that is, inner loop determines the number of each row, outer loop once per cycle, internal loop cycle num Times
{
printf ("*");
}
for (b = 0; b < num-a; b++)
{
printf ("-");
}
printf ("\ n");
}
return 0;
}
=============================================================================
Loop statement Case-window movement

The code under VS2017 is as follows:

#include <stdio.h>

#include <windows.h>//Use the window to move, you need to take advantage of the API function, you need to include a header file

int main ()
{
int x = 100;
int y = 100;
int status = 0; 0 represents a left-to-right run, and 1 means running from right to left.
int status1 = 0; 0 means running from top to bottom, and 1 for running up and down.
int status2 = 1; 0 for the x-axis, 1 for the y-axis not moving.

while (1)
{
SetWindowPos (HWND) 0x00830968, NULL, X, Y, 300, 200, 0);//This function can change the position and size of a window

if (status = = 0 && Status2 = = 1)
x + = 10;

if (x >= 1000)
{
Status1 = 0;
STATUS2 = 0;
}
if (Status1 = = 0 && Status2 = = 0)
Y + = 10;

if (y >= 800)
{
status = 1;
STATUS2 = 1;
}
if (status = = 1 && status2 = = 1)
X-= 10;

if (x <=)
{
Status1 = 1;
STATUS2 = 0;
}
if (Status1 = = 1 && status2 = = 0)
y = ten;
    
if (y <=)
{
Status = 0;
STATUS2 = 1;  
}
//if (status = = 0 && Status2 = = 1)
//x + = ten;
The function of the//sleep function is the program sleep, the parameter unit is milliseconds, the 1000ms is 1 seconds
}
/*
The parameters of the function are explained as follows:
The first parameter: is the handle to the window. Handle: Is a concept of Windows, that is, all windows have a unique number, this number is called a handle. (Note that the handle is 16 binary) How does the
get a handle to a window? In vs2017/Tools Options/spy++ (spy means)/Find window/Finder Tool/Drag to corresponding window
second parameter: default write null.
Third parameter: is the x-coordinate of the window position.
Fourth parameter: is the y-coordinate of the window position.
Fifth parameter: is the window width.
Sixth parameter: is the height of the window.
Seventh parameter: default write 0.

(0,0) Upper left corner
------------------------------X-Axis
|
|
|
|y Shaft

*/
return 0;
}

=============================================================================

Basic C Language Learning 04

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.