Data Types in C and Storage Classes Tutorial and MCQ
Table of Contents
Learn Data Types in C Tutorial and MCQ Questions and Answers on Basics to attend job placement exams, interview questions, college viva and Lab Tests . C Programming Data Types Tutorial online test
Data Types in C
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 Tutorial
Data Types in C
Basic Data Types in C 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) Int Data Type in c
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) Short Data Type in c
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) Long Data Type in c
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) Float Data Type in c
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) Double Data Type in c
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) Long Double Data Type in c
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) Char Data Type in c
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.
C Programming Storage Classes Tutorial
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.
C Storage Classes
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.
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.
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.
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 23]
Data Types in C MCQ
1) Choose a correct statement regarding automatic variables.
A) #include<stdio.h>
main() { auto int a; printf("%d", a); } //output is compiler error. a is not initialized.
B) #include<stdio.h>
main() { auto int a; printf("%d", a); } //output = 0
C) #include<stdio.h>
main() { auto int a; printf("%d", a); } //output = null
D) #include<stdio.h>
main() { auto int a; printf("%d", a); } //output = some random number
Answer [=] D
Explanation:
Yes. If an integer is not initialized, some random number in integer range will be displayed.
2) What is the output of the program.? #include<stdio.h>
int main() { printf("Hello Boss."); }
A) Hello Boss.
B) hello boss
C) No output
D) Compiler error
Answer [=] D
Explanation:
Notice the return type int before main() method. So main should maintain a return 0; or return somenumber; statement.
3) What is the output of the program.?
int main() { auto int a=10; { auto int a = 15; printf("%d ", a); } printf("%d ", a); return 1; }
A) 10 10
B) 10 15
C) 15 10
D) Compiler error
Answer [=] C
Explanation:
Automatic or Register variables have block scope and life. Most recent definition takes precedence. {…} is a block.
4) What is the output of the program.?
int main() { register a=10; { register a = 15; printf("%d ", a); } printf("%d ", a); return 20; }
A) 15 20
B) 15 10
C) 10 15
D) 15 15
Answer [=] B
Explanation:
Automatic and Register variables have only Block Scope. Always local definition takes precedence.
5) What is the output of the C Program statement.?
register int b; prinf("%d", b);
A) null
B) 0
C) random integer number
D) random real number
Answer [=] C
Explanation:
auto and register variables hold garbage values by default.
6) What is the output the program.?
int main() { register a=80; auto int b; b=a; printf("%d ", a); printf("%d ", b); return -1; }
A) Compiler error. You can not assign register value to int variable.
B) 80 80
C) 80 0 Register value can not be copied.
D) Compiles, but output is none.
Answer [=] B
7) What is the output of the program.?
void myshow(); int main() { myshow(); myshow(); myshow(); } void myshow() { static int k = 20; printf("%d ", k); k++; }
A) 20 20 20
B) 20 21 21
C) 20 21 22
D) Compiler error.
Answer [=] C
Explanation:
Variable of type static holds its value until the end of program execution. Static variables do not initialize again and again with function calls.
8) What is the output of the program.? #include<stdio.h>
static int k; int main() { printf("%d", k); return 90; }
A) -1
B) 0
C) 90
D) Compiler error
Answer [=] B
Explanation:
Default value of a static variable is zero by default.
9) What is the output of the program.?
int main() { register k = 25; printf("%d", &k); return 90; }
A) prints of address of variable k.
B) 25
C) 0
D) Compiler error
Answer [=] D
Explanation:
You can not use Ampersand & operator with a Register variable. Register is part of CPU and accessing it is not authorized.
10) The statement below is …..a
extern int p;
A) Declaration
B) Definition
C) Initialization
D) None of the above
Answer [=] A
Explanation:
Usually, most of the C statements with extern keyword are Declarations.