C Language Data Types & Operators

Data Types

Data types are those keywords (identifies – in case of customize data type), which are used to define type of the data. They also tell to the compiler how much memory to be required for those variables. Data Types are categorized in following categories:

Basic & Primitive Data Types:
Basic data types are the built in data types which are basically arithmetic types.

Data Type Required size in Memory Range
char 1 byte -128 to 127 or 0 to 255
unsigned char 1 byte 0 to 255
signed char 1 byte -128 to 127
int 2 or 4 bytes -32,768 to 32,767 or -2,147,483,648 to 2,147,483,647
unsigned int 2 or 4 bytes 0 to 65,535 or 0 to 4,294,967,295
short 2 bytes -32,768 to 32,767
unsigned short 2 bytes 0 to 65,535
long 4 bytes -2,147,483,648 to 2,147,483,647
unsigned long 4 bytes 0 to 4,294,967,295
float 4 byte 1.2E-38 to 3.4E+38
double 8 byte 2.3E-308 to 1.7E+308
long double 10 byte 3.4E-4932 to 1.1E+4932


User Defined / Derived / Non Primitive Data Types:
Data Types which are derived from the primitive / basic data types are known as User Defined/ Derived Data Types.
Pointer, Array, Structure, Union

void Data Type:
void means nothing , it is used when no data type needed. Generally void is used in function parameters and return types, when return type or/and parameter is nothing.

Operators in C Language

Operators are special symbols which are used to perform some specific mathematical or logical operations. Operators are also known as C Tokens, these are very useful in programming even real life, and without operators no one can do programming.

Classification of Operators:
  • Assignment Operator
  • Arithmetic operators
  • Relational Operators
  • Logical Operators
  • Increments and Decrement Operators
  • Conditional Operators
  • Bitwise Operators

Assignment Operator:
Assignment operator is one of the most important operator in any programming language, it assigns the value of right handed expression, variable and value (constant value) to the left side variable. Remember variable must be at the left side of the assignment operator.
variableName = expression;
    int num;
    num = 123;        /*Will assign 123 to num*/
    num = 10+20; /* Will assign the result of expression 10+20  to num*/
    

Arithmetic operators:
The operators which are used to perform arithmetic operations are called Arithmetic Operators.
Arithmetic Operators are:
+       Addition            To add two operands
-       Subtraction         To subtract two operands
*       Multiplication      To multiply two operands 
/       Divide              To divide two operands (returns quotient)
%       Modulus             To get remainder

Consider the example:
    #include < stdio.h >
    int main()
    {

	    int num1,num2;
	    int result;
	    float result1;

	    num1	=10;
	    num2	= 3;

	    result=num1+num2;
	    printf("\nAddition of %d and %d is = %d",num1,num2,result);

	    result=num1-num2;
	    printf("\nSubtraction of %d and %d is = %d",num1,num2,result);

	    result=num1*num2;
	    printf("\nMultiplication of %d and %d is = %d",num1,num2,result);

	    result1=(float)num1/(float)num2;
	    printf("\nQuotient dividing %d by %d is  = %f",num1,num2,result1);

	    result=num1%num2;
	    printf("\nRemainder dividing %d by %d is  = %d",num1,num2,result);
	    return 0;
    }
    
Output
    Addition of 10 and 3 is = 13
    Subtraction of 10 and 3 is = 7
    Multiplication of 10 and 3 is = 30
    Quotient dividing 10 by 3 is  = 3.333333
    Remainder dividing 10 by 3 is  = 1
    
Here, result1=(float)num1/(float)num2; is an float type expression, so we are converting integer type to float type, it is know as Cast Type/Type Conversion.

Relational Operators:
These operators are used to compare two values and return zero (as FALSE) or non zero (as TRUE) values.
Relational Operators are:
==		Equal To
<		Lest Than	
>		Greater Than
<=		Less Than or Equal To
>=		Greater Than or Equal to
!=		Not Equal To

Consider the example:
    #include < stdio.h >
    int main()
    {

	    int num1,num2;
	
	    num1	=10;
	    num2	= 3;

	    if(num1 == num2)
		    printf("\n%d and %d are Equal.",num1,num2);
	    else
		    printf("\n%d and %d are not Equal.",num1,num2);

	    if(num1 != num2)
		    printf("\n%d is not equal to %d.",num1,num2);
	    else
		    printf("\n%d is  equal to %d.",num1,num2);

	    if(num1 < num2)
		    printf("\n%d is less than %d.",num1,num2);
	    else
		    printf("\n%d is not less than %d.",num1,num2);

	    if(num1 > num2)
		    printf("\n%d is greater than %d.",num1,num2);
	    else
		    printf("\n%d is not greater than %d.",num1,num2);

	    if(num1 <= num2)
		    printf("\n%d is less than or equal to %d.",num1,num2);
	    else
		    printf("\n%d is not less than or equal to %d.",num1,num2);

	    if(num1 >= num2)
		    printf("\n%d is greater than or equal to %d.",num1,num2);
	    else
		    printf("\n%d is not greater than or equal to %d.",num1,num2);

	    return 0;
    }
    
Output
    10 and 3 are not Equal.
    10 is not equal to 3.
    10 is not less than 3.
    10 is greater than 3.
    10 is not less than or equal to 3.
    10 is greater than or equal to 3.
    

Logical Operators:
The operators which are used to check more than one condition whether they are true or not. It is very useful when you have multiple conditions and want to take decisions based on them conditions.
Logical Operators are:
&&(Logical AND)
Returns TRUE (1), when all conditions are TRUE.

||(Logical OR)	
Returns TRUE (1), when at least one condition is TRUE.

!(Logical NOT)
Return TRUE (1) if condition is FALSE(0) and returns FALSE (0) if condition is TRUE (1)


Consider the example:
    #include<stdio.h>
    int main()
    {
	    int val=10;

	    if( (val>=10) && (val<=20))
		    printf("\nTRUE-1");
	    else
		    printf("\nFALSE-1");

	    if( (val>=10) || (val<=20))
		    printf("\nTRUE-2");
	    else
		    printf("\nFALSE-2");

	    if(!(val>=10))
		    printf("\nTRUE-3");
	    else
		    printf("\nFALSE-3");
        return 0;
    }
    
Output
        TRUE-1
        TRUE-2
        FALSE-3
    












Comments and Discussions

Ad: Are you a blogger? Join our Blogging forum.




Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates


© https://www.includehelp.com some rights reserved.