Typedef Vs Define In C

typedef:

A typedef in C programming serves as a keyword that establishes a fresh moniker for current data types. However, it does not introduce a novel data type to the predefined ones. Instead, it assigns descriptive titles to already established data types like int, char, float, and so on. This definition is typically positioned outside the main function within a program. Essentially, typedef is employed to rename the existing data types in C programming.

Syntax

Example

typedef data_type newName
or typedef <existing_name> <alias_name>

In the provided syntax, the current name represents the predefined data types or variable name. The ' alias name ' or newName specifies the alternative name for an existing data type or variable in a C program.

Example 1: Imagine a scenario where a program is utilizing the typedef keyword within the C programming language.

Type.c

Example

#include <stdio.h>
typedef int Length; // typedef provide the int data type to a new name as Length 
int main()
{
Length num1, num2, sum; // here Length variable is treated as the int data type
printf(" Enter the first number:");
scanf(" %d", & num1);
printf(" Enter the second number:");
scanf(" %d", & num2);
sum = num1 + num2;
printf(" The sum of the two numbers is: %d", sum);
return 0;
}

Output:

Output

Enter the first number: 20
Enter the second number: 10
The sum of the two numbers is: 30

Example 2: Let's explore a different scenario where we employ the typedef with unsigned char in the C programming language.

program.c

Example

#include <stdio.h>
#include <conio.h>
typedef unsigned char Bit;
int main()
{
Bit B1, B2;
B1 = 'D';
B2 = 'E';
printf( " Demonstrate the use of typedef Keyword ");
printf (" Print a single Byte: %c", B1);
printf (" Print a single Byte: %c", B2);
return 0;
}

Output:

Output

Demonstrate the use of typedef Keyword
Print a single Byte: D
Print a single Byte: E

Use of typedef keyword in Structure

Let's explore a code example that employs the typedef keyword within a structure to assign a different name to the struct and set initial values for the structure variables, as shown below:

Struct.c

Example

#include <stdio.h>
#include <string.h>
 
typedef struct Students {
   int roll_no;
   char name[50];
   char Subject[60];
   char teacher_name[80];
} Stud;
 
int main( ) {

   Stud S1;
   S1.roll_no = 30;
   strcpy( S1.name, "Lockie Ferguson");
   strcpy( S1.Subject, "Mathematics"); 
   strcpy( S1.teacher_name, "Jasmine");
   
 
   printf( "Student Roll No: %d\n", S1.roll_no);
   printf( "Student Name: %s\n", S1.name);
   printf( "Student Subject: %s\n", S1.Subject);
   printf( "Student Teacher Name: %s\n", S1.teacher_name);

   return 0;
}

Output:

Output

Student Roll No: 30
Student name: Lockie Fergusion
Student Subject: Mathematics
Student Teacher Name: Jasmine

In the given program, the typedef keyword is employed to create an alias for the struct named Students, which is then utilized to declare and assign values to variables within it.

define

A #define statement is a preprocessor directive that is employed to define constant aliases for different data values. This feature is utilized to specify constant variables for diverse data types within the C programming language. These definitions are typically declared outside the main body of the program.

Syntax

Example

#define token value

In the provided syntax, the #define directive serves as a pre-processor command that assigns a token as a constant variable identifier, with the assigned value representing the token's value. Once the constant variable is set up in the pre-processor, we are able to access and utilize the specified value by referencing the variable within the program.

Example 1: Let's explore a sample code demonstrating the utilization of the #define pre-processor directive in the C programming language.

Define.c

Example

#include <stdio.h>
#include <conio.h>
#define PI 3.14 // define the constant variable 
void main()
{
printf (" Display the PI Constant value using the #define is: %f", PI);
getch();
}

Output:

Output

Display the PI Constant value using the #define is: 3.14

Consider an example where a program is written in C to calculate the area of a circle utilizing the #define pre-processor directive.

Area.c

Example

# include <stdio.h>
#include <conio.h>
#define PI 3.14 // define the constant variable 
void main()
{
int r, area;
printf (" Enter the radius of the circle: ");
scanf ("%d", &r);
area = PI * r * r;
printf (" The area of the circle is: %d", area);
getch();
}

Output:

Output

Enter the radius of the circle : 5
The area of the circle is: 78

In the provided code, we utilize PI as a constant variable by employing the #define preprocessor directive. Consequently, when the code is run, the value of PI is automatically substituted into the program.

Example 3: Let's examine a code example showcasing the significance of typedef compared to #define when dealing with data types.

Pgrm.c

Example

#include <stdio.h>
#include <conio.h>
typedef char* ptr;
#define PTR char
int main()
{
ptr x, y, z;
PTR p, q, r;
printf (" sizeof x : %d\n", sizeof(x) );
printf (" sizeof y : %d\n", sizeof(y) );
printf (" sizeof z : %d\n", sizeof(z) );
printf (" sizeof p : %d\n", sizeof(p) );
printf (" sizeof q : %d\n", sizeof(q) );
printf (" sizeof r : %d\n", sizeof(r) );
return 0;
}

Output:

Output

Sizeof x:8
Sizeof y:8
Sizeof z:8
Sizeof p:8
Sizeof q:1
Sizeof r:1

Difference between the typedef and the #define in C

Here are the variances between typedef and #define:

SN Typedef #define
1. Typedef is a keyword in the C programming language. #define is a pre-processor and used as macro used in C programming.
2. It is a keyword used to provide an alternate name to the existing data types only. And that name can be used to initialize the variables in a program. A #define is used to define an alias for values.
3. The compiler performs it. The pre-processor performs it.
4. It uses a semicolon to terminate the statement. It does not use a semicolon to terminate the statement.
5. It defines the actual definition of a new data type. A #define is used to just copy-paste the define values where it calls or uses.
6. It follows the scope rule that defines if a new data type is in scope (inside a function), the new type name will only be visible till the scope of the data type. When the #define pre-processor encounters in a program, it replaces all the occurrence of the variables with the defined values; after that, no scope is followed.
7. Define outside of the main() function. Define outside of the main() function.

Input Required

This code uses input(). Please provide values below: