C Weekly | Data Types In C | Variables | Comments | Simple Program

Data Types

Hello everyone !!

Till now you are aware about what is c? why c is popular and how a c code runs in system, what are the rules for declaring variables??
Before data types we will try to understand what so called variable is??

Do you know what is variable ??
A variable is just a name of a block of memory in our computer storage area where we can store different type of data for performing different operations on that data.

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.


Understanding Data types

Simply , if we see what type of data can be there in a memory?? It can be of any type depending upon a given problem or depending upon program need.

So, every memory having some data must have specific type of that data. This is called data type .

In technical terms we can define data types as "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.

Note : The data type of variable determines how much space it occupy in memory.

How data types are used to declare functions will be discussed later in this series !!

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)

Note : Later versions of C Introduced bool as primitive data type. (This will be discussed later in the series.)

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)

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*/

Writing Your First C Program

#include<stdio.h>                                                              //line 1
#include<conio.h>                                                             //line 2
void main()                                                                         //line 3
{                                                                                          //line 4
clrscr();                                                                               //line 5
printf("Hello World!! This is my first C Program");         //line 6
getch();                                                                               //line 7
}                                                                                          //line 8

Explanation of Program

line 1 : This line incude header file stdio(standard inut output ) which contains predefined function printf() and scanf(). # is a preprocessor directive and include is a command.

line 2 : This line include header file conio(Console input output) which contains predefined functions clsrscr() and getch().

line 3 :This is main function or we can say a driver function .this is the part from where our program runs that is why it is also called as the driver function. Every code of a program ,directly or indirectly,comes inside the main function.

void used in front of main function is the return type of main function . Every function returns some value so main function returns value of void type.

line 4 : Opening braces of a function.

line 5 : Predefined function for clearing console output screen.

line 6 : Predefined function to print message on screen.

line 7 : Predefined function to accept a single character which in turn holds the console output screen until a key is pressed.

line 8 : Closing braces of a function.

Note -> ';'  is a statement terminating symbol which denotes the end of a statement ,expressions,etc.

Note -> The another symbol '//' is used to add comments in our code. anything followed by // is not readable by compiler ,there is no meaning of it to compiler .it is only used for the reference of programmer.


Output Of this Program :


Hello World!! This is my first C Program






Go For Live Demo

Comments

Post a Comment

Popular posts from this blog

Conditional Statements and Conditional Operator | C Weekly

Fundamentals of C | feel_the_coder | C Weekly