You are on page 1of 16

I.

DATATYPES IN C
Each variable in C has an associated data type. Each data type requires different amounts of
memory and has some specific operations which can be performed over it. It specifies the
type of data that the variable can store like integer, character, floating, double, etc. The data
type is a collection of data with values having fixed values, meaning as well as its
characteristics.

The data types in C can be classified as follows:


Types Description

Primitive Data Arithmetic types can be further classified into integer and floating data
Types types.

The data type has no value or operator and it does not provide a result to
Void Types its caller. But void comes under Primitive data types.

User Defined It is mainly used to assign names to integral constants, which make a
DataTypes program easy to read and maintain

The data types that are derived from the primitive or built-in datatypes are
Derived types referred to as Derived Data Types.

Different data types also have different ranges up to which they can store numbers. These
ranges may vary from compiler to compiler. Below is a list of ranges along with the memory
requirement and format specifiers on the 32-bit GCC compiler.
Memory Format
Data Type (bytes) Range Specifier

short int 2 -32,768 to 32,767 %hd

unsigned short int 2 0 to 65,535 %hu

unsigned int 4 0 to 4,294,967,295 %u

int -2,147,483,648 to %d
4 2,147,483,647

long int 4 -2,147,483,648 to %ld


2,147,483,647

unsigned long int 4 0 to 4,294,967,295 %lu

long long int 8 -(2^63) to (2^63)-1 %lld

unsigned long long 0 to


int 8 18,446,744,073,709,551,615 %llu

signed char 1 -128 to 127 %c

unsigned char 1 0 to 255 %c

float 4 1.2E-38 to 3.4E+38 %f

double 8 1.7E-308 to 1.7E+308 %lf

long double 16 3.4E-4932 to 1.1E+4932 %Lf

1. Integer Types:
The integer data type in C is used to store the whole numbers without decimal values. Octal
values, hexadecimal values, and decimal values can be stored in int data type in C. We can
determine the size of the int data type by using the sizeof operator in C. Unsigned int data
type in C is used to store the data values from zero to positive numbers but it can’t store
negative values like signed int. Unsigned int is larger in size than signed int and it uses “%u”
as a format specifier in C programming language. Below is the programming implementation
of the int data type in C.
 Range: -2,147,483,648 to 2,147,483,647
 Size: 2 bytes or 4 bytes
 Format Specifier: %d
Note: The size of an integer data type is compiler-dependent, when processors are 16-bit
systems, then it shows the output of int as 2 bytes. And when processors are 32-bit then it
shows 2 bytes as well as 4 bytes.

// C program to print Integer data types.


#include <stdio.h>
int main()
{
// Integer value with positive data.
int a = 9;

// integer value with negative data.


int b = -9;

// U or u is Used for Unsigned int in C.


int c = 89U;
// L or l is used for long int in C.
long int d = 99998L;

printf("Integer value with positive data: %d\n", a);


printf("Integer value with negative data: %d\n", b);
printf("Integer value with an unsigned int data: %u\n", c);
printf("Integer value with an long int data: %ld", d);

return 0;
}

Output
Integer value with positive data: 9
Integer value with negative data: -9
Integer value with an unsigned int data: 89
Integer value with an long int data: 99998

2. Character Types
Character data type allows its variable to store only a single character. The storage size of the
character is 1. It is the most basic data type in C. It stores a single character and requires a
single byte of memory in almost all compilers.
 Range: (-128 to 127) or (0 to 255)
 Size: 1 byte
 Format Specifier: %c
// C program to print Integer data types.
#include <stdio.h>

int main()
{

char a = 'a';
char c;

printf("Value of a: %c\n", a);

a++;
printf("Value of a after increment is: %c\n", a);

// c is assigned ASCII values


// which corresponds to the
// character 'c'
// a-->97 b-->98 c-->99
// here c will be printed
c = 99;

printf("Value of c: %c", c);

return 0;
}

Output
Value of a: a
Value of a after increment is: b
Value of c: c

3. Floating-Point Types
In C programming float data type is used to store floating-point values. Float in C is used to
store decimal and exponential values. It is used to store decimal numbers (numbers with
floating point values) with single precision.
 Range: 1.2E-38 to 3.4E+38
 Size: 4 bytes
 Format Specifier: %f

// C Program to demonstrate use


// of Floating types
#include <stdio.h>

int main()
{
float a = 9.0f;
float b = 2.5f;

// 2x10^-4
float c = 2E-4f;
printf("%f\n",a);
printf("%f\n",b);
printf("%f",c);

return 0;
}

Output
9.000000
2.500000
0.000200

4. Double Types
A Double data type in C is used to store decimal numbers (numbers with floating point
values) with double precision. It is used to define numeric values which hold numbers with
decimal values in C. Double data type is basically a precision sort of data type that is capable
of holding 64 bits of decimal numbers or floating points. Since double has more precision as
compared to that float then it is much more obvious that it occupies twice the memory as
occupied by the floating-point type. It can easily accommodate about 16 to 17 digits after or
before a decimal point.
 Range: 1.7E-308 to 1.7E+308
 Size: 8 bytes
 Format Specifier: %lf

