0% found this document useful (0 votes)
5 views14 pages

Unit-II Part-3

Uploaded by

tv5874
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views14 pages

Unit-II Part-3

Uploaded by

tv5874
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 14

School of Computing Science and Engineering

Course Code: E2UC101C Course Name: Programming for Problem Solving

UNIT 2
CONSTRUCTS OF C

Datatypes and storage classes

Name of the Faculty: Tarachand Verma Program Name: B.Tech (CSE)


Recapitulations

 Process of writing a C program


 C Character Set
 C Tokens
 Keywords
 Identifiers/Variables
 Constants
 Datatypes in C
Objectives

 Datatypes revisited
 long
 short
 signed
 unsigned
 Storage classes
 auto
 register
 extern
 static
Datatypes Revisited

Integers, long and short


 C offers a variation to integer data by using keywords – long and short.
 Based on the 16-bit or 32-bit compiler, the size of each varies as below:
 Some generic rules:
 short is at least 2 bytes big. Compiler short int long
 long is atleast 4 bytes big. 16-bit (Turbo C/C++) 2 2 4
 short is never bigger than int. 32-bit (Visual C++) 2 4 4
 int is never bigger than long.
 Declarations:
 long int i;
 long abc;
 short int j;
 short xyz;
Datatypes Revisited

Integers, signed and unsigned


 When value stored by integer is always positive, we can use keyword unsigned as:
unsigned int num_students;
unsigned i;
 With above declaration, the permissible range of integer values for 16-bit compiler will
shift from -32768 to +32767 to range 0 to 65535.

Chars, signed and unsigned


 unsigned and signed char, both occupy 1 byte each.
 signed char has a range from -128 to 127 whereas unsigned char has range from 0
to 255.
Datatypes Revisited

Float, Double and long


 float occupies 4 bytes in memory ranging from -3.4e38 to +3.4e38.
 double occupies 8 bytes in memory ranging from -1.7e308 to +1.7e308.
 If situation demands even a bigger range then long double can be used to obtain
range of -1.7e4932 to +1.7e4932.
 long double occupies 10 bytes in memory.
Storage Classes

 A variable’s storage class tells us:


 where the variable would be stored.
 what will be the initial value or default value.
 what is the scope of the variable i.e. in which functions the value of the variable would be available.
 what is the life of variable i.e. how long would the variable exist.

 There are four storage classes in C:


 Automatic storage class
 Register storage class
 Static storage class
 External storage class
Automatic Storage Class

 This is the default storage class.


 The details are as follows:
 Storage - Memory
 Default initial value - Garbage value (unpredictible)
 Scope - Local to the block in which variable is defined
 Life - Till control remains within the block in which variable is defined.
 Keyword - auto

 Declaration is done as:


auto int i,j;
Register Storage Class

 This stores the data in CPU registers.


 Since registers are limited, if a CPU register is unavailable, the data is stored as
auto despite being declared with register storage class.
 The details are as follows:
 Storage - CPU registers
 Default initial value - Garbage value (unpredictible)
 Scope - Local to the block in which variable is defined
 Life - Till control remains within the block in which variable is defined.
 Keyword - register

 Declaration is done as:


register int i,j;
External Storage Class

 They are declared outside all functions.


 They are available to all the functions that wish to use them..
 The details are as follows:
 Storage - Memory
 Default initial value - Zero
 Scope - Global
 Life - As long as the program’s execution doesn’t come to an end.
 Keyword - extern

 Declaration is done as:


extern int i;
Static Storage Class

 This hold the value in between function calls.


 Useful as counters.
 The details are as follows:
 Storage - Memory
 Default initial value - Zero
 Scope - Local to the block in which variable is defined
 Life - Value of the variable persists between different function calls.
 Keyword - static

 Declaration is done as:


static int i,j;
Static Storage Class

Static Example Non-static example

OUTPUT: 1 2 OUTPUT: 1 1
References

 Let us C by Yashwant Kanetkar


 EzEd Channel
 TutorialPoint
 Geeksforgeeks.org
Thank you!!!

You might also like