当前位置:网站首页>Review C language I

Review C language I

2022-06-11 22:33:00 yjhklxx

#include<stdio.h>  //define,include It's preprocessing instructions 
int main()   // There can only be one main
{
    printf("hello");// Function call operator ()  printf   Print 
    return 0;
}

Type and size :

char         Character data type         size    1 byte     %c
short        Short                              2         %d
float         Single-precision floating-point   decimal          4         %f   
double     Double precision floating point                   8        %lf       
long         Long integer                              4         %d    
int             plastic                                 4         %d      
long long  Longer integers                      8         %d     

  Local and global variables : In braces {} Inside is a local variable , External is a global variable , If the two names are the same, local priority will be given


#include<stdio.h>
int b=0  // Global variables 
int main()
{
    int a=0  // local variable 
	return 0;
}

Example : Sum up

#include<stdio.h>
int main()
{
	int a;
	int b;
	int sum;                  //scanf It's the input function  
	scanf("%d %d", &a, &b);   // One %d Corresponding to an integer, the first integer is put into a, The second one goes to b
	sum = a + b;
	printf("sum=%d\n", sum);
}

Scope : The names used in a piece of code are not always valid ,
The scope of the code that defines the usability of the name is the scope of the name .
The scope of a local variable : Is the local scope of the variable
overall situation : The whole project
Life cycle : The time period between the creation and destruction of variables
local variable : Into the local range, life begins , Out of range, end of life
Global variables : The life cycle of a program


Constant

#define=100 // Defined constants can be defined internally
#include<stdio.h>
int main()
{
    1 Literal constants
      /*3
      a
      3.14*/
    2 const Modified constant variable
    const int sum=10//sum Is a constant variable called const Modifier cannot be modified
    #define Defined identifier constant
     Enumeration constants   One by one
}
enum Sex Enumerate keywords  
{
     Future values of variables of this enumeration type
    MALE=3 // Assign initial value to
    FEMALE
    SECRET
};

  A string is a string of characters —— A string of characters enclosed in double quotation marks
The string hides a... At the end \0 The characters of
\0 Is the end of string flag
Find the length of string character strlen  Use header file #include<string.h>
\0 No length calculation

#include<stdio.h>
int main()
{
	"hello";
}
// Select statement 
#include<stdio.h>
int main()
{
	int a = 0;
		printf(" Please enter a number ");
		scanf("%d", &a);
		if (a < 60)
			printf(" fail, ");
		else
			printf(" pass ");
		return 0;
}
// Loop statement 
#include<stdio.h>
int main()
{
	int line = 0;
	while (line < 3000)// loop 
	{
		printf(" study hard :%d\n",line);
		line++;
	}
	if (line == 3000)
	{
		printf(" Study hard \n");
	}
	return 0;
}
 
// Arithmetic ordinary sum function 
// Ordinary 
#include<stdio.h>
int main()
{
	int sum1 = 0;
	int sum2 = 0;
	scanf("%d %d", &sum1, &sum2);
	int sum = sum1 + sum2;
	printf("%d\n", sum);
	return 0;
}
// function 
#include<stdio.h>
int Add(int x, int y)
{
	int z = 0;
	z = x + y;
	return z;
}
int main()
{
	int sum1 = 0;
	int sum2 = 0;
	scanf("%d %d", &sum1, &sum2);
	int sum = Add(sum1, sum2);
	printf("%d\n", sum);
	return 0;
}
// Array : A group of elements of the same type 
//10 integer 
#include<stdio.h>
int main()
{          
	int arr[10] = { 1,2,3,4,5,6,7,8,9,10 };
	 Each element has a subscript. Use the subscript if you want to use it 1:arr[0] from 0 Go to... In turn 9  Subscript reference operator []
	 Save characters with char
	 Want to access all 
	int i = 0;
	while (i < 10)
	{
		printf("%d\n", i);
		i++;
	}
	return 0;
}

The operator
Arithmetic operators :+,-,*,/,%(% It's a mold , It's also a surplus )
Shift operator :>>( With move character ),<<( Left shift character )
Bit operators :& Press to locate ,| Press bit or ,^ Bitwise XOR
Assignment operator =,+=,-=,*=,/=,&=,^=,|=,>>=,<<=
int a=2;  a=a+5 This can also be expressed as a+=5 The others are the same
Monocular operators :! Logical anti operation ,- negative ,+ integrity ,& Address fetch ,* Indirect access operators
sizeof The type length of the operands ,~ Bitwise negation of a familiar binary
++,--,( type ) Cast
a+b  + There are two operands , Binocular operator . The unary operator has only one operand
0 Said the false ,1 Said really ,

#include<stdio.h>
int main()
{
	printf("%d",sizeof(int......))//sizeof It is calculated that the big and small brackets can be omitted 
	if (a)                           You can also calculate the size of an array in bytes 
	{
		a For real 
	}
	if (!a)
	{
		a For this 
	}
}

  Integers are stored in memory as complements . An integer has... In binary : Original code ; Inverse code ; Complement code

//~ The header file for is include<stdio.h>
#include<stdio.h>
int main()
{
	int a = 0;
	printf("%d", ~a);// Bitwise inversion converts all the digits in the binary digits ,1 become 0,0 become 1
	return 0;
}

