EDUCBA

EDUCBA

MENUMENU
  • Free Tutorials
  • Free Courses
  • Certification Courses
  • 600+ Courses All in One Bundle
  • Login

C Operators

Home » Software Development » Software Development Tutorials » C Programming Tutorial » C Operators

C Operators

What are operators in C?

Operators are symbols that help in performing operations of mathematical and logical nature. The classification of C operators are as follows:

  • Arithmetic
  • Relational
  • Logical
  • Bitwise
  • Assignment
  • Conditional
  • Special

1. Arithmetic Operators

These operators are responsible for performing arithmetic or mathematical operations like addition (+), subtraction (-), multiplication (*), division (/), the remainder of the division (%), increment (++), decrement (–).

Start Your Free Software Development Course

Web development, programming languages, Software testing & others

There are two types of arithmetic operators:

  • Unary Operators: This type of operator works with a single value (operand) like ++ and –.
  • Binary Operators: This type of operator works with two operands like +,-,*,/

Here is a tabular form of the number of arithmetic operators in C with the functions they perform.

Operator Function
+ Adds two values
– Subtract second value from first
* Multiply two values
/ Divide numerator by the denominator
% Remainder of division
++ Increment operator – increases integer value by one
— Decrement operator – decreases integer value by one

Example: C Program using arithmetic operators

#include <stdio.h>
int main()
{
int a = 12, b = 6, c;
c = a + b;
printf("a+b = %d \n", c);
c = a - b;
printf("a-b = %d \n", c);
c = a *b;
printf("a*b = %d \n", c);
c = a / b;
printf("a/b = %d \n", c);
c = a % b;
printf("Remainder when a divided by b = %d \n", c);
return 0;
}

Output :

a+b = 18

a-b = 6

a*b = 72

a/b = 2

Remainder when a divided by b=0

2. Relational Operators

When we want to compare the values of two operands, then relational operators are used. If we want to check that is one operand is equal to or greater than other operands, then we use >= operator.

The below table lists of the relational operators in C with their functions.

Operator Function Example
== This will check if two operands are equal 6 == 2 returns 0
!= This will check if two operands are not equal. 6 != 2 returns 1
> This will check if operand on the left is greater than operand on the right 6 > 2 returns 1
< This will check if operand on the left is smaller than the right operand 6 < 2 returns 0
>= This will check if the left operand is greater than or equal to the right operand 6 >= 2 returns 1
<= This will check if operand on left is smaller than or equal to the right operand 6 <= 2 return 0

Example: C Program using logical operators

#include <stdio.h>
int main()
{
int a = 7, b = 7, c = 10;
printf("%d == %d = %d \n", a, b, a == b); // true
printf("%d == %d = %d \n", a, c, a == c); // false
printf("%d > %d = %d \n", a, b, a > b); //false
printf("%d > %d = %d \n", a, c, a > c); //false
printf("%d < %d = %d \n", a, b, a < b); //false
printf("%d < %d = %d \n", a, c, a < c); //true
printf("%d != %d = %d \n", a, b, a != b); //false
printf("%d != %d = %d \n", a, c, a != c); //true
printf("%d >= %d = %d \n", a, b, a >= b); //true
printf("%d >= %d = %d \n", a, c, a >= c); //false
printf("%d <= %d = %d \n", a, b, a <= b); //true
printf("%d <= %d = %d \n", a, c, a <= c); //true
return 0;
}

Popular Course in this category
C Programming Training (3 Courses, 5 Project)3 Online Courses | 5 Hands-on Projects | 34+ Hours | Verifiable Certificate of Completion | Lifetime Access
4.5 (5,617 ratings)
Course Price

View Course

Related Courses
C++ Training (4 Courses, 5 Projects, 4 Quizzes)Java Training (40 Courses, 29 Projects, 4 Quizzes)

Output:

7 == 7 = 1

7 == 10 = 0

7 > 7 = 0

7 > 10 = 0

7 < 7 = 0

7 < 10 = 1

7 != 7 = 0

7 != 10 = 1

7 >= 7 = 1

7 >= 10 = 0

7 <= 7 = 1

7 <= 10 = 1

3. Logical Operators

Logical Operators are used for True or False results.

The table below lists out the logical operators used in C