// C Program to demonstrate
// use of double data type
#include <stdio.h>

int main()
{

double a = 123123123.00;
double b = 12.293123;
double c = 2312312312.123123;

printf("%lf\n", a);

printf("%lf\n", b);
printf("%lf", c);

return 0;
}

Output
123123123.000000
12.293123
2312312312.123123

1. Void Data types


The void data type in C is used to specify that no value is present. It does not provide a result
value to its caller. It has no values and no operations. It is used to represent nothing. Void is
used in multiple ways as function return type, function arguments as void, and pointers to
void.
Syntax:
// function return type void

void exit(int check);

// Function without any parameter can accept void.

int print(void);

// memory allocation function which


// returns a pointer to void.
void *malloc( size_t size);

// C program to demonstrate
// use of void pointers
#include <stdio.h>

int main()
{
int val = 30;
void *ptr = &val;
printf("%d", *(int *)ptr);
return 0;
}

Output
30
We can use the sizeof() operator to check the size of a variable. See the following C program
for the usage of the various data types.

// C Program to print size of


// different data type in C
#include <stdio.h>
int main()
{
int size_of_int=sizeof(int);
int size_of_char= sizeof(char);
int size_of_float=sizeof(float);
int size_of_double=sizeof(double);

printf("The size of int data type : %d\n",size_of_int );


printf("The size of char data type : %d\n",size_of_char);
printf("The size of float data type : %d\n",size_of_float);
printf("The size of double data type : %d",size_of_double);

return 0;
}

Output
The size of int data type : 4
The size of char data type : 1
The size of float data type : 4
The size of double data type : 8

II. ENUMERATION CONSTANTS


In C programming, an enumeration type (also called enum) is a data type that consists of
integral constants. To define enums, the enum keyword is used.

enum flag {const1, const2, ..., constN};

By default, const1 is 0, const2 is 1 and so on. You can change default values of enum
elements during declaration (if necessary).

// Changing default values of enum constants

enum suit {

club = 0,

diamonds = 10,

hearts = 20,

spades = 3,

};
Enumerated Type Declaration:
When you define an enum type, the blueprint for the variable is created. Here's how you can
create variables of enum types.

enum boolean {false, true};

enum boolean check; // declaring an enum variable

Here, a variable check of the type enum boolean is created.


You can also declare enum variables like this.

enum boolean {false, true} check;

Here, the value of false is equal to 0 and the value of true is equal to 1.
Example: Enumeration Type

#include <stdio.h>

enum week {Sunday, Monday, Tuesday, Wednesday, Thursday, Friday, Saturday};

int main()
{
// creating today variable of enum week type
enum week today;
today = Wednesday;
printf("Day %d",today+1);
return 0;
}

Output

Day 4

Why enums are used?


An enum variable can take only one value. Here is an example to demonstrate it,

#include <stdio.h>

enum suit {
club = 0,
diamonds = 10,
hearts = 20,
spades = 3
} card;
int main()
{
card = club;
printf("Size of enum variable = %d bytes", sizeof(card));
return 0;
}

Output

Size of enum variable = 4 bytes

Here, we are getting 4 because the size of int is 4 bytes.


This makes enum a good choice to work with flags.

How to use enums for flags?


Let us take an example,

enum designFlags {
ITALICS = 1,
BOLD = 2,
UNDERLINE = 4
} button;

Suppose you are designing a button for Windows application. You can set
flags ITALICS, BOLD and UNDERLINE to work with text.
There is a reason why all the integral constants are a power of 2 in the above pseudocode.

// In binary

ITALICS = 00000001

BOLD = 00000010

UNDERLINE = 00000100

Since the integral constants are a power of 2, you can combine two or more flags at once
without overlapping using bitwise OR | operator. This allows you to choose two or more flags
at once. For example,

#include <stdio.h>

enum designFlags {
BOLD = 1,
ITALICS = 2,
UNDERLINE = 4
};

int main() {
int myDesign = BOLD | UNDERLINE;

// 00000001
// | 00000100
// ___________
// 00000101

printf("%d", myDesign);

return 0;
}

Output

When the output is 5, you always know that bold and underline is used.
Also, you can add flags according to your requirements.

