Introduction to C language learning

This article continues to introduce operators

)

6. Monocular operator

6.1 introduction to monocular operators

Operatorfunction
!Logical reverse operation
-negative
+positive
&Take address
sizeofThe type length of the operand in bytes
~The binary bit negation of a number
- -Front, rear --
++Front, rear++
*Indirect access operator
(type)Cast type

Code demonstration:

#include<stdio.h>
int main()
{
	int a = -10;
	int* p = NULL;
	printf("%d\n", !2);
	printf("%d\n", !0);
	a = -a;
	p = &a;
	printf("%d\n",sizeof(a));
	printf("%d\n", sizeof a);
	printf("%d\n", sizeof(int));
	//printf("%d\n", sizeof int); report errors
	return 0;
}

Operation results:

It can be seen that the length of int type is 4 bytes

Code demonstration:

#include<stdio.h>
int main()
{
	int a = -10;
	printf("%d\n", sizeof a);
	printf("%d\n", sizeof int);//report errors
	return 0;
}

Note: if an error is reported on the seventh line, the expression printf ('% d \ n', sizeof (int)); should be entered;, Only a single character can be entered without parentheses

6.2 sizeof and array

Code demonstration:

#include <stdio.h>
int main()
{
	int a = 0;
	int arr[3] = {1,2,3};
	char str[] = "hello";
    int length=sizeof(arr)/sizeof(int);
	int len_a = sizeof(a);
	int len_arr = sizeof(arr);
	int len_str = sizeof(str);
	printf("len_a=%d\n",len_a);
	printf("len_arr=%d\n", len_arr);
	printf("len_str=%d\n", len_str);
	printf("length=%d\n",length);
	return 0;
}

Operation results:

It can be seen that: array length = memory space occupied by the array / space occupied by a single element

6.3 + + and -- operators

Pre + + and --

Code demonstration:

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

Operation results:

analysis:
In int x = + A; In, first self increment a, and then use a, that is, the value of the expression is the value after the self increment of A. similarly, int y = --a; In, a is reduced first and then used

Post + + and --

Use a first and then add. That is, in the above example, int x = + A; In, first use a = 0, and then increase a = 1, that is, x = 0, y = 1

7. Relational operators

namefunction
>
>=
<
<=
!=Used to test "inequality"
==Used to test "equal"

Note: in the process of programming = = and = are written incorrectly, resulting in errors

8. Logical operators

&&Logic and
||Logical or
Logical and: if one of the two values is false, it is judged to be false
Logical or: if one of the two values is true, it is judged to be true

Distinguish logical and bitwise AND
Distinguish between logical or and bitwise OR

1&2----->0
1&&2---->1
1|2----->3
1||2---->1

9. Conditional operator

exp1 ? exp2 : exp3

Code demonstration:

#include <stdio.h>
int main()
{ 
	int a = 10;
	int b = 0;
	int c = 0;
	if (a > b)
		b = 3;
	else
		b = -3;
	c = (a > b ? 3 : -3);
	printf("%d %d\n", b, c);
	return 0;
}

Operation results:

analysis:
exp1 ? exp2 : exp3
If expression 1 is true, then expression 2 is evaluated, and the result of expression 2 is the result of the whole formula
If expression 1 is false, expression 3 is evaluated, and the result of expression 3 is the result of the whole formula
Conditional operators are more concise than conditional statements

10. Comma expression

exp1, exp2, exp3, ...expN

Comma expressions are multiple expressions separated by commas

#include <stdio.h>
int main()
{ 
	int a = 1; 
	int b = 2; 
	int c = (a > b, a = b + 10, a, b = a + 1);//comma expression 
	printf("%d\n", c);
	return 0;
}

Operation results:

Analysis: comma expression is executed from left to right. The result of the whole expression is the result of the last expression

11. Subscript references, function calls and structure members

11.1 subscript reference

[] subscript reference operator operand: an array name + an index value

int arr[10]; Create array
arr[9] =10; Practical subscript reference operator
The two operands of [] are arr and 9.

11.2 function call

() function call operator

Code demonstration:

#include <stdio.h>
void test1() 
{ 
	printf("haha\n");
} 
void test2(const char* str) 
{
	printf("%s\n", str); 
}
int main()
{ 
	test1();
	//Utility () as a function call operator. 
	test2("hello world!");
	//Utility () as a function call operator. 
	return 0;
}

Operation results:

Analysis: accept one or more operands: the first operand is the function name, and the remaining operands are the parameters passed to the function.

11.3 accessing members of a structure

Access method:

. structure member name
->Structure pointer - > member name

Code demonstration:

#include <stdio.h>
//student
struct Stu
{
	//member
	char name[20];
	int age;
	char sex[10];
	char tele[12];
};
void print(struct Stu* ps)
{
	printf("%s %d %s %s\n", (*ps).name, (*ps).age, (*ps).sex, (*ps).tele);
	//->
	//Structure pointer variable - > member name
	printf("%s %d %s %s\n", ps->name, ps->age, ps->sex, ps->tele);
}

int main()
{
	struct Stu s = {"zhangsan", 20, "nan", "15596668862"};
		//Structure object member name
	printf("%s %d %s %s\n", s.name, s.age, s.sex, s.tele);
	print(&s);
	return 0;
}

Operation results:

summary

This concludes the introduction of operators in these two issues. Thank you very much for pointing out any wrong comments!! Finally, if this article is helpful to you, please connect it three times. Thank you for your support.

Tags: C Visual Studio Code

Posted by kiwiwilliam on Fri, 15 Apr 2022 01:17:59 +0930