"C Language Elementary" Chapter 2 - Getting to Know C Language (2)

Getting to Know C Language Part 2


select statement

If you study hard, you will find a good offer. If you don't study hard, go home and sell sweet potatoes. This is the select statement.

选择语句类型

  • Single branch: if statement, else statement;
  • Multi-branch: if elseif else statement, switch case statement;

If the expression in the if evaluates to true, the if statement is executed, otherwise the else statement is executed.

Here I use ifto achieve:

#include<stdio.h>
int main()
{
    
    
	int i = 0;
	printf("今天你学习了吗?1 or 0\n");
	scanf("%d", &i);
	if (i == 1)
		printf("good offer!\n");
	else
		printf("回家卖红薯!\n");
	return 0;
}

loop statement

A loop statement means doing the same job repeatedly
循环语句类型:

  • for statement
  • while statement
  • do while statement

The three looping statements have their own advantages in different scenarios.
Example:
Xiao Ming is determined to learn an algorithm question every day, and when he writes a hundred questions, he will post a circle of friends

#include<stdio.h>
int main()
{
    
    
	int count = 0;
	while (count<100)
	{
    
    
		count++;
	}
	printf("%d\n发朋友圈", count);
	return 0;
}

function

The function is 可以实现某种功能的模块
the function of the function: it is used to simplify the code, realize the reuse of the code, and realize the specific function eg: calculator
Example: realize the function of addition

#include<stdio.h>
int sum(int x, int y)
{
    
    
	return x + y;
}
int main()
{
    
    
	int a = 0, b = 0, c = 0;
	printf("please input two members:>");
	scanf("%d%d", &a, &b);
	c = sum(a, b);
	printf("c=%d\n", c);
	return 0;
}

array

Array: is used to store a set of elements of the same type

array definition

int arr[10]={
    
    1,2,3,4,5,6,7,8,9,10};

subscript of the array

Each element of the array has a subscript, and the subscript 从0开始can be used to locate its corresponding value
insert image description here
If we want to get the fifth element in the array, we can do this

int arr[10]={
    
    1,2,3,4,5,6,7,8,9,10};
printf("%d\n",arr[4]);

print array

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

operator

  • Arithmetic operators
    +, -, *, /, %

  • Shift operators
    << (left shift), >> (right shift)

  • Bitwise operators
    & (bitwise and), | (bitwise or), ^ (bitwise exclusive or)

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

  • unary operator
    insert image description here

  • relational operator
    insert image description here

  • Logical Operators
    && Logical And
    || Logical Or

  • Conditional operator
    exp1?exp2:exp3

  • comma expression
    exp1,exp2,exp3,...expN

  • Subscript reference, function call and structure member variable access operators
    [], (), ., ->


common keywords

insert image description here


define constants and macros

//define定义标识符常量
#define MAX 1000
//define定义宏
#define ADD(x, y) ((x)+(y))
#include <stdio.h>
int main()
{
    
    
    int sum = ADD(2, 3);
    printf("sum = %d\n", sum);

    sum = 10 * ADD(2, 3);
    printf("sum = %d\n", sum);

    return 0;
}

pointer

Variables are created in memory (space is allocated in memory), and each memory unit has an address, so variables also have addresses.
Save the address of a variable into a new variable, this new variable is called指针变量
insert image description here

structure

Structures can describe complex types in C language, such as students: name + age + gender + student number, but using a single type to describe requires many types of definitions, while structures can:

//结构体类型的定义
struct Stu
{
    
    
    char name[20];//名字
    int age;      //年龄
    char sex[5];  //性别
    char id[15]//学号
};

So far, the first knowledge of C language is over. In the next article, I will continue to share each knowledge point in detail and look forward to the next issue~~

Guess you like

Origin blog.csdn.net/hsjsiwkwm/article/details/130907815