Simple point - c-Lesson5 (structure, branch statement (if, switch), difference between p and * p)

1. Structure

(1) A structure definition is a collection that describes different data types or the same data type. (complex type)
(2) Access structure: pointer access structure (- >); Variable name access structure (.)
(3) The difference between p and * p depends on whether it is a left value or a right value.

#if 1 
#include <stdio.h>
#include <windows.h>

#define SIZE 20

//Define a structure
struct Stu{
	char name[SIZE];
	int age;
	char sex[SIZE];
	char telephone[SIZE];
};
int main()
{
	//Access structure
	//1. Variable name access (struct Stu is structure type)
	struct Stu Tom = {"tom",12,"male","1234567"};
	printf("%s %d %s %s\n", Tom.name, Tom.age, Tom.sex, Tom.telephone);

	//2. Pointer access (struct Stu * is the structure pointer type)
	struct Stu* ps = NULL;//When you don't know where the pointer points, let it point to null. If you don't point to it, you will point randomly and generate random values, that is, wild pointers.
	ps = &Tom;
	printf("%s %d %s %s\n", (*ps).name, (*ps).age, (*ps).sex, (*ps).telephone);

	struct Stu* p = &Tom;
	printf("%s %d %s %s\n", p->name, p->age, p->sex, p->telephone);
	system("pause");
	return 0;
}
#endif



2. Branch statement if statement

(1) A semicolon in C language; Separated is a statement.
(2) if select statement:
a.if (expression) {statement;} Single choice 1
b.if (expression) {statement;} else {statement;} One out of two
c.if (expression) {statement;} else if (expression) {statement;} else {statement;} Multiple choice 1
Note: 1 Judging from top to bottom, if the above one is true, the latter one will not be implemented.
2. The expression must be true. In c language, 0 is false and non-0 is true. Prevent logic error, 1 == flag; flag1 is not recommended. If it is written as =, an error will occur.
3. {} encloses a code block, and the internal variables are local variables.
(3) Judgment of two parallel if conditions (judgment of each condition)
if (expression) {statement;}
if (expression) {statement;}
(4) Hanging else: the principle of proximity is adopted to solve this problem. That is to write code in a standardized way. The {} is {}. First, the code style and second, the logic are relatively clear.
(5) Example: A. judge whether a number is odd
b. Output odd number between 1-100

2.1 single choice 1

#if 0
#pragma warning(disable:4996)
#include <stdio.h>
#include <windows.h>
int main()
{
	int age = 0;
	scanf("%d", &age);
	if(age<18)
	{
		printf("under age\n");
	}

	system("pause");
	return 0;
}
#endif

2.2 alternative 1

#if 0
#pragma warning(disable:4996)
#include <stdio.h>
#include <windows.h>
int main()
{
	int age = 0;
	scanf("%d", &age);
	if(age<18)
	{
		printf("under age\n");
	}
	else
	{
		printf("adult\n");
	}

	system("pause");
	return 0;
}
#endif

2.3 multiple choice 1

#if 0
#pragma warning(disable:4996)
#include <stdio.h>
#include <windows.h>
int main()
{
	int age = 0;
	scanf("%d", &age);
	if(age<18)
	{
		printf("juvenile\n");
	}
	else if(age>=18 && age<30)//Else if (age < 30) -- this kind of judgment is from top to bottom. At this step, it means that age > = 18 has been reached. This follows the principle of closing left and opening right.
	{
		printf("youth\n");
	}
	else if(age>=30 && age<50)//else if(age<50)
	{
		printf("middle age\n");
	}
	else if(age>=50 && age<80)//else if(age<80)
	{
		printf("old age\n");
	}
	else
	{
		printf("Old age never dies\n");
	}
	system("pause");
	return 0;
}
#endif

2.4 parallel if

#if 0
#include <stdio.h>
#include <windows.h>
int main()
{
	int flag= 1;
	if(1 == flag)
	{
		printf("xixi\n");
	}
	if(1 == flag%2)
	{
		printf("haha\n");
	}
	//The result is xixi, haha, because both conditions are true.
	system("pause");
	return 0;
}
#endif

2.5 suspend else and match if nearby. The solution is to write {}.

#if 0
#include <stdio.h>
#include <windows.h>
int main()
{
	//Write {} in the following code to output haha
	int a = 0;
	int b = 2;
	if(a == 1)
	{
		if(b == 2)
		{
			printf("hehe\n");
		}
	}
	else
	{
		printf("haha\n");
	}

	//In the following code, you think you can output hehe, but you don't output anything, because else matches the second if.
	/*int a = 0;
	int b = 2;
	if(1 == a)
		if(2 == b)
			printf("hehe\n");
	else
		printf("haha\n");*/

	system("pause");
	return 0;
}
#endif

2.6 judge whether a number is odd

#if 0
#include <stdio.h>
#include <windows.h>

int IsJi(int _x)
{
	//0 is not an odd number, 1 is an odd number
	//return _x%2 == 1 ? 1:0;
	//return _x%2 == 1;
	//return _x%2;

	/*understand:
		3: 011 --- 2^1+2^0
		4: 100 --- 2^2
		10:1010 --- 2^3 + 2^1
		11:1011 --- 2^3 + 2^1 + 2^0 --- 2*( 2^2 + 2^0) + 2^0
		As can be seen from the above, to judge whether a number is odd, you only need to judge whether the afternoon bit is 1. If it is 1, it is odd, and if it is 0, it is even.
		eg:11
		00000000 00000000 00000000 00000001
		00000000 00000000 00000000 00001011 &
		-----------------------------------
		00000000 00000000 00000000 00000001 Odd number
	*/
	        
	return _x&1;
}

