C programming typedef keyword

We have already learnt about the keywords and data types in c programming. In this tutorial we will be learning the typedef keyword in C programming.

C programming typedef keyword

Consider the following example - Suppose we are storing only roll numbers then it would be very useful and meaningful if we have data type “roll”.

typedef keyword - defining types

#include<stdio.h>
int main() 
{
typedef int Roll;
Roll num1 = 40,num2 = 20;
printf("Roll number 1 : %d",num1);
printf("Roll number 2 : %d",num2);
return(0);
}

Explanation of Typedef :

In the above program we want to give another name to an integer data type i.e “roll”. We can do this using typedef statement as below -

typedef int Roll;
Roll num1 = 40,num2 = 20;

However we can also use integer data type after we create another data type “Roll”. typedef keyword would just create similar data type.

Application of typedef keyword

Renaming existing data type

typedef keyword is used to rename the existing data type. suppose you are storing price values in floating variables then it would be useful to have a data type called price, so you can create alias data type of floating point number.

typedef float price

using above statement we have created an alias of floating point data type which will behave similar to floating data type. We can declare variables of price data type using below syntax-

price p1,p2;

Creating structure type

typedef struct
{
  type member1;
  type member2;
  type member3;
}type_name;

Consider following example -

#include<stdio.h>
#include<conio.h>
#include<string.h>
typedef struct student {
   char name[50];
   int marks;
} stud;
void main() {
   stud s1;
   printf("\nEnter student record\n");
   printf("\nStudent name : ");
   scanf("%s", s1.name);
   printf("\nEnter student marks : ");
   scanf("%d", &s1.marks);
   printf("\nStudent name is %s", s1.name);
   printf("\nRoll is %d", s1.marks);
   getch();
}

Whenever we create a structure then we are treated it as new type of data. In C programming typedef keyword gives you facility to rename data type.

In the further chapter (in Structure) we will again go through this chapter in details.

Creating alias for complex type

Suppose you are declaring double pointer like below example -

int **ptr;

It would be helpful and meaningful if you create data type intDoublePointer like below -

typedef int** intDoublePointer;
intDoublePointer p1,p2;

Examples of typedef keyword

Below are some of the examples of typedef keyword in c programming.

C enum : Enumerated Types

Enum [Enumerated] : User defined Data Type in C

Syntax:

enum identifier {value1, value2,.... Value n};
  • enum is ” Enumerated Data Type “.
  • enum is user defined data type
  • In the above example “identifier” is nothing but the user defined data type .
  • Value1,Value2,Value3….. etc creates one set of enum values.
  • Using “identifier” we are creating our variables.

Let’s Take Look at Following Example …
Example :

enum month {JAN,FEB,MAR,APR,MAY,JUN,JUL,AUG,SEP,OCT,DEC};
enum month rmonth;
  1. First Line Creates “User Defined Data Type” called month.
  2. It has 12 values as given in the pair of braces.
  3. In the second line “rmonth” variable is declared of type “month” which can be initialized with any “data value amongst 12 values”.
rmonth = FEB;
  1. Default Numeric value assigned to first enum value is “0”.
  2. Numerically JAN is given value “0”.
  3. FEB is given value “1”.
  4. MAR is given value “2”.
  5. APR is given value “3”.
  6. MAY is given value “4”.
  7. JUN is given value “5”.
  8. and so on…..
printf("%d",rmonth);
  • It will Print “1” on the screen because “Numerical Equivalent” of “FEB” is 1 and “rmonth” is initialized by “FEB”.
  • Generally Printing Value of enum variable is as good as printing “Integer“.

Sample Program :

#include< stdio.h>
void main()
{
int i;
enum month {JAN,FEB,MAR,APR,MAY,JUN,JUL,AUG,SEP,OCT,DEC};
clrscr();
  for(i=JAN;i<=DEC;i++)
      printf("\n%d",i);
}

Output :

01234567891011

Consider -

for(i=JAN;i<=DEC;i++)

Above Statement is
Similar to - [ Replace JAN , DEC with equivalent Numeric Code ]

for(i=0;i<=11;i++)

C integer Data Type

Integer Data Type : About Short Integer & Long Integer

  1. Integers are whole numbers with a wide range of values that are machine dependent.
  2. Integer occupies 2 bytes memory space and its value range limited to -32768 to +32767

How this range is Calculated ?

Size of Integer =  2  bytes
No of Bits      =  16 bits  
                =  MSB bit is considered as Sign Bit
                   (Skip that bit)
                =  16 bits - 1 bit
                =  15 bits

Maximum Range of Integer

