Data types, Operations and Operators in C program: Study C in 7 days

Data type in C programming language 4th day

Data types indicate the nature of data; it gives some information to user. So the user should decide the type of data at the initial stage of the program, then only the compiler can interpret the data. There are different types of data types in C program language. Operators are used to perform certain operations in C. In our 4th day of Study C in 7 days we are going to discuss about C program data types.

Integer data type

It represents the integer data. It can be represented by a keyword ‘int’ in the C language, also an integer data type can hold positive and negative values. 2 byte memory will allocate for this ‘int’.

Character data type

To represent a character value we are using character data type. This is represented by keyword ‘char’.
1 byte memory is reserved for char.

Floating point data type

It is used to represent a floating point number  (eg: 12.47)
Syntax is 'float'

Different types of Operators in C

The symbols that give instructions to perform different types of mathematical and logical operations are called operators. We can define operators as the tools which can alter the values of data and variables. The important operators used in C language are:
  • Arithmetic operators 
  • Logical operators 
  • Relational operators 
  • Assignment operators 
  • Conditional operators 
  • Increment or decrement operators 
  • Bit wise operators 
  • Special operators

Arithmetic operators

Arithmetic operators are commonly used operator in programming languages. C programming language consists of Binary arithmetic and Unary arithmetic. The data, which is operated by the operator, is called operand. In Unary operation there will be only one operand (eg: -1 Negative number). To perform a binary arithmetic, we need at least 2 operands. Different arithmetic operators are given below:

Operator
Use
Example
Result
+
To add two numbers
i=3+2
5
-
For subtraction
i=3-2
1
*
For multiplication
i=3*2
6
/
For division
i=3/2
1
%
Modular division
(Reminder after division)
i=10%3
1

Logical operators

Logical operators are also called Boolean operators. These operators are used to check two or more conditions. Boolean operators are followed:

Operator
Use
Example
&&
Logical AND
if(a>b&&c<d)
||
Logical OR
if(a>b||c<d)
!
Logical NOT
if(a>b!c<d)

Relational operators

Operators used to compare two values according to their relation are called Relational operators. These are also called Comparison operators. For performing relational operation we must need two operands, then only we can illustrate and check the relation between them. 6 relational operators are:

Operator
Use
Example
< 
Less than
if(a<b)
<=
Less than or equal to
if(a<=b)
> 
Greater than
if(a>b)
>=
Greater than or equal to
if(a>=b)
==
Equal
if(a==b)
!=
Not equal
if(a!=b)

Assignment operators

Assignment operators are used to assign a value to a variable. ‘=’ is used as assignment operator. For example i=10;

Compound Assignment operators

These operators are used to assign a value to variable after getting a desired result.
For example x+=z; That is x=x+z;
Both of the above expressions have same meaning but first expression uses short hand Compound assignment operator and second expression uses simple assignment operator. The different compound operators are:

Operator
Example
Meaning
+=
x+=y
x=x+y
-=
x-=y
x=x-y
*=
X*=y
x=x*y
/=
x/=y
x=x/y
%=
X%=y
x=x%y

Conditional operators

These operators work according to some condition. The syntax is given by
i=expression1? Expression2:expression3
Eg: i=a>b?a:b;
The meaning of above statement is if a>b, then i=a, else i=b

Increment or Decrement operators

The powerful operators in C are increment and decrement operator. ‘++’ is used as increment operator and ‘--’ used as decrement operator.
Eg: i++; meaning   i=i+1; 
    i--; meaning i=i-1;

Bit wise operators

Bitwise operators are used to test the bit or to shift the bits to either left or right. Operators and its use are given below:

Operator
Use
Example
&
Bitwise AND
101&001=001
|
Bitwise OR
110|101=111
<< 
Left shift
110<<2=101
>> 
Right shift
110>>2=011
~
One’s compliment
~110=001

Special operators

Comma operator (,) and Size of operator are the special operators in C.

Comma operator (,)
It is used to connect related expressions.
Eg: Sum= (a=7,b=3,a+b); Here first the compiler assign ‘5’ to ‘a’ then ‘7’ to ‘b’ after that it calculate the result of 7+3 and the result 10 is assigned to the variable ‘Sum’.

Size of operator
For calculating the length of a variable we are using size of operator. It will calculate the number of bytes in that variable.
Syntax
i=size of(variable);

So that's all for today's C tutorial. Use the comment box below for any C program help.
Ads


0 comments:

Post a Comment

 

Copyright © 2011 CircuitsGallery| ToS | Privacy Policy |

Contact Us | Write For Us | Advertise on Circuits Gallery | About Us