Programs on Operators and Expressions in C

/*
Author : ITVoyagers (https://itvoyagers.in/)

Date :25th December 2018

Description : Programs on Operators and Expressions.
*/

#include<stdio.h>
#include<conio.h>
void main()
{
    int a=5,b=3,*p;
    p=&a;
    clrscr();

    /************ Unary Operators ***********/

    printf("nUnary OperatorsnOperators that operates or works with a single operand are unary operators. For example: (++ , –)n");

    printf("nBitwise Complement ~a=%d",~a);
    printf("nUnary Minus -a=%d",-a);
    printf("nPointer Indirection *p=%d",*p);
    printf("nAddress &a=%d",&a);
    printf("nUnary Plust +a=%dn",+a);

    /************ Binary Operators ***********/

    printf("nBinary OperatorsnOperators that operates or works with two operands are binary operators.For example: (+ , – , * , /)n");
    printf("na+b=%d",a+b);
    printf("nArithmetic Operatorsn");
    printf("na+b=%d",a+b);
    printf("na-b=%d",a-b);
    printf("na/b=%d",a/b);
    printf("na*b=%d",a*b);
    printf("na+b=%dn",a%b);

    /************ Increment Operators ***********/

    printf("nIncrement Operatorsn");
    printf("nPost-Increment a++ = %d",a++);
    printf("nPre-Increment ++a = %dn",++a);

    /************ Decrement Operators ***********/

    printf("nDecrement Operatorsn");
    printf("nPost-Decrement a-- = %d",a--);
    printf("nPre-Decrement --a = %dn",--a);

    /************ Teranary Operators with Relational Operators ***********/

    printf("nTeranary Operators () ? with Relational Operatorsnn");
    printf("a>b?: ");
    a>b?printf("a is greatern"):printf("b is greatern");
    printf("a<b?: ");
    a<b?printf("b is greaterrn"):printf("a is greatern");
    printf("a<=b?: ");
    a<=b?printf("b is greatern"):printf("a is greatern");
    printf("a>=b?: ");
    a>=b?printf("a is greatern"):printf("b is greatern");
    printf("a==b?: ");
    a==b?printf("both are equaln"):printf("both are not equaln");
    printf("a!=b?: ");
    a!=b?printf("both are not equaln"):printf("both are equaln");

    /************ Teranary Operators with Logical Operators ***********/

    printf("nTeranary Operators () ? with Logical Operatorsnn");
    printf("a>b && a>0: ");
    (a>b) && (a>0)?printf("a is greater positive numbern"):printf("b is greater positive numbern");
    printf("a>b || a>0: ");
    (a>b) || (a>0)?printf("a is positive numbern"):printf("a is negative numbern");
    printf("!(a>b): ");
    !(a>b)?printf("b is greater positive numbern"):printf("a is greater positive numbern");

    /************ Assignment Operators ***********/

    printf("nAssignment Operatorsnn");
    a=6;
    printf("a=6 : %dn",a);
    a+=6;
    printf("a+=6 : %dn",a);
    a-=6;
    printf("a-=6 : %dn",a);
    a*=6;
    printf("a*=6 : %dn",a);
    a/=6;
    printf("a/=6 : %dn",a);
    a%=4;
    printf("a%c=4 : %dn",'%',a);
    getch();
}

Output:

Unary Operators
Operators that operates or works with a single operand are unary
operators. For example: (++ , -)

Bitwise Complement ~a=-6
Unary Minus -a=-5
Pointer Indirection *p=5
Address &a=2358844
Unary Plust +a=5

Binary Operators
Operators that operates or works with two operands are binary
operators.For example: (+ , – , * , /)
a+b=8

Arithmetic Operators
a+b=8
a-b=2
a/b=1
a*b=15
a+b=2

Increment Operators
Post-Increment a++ = 5
Pre-Increment ++a = 7

Decrement Operators
Post-Decrement a– = 7
Pre-Decrement –a = 5

Teranary Operators () ? with Relational Operators
a>b?: a is greater
a<b?: a is greater
a<=b?: a is greater
a>=b?: a is greater
a==b?: both are not equal
a!=b?: both are not equal

Teranary Operators () ? with Logical Operators
a>b && a>0: a is greater positive number
a>b || a>0: a is positive number
!(a>b): a is greater positive number

Assignment Operators
a=6 : 6
a+=6 : 12
a-=6 : 6
a*=6 : 36
a/=6 : 6
a%=4 : 2

Leave a Comment