MCQ and Tutorial on C Programming Storage Classes and Data Types
Table of Contents
Learn C Programming Data Types and Data Types and Storage Classes Tutorial and MCQ Questions and Answers on Basics to attend job placement exams, interview questions, college viva and Lab Tests . C Programming Data Types and Storage Classes Tutorial online test , Data Types and Storage Classes MCQ , Data Types and Storage Classes Quiz
C Programming Data Types Tutorial
A programming language should deal with different types of data. The type of data can be a Whole number, Real number or Character string. C language comes with predefined data types like int, long, float, double and char. We use the word define when we are initializing or assigning a constant value to a variable.
Different Types of Buckets hold different types of Data. A bucket can be thought of like a memory location. C Programming Data Types and Storage Classes Tutorial
C Data Types
Basic C data types are as follows.
- int – Integer data type
- short – Integer data type smaller than int
- long – Integer data type bigger than int
- float – Real number data type
- double – Real number data type bigger than float
- long double – Real number data type bigger than double
- char – Character data type
1) C Int Data Type
Integers are whole numbers without any decimal points. Size of an int variable is 4 bytes maximum in a 32 bit compiler. In 16 bit old compilers, size of int is only 2 bytes. ‘u’ is appended to define unsigned int variable. To represent signed variables, keyword ‘signed’ is not required. You can use if you want. Compiler will not through error.
int range
Range of signed int variable is from -2147483648 to 2147483647.
Range of unsigned int variable is from 0 to 4294967295.
10, 35, 876, 001 are int type.
0.5, 10.7, 22.5, 123.456789 are not int type. These are float or double type.
Declaring an Int Variable
int k;
Defining an int Variable
int k=10; int p; p=25; unsigned int a= 334u;
Format Specifier for Reading using Scanf
To read or scan signed int values, format Specifier ‘%d’ is used.
To scan unsigned in values, format specifier ‘%u’ is used.
Format Specifier for Printing using Printf
To print signed int values, format specifier ‘%d’ is used.
To print unsigned int values, format specifier ‘%u’ is used.
2) C Short Data Type
Size of a short variable is 2 bytes which is half the size of an int variable.
short range
Range of signed short variable is from -32768 to 32767.
Range of unsigned short variable is from 0 to 65535.
Declaring and Defining Short Variable
short pp = 123; short qq; qq = 456;
Format Specifier for Reading using Scanf
To read or scan signed short values, format Specifier ‘%d’ is used.
To scan unsigned short values, format specifier ‘%u’ is used.
Format Specifier for Printing using Printf
To print signed short values, format specifier ‘%d’ is used.
To print unsigned short values, format specifier ‘%u’ is used.
3) C Long Data Type
Size of a long variable is 4 bytes. Letter ‘l’ or ‘L’ is used to define signed long variable. Letters ‘lu’ or ‘ul’ is used to define unsigned long variable.
long range
Range of signed long variable is from -2147483648 to 2147483647.
Range of unsigned long variable is from 0 to 4294967295.
Declaring and Defining long variable
long ab = 45L; unsigned long p = 5678912ul;
Format Specifier for Reading using Scanf
To read or scan signed long values, format Specifier ‘%ld’ is used.
To scan unsigned long values, format specifier ‘%lu’ is used.
Format Specifier for Printing using Printf
To print signed long values, format specifier ‘%ld’ is used.
To print unsigned long values, format specifier ‘%lu’ is used.
4) C Float Data Type
Float numbers are simply real numbers with decimal points. You can append letter ‘f’ to define a floating point constant. Size of a float variable is 4 bytes.
3.1415 is a double data type constant.
3.1415f is a float data type constant. Use an explicit ‘f’ letter.
Declaring and Defining float variable
float ab = 3.14f; float p = (float)5678.1234; float q; q = 45.678f;
float range
Range of floating point variable can be from -3.4e38 to +3.4e38.
Format Specifier for Reading using Scanf
To read or scan float values, format Specifier ‘%f’ is used.
Format Specifier for Printing using Printf
To print float values, format specifier ‘%f’ is used.
5) C Double Data Type
C double data type is two times the size of a float variable. It can hold a real number with 8 bytes memory. Any real number without any appending letter is a double data type constant.
3.14 is a double data type constant.
Declaring and Defining double variable
double score= 98.12345678;
double range
Range of double variable can be from -1.7e308 to +1.7e308.
Format Specifier for Reading using Scanf
To read or scan double values, format Specifier ‘%lf’ is used.
Format Specifier for Printing using Printf
To print double values, format specifier ‘%lf’ is used.
6) C Long Double Data Type
C long double data type is two times the size of a double variable. It can hold a real number with 10 bytes memory. Any real number with an appending letter ‘l’ or ‘L’ is a long double constant.
3.14L is a long double data type constant.
Declaring and Defining double variable
long double score= 98.12345678L;
long double range
Range of long double variable can be from -1.7e4932 to +1.7e4932.
Format Specifier for Reading using Scanf
To read or scan long double values, format Specifier ‘%Lf’ is used.
Format Specifier for Printing using Printf
To print long double values, format specifier ‘%Lf’ is used.
7) C Char Data Type
C char data type is used to represent or store letters and symbols. Its size is only 1 byte.
ASCII – American Standard Code for Information Interchange defined integer codes for each letter, number and symbol.
Declaring and Defining char variable
char group= 'M';
char range
Range of signed char variable can be from -128 to + 127.
Range of unsigned char variable can be from 0 to + 255.
Format Specifier for Reading using Scanf
To read or scan character values, format Specifier ‘%c’ is used.
Format Specifier for Printing using Printf
To print character values, format specifier ‘%c’ is used.
what are storage classes in c
A Storage Class in C language decides the following.
- Where to store a variable in memory
- Default value to be assigned to the variable at the time of creation.
- Scope of a variable within which the variable can store value and outside which the variable is not accessible.
- Life of the variable or the amount of time variable is kept for use.
Each and every variable in C language requires both Data Type and Storage Class. By default, ‘auto’ storage class is applied to a variable.
how many storage classes in c
There are four types of Storage Classes in C language. Keywords used to declare or define C storage classes are auto, register, static and extern.
- Auto Storage Class or Automatic Storage Class
- Register Storage Class
- Static Storage Class
- Extern Storage Class
1. Auto Storage Class
An ‘auto’ storage variable is stored in memory. This memory is called Stack Memory.
Default value of an auto storage variable is garbage value or random value.
Scope of an auto variable is local to the block where it is defined.
Life of an auto variable is till the execution control stays within the block where it is defined.
In the below example, variable ‘a’ is declared without ‘auto’ keyword. But the compiler treats variable like auto variable only. There is no need to explicitly mention ‘auto’ storage class. It is applied to variables by default.
int main() { int a; //same as auto int a; auto int b; printf("%d %d", a, b); return 0; }
As there is no value assigned to variables, output of the above program is just garbage values. A garbage value is simply a random number of declared type. Here the random value is int type.
int main() { auto int a=20; { int a=10; printf("%d",a); } printf("%d",a); return 0; } //output = 10 20
In the above example, outer a=20 is different from inner a=10. Curly braces define a block in C language. So, a=10 is cleared from memory after the block. So after printing a=10, execution control goes to outer printf to print a=20. After completion of the block, scope and life of an ‘auto’ variable dies.
In the above infographic image, an auto variable is compared with an AUTO CLOSE BOOK. For each new person, page number of the book is new and random. There is no stored value between function calls.
2. Register Storage Class
A Register storage variable is similar to ‘auto’ storage variable. Only difference between an auto variable and register variable is the memory location where the variable is stored. Register variables are stored in CPU Registers. These registers are extra memory locations along with RAM memory locations. CPU registers allow fast reading and writing of data. So processor need not ask RAM to read and write data, thus saving time.
Default value of a Register storage variable is garbage value.
Scope of a Register storage variable is local to the block where it is defined.
Life of a Register variable is till the execution control stays within the block where it is defined.
int main() { register int a=1; while(a<10) { printf("%d ", a); a++; } }
Register storage class variables are often used with loop counters or iteration variables to reduce execution time on repetitive works. Also, if a variable is used at many places in a program, then it is better to declare that variable under Register storage class to save time.
The number of CPU registers are always limited. So there is no guarantee that the storage class ‘register’ ensures storing the variable in CPU registers. You do not get any error. CPU treats the variable like an auto storage class variable and uses stack memory without any warning.
Also note that the size of a CPU register may be 16 bit or 32 bit. If the variable size is more than the size of the register, CPU automatically places the variables in auto storage class. Suppose you want to store a float variable in Register storage class of 16 bit register CPU. Even if you declare explicitly like register float abc=1.2f;, it is treated like an automatic storage class variable.
3. Static Storage Class
A static storage class variable retains its value between function calls. This is called persistence. These static variables die only with the program execution. In case of auto storage variable and register storage variable, value is reset to default after the block or function execution ends. So these auto and register variables can not retain value between function calls.
Static variables are stored in Data Segment Memory instead of Stack Memory.
Default value of a static variable is Zero.
Scope of a static variable is with in the block or function where it is declared.
Life of a static variable exists till the program execution ends.
Example 1:
In the image above with a computer and books on a table, a static variable is like a book with a pen. You can keep pen to maintain page number. Even if you close the book (function or block ends), pen maintains the book number. Next time you open the book (subsequent function or block calls), last page number is maintained.
Example 2:
If you open many windows in a computer and press sleep button, the computer goes to sleep. Once you wake the PC again, all programs are restored with the previous state. Similarly, the value of a static variable persists between function calls. Once you shutdown (program execution ends) the computer, all programs (variables) are removed from memory.:
Example Program:
int main() { display(); display(); } void display() { //invoked only for very first function call static int a=10; static int c; //executed everytime fresh as it is just an assignment c=30; //invoked fresh every time auto int b=20; a++; b++; c++; printf("%d %d %d\n", a, b, c); } //output //11 21 31 //12 21 31
4. Extern Storage Class
Extern or External storage class variables are called global variables. These variables are available to all functions of that C Program. These variables are defined outside of all functions. Variables that are declared inside a function or block are called local variables.
Extern variables are stored in Memory or RAM.
Default value of an extern storage class variable is Zero like static variable.
Scope of an extern variable is global or the entire program.
Life of an extern variable is until the program execution ends.
When to use external variables?
Declare and define an extern variable when you want to make the variable available to all functions of the C program. Also, if you want to make the variable available to functions outside the C program where you defined it.
To use external variables in your program, you need to do two things.
- Use include<> statement to import the C files into your program.
- Declare the extern variable in other C files with an ‘extern’ keyword.
C Programming Storage Classes
Example 1:
int stock=5; int main() { display(); stock++; display(); } void display() { printf("Stock=%d\n",stock); } //output //Stock=5; //Stock=6; //variable stock is available to both main() and display() functions.
C Storage Classes
Example 2:
To use a variable that is not defined until the execution point use ‘extern’ keyword.
int main() { extern int stock; printf("Stock=%d\n",stock); } extern int stock=5;
In other programming languages, the order of definition of global variables is not important.
C Programming Storage Classes
Example 3:
You can make your program variable available to included C Files using the extern keyword.
#include<display.c> void showme(); int stock=10; int main() { printf("Stock=%d\n",stock); showme(); } //display.c file extern int stock; void showme() { stock++; printf("Stock=%d", stock); } //output // Stock=10 // Stock=11
In other programming languages, the order of definition of global variables is not important.
[WpProQuiz 21]
what are storage classes in c MCQ
1) Identify wrong C Keywords below.
A) auto, double, int, struct
B) break, else, long, switch
C) case, enum, register, typedef
D) char, extern, intern, return
Answer [=] D
Explanation:
‘intern’ is not a keyword. Remaining are all valid keywords.
2) Identify wrong C Keywords below.
A) union, const, var, float
B) short, unsigned, continue, for
C) signed, void, default, goto
D) sizeof, volatile, do, if
Answer [=] A
Explanation:
‘var’ is not a valid keyword.
3) Identify wrong C Keywords below.
A) static, while, break, goto
B) struct, construct, signed, unsigned
C) short, long, if, else
D) return, enum, struct, do
Answer [=] B
Explanation:
construct is not a keyword.
All 32 Keywords are given for reference. 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.
4) Find a correct C Keyword below.
A) breaker
B) go to
C) shorter
D) default
Answer [=] D
5) Find a correct C Keyword below.
A) work
B) case
C) constant
D) permanent
Answer [=] B
6) Find a correct C Keyword.
A) Float
B) Int
C) Long
D) double
Answer [=] D
Explanation:
All C Keywords are in lower case.
7) Types of Integers are.?
A) short
B) int
C) long
D) All the above
Answer [=] D
Explanation:
Size of int < long.
8) Types of Real numbers in C are.?
A) float
B) double
C) long double
D) All the above
Answer [=] D
Explanation:
Size of float < double < long double
9) signed and unsigned representation is available for.?
A) short, int, long, char
B) float, double, long double
C) A & B
D) None of the above
Answer [=] C
Explanation:
Real numbers like float, double and long double do not support unsigned representation.
10) Size of a Turbo C C++ compiler is.?
A) 16 bit
B) 32 bit
C) 64 bit
D) 128 bit
Answer [=] A
11) Size of a GCC or Visual Studio C Compiler is.?
A) 16 bit
B) 32 bit
C) 64 bit
D) 128 bit
Answer [=] B
12) Sizes of short, int and long in a Turbo C C++ compiler in bytes are.?
A) 2, 2, 4
B) 2, 4, 4
C) 4, 8, 16
D) 8, 8, 16
Answer [=] A
13) Sizes of short, int and long in Visual Studio or GCC compiler in bytes are.?
A) 2, 2, 4
B) 2, 4, 4
C) 4, 4, 8
D) 4, 8, 8
Answer [=] B
14) Range of signed char and unsigned char are.?
A) -128 to +127 0 to 255
B) 0 to 255 -128 to +127
C) -128 to -1 0 to +127
D) 0 to +127 -128 to -1
Answer [=] A
Explanation:
Advantage of an unsigned representation is only to increase the upper limit i.e positive limit. Size of a char remains same i.e 1 Byte.
15) Ranges of signed int and unsigned int are.?
A) 0 to 65535 -32768 to +32767
B) -32768 to +32767 0 to 65535
C) -32767 to +32768 0 to 65536
D) 0 to 65536 -32767 to +32768
Answer [=] B
Explanation:
Default assumption is Turbo C/C++, 16 bit compiler. Size of an int is 2 bytes for both signed and unsigned representation.
16) Size of float, double and long double in Bytes are.?
A) 4, 8, 16
B) 4, 8, 10
C) 2, 4, 6
D) 4, 6, 8
Answer [=] B
Explanation:
Real numbers are represented in float, double and long double format.
eg. float interest = 12.55f;
17) Range of singed long and unsigned long variables are.?
A) -2147483647 to +2147483648 0 to 4294967295
B) -2147483648 to +2147483647 0 to 4294967296
C) -2147483648 to +2147483647 0 to 4294967295
D) 0 to 4294967295 -2147483648 to +2147483647
Answer [=] C
Explanation:
Size of a long variable is 4 Bytes or 32 bits.
(2)^32.
18) Range of float variable is.?
A) -3.2e38 to +3.2e38
B) -3.8e32 to +3.8e32
C) -3.4e34 to +3.4e34
D) -3.4e38 to +3.4e38
Answer [=] D
Explanation:
e represents exponential.
19) Left most bit 0 in Singed representation indicates.?
A) A Positive number
B) A Negative Number
C) An Unsigned number
D) None of the above
Answer [=] A
Explanation:
For negative numbers 1 is used as a left most bit.
20) If you do not specify a storage class for a Variable.?
A) You get compiler error.
B) You get a compiler warning.
C) Output is null always
D) None of the above
Answer [=] D
Explanation:
Yes. Even if you do not specify a Storage class for a Variable, AUTOMATIC storage class is applied.