Data Types in C Language

Preview

Data types in C Language

Data type is the way to specify the type of data as well as the range of values that can be stored in the data object or a variable. C supports three types of data types:-

  • Primary Data Types
  • Derived Data Types
  • User Defined Data Types

Primary Data Types

Primary data types are those which are provided by C language. There are four primary data types provided by C as follows:

  • Integer
  • Floating point
  • Character
  • Void

 Integer Data Type

Integer data type is used to declare variables which can contain integer values i.e. the variable of integer data type can contain numeric values without the decimal point.

Different Integer data types are:

Name

of

Data Type

Size

in

Bytes

Range

of

Values

short1

-27 to 27 –1

(-128 to 127)

signed short1

-27 to 27 –1

(-128 to 127)

unsigned short1

0 to 28-1

(0 to 255)

int2

-215 to 215 –1

(-32768 to 32767)

signed int2

-215 to 215 –1

(-32768 to 32767)

unsigned int2

0 to 216-1

(0 to 65535)

long4

-231 to 231 –1

(-2,147,483,648 to 2,147,483,647)

signed long4

-231 to 231 –1

(-2,147,483,648 to 2,147,483,647)

unsigned long4

0 to 231 –1

(0 to 4,294,967,295)

 

Floating point Data Type

Floating point data type is used to declare variables which can contain values which can contain decimal point. These variables can take positive as well as negative values.

Different types of floating point data type are:

Name

of

Data Type

Size

in

Bytes

Range

of

Values

float4±3.4X10-38 to ±3.4X 10+38
double8±1.7X10-308 to ±1.7X 10+308
Long double10±3.4X10-4932 to ±3.4X 10+4932

 Character Data Type

Character data type is used to declare variables which can contain any value which can include alphabets, digits, special characters as well as spaces.

Character data type can be categorized into following types:

Name

of

Data Type

Size

in

Bytes

Range

of

Values

char1

-27 to 27 –1

(-128 to 127)

signed char1

-27 to 27 –1

(-128 to 127)

unsigned char1

0 to 28 –1

(0 to 255)

 

void

void means null or empty. This data type is basically used to specify that a function doesn’t return anything.

Derived Data Types

Derived data types are those which are derived from primary data types.  There are following types of secondary data types provided by C Language.

Array

It is defined as collection of homogeneous types of elements having same size, type and range of values which can be stored in it. Arrays are basically categorized into following types:-

  • One Dimensional Array
  • Two Dimensional Array
  • Multi Dimensional Array

Function

It is defined as named block of code which can be called anywhere in the same program or another program.

A function reduces the length as well as complexity of the program. Functions are the basic unit of program development in C language.

Functions are basically categorized into two types:-

  • Library Functions
  • User Defined Functions

Pointer

It is basically used to store the address of another variable. Pointers provide faster execution of program. They also help for efficient utilization of memory. 

They can also be used to write very complex programs to solve various complex problems. There are two operators used with pointers

  • * (Pointer to)
  • & (Address of)

const

The keyword const can be used to declare a named constant. The declaration of named constant looks like a normal variable declaration except that the declaration is prefixed with the keyword const.

Example: const int a=10;

User Defined Data Types

User defined data types are those which are designed by the programmer depending upon his requirement.There are following types of user defined data types provided by C programming language.

Structure

It is defined as collection of heterogeneous types of elements. Structure can contain a multiple variables of different data types under a single name of structure.

Example:

struct student
{
int rollno;
char name[20];
};

Union

It is also defined as collection of heterogeneous types of elements. Union may contain a number of variables of different data types under a single name of union.

Unions are basically used where we need to use anyone of the members of a union at a time. A union starts with the keyword union.

Example: 

union student
{
int rollno;
char name[20];
};

typedef

typedef is used to give a new name to the existing data type. The syntax for using typdef is as follows:

typedef <data_type> <new_name>

Here, typedef is a keyword.

data_type refers to the name of an existing data type which we want to give a new name.

new_name is the user defined name given to the existing data type.

Example:

typedef  int distance.

In this example, data type int has been named as distance. We can use the word distance as a data type to declare variables of int type as:

distance one,two;

 Program to demonstrate the use of typedef

#include<stdio.h>
#include<conio.h>
void main()
{
typdef  int distance;
distance one;
clrscr();
one=1500;
printf(”\n one=%d”,one);
getch();
}

Output:
one=1500

enum

enum stands for enumerated. enum is used to define a sequence of named constants which contain consecutive values starting from 0 (zero).

The value of any constant depends upon the value of previous constant in the list. We can define an enumerated data type as:

enum <data_name> { List_of_named_constants };

enum is a keyword to define an enumerated data type.

data_name is user defined name for enumerated data type.

List_of_named_constants specifies the list of named constants which will become part of enumerated data type.

Example:

enum month{jan, feb, mar, apr, may=10, jun, jul, aug, sep, oct, nov, dec};

In this example, JAN has been assigned value 1, so FEB will automatically contain 2, and so on. MAY has been assigned 10 so JUN will contain value 11 and JUL will contain 12 and so on.

Program to demonstrate the use of enum

