EDUCBA

EDUCBA

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

Expression in C

Home » Software Development » Software Development Tutorials » C Programming Tutorial » Expression in C

Expression in C

Introduction to Expression in C

An expression in C is defined as 2 or more operands are connected by one operator and which can also be said to a formula to perform any operation. An operand is a function reference, an array element, a variable, or any constant. An operator is symbols like “+”, “-“, “/”, “*” etc.

Let’s observe this example:

Start Your Free Software Development Course

Web development, programming languages, Software testing & others

A*B

In the above expression multiplication symbol (*) is said to be an operator and A and B are said to be 2 operands.

Types of Expression in C

There are 4 types of expressions:

  1. Arithmetic expressions
  2. Relational expressions
  3. Logical expressions
  4. Conditional expressions

Every expression of these 4 types takes certain types of operands and used a specific type of operators. The result of this expression operation produces a specific value.

Example:

addition=(12/5)+(A-B);

From this line after equal operator(=) is an expression((12/5)+(A-B)) and total line is said to be a statement(addition=(12/5)+(A-B);).

How does Expressions works in C?

Expressions in C are built from combinations of operators, let’s see them as described below.

1. Arithmetic Expressions

Addition (+), Subtraction(-), Multiplication(*), Division(/), Modulus(%), Increment(++) and Decrement(–) operators are said to “Arithmetic expressions”. This operator works in between operands. like A+B, A-B, A–, A++ etc.

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)

Syntax:

A+B;
A-B;
A*B;
A/B;

Example:

Code:

//used to include basic C libraries
#include <stdio.h>
//main method for run c application
int main()
{
//declaring variables
int a,b,result;
//Asking the user to enter 2 numbers
printf("Enter 2 numbers for Arithmetic operation \n");
//Storing 2 numbers in varaibles a and b
scanf("%d\n%d",&a,&b);
//Arithmetic operations and its result displaying
result = a+b;
printf("================ARITHMETIC EXPRESSIONS==============\n");
printf("Addition of %d and %d is = %d \n",a,b,result);
result = a-b;
printf("Subtraction of %d and %d is = %d \n",a,b,result);
result = a*b;
printf("Multiplication of %d and %d is = %d \n",a,b,result);
result = a/b;
printf("Division of %d and %d is = %d \n",a,b,result);
result = a%b;
printf("Modulus(Remainder) when %d divided by %d = %d \n",a,b,result);
int c=a;
result = a++;
printf("Post Increment of %d is = %d \n",c,result);
result = ++a;
printf("Pre Increment of %d is = %d \n",c,result);
result=a--;
printf("Post decrement of %d  is = %d \n",c,result);
result=--a;
printf("Pre decrement of %d is = %d \n",c,result);
printf("==========================================");
return 0;
}

Output:

Expression in C Example 1

2. Relational Expressions

== (equal to), != (not equal to), != (not equal to), > (greater than), < (less than), >= (greater than or equal to), <= (less than or equal to) operators are said to “Relational expressions”.This operators works in between operands. Used for comparing purpose. Like A==B, A!=B, A>B, A<B etc.

Syntax:

A==B;
A!=B;
A<B;
A>B;

Example:

Code:

//used to include basic C libraries
#include <stdio.h>
//include boolean library in c
#include <stdbool.h>
//main method for run c application
int main()
{
//declaring variables
int a,b;
bool result;
//Realational Expressions and its result displaying
printf("================RELATIONAL EXPRESSIONS==============\n");
//equal expression
a=10, b=10;
result=(a==b);
if(result)
{
printf("%d and %d are equal\n",a,b);
}
//not equal expression
a=10, b=5;
result=(a!=b);
if(result)
{
printf("%d and %d are not equal\n",a,b);
}
//greater expression
a=10, b=20;
result=(a<b);
if(result)
{
printf("%d is greater than %d\n",a,b);
}
//lesser expression
b=10, a=20;
result=(a>b);
if(result)
{
printf("%d is less than %d\n",b,a);
}
printf("==========================================");
return 0;
}

Output:

Expression in C Example 2

3. Logical Expressions

&&(Logical and), ||(Logical or) and !(Logical not) operators are said to “Logical expressions”. Used to perform a logical operation. This operator works in between operands. Like A&&B, A||B,A!B etc.

Syntax:

A&&B;
A||B;
A!B;

Example:

Code:

//used to include basic C libraries
#include <stdio.h>
//include boolean library in c
#include <stdbool.h>
//main method for run c application
int main()
{
//declaring variables
int a,b;
bool result;
//Logical Expressions and its result displaying
printf("================LOGICAL EXPRESSIONS==============\n");
//logical and(&&) expression
a=10, b=20;
result=(a>5&&b>10);
if(result)
{
printf("I AM LOGICAL AND RESULT\n");
}
//logical or(||) expression
a=10, b=5;
result=(a>10||b>4);
if(result)
{
printf("I AM LOGICAL OR RESULT\n");
}
//logical not(!) expression
a=10, b=5;
result=(!(a==20));
if(result)
{
printf("I AM LOGICAL NOT RESULT\n");
}
printf("==========================================");
return 0;
}

Output:

Expression in C Example 3

4. Conditional Expressions

?(Question mark) and :(colon) are said to “Conditional expressions”. Used to perform a conditional check. It has 3 expressions first expression is condition. If it is true then execute expression2 and if it is false then execute expression3. Like (A>B)?”A is Big”:”B is Big”.

Syntax:

(X+2=10)?'true':'false';

Example:

Code:

//used to include basic C libraries
#include <stdio.h>
//include boolean library in c
#include <stdbool.h>
//main method for run c application
int main()
{
//declaring variables
int a,b;
char result;
//Asking the user to enter a number
printf("Enter a number for conditional operation=>");
//Storing a number in varaibles a
scanf("%d",&a);
//Realational Expressions and its result displaying
printf("================CONDITIONAL EXPRESSIONS==============\n");
//conditional expression
result=(a>=18)?'Y':'N';
if(result=='Y')
{
printf("YOU ARE ELIGIBLE FOR VOTER ID\n");
}
else
{
printf("YOU ARE NOT ELIGIBLE FOR VOTER ID\n");
}
printf("==========================================");
return 0;
}

Output:

Conditional Operations Example 4

Conditional Operations Example 4

Conclusion

Expression in C is said to be a formula which is formed 2 or more operands and one operator. Arithmetic expressions, Logical expressions, Conditional expressions and Relational expressions are some of the expressions in C.

Recommended Articles

This is a guide to Expression in C. Here we discuss the Introduction to Expression in C and its types along with the different examples and code implementation. You can also go through our other suggested articles to learn more –

  1. Prime Numbers in C
  2. Reverse Number in C
  3. Regular Expression in C
  4. Regular Expression in C#

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
  • 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
  • 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
  • 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
  • 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
  • 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