×

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

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.

Assert() Function in C

Assert (): In C, the statements are executed with the exit statement. In C language declare, and it tests the condition parameters. If the statement executed will be false, it shows...

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

Explain the two-way selection in C

In C programming, a two-way selection statement is used to choose between two different options based on a Boolean expression. The two-way selection statement in C is the if statement. The...

6 minutes read.

While Loop Syntax in C

What is Loop? The statements in the sequence are repeatedly executed via looping statements in C until the condition is met. The body of a loop and a control statement make...

4 minutes read.

Call Back Function in Embedded C

Callback function are one the most remarkable systems in C. A callback function is any code that is passed as a contention to another code, so this is the last...

3 minutes read.

Fee Management System in C

The concept is to split every operation into its very own characteristic. Software is made from all of the capabilities mixed with transfer cases. An example of the capabilities may...

5 minutes read.

Entry Control Loop in C

Initially, an entry control loop verifies the termination condition at the entry point. After that, its control passes to the main body of the while or for loop if the...

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

Difference between Pre-increment and Post-increment in C

Increment operators are the kind of operators that are used to increment the cost of the operand by way of 1. In different words, the increment operator is an operator...

4 minutes read.

Isalnum() function in C

Introduction: The isalnum () is a function used in C programming language. This function checks the passing number or argument is an alphanumeric number or not. The alphanumeric number consists of the...

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

C Math Library

C Math Library The <math.h> header defines various mathematical functions and one macro. Many functions are available in this library to take double as an argument and then return double as...

4 minutes read.

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

5 minutes read.

For loop in C Programming

In the C programming language, the for loop is used to repeatedly iterate over a set of instructions or a section of code. It is frequently used to traverse array-based...

3 minutes read.

Escape Sequence in C

An escape sequence is a sequence of character that used inside the string literal or character.  All the escape sequence is used with the backslash (\) symbol. The list of an...

1 minute read.

C Language Environment Setup

To compile C program, we must have GCC compiler installed on our machine. In this C tutorial, all the examples are compiled and tested using GCC compiler. Although we can...

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

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.

Exit control loop in C

To examine the termination condition for an exit, we usually use an exit control loop in C. If the evaluation for termination condition results in true, then the control would...

3 minutes read.