#include<stdio.h>
#include<conio.h>
void main()
{
enum month{jan, feb, mar, apr, may, jun, jul, aug, sep, oct, nov, dec};clrscr();
month m;
m=MAR;
printf(”m=%d”,m);
printf(”\nDEC=%d”,DEC);
getch();
}

Output:
m=3
DEC=12

Symbolic Constant

Symbolic constant is way to define a named constant in a C program. Its value can’t be changed during the program execution.

Declaration of a symbolic constant always starts with # followed by the keyword define which is further followed by the name of symbolic constant further followed by the value to be stored in the symbolic constant.

There should be no semicolon after declaring a symbolic constant.

Example

#define PI 3.141

In this example PI is the name of symbolic constant and it contains value 3.141.

Program to demonstrate the use of symbolic constant

#include<stdio.h>
#include<conio.h>
#define PI 3.141
void main()
{
int  r=1,square;
square=PI*r*r;
printf(“\nSquare=%d”,square);
getch();
}

Output:

Square=3.141

const keyword

We can define named constants in C language using keyword const. Its value can’t be changed during the program execution.

Example

const int PI=3.141;

In this example PI is the name of the named constant and it contains value 3.141.

Program to demonstrate the use of named constant

#include<stdio.h>
#include<conio.h>
const int PI=3.141;
void main()
{
int  r=1,square;
square=PI*r*r;
printf(“\nSquare=%d”,square);
getch();
}

Output:
Square=3.141

volatile keyword

volatile keyword is a qualifier that is applied to a variable when it is declared. It tells the compiler that the value of the variable may change at any time without any action being taken by the code that compiler finds nearby.

volatile int a=10;

In this example a is the name of the volatile variable and it contains value 10.

Program to demonstrate the use of keyword volatile

#include<stdio.h>
#include<conio.h>
void main()
{
volatile a=10;
clrscr();
printf(“\na=%d”,a);
getch();
}

Output:
a=10
 
2.6 Comments
These are user defined messages written inside a program which may specify the name of the program, name of programmer, purpose of the program, purpose of any statement or any other information which can help at a later stage for quick review and easy understanding of the program.

Comments start with /* (Slash asterisk) and end with */ (Asterisk Slash). They can also be used if the comment goes across multiple lines.

Program to demonstrate the use of comments

/*
Developed By:
Lovejot
*/
void main() /*Program starts here*/
{
printf(”Comments are represented here”);
} /*program ends*/

Output:
Comments are represented here
 

Tips and tricks

  1. Variable is a name given to an existing memory location.
  2. The value of a variable can be changed in a program. It can be changed multiple times as per the requirement of the program.
  3. unsigned i  means i is an unsigned int.
  4. variables declaration should be the first statement in main(). Variables can’t be declared everywhere in the program.
  5. By default real constants are treated as double.
  6. The naming rules for variable as well as identifier are same.
  7. void is a keyword as well as a data type whose variable can’t be created.

Interview Questions

Q1. Is it acceptable to declare/define a variable in a C header file?
Variables should not be defined in header files, because the header file can be included in multiple source files, which would cause multiple definitions of the variable.

Q2.What is an lvalue and an rvalue?

An lvalue is an expression to which a value can be assigned. The “lvalue” expression is located on the left side of an assignment statement, whereas an “rvalue” is located on the right side of an assignment statement.

Q3. Where can variables be declared?

  • Before start of the main( ) function.
  • Within the main function.
  • Inside function definition after the opening {
  • In  a block of code, after the {
  • Global or external variable Declared before the start of the main( ) function.

Test Your Knowledge

Q1. What do you know about character set of C?

Q2. Explain token and its types.

Q3. Define a keyword. Write names of various keywords of C.

Q4. Explain an identifier. Write various rules to name an identifier.

Q5. Define a variable. How can we declare and initialize a variable?

Q6. Explain constant and its types.

Q7. What is an escape sequence? Explain various escape sequences available in C.

Q8. What is the difference between ‘A’ and “A” ?

Q9 Explain various data types of C.

Q10. Explain a symbolic constant.

Q11. Define a comment. Write its use. How can we use a comment in a C program.

Q12. Which are invalid variable names and why?

  • Rs1000 (ii) $250           (iii) Father’s name        (iv) student-name

(v)        Raghav parsad      (vi)newvalue1  (vii) India@india.com  (viii) Factorial

 Q13 Find out errors in following statements:

(a) float k = 3.5 *9 ;

(b) studentname = Ajay ;

(c) char number= 3 ;

(d) (a*a+b*b)=c;

Q14 Write a program for the following:

(i) i = p * r * t / 100 ;

(ii) area = ∏ r2

(iii) z =  x+y /x-y

Q15 Specify the difference between a variable definition and a variable declaration?

Q16 Where can we defind variables in C?

Q17 What do you know about const keyword?

Q18 Can comments be nested?

Q19 Write number of bytes allocated to the following string?

char txt [20] = “Hello world!\0”;

Q20. What is the maximum value an integer constant can have?

 

Best Books of C

Lesson tags: comment in c, const in c, Data types of c, enum in c, enumeriated data type in c.symbolic constant in c, floating point data types in c, in c, integer data types, volatile in c, what is lvalue
Back to: C Programming Language