as

Wednesday, 12 November 2014

Data Types and Variables

Keywords

Below is the list of keywords that are used in C language. Each keyword has a special meaning.

auto
break
case
char
const
continue
default
do
double
else
enum
extern
float
for
goto
if
int
long
register
return
short
signed
sizeof
static
struct
switch
typedef
union
unsigned
void
volatile
while

Data Types

Data types are used to specify the how much memory size will be used to store the data in a variable.
For example –
int id;
Above statement declares variable Id with data type of int. int is a data type which needs 2 bytes.

Built in Data types in C.

Data types are used to specify what kind of data a variable can hold.
Below is the list of Data types in C.
1.      char – used to store single character
2.      int – used to store natural integer
3.      float – used to store single precision numbers
4.      double - used to store double precision numbers
We can also use below qualifiers.
1.      short – used to store small integer (up to 2 bytes)
2.      long – used to store bigger integers (up to 4 bytes)
3.      signed – used to store signed numbers.
4.      unsigned – used to store unsigned numbers.
To find the size of each data type in terms of bytes, we can use below code. Please note that different machines will produce different outputs based upon processor architecture.
On my 32-bit machine, I got below output.
printf("size of short int is %d", sizeof(short int));
output – 2
printf("size of long int is %d", sizeof(long int));
output – 4
Size of float and double data type is 4 and 8 bytes on my machine respectively.

User defined data types

User defined data types are created using built in data types.
For example, I can define my own data type MYINT which is similar as int.

#include<stdio.h>               
int main()
{
   typedef int MYINT;
   MYINT marks=10;
   printf("%d marks", marks);
   getchar();
}





Above example does not make much sense. But typedef can be very useful when working with structures (We will study structures in detail later on in the book). In below example, we have defined new data type book of type struct book.

#include<stdio.h>               
int main()
{

typedef struct
{
int pages;
char * author;
}book;

   book b1;
   b1.pages = 11;
   printf("%d pages", b1.pages);
   getchar();
}



Type Conversion in C.

There are  two types of data type conversions.
1.      Implicit (Automatic type conversion)
2.      Explicit (casting)
Sometimes data is lost when compiler does implicit data type conversion. To avoid data loss, we can use explicit type conversion.
Example –

#include <stdio.h>

void main()
{
   int marks, subjects;
   double percentage;
   marks = 555;
   subjects = 6;

   //in below statement result will be 92.0
   //percentage= marks / subjects;

   //in below statement result will be 92.5 due to explicit type casting
   percentage=  (double) marks / subjects;

   printf("Percentage : %f\n",  percentage);

}




Variables

Variable is used to store the values. Variables can be declared to be of any data type.
For example –
int a;
float b;

here a is a variable that can store integer values. b is a variable that can store floating numbers.
Can you guess the output of below c program? In below program, we have declared variable a with int data type. In next statement, we are trying to store 2.2 into a. If you compile this program, you will not see any compilation errors. But the output of the program will be 2 since compiler will truncate the number to nearest integer number closer to 0.

#include<stdio.h>          
     
#include<stdlib.h>
//main function
void main()                
{
//declaration of variable
      int a;
//initialisation of variable
       a = 2.2;
//or a = 2.7
      printf("%d",a);
      getchar();
}





Sometimes we want the variable values to not change. For example in mathematics value of pi never changes. We can declare constant variables using below syntax.
const float pi=3.14;
Please note that we can not assign any value to pi later in the program since it is a constant variable.

Enumerations are also other type of constants in C. enum is a set of integer constants which are given names.
Below example will help you understand enums in a better way.


#include<stdio.h>               
#include<stdlib.h>

enum week{ sun, mon, tue, wed, thu, fri, sat};

int main()
{
    enum week dayname;
    dayname=mon;
    printf("%d day", dayname);
   getchar();
}





Some of Character constants are given below.

No.
Characters
Meaning
1
‘\’’
Single quote
2
‘\’’’
Double quote
3
‘\?’
Question mark
4
‘\a’
Audible alert
5
‘\\’
Backslash
6
‘\0’
Null
7
‘\b’
Back space
8
‘\f’
Form feed
9
‘\n’
New line
10
‘\r’
Carriage return
11
‘\t’
Horizontal tab
12
‘\v’
Vertical Tab


 Operators

There are 5 types of operators in C.
Arithmetic
-, +, %, /, *
logical
||, &&, !
Relational operators
>, < , >=, <=, !=, ==, !
Bitwise
&, |, ^, ~, <<, >>
Assignment
=, +=, -=, *=, /=, %=, &=
increment & decrement operators
++,--
Conditional
?: True or false

Sponsored Links

Popular Posts

Comments

ShareThis

Google+ Followers

Google+ Badge