17 Oct Operators in C Programming
Operators perform operations by taking one or more values, to give another value. These operations are performed on variables and values. In this lesson, we will learn about operators in C Programming.
For example:
1 2 3 |
a + b |
The types of operators are explained here with an example, where a = 5, b =10. The following are the operators in C Programming:
- Arithmetic Operators
- Assignment Operators
- Arithmetic Assignment Operators
- Relational/ Comparison Operators
- Logical Operators
- Unary Operators
- Conditional Operators
C Arithmetic Operators
Arithmetic operators perform arithmetical operations, such as addition, subtraction, division, multiplication, etc.
The following example shows how to work with Arithmetic operators in C Programming,
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 |
#include <stdio.h> void main () { int a, b, c; a = 50; b = 100; c = 150; printf("Studyopedia C Tutorial\n\n"); printf("Arithmetic Operators:\n"); printf("\nAddition Arithmetic Operator\n"); printf("Value of (a + b) is %d",(a+b)); printf("\n\nSubtract Arithmetic Operator\n"); printf("Value of (b - a) is %d",(b-a)); printf("\n\nDivision Arithmetic operator\n"); printf("Value of (c / a) is %d",(c/a)); printf("\n\nMultiplication Arithmetic operator\n"); printf("Value of (a * b) is %d",(a*b)); printf("\n\nModulus Arithmetic operator\n"); printf("Value of (c % b) is %d",(c%b)); getch(); } |
Here’s the output,
C Assignment Operator
Use the Assignment Operator when you need to assign a value to a variable. Assignment means assigning the value of the right operand(s) to the left operand.
C Arithmetic Assignment Operators
Use the Arithmetic Assignment Operator when you need to perform arithmetic operations such as add, subtract, divide, multiply, etc and at the same time assign a value.
The following example shows how to work with Arithmetic Assignment Operators in C Programming,
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 |
#include <stdio.h> void main () { int a, b, c, d, e; a = 5; b = 10; c = 15; d = 20; e = 30; printf("Studyopedia C Tutorial\n\n"); printf("Arithmetic Assignment Operators:\n"); a += 5; printf("\nAdd AND assignment operator\n"); printf("Value of a: = %d",a); b -= 5; printf("\n\nSubtract AND assignment operator\n"); printf("Value of b: = %d",b); c *= 5; printf("\n\nMultiple AND assignment operator\n"); printf("Value of c: = %d",c); d /= 10; printf("\n\nDivide AND assignment operator\n"); printf("Value of d: = %d",d); e %= 9; printf("\n\nModulus AND assignment operator\n"); printf("Value of e: = %d",e); getch(); } |
Here’s the output,
C Relational/ Comparison Operators
Compare two values with relational operators, which are also known as Comparison Operators.
The following example shows how to work with Relational Operator in C Programming,
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
#include <stdio.h> void main () { int a, b, c, d; a = 5; b = 10; c = 15; printf("Studyopedia C Tutorial"); printf("\nRelational Operators:\n\n"); printf("Value of (a == b) is %d", (a == b) ); printf("\nValue of (a != b) is %d", (a != b) ); printf("\nValue of (b > c) is %d", (b > c) ); printf("\nValue of (c < a) is %d", (c < a) ); printf("\nValue of (c >= b) is %d", (c >= b) ); printf("\nValue of (b <= c) is %d", (b <= c) ); getch(); } |
Here’s the output,
C Logical Operators
Logical operators combine conditional statements. For example, we’re considering Boolean variables a and b,
The following example shows how to work with Logical Operator in C Programming,
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 |
#include <stdio.h> void main () { int a = 30; int b = 75; int c = 0; printf("Logical Operators\n"); if ( a || b ) { printf("TRUE: Logical OR Operator\n" ); } if ( a && b ) { printf("TRUE: Logical AND Operator\n" ); } if (!(c && b)) { printf("TRUE: Logical NOT Operator\n" ); } getch(); } |
Here’s the output,
C Unary Operators
Unary operators include pre as well as post-increment and decrement operators,
The following example shows how to work with Unary Operators in C Programming,
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
#include <stdio.h> void main () { int a, b; a = 5; printf("Studyopedia C Tutorial\n"); printf("Unary Operators:\n"); printf("\nUnary Increment Operator\n"); printf("Value1: %d",a++); printf("\nValue2: %d",++a); printf("\n\nUnary Decrement Operator\n"); printf("Value1: %d",a--); printf("\nValue2: %d",--a); getch(); } |
Here’s the output,
C Conditional Operators
Conditional operator evaluates Boolean expressions, with three operands. It is also known as the ternary operator. The role is to assign a value to a variable, from two given set options.
Here’s how to use it,
1 2 3 |
variable = (expression) ? value1 ( if true) : value2 (if false) |
The following example shows how to work with the Conditional Operator in C Programming,
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
#include <stdio.h> void main () { int a, b; a = 5; printf("Studyopedia C Tutorial\n"); printf("Conditional Operator\n"); b = (a > 2) ? 100: 50; printf( "\nValue of b = %d", b ); getch(); } |
Here’s the output,
In this lesson, we learned about Operators in C Programming. We saw how to work with Arithmetic, Unary, Relational, Arithmetic Assignment, and other operators.
If you liked the tutorial, spread the word and share the link and our website Studyopedia with others:
For Videos, Join Our YouTube Channel: Join Now
Read More:
No Comments