C language description

Posted by Thethug on Sun, 24 Oct 2021 11:05:43 +0200

1, What is C language

2, The first C language program

3, Data type

4, Variable, constant

5, String + escape character + comment

6, Select statement

7, Circular statement

8, Functions

9, Array

10, Operator

11, Common keywords

12, define defines constants and macros

13, Pointer

14, Structure

1, What is C language

Computer language: C, C + +, JAVA, Python

C language is a common computer language! It is the language that people communicate with computers.

C language is a general computer programming language, which is widely used in Bottom development. With good cross platform characteristics, C language programs written in a standard can be compiled on many computer platforms.

C language is a computer programming language for computing process. Its main compilers are: Clang, GCC, WIN-TC, SUBLIME, MSVC, Turbo C, etc.

C language:

    1. C language is an underlying language

    2. C language is a small language

    3. C language is an inclusive language

Advantages of C language: high efficiency, portability, powerful function, flexibility, standard library and integration with UNIX system.

Disadvantages of C language: C language is easier to hide errors, C programs may be difficult to understand, and C programs may be difficult to modify.

2, The first C language program

Steps:

1. Create a new project

 

 Create a. c file under the source file

Local Windows debugger shortcut: Ctrl+Fn+F5

2. Write code

#Include < stdio. H > / / stdio --- > standard input / output standard input / output library
int main()
{
	printf("hello world\n");//The printf print function requires an application header file when used. "" is the printed content, \ n line feed.
	return 0;
}

3, Data type

1. Type:

char                           Character data type

short (int)                   Short

int                             plastic

long (int)                   Long integer

long long (int)           Longer integer

float                           Single-precision floating-point

double                       Double precision floating point number

2. Size

sizeof()     Is the size of the type, in bytes. One byte is equal to eight bit s.

#include <stdio.h>
int main()
{
	printf("%d\n", sizeof(char));
	printf("%d\n", sizeof(short));
	printf("%d\n", sizeof(int));
	printf("%d\n", sizeof(long));
	printf("%d\n", sizeof(long long));
	printf("%d\n", sizeof(float));
	printf("%d\n", sizeof(double));
	return 0;
}

        

#include <stdio.h>
int main()
{
	char ch = 'w';
	short a = 1;
	int b = 2;
	long c = 3;
	long long d = 4;
	float e = 0.2f;
	double f = 0.22;
	printf("%c\n", ch);//%C --- > printed characters
	printf("%d\n", a);//%D --- > Print integers in decimal form
	printf("%d\n", b);
	printf("%d\n", c);
	printf("%d\n", d);
	printf("%f\n", e);//%F ----- > Print floating point numbers in decimal form
	printf("%f\n", f);
	return 0;
}

4, Variable, constant

Variable:

1. Variables are divided into local variables and global variables.

Local variable: generally, the variables defined in the function are called local variables, which can only be inside the function.

Global variables: generally, variables defined in the global scope, that is, variables outside the function, are called global variables.

#include <stdio.h>
int weight = 500;//Variables defined outside the function -- > global variables
int main()
{
	int age = 30;//Variables defined in the function ----- > local variables
	printf("%d\n", weight);
	printf("%d\n", age);
	return 0;
}

2. Scope and lifecycle of variables

The scope of a local variable is the local range of the variable.

The scope of the global variable is: the whole project.

The life cycle of a local variable is the beginning of the scope life cycle and the end of the scope life cycle.

The life cycle of a global variable is the life cycle of the entire program.

#include <stdio.h>
int age = 20;/
int main()
{
	int build = 3;
	{
		int bleed = 20;//The life cycle of the variable bled is within {} and cannot be printed outside {}.
	}
	printf("%d\n", age);
	printf("%d\n", build);
	printf("%d\n", bleed);
	return 0;
}

#include <stdio.h>
int age = 20;//age's life cycle is the whole program, so it can also be printed there.
int main()
{
	int build = 3;//It can be printed within another brace corresponding to the same position as the previous brace
	{
		printf("%d\n", age);
		printf("%d\n", build);
	}
	printf("%d\n", age);
	printf("%d\n", build);
	return 0;
}

