Data Types | Variables | Comments in C

Data Types 


Data type refers to the type of data being used for variables ,functions, etc. A datatype defines a range of allowed values and the operations that can be performed on those values.

Different data types have different representation structure in  memory.

Types of Data types 

1. Primitive datatypes or fundamental datatype or primary datatype
2. Non-primitive datatype (These include array , enum ,structure,etc they will be discussed later) or Secondary datatype

There are mainly five type of Primitive datatypes

1. int   (it is used to store integers)
2.float  (used to store floating values of single precision)
3.char    (used to store single character)
4.double  (used to store double precision floating values)
5.void (it contains nothing)

Type Qualifiers 

Type qualifiers are used to define properties and properties of datatypes.these are of two types.

1. Size Qualifiers

They are used to define the size of datatypes. they are mainly two namely, short and long.

for example - short int ,long int ,long double

NOTE - short can be applied to int only. long can be applied to int and double both. 

2. Sign Qualifiers 

They are used to indicate the sign of datatype being used.sign can be positive or negative.
they are also two namely,signed and unsigned.\
Signed datatype is used when the number is always positive and unsigned datatype is used where the data can be positive or negative.

NOTE - Sign Qualifiers are applicable only to char and int datatype.

Size of Datatype

Each datatype enables a variable to occupy some space in memory. so how much memory in byte it is specifying is determined by the size of the datatype.datatype have size in bytes unit.

Size of any datatype can be found by using the sizeof() operator.

Range of Datatype

Each data type has some range. range specifies a bandwidth of numbers which can be used with a particular datatype.

To find out range of any datatype we use macros defined header files limits.h and float.h

NOTE - Size and Range of a datatype are machine and compiler dependent.

For Example - int has a size of 2 bytes in 16 bit operating system but it has size of 4 bytes in 32 bit operating system.

Datatype                                     Range                                            Size

signed char                                   -128 to 127                                     1
unsigned char                                0 to 255                                         1   
int                                                  -32768 to 32767                            2
unsigned int                                   0 to 65535                                     2
short int                                         -128 to 128                                    1
unsigned short int                           0 to 255                                        1
long int                                          -2147483648 to 2147483647        4
unsigned log int                             0 to 4294967295                           4
float                                               3.4E-38 to 3.4E+48                       4
double                                            1.7E-308 to 1.7E+308                   8
long double                                    3.4E-4932 to 1.1E+4932               10

(These size and ranges are for 16 bit operating system. size is in bytes)

Variables


A variable is a name of memory in computer that can be used to store values. value of a variable can be of any type ..it may be integer,floating ,character,etc..

The type of value in variable is given by the datatypes.every variable has a associated datatype which decides type of value in variable.

Variable declaration

Syntax :     qualifier data_type variable_name ;

it is not necessary to use qualifiers.

for example - int a;

This will create a block in memory named a where we can store integer values only. 

Similarly we can define any number of different variables to store values.

Variable initialization

Variable declared are needed to be initialized means they should be given some initial values to store in them.
variable declaration can be done in two ways .

1. Static initialization - In this variable is given some value at the time when we are writing our program. it is done through assignment operator .

for example :  int a=7;
So in  this variable a is initialized with 7 as its value .

2. Dynamic initialization - In this variable is assigned some value at run time. In this value is given by user of the program not the programmer.
This can be done through different library functions like scanf(),etc.

Comments in C

C Allow us to write comments in our C programs for the documentation purpose.comments are not readable by the compiler, they are used for the better understanding of code to the programmer/other programmer.

Comments are of two types 

1. Single line comment 


Any line ,statements after the double forward slash is considered as the single line comment.

for example :    //This is my first program

2. Multiple line comments

Any number of lines between the /*    and */ is considered as multiple line comment.
it is also not readable by the compiler.

for example :   /*This is my first program. it is 
                          very amazing and useful*/

NOTE - if you want to look at example of comments live demo then please have a look at our channel.We have used comments in our different programs.




Comments

Popular posts from this blog

Coding tips for Developers

Master Data Structure & Algorithms in Python

How to Start with Coding: A Beginner's Guide