×

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

#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.

Goto and Labels in C

Introduction Goto in C: The goto statement is known as the jump statement in C. The goto is used to transfer the control of a program to a predefined label. The goto...

3 minutes read.

Fseek Function in C

The fseek function is a function in the C standard library that changes the position of the file pointer in a stream-oriented file. It is typically used to move the...

3 minutes read.

Modulus on Negative Numbers in C

In this tutorial, we will explore some examples of modulus on negative number. What is Modulus of Negative number? By omitting the minus sign, one can determine the modulus of a negative...

3 minutes read.

While-Loop in C

Syntax of While Loop The syntax that has been used for the while loop in C programming language is: while (termination condition) {   // the body of the loop  } Working of While-Loop Initially, we...

3 minutes read.

Top Array Keywords in C

Sorting array in C Types of array in C Array of structure in C How do you initialize an array in C Array declaration in C Array of strings in C Multidimensional array in C Reverse array...

1 minute read.

C Program for Mean and Median of an Unsorted Array

In this tutorial, we will look at how to determine the mean and median of a given unsorted array. To determine the Mean: To get the average, mean is determined. The formula...

2 minutes read.

How to install a C compiler in windows 10-64bit

Many C compilers can be installed on your pc. The most downloaded compilers include GCC, Turbo C++, Visual Studio, etc. This article includes the installation of the Turbo C++ compiler in...

6 minutes read.

Return array from function in C

Return array from function in C C programming does not require the return to a function of a whole array as an argument. However, you can return a pointer to an array without...

2 minutes read.

How to Merge Array in C?

Introduction Merging arrays is a common task for many developers but might be difficult for beginners of C programming. Fortunately, our guide will show you how to quickly merge arrays in...

6 minutes read.

Pre-increment and Post-increment in C

The rich set of operators is supported by the c language. In c there are several operators used. The mathematical operations in the programming language are done with the operators...

4 minutes read.

#define in C

#define in C In the C programming language, the preprocessor directive acts an important role within which the #define directive is present that is used to define the constant or the...

3 minutes read.

Format Specifier in C

Format Specifier in C Format specifiers can be described as an operator that is used to print the data referred by any object or variable in combination with the printf ()...

3 minutes read.

C Decision control statement

Decision Making C conditional statements are used to specify one or more conditions. The statements will be executed if the condition becomes true otherwise alternative statement will be executed if the...

3 minutes read.

Find Day from Day in C Without using function

Introduction: In the given article, I find daily in C without using functions. It takes 365 days for the earth to revolve around the sun. It will be close to...

3 minutes read.

C Program to Find the Largest Number using Ternary Operator

In this tutorial, we will write some program to determine which of the provided integers is larger using the ternary operator, also known as the conditional operator in C. Ternary Operator: The ternary...

3 minutes read.

How to Find Square Free Numbers in C?

Introduction Square-free number program is a typical interview and academic question in C Programming. In this section, we will define square-free integers, construct algorithms and C program to print the nth...

11 minutes read.

Nested if-else statement in C

If we use an if-else statement within another if statement in a C program, it is called a nested if-else statement in C. It helps to check the condition inside...

3 minutes read.

Use of free() function in C

Introduction The free() function uses in C programming language. The free() function in the C programming language uses to release or deallocate the memory blocks; these blocks are previously allocated by calloc(), malloc() or realloc()...

3 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.