c language learning (Beginner operator 1)

Posted by amir on Tue, 09 Nov 2021 05:54:05 +0100

Operator

Let's first understand the types of operators

Arithmetic operator:

+        -        *        /        %

Let's look at some of these operators

/Operator

#define  _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <string.h>
int main()
{
	int a = 9 / 2;
	printf("%d\n", a);
	return 0;
}

The result is 4, because the integer is used. If we want to calculate the decimal, will we just replace int with float?

Next, let's try

#define  _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <string.h>
int main()
{
	float a = 9 / 2;
	printf("%f\n", a);
	return 0;
}

It is found that the result is 4.000000. Why does this happen? Because two integers perform integer division and calculate 4

If we want to get decimals, we have to change at least one of the two integers to decimal form

#define  _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <string.h>
int main()
{
	float a = 9 / 2.0;
	printf("%f\n", a);
	return 0;
}

The result is 4.500000

Let's look at the% operator again

This operator means modulo (remainder)

#define  _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <string.h>
int main()
{
	int a = 9 % 2;
	printf("%d\n", a);
	return 0;
}

The result is 1, because 9 divided by 2 leaves 1

Shift operators

<<   (shift left operator)       >> (shift right operator)

The move operator moves a binary sequence

#define  _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <string.h>
int main()
{
	int a = 2;
	int b = a << 1;
	printf("%d", b);
	return 0;
}

a=2, because the shift operator moves a binary sequence, and a is identified as an integer, which is 4 bytes and 32 bits

So the binary sequence of a is

00000000|00000000|00000000|00000010

When it moves left once

b is 00000000|00000000|00000000|00000100

So the result of running is 4

The same is true for the shift right operator

Bit operator (learn about it first)

&        ^        |

&Bitwise remainder

^Bitwise XOR

|Bitwise OR

Assignment operator

=        +=        -=        *=        /=        &=        ^=        |=        >>=        <<=

Let's see what a few operators mean

such as

a=a+5 can be written as a+=5

b=b-6 can be written as b-=6


unary operator

!         Logical reverse operation

-         negative

+         positive

&         Get address

sizeof         Gets the type length of the operand in bytes

~         Bitwise negation of a number

--         Front and rear--

++           Front and rear++

*          Indirect access operator (dereference operator)

(type)         Cast type

What is a monocular operator

a+b

+There are two operands on the left and right sides of, so the a-bit binocular operator

+ A has only one operand on the right, which is a unary operator at this time

As we can see, an operator with only one operand is a unary operator

Let's take a look!, In c language, 0 is false and non-0 bit is true

And! Will turn false into true, true into false

#define  _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <string.h>
int main()
{
	int a = 5;
	printf("%d", !a);
	return 0;
}

So the result of the run is 0

	int a = 0;

If int a=5; Change to the above code, then the running result is 1

Because true defaults to 1

Let's look at the sizeof operator again

#define  _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <string.h>
int main()
{
	int a = 10;
	printf("%d\n", sizeof (int));
	printf("%d\n", sizeof a);
	return 0;
}

The parentheses after sizeof(a) can be omitted because sizeof is not a function, but sizeof(int) cannot be omitted because the syntax does not support it

 

Topics: C Back-end