int main()
{
	int x = 0;
	printf("Please enter a number:");
	scanf("%d",&x);
	int ret = IsJi(x);
	if(ret)
	{
		printf("It's an odd number\n");
	}
	else
	{
		printf("Not odd\n");
	}
	system("pause");
	return 0;
}
#endif

2.7 output odd numbers between 1-100

#if 0
#include <stdio.h>
#include <windows.h>

int IsJi(int _x)
{
	//0 is not an odd number, 1 is an odd number
	//return _x%2 == 1 ? 1:0;
	//return _x%2 == 1;
	//return _x%2;

	/*understand:
		3: 011 --- 2^1+2^0
		4: 100 --- 2^2
		10:1010 --- 2^3 + 2^1
		11:1011 --- 2^3 + 2^1 + 2^0 --- 2*( 2^2 + 2^0) + 2^0
		As can be seen from the above, to judge whether a number is odd, you only need to judge whether the afternoon bit is 1. If it is 1, it is odd, and if it is 0, it is even.
		eg:11
		00000000 00000000 00000000 00000001
		00000000 00000000 00000000 00001011 &
		-----------------------------------
		00000000 00000000 00000000 00000001 Odd number
	*/
	        
	return _x&1;
}

int main()
{
	int start = 0;
	int end = 0;
	printf("Please enter start and end: ");
	scanf("%d%d",&start,&end);
	for(; start < end; start++)
	{
		if(IsJi(start))
		{
			printf("%d ",start);
		}
	}
	printf("\n");
	system("pause");
	return 0;
}
#endif

3. Branch statement switch... case:break; default:break

(1)definition:
	a.Single branch:
		switch((expression){case Constants: statements; break;case Constants: statements; break;...case Constants: statements; break;default: sentence; break;}
	b.Multi branch:
		switch((expression){case Constant: case Constant:...case Constants: statements; break;default: sentence; break;}
(2)matters needing attention:
	a.case It has judgment function without branch function; if Statements are conditions+judge+Branch.
	  case Must be followed by an integer constant expression; The type can be: int,short,char,long,longlong
	  case The expression does not require order.
	b.break It realizes the branch function;
	  break The actual effect of the statement is to divide the statement list into different parts.
	c.default Realize the functions that are not matched;
	  When switch The value of the expression does not match all case When the value of the tag default Clause is executed.
	  each switch Only one statement can appear default Clause;
	  Can appear anywhere in the statement list.
(3)Example look break Importance of (interview questions):
	a.break Just jumped out break If there is a cycle outside, it needs to continue.
	b.switch Allow nested use

3.1 single branch

#if 0
#include <stdio.h>
#include <windows.h>

int main()
{
	
	int day = 0;
	printf("Please enter week:");
	scanf("%d",&day);
	switch(day)
	{
	case 1:
		printf("Monday\n");
		break;
	case 2:
		printf("Tuesday\n");
		break;
	case 3:
		printf("Wednesday n");
		break;
	case 4:
		printf("Thursday\n");
		break;
	case 5:
		printf("Friday\n");
		break;
	case 6:
		printf("Saturday\n");
		break;
	case 7:
		printf("Week 7\n");
		break;
	default:
		printf("Input error, please re-enter\n");
		break;

	}
	system("pause");
	return 0;
}
#endif

Multi branch

#if 0
#include <stdio.h>
#include <windows.h>

int main()
{
	
	int day = 0;
	printf("Please enter week:");
	scanf("%d",&day);
	switch(day)
	{
	case 1:
	case 2:
	case 3:
	case 4:
	case 5:
		printf("bring a false charge against an innocent person\n");
		break;
	case 6:
	case 7:
		printf("weekend\n");
		break;
	default:
		printf("Input error, please re-enter\n");
		break;
	}
	system("pause");
	return 0;
}
#endif

3.3 see the importance of break with examples

#if 0
#include <stdio.h>
#include <windows.h>
int main()
{
	int n = 1;
	int m = 2;
	switch (n) //1
	{
	case 1:  
		m++;  //3
	case 2:
		n++;
	case 3:
		switch (n) //1
		{
		//switch allows nested use
		case 1:
			n++; //2
		case 2:
			m++; //4
			n++; //3
			break;
		}
	case 4:
		m++;//5
		break;
	default:
		break;
	}
	printf("m = %d, n = %d\n", m, n);//5 3
	system("pause");
	return 0;
}
#endif

4. Other small knowledge points

1.strlen(); It is used to obtain the effective length of the string content. The end tag '\ 0' is not included.
sizeof(): contains \ 0 and gets the space length.

2. Array initialization can be performed without specifying the array size.
eg: int arr[] = {1,2,3,4,5};
Note: variables can only be stored in: [].
3.c language keywords: Keywords in C language are C language identifiers with specific meanings and special purposes, also known as reserved words.
Define is not a keyword, but implemented by the compiler. It is used to define the preprocessing instructions of macros, not the content of c language.
Note: # the beginning is pretreatment.
4. The pointer is the address; Pointer variables are variables used to store addresses.
5. The variable modified by const is a constant variable, and its value cannot be changed.

#if 0
#include <stdio.h>
#include <windows.h>
int main()
{
	
	char arr[] = {'a','b','c'};
	char arr1[] ="abc";
	char arr2[] = {'a','b','c','\0'};
	printf("%d %d %d\n",strlen(arr),strlen(arr1),strlen(arr2));//Random value 3
	printf("%d %d %d\n",sizeof(arr),sizeof(arr1),sizeof(arr2));//3 4  4
	system("pause");
	return 0;
}
#endif

5. The difference between p and * p depends on whether it is a left value or a right value.

Tags: C C++ programming language

Posted by heminfotech on Mon, 16 May 2022 23:33:28 +0300