Operators and their types in C language - ProgrammerTech
Language:

Search

Operators and their types in C language

  • Share this:
Operators and their types in C language

Operators

Operators are special symbols that are used in arithmetic operations such as addition, subtraction, division, and logical operations that are performed on variables. There are several types of operators, namely:

  • Ordinary arithmetic operators such as (addition, subtraction, multiplication, division, remainder of division...etc).
  • Logical operators depend on relational operators in their results and have their own symbols.
  • Relational operators depend on the comparison between one value and another value, where the result is either true (true) or false (false).

In the second logical operator of the previous points, the output is 1 if the answer is correct and 0 if it is false, depending on the table of true and false for each operator, and is used to form a conditional sentence such as.

if(c==s && d==b)

In this line influential && to set the condition of equality between two parties.

 

plural effect in C language

The addition operator takes the (+) symbol and its task is to combine numbers or texts in case we want to combine texts.

#include<stdio.h>
main()
{
int a , b , c;
a=1;
b=4;
c=a+b;
printf("%d",c);
}

In this code, I declare three variables, the first is a and I give it a value of 1, the second is b and I give it a value of 4, and c is the sum of a+b. It will add the first variable with the second and store their result in the third variable and the sum is equal to 5.

 

C language increase effect

The increment operator is denoted by the symbol (++) and it means to increment one number 1 to the variable we want to increment to, for example if you give the variable an initial value of 1 and tell it to increase its value every time and print it for me as in the following example.

#include<stdio.h>
main()
{
int cs;
cs=1;
printf("cs = %d\n",cs);
cs++;
printf("cs = %d\n",cs);
++cs;
printf("cs = %d\n",cs);
}

I declared a variable named cs and gave the variable a value of 1 and every time we told it to increment the value and then print. When you run the code, the result will be:

cs = 1
cs = 2
cs = 3

 

Subtraction operator in C language

The subtraction operator is denoted by the symbol (-), and it is to subtract two numbers from each other within an arithmetic operation.

#include<stdio.h>
main()
{
int a, b, c;
a=10;
b=4;
c=a-b;
printf("%d",c);
}

In this code, I declared three variables, the first a, and I gave it a value of 10, and the second is b, and I gave it a value of 4. As for c, it is the result of subtracting a-b, it will subtract the first variable from the second and store its result in the third variable and the summation is equal to 5.

 

declension effect in C language

To decrease, we use the operator (--) which means decreasing one number 1 to the variable we want to decrease from. For example, if you give the variable an initial value of 1 and tell it each time, decrement from its variable value and print it for me as in the following example.

#include<stdio.h>
main(){
int cs;
cs=3;
printf("cs = %d\n",cs);
cs--;
printf("cs = %d\n",cs);
--cs;
printf("cs = %d\n",cs);
}

I declared a variable named cs and gave the variable a value of 3, and every time it was transferred to it, we decreased the value and then printed. When you run the code, the result will be:

cs = 3
cs = 2
cs = 1

 

The operator of the remainder of the division in C language

Means to find the remainder of the division between the two numbers and the symbol known to it is (%).

#include<stdio.h>
main()
{
printf("%d\n",5%3);
printf("%d\n",3%5);
}

Execution, the quotient in the first print sentence is 2 and in the second it is 3.

 

Relational effects in C language

Relational operators are based on a comparison between one value and another value, where the result is either true or false, one if the result is true and zero if it is false.

#include<stdio.h>
main(){
printf("%d\n",4<3);
printf("%d\n",2==3);
printf("%d\n",7>3);
}

And the output will be

0
0
1

 

Logical operators in C language

Logical operators depend in their result on their own symbols, the output is 1 if the answer is correct and 0 if it is false depending on the table of true and false for each operator.

#include<stdio.h>
main()
{
printf("%d\n", 5<3 && 5>3);
printf("%d\n", 5==3 && 5==3);
printf("%d\n", 5>3 && 5<3);
printf("%d\n", 5<3 || 5>3 );
printf("%d\n", 5<3 || 5!=3);
printf("%d\n", 5<3 || 5>3);
printf("%d\n", !5<3);
printf("%d\n", !5<=3);
printf("%d\n", !5<5);

And when you run the code, we will get the result.

0
0
0
1
1
1
1
1
1

 

C language comparison operators

Comparison Operators, whose use is either True or False, can only accept these two possibilities. We will explain each of these operations in C language with an explanation of their meaning and how to use them.

 

Comparative operations symbols in C

In the C language, there are six Comparison Operators, through which two values ​​can be compared with each other so that the result of the comparison is either true or false. And all these operations take place between two numerical values, and these operations are:

  1. The equal sign ( == ).
  2. The inequality sign ( =! ).
  3. greater than sign ( < ).
  4. Less than sign ( > ).
  5. The greater than or equal to sign ( =< ).
  6. Less than or equal to sign ( => ).

For equality ==, its result is True only if both cases are exactly equal. Otherwise, the result is False.

5 == 4 //False
5 == 5 //True

Inequality =! Its result will be False only if both values ​​are exactly equal. Otherwise, the result is True, that is, it is completely opposite to the principle of the equality process as follows.

5 != 4 //False
5 != 5 //True

For the comparison operation is greater than <, its result is true if the first value from the left side is greater than the second value from the right side.

8 > 2 //True
8 > 8 //False

This is the main difference between the greater than and greater than or equal to =< operations, where the result will be True when using the greater than or equal to operation.

8 >= 8 //True

Integer because 8 equals 8.

For the comparison operations less than > and less than or equal to => it is similar to the principle of greater than and greater or equal to, but it is opposite to it. An operation less than its result will be True if the first value on the left is less than the second value on the right, and an operation => whose result is True if the first value is less than the second value or they are equal.

What do we benefit from the product of comparison operations and how do we use it?

Comparisons can be used in conditional sentences such as the if clause that we learned about in the previous lessons, as well. In C, there is a special data type called bool, which stands for boolean, where this type can be either True or False. We can define a bool data variable just the same way we define any other variable like (int, float).

Example

#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
int main(int argc, char** argv)
{
    int x = 1;
    int y = 5;
    bool var = x == y;
    printf("x==y ? %d",var);
    return 0;
}

We created two variables, x equals 1 and another variable, y equals 5, then we created a bool variable named var and assigned the value x==y to it, then we printed the var. Its result is:

x == y ? 0

We note that it printed for us the value 0, meaning False, which is that 1 does not equal the 5, but if we put the variable x the value 5 and the variable y remains the same value 5, then this command will be achieved and print the number 1 that is True as in the figure.\

x == y ? 1

Tags:
Saja Alsadig

Saja Alsadig

من السودان مبرمجة مواقع ويب و تطبيقات أندرويد اتقن لغات "html, css, php, js" بالإضافة للتعامل مع اندرويد ستوديو و بناء التطبيقات, مهتمة بمجال التقنية منذ الصغر و كان لدي شقف كبير للدخول بالجامعه بقسم البرمجة و دخلته في عام 2013 و تعلمت كل شيئ ضمن الجامعة و خارجها من التعليم الذاتي و اتمنى أن أكون عند حسن ظن الجميع و إيصال الأمانة لكم, اعمل حاليا في إدارة موقع جامعة السودان..