×

Typedef vs define in C

Typedef VS define in C

Typedef

In the C programming language, a keyword called typedef can be used to give a type a new name. In other words, it is used to provide meaningful names to already existing variables in the program. It acts similarly as we define initially as a developer defines the alias name for the following commands. This keyword is used to re-define the name of an already present variable in the C standard.

Syntax

             typedef <existing name> <alias name>
                                     Or
             typedef type TYPE_NAME; 

Where;

            Existing name: Name of an existing variable.

            Alias name: Name given to the existing variable by a developer.

E.g.:

 #include <stdio.h>
 #include <conio.h>
 #include <string.h>
 typedef int tutorial;  /*typedef provides the data type int to a new name or an alias name
                                     as tutorial*/
 int main()
 {
 tutorial n1, n2, n3, sum; /* the alternate name tutorial will be treated as an int data
 type */
 printf (“ Enter the first number n1 = \n”);
 scanf (“ %d \n”, &n1);
 printf (“ Enter the second number n2 = \n”);
 scanf (“ %d \n”, &n2);
 printf (“ Enter the third number n3 = \n”);
 scanf (“ %d \n”, &n3);
 sum = n1 + n2 + n3;
 printf (“The of three numbers n1, n2, and n3 is: %d”, sum);
 return 0;
 } 

Output

 Enter the first number n1 = 10
 Enter the first number n1 = 20
 Enter the first number n1 = 30
 The of three numbers n1, n2, and n3 is: 60 

For instance, we can define a term BYTE for one-byte numbers such as:

typedef unsigned char BYTE;

Once an alias name ‘BYTE’ is given to a pre-defined term ‘unsigned char’, the identifier ‘BYTE’ can be used as an abbreviation for the same.

E.g.:

            BYTE b1, b2;

The uppercase letters used in the above example definitions is to remind the programmer that the type name is just a symbolic representation. One can use either uppercase, or lowercase or a combination of both uppercase and lowercase letter while defining in the beginning.

E.g.:

            typedef unsigned char byte;

Typedef can also be used to give a name to a user defined data type also. Typedef can be used along with the structure to define a new data type and then can be used to define the structure variables as such.

 #include <stdio.h>
 #include <conio.h>
 #include <string.h>
 typedef struct Books
 {
 char title[50];
 char author[50];
 char subject[100];
 int book_id;
 } Book;
 int main( )
 {
 Book book;
 strcpy( book.title, "C Programming");
 strcpy( book.author, "Dennis Ritchie");
 strcpy( book.subject, "Tutorials and examples");
 book.book_id = 110291;
 printf( "The title of the book is: %s \n", book.title);
 printf( "The author of the book is : %s\n", book.author);
 printf( "The subject of the study is : %s\n", book.subject);
 printf( "The ID of the book is (book_id) : %d\n", book.book_id);
 return 0;
 } 

Output

 The title of the book is: C programming
 The book of the book is: Dennis Ritchie
 The subject of the study is: Tutorials and examples
 The ID of the book (book_id) is: 110291 

Define

Define is a preprocessor that is used to describe constant or micro substitution. It allows the definition of macros within the source code. The macro definitions allow constant values to be declared for use.

It is represented as “#define”. The macro definitions are not variables and will not be changed by the program code like other variables. It is usually used while creating constants that represent numbers, strings, and expressions.

Syntax

             #define CNAME value
                         Or
             #define CNAME (expression) 

Where;

CNAME: It is the name of the constant, which can either be defined in uppercase or lowercase.

Value: It represents the value of the constant present.

Expression: Expressions are values that is assigned to a constant. Expression must be enclosed inside the parentheses if it has operators.

E.g.:

 #include <stdio.h>
 #include <conio.h>
 #define AGE 10
 int main()
 {
 printf (“Alex is %d years old. \n”, AGE);
 return 0;
 } 

Output

Alex is 10 years old.
 #include <stdio.h>
 #include <conio.h>
 #incldue <string.h>
 #define PI 3.14
 int main()
 {
 float area;
 int radius;
 printf (“Enter the radius of the circle: \n”);
 scanf (“%d”, &radius);
 area = PI * radius * radius;
 printf (“The area of the circle is: %.2f”, area);
 return 0;
 } 

Output

 Enter the radius of the circle: 3
 The area of the circle is: 28.26 

