DATA TYPES-3 AND OPERATORS IN C PROGRAMMING LANGUAGE:

DATA TYPES AND OPERATORS IN C PROGRAMMING LANGUAGE:

Operators in c:

Before starting this article on operators in C, Firstly we complete our data types,I will suggest you if you don't go throgh our previous articles, so firstly read out them for more better understanding. Our previous articles are listed below:
  1. INTRODUCTION TO C
  2. C PROGRAMMING: STRUCTURE AND DATA TYPES PART-1
  3. C PROGRAMMING: ABOUT C PROGRAMMING AND DATA TYPES PART-2

Let’s start…

YOU MIGHT NOT THINK THAT PROGRAMMERS ARE ARTISTS,

BUT PROGRAMMING IS AN EXTREMELY CREATIVE PROFESSION.

IT’S LOGIC BASED CREATIVITY.

character ( char ):

Keyword “char” is used for declaring character type variables. For example,

#include<stdio.h>
int main()
{
    char letter;
    printf("enter any letter:");
    scanf("%c",&letter);
    printf("letter entered is %c",letter);
return 0;
}

output:

enter any letter:p   ( suppose we enter p )
letter entered is p

void:

It means “nothing” or ” no-type”. It is an incomplete type. You can consider void as “absent”. If a function is not returning anything, its return type should be void. NOTE: You can never create variables of void type.

short and long data types:

If you are using only a small integer like( -32, 564, +54 ) , then you can use short data types. Otherwise, if you need only a large number, then you can use long data types.

You can also check the size of a variable using the sizeof() operator. For example,

#include<stdio.h>
int main()
{
    short a;
    long b;
    long long c;
    long double d;

    printf("size of short=%d bytes \n ",sizeof(a));
    printf("size of long=%d bytes \n ",sizeof(b));
    printf("size of long long =%d bytes \n ",sizeof(c));
    printf("size of long double=%d bytes \n ",sizeof(d));

    return 0;
}

output:

size of short=2 bytes
size of long=4 bytes
size of long long =8 bytes
size of long double=12 bytes

NOTE: The size of data types is depend on your architecture.

operators in c:

Let me remind you that we already discuss little bit of C operators in our previous article :C PROGRAMMING: STRUCTURE AND DATA TYPES PART-1, So, firstly go through it for better understanding.

Now, as we already discuss about “arithmetic operators”. So, it’s time to make a program on it. Let’s do it!

#include<stdio.h>
int main()
{
 //making program by using all arithmetic operators that we already discussed

int a=5 ;
float b=6;

printf("a+b=%f \n",a+b);   /* as we know, when  we apply any operation between an integer and a float the answer is always a float.*/

printf("a-b=%f \n",a-b);
printf("a*b=%f \n",a*b);
printf("a/b=%f \n",a/b);
printf("a%b=%f \n",a%b);

return 0;
}

output:

a+b=11.000000
a-b=-1.000000
a*b=30.000000
a/b=0.833333
a%b=0.3

Now, as we already discuss about “relational operators” also. So, it’s time to make a program on it. Let’s do it!

#include<stdio.h>
int main()
{
    int a=3, b=4;
    // if answer is 0, then it means false.
    // if answer is 1, then it means true.

    printf("a>b:%d \n", a>b);
    printf("a>=b :%d \n",a>=b);
    printf("a<=b :%d \n",a<=b);
    printf("a<b :%d \n",a<b);
    printf("a==b :%d \n",a==b);
    printf("a!=b :%d \n",a!=b);

    return 0;
}

output:

a>b:0
a>=b :0
a<=b :1
a<b :1
a==b :0
a!=b :1

logical operators:

  • AND Operator: If both operands are non-zero, then the condition is true.
  • OR Operator: If any of those two operands are non-zero then condition become true.
  • NOT Operator: If the condition is true, then it makes it false.

Let’s make a program on logical operators..

#include<stdio.h>
void main()
{
    int a,b;
printf("Enter values of a and b:");
scanf("%d %d", &a,&b);
printf("\n %d",(a<b)&&(a!=b));
printf("\n %d",(a<b)||(b<a));
printf("\n %d",!(a==b));
}

bitwise operator:

aba&ba|ba^b
00000
01011
11110
10011
1 stands for "true" condition. 0 stands for "false".
  • a&b: In AND, both operands have to be non-zero. Then the condition is true.
  • a|b: In OR, anyone operand has to be non-zero, then the condition is true.
  • a^b: In exclusive OR, one operand must true and the other is false, then the only condition is true.

other bitwise operators:

  • ~ binary one’s complement.
  • << binary left shift.
  • >> binary right shift.

assignment operators:

  1. = Simple assignment operator, example: int a=5;
  2. += Add AND operator, example: a=7; a+=1; ( output: a=8 )
  3. -= Subtract AND assignment operator, example: int a=7; a-=1; ( output: a=6 )
  4. *= Multiply AND assignment.
  5. /= Divide AND assignment.

miscellaneous operators:

OPERATORSDESCRIPTIONEXAMPLE
sizeof()Returns the size of a variablesizeof( a) , where a is integer, will return int’s size on that architecture.
&Returns the address of variables&a; returns the actual address of the variable.
*Pointer to a variable*a;
?:Conditional expressionIf condition is true? then value X: otherwise value Y.

Two ways to define constant in C programming:

1- Constant keyword-
EXAMPLE:

#include<stdio.h>
void main ()
{ 
      int a=8;
    const float b= 7.33;
      printf("Print value of b is %f",b);
}
2- #define preprocessor:
EXAMPLE:

#include<stdio.h>
#define PI 3.14
int main()
{ 
    int a=8;
    const float b= 7.33;
    printf("%f",PI);

return 0;
}
output:
3.14000
I HOPE EVERYTHING IS NOW CLEAR TO YOU ABOUT DATA TYPES AND OPERATORS IN C. In our next article we will discuss about if, else if , if else if statement and many more....
See you next time...
Scroll to Top