Operator Function Example (if a=1 and b=0)
&& Logical AND (a && b) is false
|| Logical OR (a || b) is true
! Logical NOT (!a) is false

Example: C Program using logical operators.

#include <stdio.h>
int main()
{
int a = 8, b = 8, c = 12, result;
result = (a == b) && (c > b);
printf("(a == b) && (c > b) equals to %d \n", result);
result = (a == b) && (c < b);
printf("(a == b) && (c < b) equals to %d \n", result);
result = (a == b) || (c < b);
printf("(a == b) || (c < b) equals to %d \n", result);
result = (a != b) || (c < b);
printf("(a != b) || (c < b) equals to %d \n", result);
result = !(a != b);
printf("!(a == b) equals to %d \n", result);
result = !(a == b);
printf("!(a == b) equals to %d \n", result);
return 0;

Output :

(a == b) && (c > b) equals to 1

(a == b) && (c < b) equals to 0

(a == b) || (c < b) equals to 1

(a != b) || (c < b) equals to 0

!(a != b) equals to 1

!(a == b) equals to 0

5. Bitwise Operators

These operators are used for bit-level operations on the operands. The operators are converted first to bit-level and then calculations are performed.

Operator Function
& Bitwise AND
| Bitwise OR
^ Bitwise exclusive OR
~ Bitwise complement
<< Shift left
>> Shift right

Example: C program for Bitwise AND

#include <stdio.h>
int main()
{
int a = 10, b = 8;
printf("Output = %d", a&b);
return 0;
}

Output = 8

Explanation:

10 = 00001010 (In Binary)

8 = 00001000 (In Binary)

Bit Operation of 10 and 8

00001010 & 00001000 = 00001000 = 8 (In decimal)

6. Assignment Operators

These types of operators are used to assign a value to a variable.

Operator Function Example
= This will assign values from right side operands to left side operand a=b
+= This will add the right operand to the left operand and assign the result to left a+=b is same as a=a+b
-= This will subtract right operand from the left operand and assign the result to left operand a-=b is same as a=a-b
*= This will multiply left operand with the right operand and assign the result to left operand a*=b is same as a=a*b
/= This will divide left operand with the right operand and assign the result to left operand a/=b is same as a=a/b
%= This will calculate modulus using two operands and assign the result to left operand a%=b is same as a=a%b

7. Conditional Operators

Also, known as Ternary Operator or? : Operator. These are used for decision-making.

Syntax: Expression 1? Expression 2: Expression 3

Here,? Represents the IF condition.

8. Special Operators

Here are some special operators used in C

Operator Function
& This operator is used to get the address of the variable.

Example: &a will give an address of a.

* This operator is used as a pointer to a variable.

Example: * a where * is a pointer to the variable a.

size of () This operator gives the size of the variable.

Example: The size of (char) will give us 1.

Example: C program using a special operator

#include <stdio.h>
int main()
{
int *ptr, q;
q = 40;
/* address of q is assigned to ptr */
ptr = &q;
/* display q's value using ptr variable */
printf("%d", *ptr);
return 0;
}

Output: 40

Recommended Articles

This has been a guide to C Operators. Here we have discuss operators used in C language with their syntax and examples. You may also look at the following articles to learn more.

  1. Comparison of C# vs JavaScript
  2. List of C-Command
  3. Career in C Programming
  4. Bitwise Operators in JavaScript

C Programming Training (3 Courses, 5 Project)

3 Online Courses

5 Hands-on Projects

34+ Hours

Verifiable Certificate of Completion

Lifetime Access

Learn More

0 Shares
Share
Tweet
Share
Primary Sidebar
C Programming Tutorial
  • Operators
    • C Operators
    • Arithmetic Operators in C
    • Relational Operators in C
    • Assignment Operators in C
    • Logical Operators in C
    • Conditional Operator in C
    • Modulus Operator in C
    • Ternary Operator in C
    • Address Operator in C
    • Unary Operator in C
    • Operators Precedence in C
    • Left Shift Operator in C
  • Basic
    • Introduction to C
    • What is C
    • Career in C Programming
    • Advantages of C
    • How to Install C
    • Best C Compilers
    • Data Types in C
    • Variables in C
    • C Keywords
    • C Command
    • Command Line Arguments in C
    • C Literals
    • Constants in C
    • Unsigned Int in C
    • String in C
  • Pointers
    • Pointers in C
    • Null pointer in C
    • Function Pointer in C
    • Double Pointer in C
    • Void Pointer in C
    • Const Pointer in C
    • Dangling Pointers in C
    • Pointer Arithmetic in C
  • Control Statement
    • Control Statements in C
    • If Statement in C
    • If-else Statement in C
    • Else if Statement in C
    • Nested if Statement in C
    • #else in C
    • Structure Padding in C
    • Nested Structure in C
    • Continue Statement in C
    • Break Statement in C
    • Switch Statement in C
    • Goto Statement in C
  • Loops
    • Loops in C
    • For Loop in C
    • While Loop in C
    • Do While Loop in C
    • Nested Loop in C
    • Infinite Loop in C 
  • Function
    • Math Functions in C
    • Hashing Function in C
    • Recursive Function in C
    • Power Function in C
    • fputs in C
    • C puts() Function
    • fprintf() in C
    • fseek() in C
    • Stderr in C
    • ASCII Value in C
    • strcat() in C
    • Inline Function in C
    • sizeof() in C
    • Function Prototype in C
    • C ftell()
  • Array
    • Arrays in C Programming
    • 2-D Arrays in C
    • 3D Arrays in C
    • Multidimensional Array in C
    • Array Functions in C
    • Strings Array in C
  • Sorting
    • Sorting in C
    • Heap Sort in C
  • Advanced
    • Constructor in C
    • Encapsulation in C
    • C Storage Classes
    • Static Keyword in C
    • File Handling in C
    • Queue in C
    • Hexadecimal in C 
    • typedef in C
    • Memory Allocation in C
    • Linked List in C
    • Volatile in C
    • Tokens in C
    • Expression in C
    • Regular Expression in C
    • Error Handling in C
    • Types of Errors in C
    • Preprocessor in C
    • Preprocessor Directives in C
    • fscanf() in C
    • #Pragma in C
    • #ifndef in C
    • #undef in C
    • Macros in C
  • C programs
    • Patterns in C Programming
    • Star Patterns in C
    • Number Patterns in C
    • Swapping in C
    • Reverse Number in C
    • Palindrome in C Program
    • Factorial in C
    • Fibonacci Series in C
    • Square Root in C
    • Random Number Generator in C
    • Prime Numbers in C
    • Escape Sequence in C
    • Reverse String in C
    • Leap Year Program in C
    • Anagram Program in C
    • Strong Number in C
    • String Concatenation in C
    • C Programming Matrix Multiplication
    • Decimal to Octal in C
    • Expression Evaluation in C
    • Decimal to Hexadecimal in C
  • Interview question
    • C Programming Interview Questions

Related Courses

C Programming Training Course

C++ Training Course

Java Training Course

Footer
About Us
  • Blog
  • Who is EDUCBA?
  • Sign Up
  • Corporate Training
  • Certificate from Top Institutions
  • Contact Us
  • Verifiable Certificate
  • Reviews
  • Terms and Conditions
  • Privacy Policy
  •  
Apps
  • iPhone & iPad
  • Android
Resources
  • Free Courses
  • Java Tutorials
  • Python Tutorials
  • All Tutorials
Certification Courses
  • All Courses
  • Software Development Course - All in One Bundle
  • Become a Python Developer
  • Java Course
  • Become a Selenium Automation Tester
  • Become an IoT Developer
  • ASP.NET Course
  • VB.NET Course
  • PHP Course

© 2020 - EDUCBA. ALL RIGHTS RESERVED. THE CERTIFICATION NAMES ARE THE TRADEMARKS OF THEIR RESPECTIVE OWNERS.

EDUCBA
Free Software Development Course

Web development, programming languages, Software testing & others

*Please provide your correct email id. Login details for this Free course will be emailed to you
Book Your One Instructor : One Learner Free Class

Let’s Get Started

This website or its third-party tools use cookies, which are necessary to its functioning and required to achieve the purposes illustrated in the cookie policy. By closing this banner, scrolling this page, clicking a link or continuing to browse otherwise, you agree to our Privacy Policy

EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you
EDUCBA Login

Forgot Password?

EDUCBA
Free Software Development Course

Web development, programming languages, Software testing & others

*Please provide your correct email id. Login details for this Free course will be emailed to you

Special Offer - C Programming Training (3 Courses, 5 Project) Learn More