#include <stdio.h>
int a = 100;
int main()
{
	int a = 10;
	printf("%d\n", a);//When local variables are the same as global variables, local variables take precedence.
	return 0;
}

#include <stdio.h>
int a = 100;
int main()
{
	printf("%d\n", a);//When the local variable is behind the printf function and the local variable is consistent with the global variable, the global variable takes precedence.
	int a = 200;
	return 0;
}

 

Use of variables: (calculate the sum of two numbers)

#include <stdio.h>
int main()
{
	int num1 = 0;
	int num2 = 0;
	int sum = 0;
	scanf("%d %d", &num1, &num2);
	sum = num1 + num2;
	printf("%d\n", sum);
	return 0;
}

  Constant:

1. Constants are divided into literal constants, const modified constant variables, #define defined identifier constants and enumeration constants

Literal constants: numbers, 0, 1, 2

Const modified constant variable: const can modify a variable into a constant so that the variable will not be changed, but its essence is still a constant.

#include <stdio.h>
int main()
{
	const int a = 30;
	printf("a = %d\n", a);
	a = 50;
	printf("a = %d\n", a);

	return 0;
}

​
#include <stdio.h>
int main()
{
	int a = 30;
	
	printf("a = %d\n", a);
	a = 50;
	printf("a = %d\n", a);
	return 0;
}

​

 

#include <stdio.h>
int main()
{
	const int a = 4;
	char arr[4] = {'a','b','c','\0'};
	printf("%s\n", arr);
	return 0;
}

 

#include <stdio.h>
int main()
{
	const int a = 4;
	char arr[a] = { 'a','b','c','\0' };
	printf("%s\n", arr);
	return 0;
}

#Identifier constant defined by define: equal to constant.

#include <stdio.h>
#define MALE 50
int main()
{
	printf("%d\n", MALE);
	char arr1[4] = { 'a','b','c','\0' };
	char arr2[MALE] = { 'a','c','e','\0' };
	printf("%s\n", arr1);
	printf("%s\n", arr2);
	return 0;
}

  Enumeration constants: list them one by one.

#include <stdio.h>
enum Sex
{
	MALE,
	FEMALE,
	SECRET
};
int main()
{
	printf("%d\n", MALE);
	printf("%d\n", FEMALE);
	printf("%d\n", SECRET);
	return 0;

}

#include <stdio.h>
enum Month
{
	JANUARY,
	FEBRUARY,
	MARCH,
	APRIL,
	MAY,
	JUNE,
	JULY,
	AUGUST,
	SEPTEMBER,
	OCTOBER,
	NOVEMBER,
	DECEMBER
};
int main()
{
	printf("%d\n", JANUARY);
	printf("%d\n", FEBRUARY);
	printf("%d\n", MARCH);
	printf("%d\n", APRIL);
	printf("%d\n", MAY);
	printf("%d\n", JUNE);
	printf("%d\n", JULY);
	printf("%d\n", AUGUST);
	printf("%d\n", SEPTEMBER);
	printf("%d\n", OCTOBER);
	printf("%d\n", NOVEMBER);
	printf("%d\n", DECEMBER);
	return 0;
}

  5, String + escape character + comment 1

character string:

Strings are enclosed in double quotes and characters are enclosed in single quotes. The end flag of the string is \ 0.

1)

#include <stdio.h>
int main()
{
	printf("hello world\n");
	char a = 'k';
	printf("%c\n",a);
	return 0;
}

 2)

#include <stdio.h>
int main()
{
	char arr1[] = "hello";
	char arr2[] = { 'h','e','l','l','o' };
	char arr3[] = { 'h','e','l','l','o','\0' };
	printf("%s\n", arr1);
	printf("%s\n", arr2);
	printf("%s\n", arr3);
	return 0;
}

 

  Escape character

 \?-------------> Used when writing consecutive question marks to prevent them from being parsed into three letter words

      ??) Some compilers will be compiled into], and adding \ can prevent it from being parsed into].

\'----------------> used to represent character constants'

\"----------------> used to represent the double quotation marks inside a string (the double quotation marks form a pair with the front)

\--------------->Used to indicate a backslash to prevent it from being interpreted as an escape sequence character

\a -- -- > warning character, beep

\B ------------------ > backspace