if (myDesign & ITALICS) {

// code for italics

Here, we have added italics to our design. Note, only code for italics is written inside
the if statement.
You can accomplish almost anything in C programming without using enumerations.
However, they can be pretty handy in certain situations.

III. STORAGE CLASSES

Storage classes in C are used to determine the lifetime, visibility, memory location, and initial
value of a variable. There are four types of storage classes in C

o Automatic
o External
o Static
o Register
Storage Storage Default Scope Lifetime
Classes Place Value

auto RAM Garbage Local Within function


Value

extern RAM Zero Global Till the end of the main program Maybe
declared anywhere in the program

static RAM Zero Local Till the end of the main program,
Retains value between multiple
functions call

register Register Garbage Local Within the function


Value

Automatic
o Automatic variables are allocated memory automatically at runtime.
o The visibility of the automatic variables is limited to the block in which they are
defined.
o The scope of the automatic variables is limited to the block in which they are defined.
o The automatic variables are initialized to garbage by default.
o The memory assigned to automatic variables gets freed upon exiting from the block.
o The keyword used for defining automatic variables is auto.
o Every local variable is automatic in C by default.

Example 1
#include <stdio.h>
int main()
{
int a; //auto
char b;
float c;
printf("%d %c %f",a,b,c); // printing initial default value of automatic variables a, b, an
d c.
return 0;
}
Output:

garbage garbage garbage

Example 2
#include <stdio.h>
int main()
{
int a = 10,i;
printf("%d ",++a);
{
int a = 20;
for (i=0;i<3;i++)
{
printf("%d ",a); // 20 will be printed 3 times since it is the local value of a
}
}
printf("%d ",a); // 11 will be printed since the scope of a = 20 is ended.
}

Output:

11 20 20 20 11

Static
o The variables defined as static specifier can hold their value between the multiple
function calls.
o Static local variables are visible only to the function or the block in which they are
defined.
o A same static variable can be declared many times but can be assigned at only one time.
o Default initial value of the static integral variable is 0 otherwise null.
o The visibility of the static global variable is limited to the file in which it has declared.
o The keyword used to define static variable is static.

Example 1
#include<stdio.h>
static char c;
static int i;
static float f;
static char s[100];
void main ()
{
printf("%d %d %f %s",c,i,f); // the initial default value of c, i, and f will be printed.
}

Output:

0 0 0.000000 (null)

Example 2
#include<stdio.h>
void sum()
{
static int a = 10;
static int b = 24;
printf("%d %d \n",a,b);
a++;
b++;
}
void main()
{
int i;
for(i = 0; i< 3; i++)
{
sum(); // The static variables holds their value between multiple function calls.
}
}

Output:

10 24
11 25
12 26

Register
o The variables defined as the register is allocated the memory into the CPU registers
depending upon the size of the memory remaining in the CPU.
o We can not dereference the register variables, i.e., we can not use &operator for the
register variable.
o The access time of the register variables is faster than the automatic variables.
o The initial default value of the register local variables is 0.
o The register keyword is used for the variable which should be stored in the CPU
register. However, it is compiler?s choice whether or not; the variables can be stored in
the register.
o We can store pointers into the register, i.e., a register can store the address of a variable.
o Static variables can not be stored into the register since we can not use more than one
storage specifier for the same variable.

Example 1
#include <stdio.h>
int main()
{
register int a; // variable a is allocated memory in the CPU register. The initial default v
alue of a is 0.
printf("%d",a);
}

Output:

Example 2
#include <stdio.h>
int main()
{
register int a = 0;
printf("%u",&a); // This will give a compile time error since we can not access the addr
ess of a register variable.
}

Output:

main.c:5:5: error: address of register variable ?a? requested


printf("%u",&a);
^~~~~~

External
o The external storage class is used to tell the compiler that the variable defined as extern
is declared with an external linkage elsewhere in the program.
o The variables declared as extern are not allocated any memory. It is only declaration
and intended to specify that the variable is declared elsewhere in the program.
o The default initial value of external integral type is 0 otherwise null.
o We can only initialize the extern variable globally, i.e., we can not initialize the external
variable within any block or method.
o An external variable can be declared many times but can be initialized at only once.
o If a variable is declared as external then the compiler searches for that variable to be
initialized somewhere in the program which may be extern or static. If it is not, then the
compiler will show an error.

Example 1
#include <stdio.h>
int main()
{
extern int a;
printf("%d",a);
}

Output

main.c:(.text+0x6): undefined reference to `a'


collect2: error: ld returned 1 exit status

Example 2
#include <stdio.h>
int a;
int main()
{
extern int a; // variable a is defined globally, the memory will not be allocated to a
printf("%d",a);
}

Output

Example 3
#include <stdio.h>
int a;
int main()
{
extern int a = 0; // this will show a compiler error since we can not use extern and initia
lizer at same time
printf("%d",a);
}
Output

compile time error


main.c: In function ?main?:
main.c:5:16: error: ?a? has both ?extern? and initializer
extern int a = 0;

Example 4
#include <stdio.h>
int main()
{
extern int a; // Compiler will search here for a variable a defined and initialized somew
here in the pogram or not.
printf("%d",a);
}
int a = 20;

Output

20

Example 5
extern int a;
int a = 10;
#include <stdio.h>
int main()
{
printf("%d",a);
}
int a = 20; // compiler will show an error at this line

Output

compile time error

You might also like