Different Types Of Operators In C With Example

Our today’s C Programming tutorial for beginners lesson is going to be about the Usage of #define in c and other operators that we have to use while coding with c. The C programming language offers a wide variety of operators that you can use to manipulate data in your programs. Operators are symbols (words) that are used in your code to perform various operations on data. So, Today we are going to learn the different types of operators and their usage in C programming throughout this post.


 

 

Different Types Of Operators In C With Example



 

 

 

 

 

How To Declare A Constant Using #define Function In C:

During programming sometimes we need to use a value more than once throughout the program. Or even a constant value for so many programs. For example, we have to use the value of pii 3.1416 so many times throughout a program. If we define pi as 3.1416 then we can use pi instead of 3.1416 as many times as we want throughout the whole program.

 

Syntax for a #define function is:

#define constant_name value

 

To use a constant function it has same naming rule as for using variable. Now, look at this program:

#include <stdio.h>
#define pi 3.1416
#define ll long long int
int main ()
{
ll area,r=2343524;
area=pi*r*r;
printf(“Area=%lldn”,area);

return 0;
}
Output: Area=17253996246710.

Which is a long long int type data. As you see we define long long int as ll. So, in the main function, we used ll instead of long long int. Also, we used pi instead of 3.1416 as it was defined at the start of the program. You can use anything instead of any large number or even any function.

Now look at this program:

#include <stdio.h>
#define ll long long int
#define sc1(k) scanf(“%lld”,&k)
#define pf(k) printf(“%lldn”,k)
int main ()
{
ll a;
sc1(a);
pf(a*5);

return 0;
}


Input: 5

Output: 25

 

Here you can see we defined sc1 for the use of scanf and pf instead of printf function to take and give output for 1 long long int type variable. In this way, you can make your program easier and fast. Now you will not get so much benefit by using the #define function but you will find it very much useful later. When we need to make a long program #define function will help us to reduce our code length also our code will be easier to understand and also it will be easier to debug. So, from today and in the future programs we will try to use the #define function for making our code easier and short.

 

 

 

 

Different Operators And Operations In C Program:

 

The C programming language offers a wide variety of operators that you can use to manipulate data in your programs. Operators are symbols (words) that are used in your code to perform various operations on data.

There are several different types of operators you can use in C:

-Arithmetic Operators

-Logical Operators

-Bitwise Operators

-Shift/Assign Operator

-Assignment Operations

-Bitwise Logical Functions

Let’s now go through them one by one and show you how to use these operators. These are very useful in C programming for various calculations and comparisons operations on data or numbers.

 

 

 

 

 

 

Arithmetic Operators:

 

Let us, first of all, understand what is the arithmetic operator? What does it do, why would I ever need an arity operator in my program?

 

 

In c program, we can use a normal mathematical operator for mathematical operation. Like sum=a+b, multi=a*b, div=a/b. Also, there were some other operators which we can use for mathematical operations. Look at the following program:

 

#include <stdio.h>
#define ll long long
#define sc(k,p) scanf(“%lld %lld”,&k,&p)
int main ()
{
ll a,b,c,d;
sc(a,b);
c=a;
printf(“c=%lldn”,c);
c+=b;
printf(“c=%lldn”,c);
c*=a;
printf(“c=%lldn”,c);
c++;
printf(“c=%lldn”,c);
++c;
printf(“c=%lldn”,c);
d=a+c++;
printf(“c=%lld, a=%lld and d=%lldn”,c,a,d);
d=++c+a;
printf(“c=%lld, a=%lld and d=%lldn”,c,a,d);

return 0;
}

 

 

For this program suppose a and b are 5 and 10 so the output will be following:

c=5
c=15
c=75
c=76
c=77
c=78, a=5 and d=82
c=79, a=5 and d=84


Here at first, we take two inputs a and b. Then we assign a on c. Now, c contains the value of a. So, c=5.

Here, = is an assignment operator which is used to assign a particular value to a particular variable. Then we used c+=b which is similar to c= c+b. On this, anything written after the = operator will be added with the current value of c. So, c becomes 5+10=15 in this step. Then we used c*=a which is similar to the previous operation. In this step, anything written after the = will be multiplied by the current value of c. So, c becomes 75 in this step. Then we used the ++ operation. ++ means adding 1 with the current value of the particular variable. — will decrease the value of that variable by 1. These are called increment and decrement operators. And these mathematical (+, -, *, /, %) operators are called arithmetic operators.

Here you can see first I used c++ and then ++c. For both cases c increases by the value 1. But there was a basic difference between this ++c and c++.

 

 

Prefix operator:

c++ or c– is an example of a prefix operator. The prefix operator uses the current value of the variable in the operation and then increases or decreases its value by 1. As you can see in the last 2nd line of our output after prefix increment in c  c=78, a=5 but their sum is d=82. That is because at first, c used its current value which is 77 in the operation so d=77+5=82 then it was increased by 1 and become 78.

 

 

 

Post-fix operator:

++c or –c is an example of the postfix operator. This type of operator first increases or decreases the value of the variable by 1 and then uses this in the operation. As you can see our last output is c=79, a=5, and d=84.