\F ------------------- > feed character

\N ------------------ > line feed

\R ------------------- > Enter

\T ------------------- > horizontal tab

\V ------------------- > vertical tab

\DDD ------------------ > DDD represents one to three octal digits

\XDD ------------------ > DD represents two hexadecimal digits

#include <stdio.h>
int main()
{
	printf("\'\n");
	printf("\"\n");
	printf("\\n\n");
	printf("hello\bword\n");
	printf("hello\fword\n");
	printf("hello\rword\n");
	printf("hello\tword\n");
	printf("hello\vword\n");
	printf("\326\n");
	printf("\xa5\n");
	return 0;
}

  notes

C language annotation method: 1)//   2)/*       */

6, Select statement

if statement and switch statement

if statement:

1. if (expression) statement

2. if (expression) {multiple statements}

3. if statement else statement

4,

if (expression)

sentence  

else if (expression)

sentence

...

else 

sentence

5. Suspended else   Else matches the nearest if

switch statement:

switch (expression)

{           case constant expressions: statements

           ...

            case constant expressions: statements

            default: statement

}

7, Circular statement

While statement: while (expression) statement

//Write a square table
#include <stdio.h>
int main()
{
	int i, n = 0;
	printf("This program prints a table of squares.\n");
	printf("Enter number of entries in table: ");
	scanf("%d", &n);
	i = 1;
	while (i <= n)
	{
		printf("%10d%10d\n", i, i * i);
		i++;
	}
	return 0;
}

Do statement: do statement while (expression)

#include <stdio.h>
int main()
{
	int i = 10;
	do
	{
		printf("T times %d and counting\n",i);
		--i;
	} while (i > 0);
	return 0;
}

  For statement: for (expression 1; expression 2; expression 3) statement

#include <stdio.h>
int main()
{
	int i = 10;
	for (i = 10; i > 0; --i)
		printf("T times %d and counting\n", i);
	return 0;
}

8, Functions

Functions: functions are the building blocks of C language. Each function is essentially a small program with its own declarations and statements. Functions can be divided into small blocks to facilitate people to understand and modify the program.

Function is characterized by simplified code and repeated code.

#include <stdio.h>
int Add(int x, int y)
{
	int z = x + y;
	return z;
}
int main()
{
	int num1, num2, sum = 0;
	printf("Enter two function values:");
	scanf("%d %d", &num1, &num2);
	sum = Add(num1, num2);
	printf("%d\n", sum);
	return 0;
}

#include <stdio.h>
double average(double a, double b)
{
	return (a + b) / 2;
}
int main()
{
	double x, y, z = 0.00;
	printf("Enter three numbers: ");
	scanf("%lf %lf %lf", &x, &y, &z);
	printf("Average of %g and %g : %g\n", x, y, average(x, y));
	printf("Average of %g and %g : %g\n", x, z, average(x, z));
	printf("Average of %g and %g : %g\n", y, z, average(y, z));
	return 0;
}

 

  9, Array

Array: an array is a data structure containing multiple data values, and each data value has the same data type. These data values are called elements. They can be selected one by one according to the position of the elements in the array. An array is a collection of elements of the same type.

Subscript of array:

#include <stdio.h>
int main()
{
	int i = 0;
	int arr[10] = { 1,2,3,4,5,6,7,8,9,10 };
	while (i < 10)
	{
		printf("%d\n", arr[i]);
		i++;
	}
	printf("%d\n", arr[4]);
	return 0;
}

 

  10, Operator

Arithmetic operator: + -*   / (take quotient, the integer division result is an integer, there is a decimal on both sides, and the result is a decimal)   % (remainder \ modulo, this operator can only be used for integers)

#include <stdio.h>
int main()
{
	int num1, num2, sum, a, d, e = 0;
    scanf("%d %d", &num1, &num2);
	sum = num1 + num2;
	a = num1 - num2;
	d = num1 / num2;
	e = num1 % num2;
	printf("%d\n%d\n%d\n%d\n", sum, a, d, e);
	return 0;
}

Shift operator: < < (left shift, in binary form)      >> (right shift, in binary form)