Max Range       =  2 Raise to 15
                =  32768

Minimum Range of Integer

Min Range       =  2 Raise to -15
                =  -32768

Types of Integer Data Types :

  1. Integer
  2. Short Integer
  3. Long Integer

Some Important Points :

  • Range of Integer is [2^-15 to 2^+15]
  • Each type is again classified into signed and unsigned integer.
  • Unsigned integer don’t have sign bit , so its range is 0 to 65535
  • A short int requires half the amount of storage than normal integer.
  • Unlike signed integer, unsigned integers are always positive and use all the bits for the magnitude of the number. Therefore the range of an unsigned integer will be from 0 to 65535.
  • The long integers are used to declare a longer range of values and it occupies 4 bytes of storage space.

Syntax of Declaring Integer

int num1;
short int num2;
long int num3;

Example of Integer Data types

  • 500
  • 6764
  • 32100
  • 32766

C data types

What is Data Type in C Programming ?

  1. A Data Type is a Type of Data.
  2. Data Type is a Data Storage Format that can contain a Specific Type or Range of Values.
  3. When computer programs store data in variables, each variable must be assigned a specific data type.

Some of the Data Types Supported by C :

Data TypekeywordDescription
Integer Data TypeintStores the Integer Value
Float Data TypefloatStores the Floating Point Value
Character Data TypecharStores the Single Character Value
Long Data TypelongStores the Long range Integer Value
Double Data TypedoubleStores the long range Floating Value

Explanation :

  1. Whenever we declare variable in Computer’s memory, Computer must know the type of the data to be stored inside the memory.
  2. If we need to store the single character then the size of memory occupied will be different than storing the single integer number.
  3. The memory in our computers is organized in bytes. A byte is the minimum amount of memory that we can manage in C.
  4. A byte can store a relatively small amount of data one single character or a small integer (generally an integer between 0 and 255).

Re-commanded Readings : Data Type

Size Required to Store Variable of Different Data Types

Data TypeBorland C/C++ CompilerVisual C++
Integer2 Bytes4 bytes
Float4 Bytes4 Bytes
Character1 Byte1 Byte
Long4 Byte8 Byte

C enum data type example

#include<stdio.h>
#include<conio.h>
main()
{
 int roll1,roll2;
 enum standard {FIRST,SECOND,THIRD,FOURTH};
 enum standard s1,s2;
 clrscr();
 printf("\n Enter the roll numbers for two students");
 scanf("%d%d",&roll1,&roll2);
 s1=FIRST;
 s2=FOURTH;  /*assigning the standards*/
 printf("\nThe Roll Number %d  is in %d st Standard",roll1,s1+1);
 printf("\nThe Roll Number %d  is in %d th Standard",roll1,s2+1);
 getch();
}

C Deciding integer type

We already know different data types in C Programming. C Provides different type of integer such as - Integer,long,short.

Now our concern is about the usage of the data types. Which integer type we should use to fulfil our requirement. Below is the answer to our question.

Different Type of data types : integer | long | byte

How One Should Decide Which Data Type of Integer we should use in order fulfil our requirement ?

  1. Integer data type Can be Divided can be used up to great extend depending upon application.
  2. Character data type can hold values up to 127
  3. Short int and int data type can hold values up to 32,767
  4. Long integer data type can hold values up to 2,147,483,647.

How to Decide ?

  1. You need to decide type of integer used in program at compile time. We need to analyse the program to get clear picture of the data types used.
  2. Suppose you need to application which will accept a number greater than 1 lac then in that case you should choose higher range of data types.
  3. Suppose you need to use status flags like processor i.e 1 bit for each status then you can use byte data type
  4. Also decide the scope of the variable and the probable use of variable used in program.
  5. Suppose we are using a variable as counter which will count 10 numbers then short int is sufficient to store the data.
  6. In some cases if we are writing program that will accept 4-Digit number then we must use integer or long integer depending upon range specified in the below table.

Reference : data type table

TypeStorage sizeValue range
char1 byte-128 to 127 or 0 to 255
unsigned char1 byte0 to 255
signed char1 byte-128 to 127
int2 or 4 bytes-32,768 to 32,767 or -2,147,483,648 to 2,147,483,647
unsigned int2 or 4 bytes0 to 65,535 or 0 to 4,294,967,295
short2 bytes-32,768 to 32,767
unsigned short2 bytes0 to 65,535
long4 bytes-2,147,483,648 to 2,147,483,647
unsigned long4 bytes0 to 4,294,967,295

Conclusion :
below is the conclusion -

SIZE(char) <= SIZE(short) <= SIZE(int) <= SIZE(long)