The calculation of negative numbers -1                                  Calculation of positive integers
10000000000000000000000000000001( Original code )    Original code , Inverse code , The complement is the same
11111111111111111111111111111110( Inverse code )    The beginning indicates positive and negative
11111111111111111111111111111111( Complement code )    1 Negative ,0 Being positive

About ++ and --

#include<stdio.h>
int main()
{
	int a = 10;
	int b = ++a;// In front of ++  First ++, After use 
	printf("%d\n", b);//11
	printf("%d\n", a);//11
	return 0;
}
#include<stdio.h>
int main()
{
	int a = 10;
	int b = a++;// After ++   First use , after ++
	printf("%d\n", b);//10
	printf("%d\n", a);//11
	return 0;
}

-- And ++ The same is not written

Type cast

#include<stdio.h>
int main()
{
	int a = (int)3.14;// Cast 
	printf("%d", a);
	return 0;
} 

The operator

                  Greater than     Greater than or equal to     Less than     Less than or equal to     It's not equal to     be equal to               
About operators : >            >=           <           <=             !=          ==
Logical operators :&& Logic and ( It's all true ),|| Logic or ( One for true is true )
Conditional operators ( ternary operators ):?  :               exp1?exp2:exp3
exp1 If set up ,exp2 Calculation , Structure of the entire expression :exp2 Result
exp1 If not ,exp3 Calculation , Structure of the entire expression :exp3 Result

Comma expression : A comma separated string of expressions ; Calculate... From left to right

The result of the entire expression is the last result

#include<stdio.h>
int main()
{
	int a = 0;
	int b = 3;
	int max = 0;
	if (a > b)   No conditional operators 
		max = a;  
	else
 	max = b;
	max = a > b ? a : b;// Use the conditional operator 
	printf("%d", max);
	return 0;
}

 

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

Structural members . ->
c Keywords provided by language , Can't do variable name
auto It's automatic —— Every local variable is auto modification , Basically omitted
extern Is used to declare external symbols
register Register key
static Static   union Consortium   void nothing
Common keywords auto,break,case,char,const,continue,default,do,
if,else,double,enum,extern,flaot,for,goto,long,int,register
return,short,signed,sizeof,static,struct,switch,union
typedef,unsigned,void,volatile,while
typedef The type definition Type redefinition

#include<stdio.h>
typedef unsigned int u_int;
int main()
{
	unsigned int num1 = 100;
	u_int num2 = 100;        // These two are the same 
	return 0;
}

 static Static         // Global variables can be applied in the whole project when setting a global variable in another file extern To declare
1. Modify local variables ;2. Modify global variable ;3. Modify function
Modify local variables and change the life cycle of local variables ( Essentially changes the storage type of variables )
When decorating global variables , The modified variable can only be used inside its own source file. Other source files cannot be used
Global variables can be used in other source files because they have external link properties
By static After modification, it becomes an internal connection attribute. Other source files cannot receive this static global variable
Decorating functions are similar to global variables

#include<stdio.h>
void test()
{
	static int i = 1;// useless static ten 2
	i++;             // It was used 2~11
	printf("%d ", i);
}
int main()
{
	int a = 0;
	while (a < 10)
	{
		test();
		a++;
	}
	return 0;
}

 

//#define Define constants and macros      Preprocessing instruction 
//define Defining symbols ( Constant )
#include<stdio.h>
#define max 1000
int main()
{
	printf("%d", max);
	return 0;
}
//define Defining macro     A macro is a replacement 
#include<stdio.h>
#define Add(x,y) ((x)+(y))
int main()
{                             // nothing 4* When is 11 
	printf("%d", 4*Add(5, 6));//44
	return 0;
}

Initial pointer
The pointer is the address ; Using pointer is to use the address in the pointer

#include<stdio.h>
int main()
{
	int a = 10;//a Allocate space in memory -4 Bytes 
	printf("%p\n", &a);//%p For printing addresses    & To get the address operator 
	int* pa = &a;//pa It's used to store the address , stay c In language pa It's called a pointer variable 
	//* explain pa It's a pointer variable 
	//int explain pa The object of execution is int Type of ; by char When is char type 
	*pa = 20;   //* Dereference operation    *pa It's through pa Find the address in a
	printf("%d", a);
	return 0;
}
#include<stdio.h>       // The size of the pointer is 32 Bit platform is 4 Bytes (byte), stay 64 Bit platform is 8 Bytes (byte)
int main()
{
	printf("%d\n", sizeof(char*));       //4
	printf("%d\n", sizeof(short*));      //4
	printf("%d\n", sizeof(int*));        //4
	printf("%d\n", sizeof(long*));       //4
	printf("%d\n", sizeof(long long*));  //4
	printf("%d\n", sizeof(double*));     //4
	printf("%d\n", sizeof(float*));      //4
	return 0;
}

Structure can make c Language creates new types

#include<stdio.h>
struct stu
{
	char name[20];// Specify size 
	int age;
	double score;
};
int main()
{
	struct stu s = { " the other woman ",20,68.5 };// Structure initialization 
	printf("1:%s %d %lf\n", s.name, s.age, s.score);// Structural variable 、 Member variables 
	struct stu* pa = &s;
	printf("2:%s %d %lf\n", (*pa).name, (*pa).age, (*pa).score);
	printf("3:%s %d %lf\n", pa->name, pa->age, pa->score);
	//1,2,3 The print results of are the same ;2 And 3 Using a pointer 
	return 0;
}

原网站

版权声明
本文为[yjhklxx]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/162/202206112145229079.html