#include <stdio.h>
int main()
{
	int a = 3;     //a:00000000000000000000000000000011  
	int b = a >> 1;//b: 00000000000000000000000000000000000001 move one bit to the right
	int c = a << 1;//c: 0000000000001000000110 move one bit to the left
	printf("b:%d\nc:%d\n", b, c);
	return 0;
}

  Bitwise operators:

&: in bitwise and binary form, 0 is 0, and both are one and one)

|: (by bit or in binary form, one is one, both are 0 and 0)  

^: (bitwise exclusive or, in binary form, & the same is 0, and the different is one)   

#include <stdio.h>
int main()
{
	int a = 3;       //00000000 00000000 00000000 00000011
	int b = 6;       //00000000 00000000 00000000 00000110
	int c, d, e = 0; 
	c = a & b;       //00000000 00000000 00000000 00000010  2
	d = a | b;       //00000000 00000000 00000000 00000111  7
	e = a ^ b;       //00000000 00000000 00000000 00000101  5
	printf("c=%d\nd=%d\ne=%d\n", c, d, e);
	return 0;
}

  Assignment operator:=    +=   -=   *=   /=   &=    ^=    |=    >>=    <<=

Unary operator (only one operand):

! (logic is negative, 0 is false and 1 is true)

-    +    &    sizeof   

~(inverse by bit, binary form. Negative integer has original code, inverse code and complement in binary. Original code 1 becomes 0, 0 becomes 1 becomes inverse code, inverse code + 1 is complement, and complement takes inverse)

--   (front)--   First -- use later, set later -- use first and then --)    

++   (front)++   First + + then use, then set++   Use first and then + +)     

#include <stdio.h>
int main()
{
	int num1 = 10;
	int num2 = 10;
	int num3 = 10;
	int num4 = 10;
	int b, c, d, e = 0;
	b = ++num1;
	c = num2++;
	d = --num3;
	e = num4--;
	printf("b=%d\nc=%d\nd=%d\ne=%d\n", b, c, d, e);
	return;
}

*     (type)    

Relational operator: >     >=    <    <=    !=    ==

#include <stdio.h>
int main()
{
	int a, b, MAX = 0;
	scanf("%d %d", &a, &b);
	if (a > b)
		printf("MAX = %d\n", a);
	else
		printf("MAX = %d\n", b);
	return 0;
}

  Logical operators: & & (logical and)       || (logical or)

#include <stdio.h>
int main()
{
	int a, b = 0;
	scanf("%d %d",&a, &b);
	if (a > 10 && b > 20)
		printf("abc");
	else
		printf("def");
	return 0;
}

#include <stdio.h>
int main()
{
	int a, b = 0;
	scanf("%d %d",&a, &b);
	if (a > 10 || b > 20)
		printf("abc");
	else
		printf("def");
	return 0;
}

 

  Conditional operator (ternary operator):   exp1 ? exp2 : exp3         (Exp1: expression. Exp2: output when the result is true. EXP3: output when the result is false)      

#include <stdio.h>
int main()
{
	int a, b, MAX = 0;
	scanf("%d %d", &a, &b);
	MAX = (a > b ? a : b);
	printf("%d\n", MAX);
	return 0;
}

  Comma expression:   Exp1, exp2, EXP3,... Expn (the comma expression is read from left to right, and the result is the last calculated result)