Difference between typedef and define

  • Typedef is limited to only in giving symbolic names to types, while define can be used to define an alias for values such as, e.g., you can define 1 as ONE, 3.14 as PI, and so on.
  • Typedef interpretation is generally performed by the compiler whereas define statements are performed by the preprocessor.
  • Define should not be terminated with a semicolon (;), but typedef should always be terminated with a semicolon.
  • Define will only copy paste the definition values at a point of use, while typedef is the actual definition of any new type.
  • Typedef follows the scope rule, that is, if a new type is defined in any scope (inside a function), then the new type name will only be visible till the scope is present. In case of define, when the preprocessor encounters define, it replaces all the occurrences, after that no scope rule is followed.

Related Topics

LCM of two numbers in C

LCM is a mathematical term which stands for Least Common Multiple. LCM of any two numbers is the smallest positive value(number) which is evenly divisible by the two given numbers. Consider...

4 minutes read.

#include in C

In the C programming language, #include is another way of inferring a standard, or a user defines file into the application or program. The preprocessor of the programming standard generally...

4 minutes read.

Snake Game in C

Snake game in C is basic desktop console program which was created in 1970’s. It is an old classic game which was played by every child across the world. This...

4 minutes read.

Infix to Postfix program in C

Infix Expression: In infix expression, an operator is placed between the two operands. Example: x + y, here operator + is placed between operands x and y. Postfix Expression: In...

3 minutes read.

Program to Find Mode of an Array in C

Mode is the highest occurring value or number in a given set of data elements. In a group of data values, a value that occurs most frequently is considered to...

3 minutes read.

Attributes in C

Attributes are one of modern C++ key features. It is the process by which initiator can add more information to the language instead of introducing new keywords for every attribute....

4 minutes read.

Multiplication Table in C

Displaying table up to 10 #include <stdio.h> int main() {     int num, i = 1;     printf("     Enter any Number:");     scanf("%d", &num);     printf("Multiplication table of %d: ", num);  ...

1 minute read.

Ceil and Floor in C

In arithmetic, a rational number is a number that can be expressed as the quotient p/q of two integers. Where q is zero. The set of rational numbers includes all...

6 minutes read.

Floyd’s triangle in C

Floyd’s triangle in C Floyd’s triangle is named after a person Robert Floyd. It is a right-angled triangle that is of natural numbers starting from 1 and consecutively selects the upcoming...

4 minutes read.

Binary Search in C with Best and Worst Time Complexity

What is Binary Search? Binary search is an algorithm that is used to find an element in a sorted array efficiently. It has a time complexity of O(log n). It means...

3 minutes read.

fopen() function in C

fopen() function, is one of the file handling functions. It is used to open the existing file and perform operations on the file. If the file does not exist, it...

3 minutes read.

Flow chart of While loop in C

This is a flowchart that represents the process of executing the while loop in the C programming language.Generally, as we know there are three main components of while loop:1. The...

3 minutes read.

#error #pragma in C

#error, #pragma in C #error, also known as the error directive in the C language. It will not allow you to make any compilation fail and immediately issues a statement which...

4 minutes read.

Comments in C

Comments are used to comment on the line of code in the program. Comments are a way of inserting remarks and reminders into code without affecting its behavior. The compiler...

1 minute read.

Explain the Increment and Decrement Operators in C

In the C programming language, the increment operator (++) and the decrement operator (--) are used to increase or decreasing a variable’s value by 1, respectively. The increment operator is written...

10 minutes read.

Prime Number Program in C using for Loop

In this article, we will know about the procedure of checking whether a natural number inputted by the user is a prime number or non-prime number. Definition of the prime number A...

3 minutes read.

Bank Account System in C using File Handling

This tells about the creation of bank account system using the C language and handling of files in C. Approach Let us see the approaches and the functions how they are covering...

5 minutes read.

FIFO Example in the C Language

FIFO is an acronym that means "First In, First Out." It is a data structure handling method in which the first element is handled first, and the last element is...

3 minutes read.

Library Functions in C

What are library functions? Library functions are the built-in functions (functions provided by the system) which are stored in the library. The library is the common location where these functions are...

3 minutes read.

Calendar application in C

Introduction to the calendar application We all are familiar with the calendar. It plays a crucial role in our daily life. We run toward the calendar whenever we want to know...

6 minutes read.