Because of the post-fix increment in c variable, it was increased by 1 at first and become 78 to 79 then it was used in the operation. Hence, d=79+5=84.

There was another operator %. This is used to find the remainder of a division. For example:

k=78%5.

k will be 3 as a reminder of 78/5 is 3.

 

Now, look at our last program for today.

 

#include <stdio.h>
int main ()
{
int x=5,y,z;
y=x;
x+=7;
printf(“Value of x is %d and value of y is %d n”,x,y);

return 0;
}
Can you tell me what is the value for x and y in the output? Both of them are 12 right?

But no. Both of them will not be 12. Because y=x is not a mathematical equation. = is used to assign the value of x in y first. But then it has no relationship with x. So, whatever the value of x was changed after that line y will never be changed until we make any change in y. So, in the output x will be 12 but y will be 5.

But if you want to make an expression like y=x. And for any change in x will change the value of y automatically. You can do this using the pointer variable. But don’t think about it now. We will learn about pointers later when will we reach that level.

 
 
 
 
 
 
 
 

Logical Operators With Example In C:

 

There are several types of logical operators available in the C language. These include:

The Bitwise Operator Let us take a closer look at the bitwise operator and see what it really is.

Let us take an example where we will see how the bitwise operator works in practice. Let’s consider the following function:

A == B || C

The above statement says “If A is equal to B or C, then store it as true and if one of them is not equal then store it as false”. We know a true value has 1 set bit while a false value on the other hand has 0 sets of bits. So what I’ve shown you here is a bitwise operator (&&). What this tells the compiler to do, is that if either A or B is “1”, then store it as true and if one of them i.e., B OR C is false, then return zero value on its result because 0-bits indicate falsy condition.

 
 
 
 
 
 
 

Bitwise Logical Functions:

 

Also, we have C language built-in functions for performing bitwise logical operations. Let us create a simple expression:

A & B || 0 xor 1

This means the following in pseudo-code; “If A and not zero or one (1) OR of both bits are true continue with next instruction”

So what actually happens is that compiler looks at each individual sequence of bytes on the right-hand side which form an operand and returns true only if all those bits (bitwise in this example) are the same. This is what we have to use for testing equality of values i.e., “===” operator and doing Bitwise AND, OR etc or making logical decisions based on these tests

The Bitwise Exclusive-OR Operator How about some bit flip operations? Take a look at the following:

Let us take an instance where we would need to flip all the bits OR needs to be true,  AND needs to be false and NOT need a flip. For this, we just swap values of Drive A and B using the bitwise operator “^”

However in x86_64 processors for theoretical purposes, there are 32-bits in each word (network packet). That’s why you always have an extra set bit available except when more than one byte is being manipulated at once with the same source. It was becoming really difficult right? Don’t think about it now if you are a beginner in C programming. Because you will need to know these things in the later stages of your programming career. So, don’t try to overthink this operator type. You have learned enough about operators that are necessary while programming using the C language.

 
 
 
 
 
 
 
 
 
 

What Are The Benefits Of Using A Good Operator In C Programming?

 

Operators are a fundamental part of c programming and play an important role in the language. They allow you to perform complex operations on data within a variable without having to write out the code multiple times. For example, you can use the ++ operator to add two numbers together, or you can use the << operator to shift a number left by one position.
There are a variety of operators that are available in c, and you should familiarize yourself with all of them so that you can use them efficiently. Additionally, using good operators can make your code more readable and easier to understand. They can also make your code more efficient and reliable, as they reduce the number of lines of code that need to be executed. When you use good operators, your code will run faster and be less error-prone. So, next time you are writing code, make sure to consider the operators that are available to you.

 

 

 

 

 

 

 
 

How Do I Know If My Operator Is Correct Or Not?

 

This is a difficult question to answer, as there is no surefire way to know for sure. However, there are a few things that you can do to help determine whether or not your operator is correct. First, make sure that you are using the most up-to-date information. For example, if you are using a linear operator, it is important to update your data every time a new block is created. Second, always use a reputable operator. There are a few quality operators out there, and it is worth your time to research which one is the best for your needs.
Last but not least, be sure to test your operator on a small scale first. This will help you to develop a sense of whether or not it is correct for your data. Once you have determined that your operator is correct, be sure to implement it on a larger scale.

 
 
 
 
 
 
 
 

What Is A Good Way To Determine Which Operators Should Be Used And Which Ones Shouldn’t Be Used In C Programming?

 

There is no one-size-fits-all answer to this question, as the best way to determine which operators should be used and which ones shouldn’t be used in c programming depends on the specific situation. However, some general tips that may be useful include:
– Always use parentheses when using operators such as subtraction, multiplication, and division;
– Avoid using the bitwise operators (&, |, ^, etc.), as they can be tricky to understand and can lead to programming errors;
– When stringing together multiple statements, use semicolons to ensure that the statements are executed one after the other;
– Be sure to read the documentation for each operator to understand its full capabilities.

 
 
 
 
 
It is important to use the most up-to-date information when programming, as well as using a reputable operator. Testing your operator on a small scale before implementing it on a larger scale will help you to avoid programming errors.
 

So, that is it for today. Try to practice as much as you can. Happy coding.

Similar Posts

Leave a Reply

Your email address will not be published. Required fields are marked *