#include <stdio.h>
int main()
{
	int a = 2;
	int b = 3;
	int c = 4;
	int d = (b = a + 2, c = b++, a = c * b);// b = 4, c = 5, a = 20;
	printf("%d\n", d);
	return 0;
}

  Subscript references, function calls, and structure members: []     ()     `      ->

11, Common keywords         

Auto (auto, auto zero)                     break (terminate, in loop)                             case    

Continue (for loop, continue)           const (constant attribute, modified variable, modified pointer)       char      Default (switch) case statement   (default)   do                                                                   double         enum (enumeration)                                 extern (declare external symbol)                               float                       for                                                     goto                                                               if                           int                                                     long                                                               while                       Register (register)                           Return (return function)                                   short               signed                             sizeof                                                           static     struck (structure)                             switch                                                           else                     typedef (type redefinition)                     union (Consortium)                                           volatile                     unsigned                       void (no, empty)                        

#include <stdio.h>
typedef unsigned int u_int;//Use u_int to represent the undefined int typedef type redefinition
int main()
{
	unsigned int a = 100;
	u_int b = 100;
	printf("%d\n%d\n", a, b);
	return 0;
}

#include <stdio.h>
int main()
{
	register int a = 10;//It is recommended to put a in the register. I don't know whether to put it in the register. The address of the register variable cannot be taken
	return 0;
}

static   Modify local variables, global variables and functions.

When modifying a local variable, convert the local variable from the stack area to the static area and change the type of the variable, so that the static local variable will not be destroyed when it is out of its scope, which is equivalent to changing the life cycle.

#include <stdio.h>
void test()
{
	int a = 1;
	a++;
	printf("%d\n", a);
}


int main()
{
	int i = 0;
	while (i < 10)
	{
		test();
		i++;
	}
	return 0;
}

#include <stdio.h>
void test()
{
	static int a = 1;
	a++;
	printf("%d\n", a);
}
int main()
{
	int i = 0;
	while (i < 10)
	{
		test();
		i++;
	}
	return 0;
}

  When static modifies a global variable: a global variable can only be selected in the source file and cannot be used across files; when a global variable is modified by static, the external link of the variable becomes an internal attribute, so that the variable can only be used in its own source file.

  Modifying a function is the same as modifying a global variable

 

  12, define defines constants and macros

Define constants:

#include <stdio.h>
#define M 100
#define STR "hehe" / / the string is enclosed in double quotes
int main()
{
	printf("%d\n", M);
	printf("%s\n", STR);
	return 0;
}

  Defining macros: Custom uppercase

#include <stdio.h>
#define ADD(x,y) ((x) + (y))
int main()
{
	int a, b = 0;
	scanf("%d %d", &a, &b);
	printf("%d\n", ADD(a, b));
	return 0;
}

 

  13, Pointer

Most computers divide the memory into bytes. Each byte can store 8 bits of information. Each byte has a unique address to distinguish it from other bytes in the memory.

Memory is an important memory in the computer. The operation of computer programs is carried out in memory. Therefore, in order to use memory effectively, the memory is divided into small memory units. The size of each memory unit is 1 byte. In order to effectively access each unit of memory, the memory is numbered. These numbers are called the address of memory.

The 32-bit operating system is 4G. In the 32-bit operating system, the pointer variable size is 4 bytes. The 64 bit operating system is 8G, and the 64 bit operating system pointer variable size is 8 bytes.

x86 is 32-bit and x64 is 64 bit.

#include <stdio.h>
int main()
{
	int a = 10;
	int* p = &a;//&Take the address operator, P is used to store the address, so p is the governing variable.
	*p = 30;//*Dereference operator, find the geological variable stored in p, and assign the value of 30 to it
	printf("%p\n", &a);//%p print address form
	printf("%d\n", a);
	return 0;
}

 

14, Structure

Structures are used to describe something complex.

#include <stdio.h>
struct stu
{
	char name[20];
	int age;
	char sex[10];
};
int main()
{
	struct stu stu1 = { "Student 1", 20, "male" };
	struct stu stu2 = { "Student 2", 30, "female" };
	struct stu stu3 = { "Student 3", 15, "secrecy" };
	struct stu* p1 = &stu1;
	struct stu* p2 = &stu2;
	struct stu* p3 = &stu3;
	printf("%s %d %s\n", stu1.name, stu1.age, stu1.sex);
	printf("%s %d %s\n", (*p1).name, (*p1).age, (*p1).sex);
	printf("%s %d %s\n", p1->name, p1->age, p1->sex);
	printf("%s %d %s\n", stu2.name, stu2.age, stu2.sex);
	printf("%s %d %s\n", (*p2).name, (*p2).age, (*p2).sex);
	printf("%s %d %s\n", p2->name, p2->age, p2->sex);
	printf("%s %d %s\n", stu3.name, stu3.age, stu3.sex);
	printf("%s %d %s\n", (*p3).name, (*p3).age, (*p3).sex);
	printf("%s %d %s\n", p3->name, p3->age, p3->sex);//->Dereference operator
	return 0;

}

 

Topics: C C++ C#