Open In App

C typedef

Last Updated : 10 Apr, 2023
Comments
Improve
Suggest changes
Like Article
Like
Save
Share
Report
News Follow

The typedef is a keyword that is used to provide existing data types with a new name. The C typedef keyword is used to redefine the name of already existing data types.

When names of datatypes become difficult to use in programs, typedef is used with user-defined datatypes, which behave similarly to defining an alias for commands.

C typedef Syntax

typedef existing_name alias_name;

After this declaration, we can use the alias_name as if it were the real existing_name in out C program.

Example of typedef in C

typedef long long ll;

Below is the C program to illustrate how to use typedef.

C




// C program to implement typedef
#include <stdio.h>
 
// defining an alias using typedef
typedef long long ll;
 
// Driver code
int main()
{
    // using typedef name to declare variable
    ll var = 20;
    printf("%ld", var);
 
    return 0;
}


Output

20

Use of typedef in C

Following are some common uses of the typedef in C programming:

  • The typedef keyword gives a meaningful name to the existing data type which helps other users to understand the program more easily.
  • It can be used with structures to increase code readability and we don’t have to type struct repeatedly.
  • The typedef keyword can also be used with pointers to declare multiple pointers in a single statement.
  • It can be used with arrays to declare any number of variables.

1. typedef struct

typedef can also be used with structures in the C programming language. A new data type can be created and used to define the structure variable.

Example 1: Using typedef to define a name for a structure

C




// C program to implement
// typedef with structures
#include <stdio.h>
#include <string.h>
 
// using typedef to define an alias for structure
typedef struct students {
    char name[50];
    char branch[50];
    int ID_no;
} stu;
 
// Driver code
int main()
{
    stu st;
    strcpy(st.name, "Kamlesh Joshi");
    strcpy(st.branch, "Computer Science And Engineering");
    st.ID_no = 108;
 
    printf("Name: %s\n", st.name);
    printf("Branch: %s\n", st.branch);
    printf("ID_no: %d\n", st.ID_no);
    return 0;
}


Output

Name: Kamlesh Joshi
Branch: Computer Science And Engineering
ID_no: 108

2. typedef with Pointers

typedef can also be used with pointers as it gives an alias name to the pointers. Typedef is very efficient while declaring multiple pointers in a single statement because pointers bind to the right on the simple declaration. 

Example:

typedef int* Int_ptr;
Int_ptr var, var1, var2;

In the above statement var, var1, and var2 are declared as pointers of type int which helps us to declare multiple numbers of pointers in a single statement. 

Example 2: Using typedef to define a name for pointer type.

C




// C program to implement
// typedef with pointers
#include <stdio.h>
 
typedef int* ptr;
 
// Driver code
int main()
{
    ptr var;
    *var = 20;
 
    printf("Value of var is %d", *var);
    return 0;
}


Output

Value of var is 20

3. typedef with Array

typedef can also be used with an array to increase their count. 

Example:

typedef int arr[20]

Here, arr is an alias for an array of 20 integer elements.

// it's same as Arr[20], two-Arr[20][23];
arr Arr, two-Arr[23]; 

Example 3: Using typedef to define an alias for Array.

C




// C program to implement typedef with array
#include <stdio.h>
 
typedef int Arr[4];
 
// Driver code
int main()
{
    Arr temp = { 10, 20, 30, 40 };
    printf("typedef using an array\n");
 
    for (int i = 0; i < 4; i++) {
        printf("%d ", temp[i]);
    }
    return 0;
}


Output

typedef using an array
10 20 30 40 

C typedef vs #define

The following are the major difference between the typedef and #define in C:

  1. #define is capable of defining aliases for values as well, for instance, you can define 1 as ONE, 3.14 as PI, etc. Typedef is limited to giving symbolic names to types only.
  2. Preprocessors interpret #define statements, while the compiler interprets typedef statements.
  3. There should be no semicolon at the end of #define, but a semicolon at the end of typedef.
  4. In contrast with #define, typedef will actually define a new type by copying and pasting the definition values.

Below is the C program to implement #define:

C




// C program to implement #define
#include <stdio.h>
 
// macro definition
#define LIMIT 3
 
// Driver code
int main()
{
    for (int i = 0; i < LIMIT; i++) {
        printf("%d \n", i);
    }
    return 0;
}


Output

0 
1 
2 

FAQs on typedef in C

1. What is typedef in C?

The C typedef statement defines an alias or a nickname for the already existing data type.

2. What is typedef struct?

The typedef struct is the statement used to define an alias for the structure data type.

3. What is typedef enum?

The typedef enum is used to define the alias for the enumeration data type.



Previous Article
Next Article

Similar Reads

Is there any equivalent to typedef of C/C++ in Java ?
In one line, There is nothing in Java which is equivalent to typedef of C++. In Java, class is used to name and construct types or we can say that class is the combined function of C++'s struct and typedef. But that is totally different thing and not the equivalent of typedef anywhere. typedef: It is a keyword not a function that is used in C/C++ l
2 min read
C Programming Language Tutorial
In this C Tutorial, you’ll learn all C programming basic to advanced concepts like variables, arrays, pointers, strings, loops, etc. This C Programming Tutorial is designed for both beginners as well as experienced professionals, who’re looking to learn and enhance their knowledge of the C programming language. What is C?C is a general-purpose, pro
8 min read
Top 50 C Coding Interview Questions and Answers (2024)
C is the most popular programming language developed by Dennis Ritchie at the Bell Laboratories in 1972 to develop the UNIX operating systems. It is a general-purpose and procedural programming language. It is faster than the languages like Java and Python. C is the most used language in top companies such as LinkedIn, Microsoft, Opera, Meta, and N
15+ min read
Top 25 C Projects with Source Codes for 2024
If you're looking for project ideas to boost your C Programming skills, you're in the right spot. Programming is about problem-solving and adapting to ever-changing technology. Start with C, the foundation of many modern languages, to refine your programming abilities. Despite being introduced 50 years ago, C remains a top choice for beginners due
14 min read
printf in C
In C language, printf() function is used to print formatted output to the standard output stdout (which is generally the console screen). The printf function is a part of the C standard library &lt;stdio.h&gt; and it can allow formatting the output in numerous ways. Syntax of printfprintf ( "formatted_string", arguments_list);Parametersformatted_st
5 min read
Pattern Programs in C
Printing patterns using C programs has always been an interesting problem domain. We can print different patterns like star patterns, pyramid patterns, Floyd's triangle, Pascal's triangle, etc. in C language. These problems generally require the knowledge of loops and if-else statements. In this article, we will discuss the following example progra
15+ min read
C Programs
To learn anything effectively, practicing and solving problems is essential. To help you master C programming, we have compiled over 100 C programming examples across various categories, including basic C programs, Fibonacci series, strings, arrays, base conversions, pattern printing, pointers, and more. These C Examples cover a range of questions,
8 min read
Switch Statement in C
Switch case statement evaluates a given expression and based on the evaluated value(matching a certain condition), it executes the statements associated with it. Basically, it is used to perform different actions based on different conditions(cases). Switch case statements follow a selection-control mechanism and allow a value to change control of
8 min read
C Programming Interview Questions (2024)
At Bell Labs, Dennis Ritchie developed the C programming language between 1971 and 1973. C is a mid-level structured-oriented programming and general-purpose programming. It is one of the old and most popular programming languages. There are many applications in which C programming language is used, including language compilers, operating systems,
15+ min read
scanf in C
In C programming language, scanf is a function that stands for Scan Formatted String. It is used to read data from stdin (standard input stream i.e. usually keyboard) and then writes the result into the given arguments. It accepts character, string, and numeric data from the user using standard input.scanf also uses format specifiers like printf.sc
2 min read
C Hello World Program
The “Hello World” program is the first step towards learning any programming language and also one of the simplest programs you will learn. To print the "Hello World", we can use the printf function from the stdio.h library that prints the given string on the screen. C Program to Print "Hello World"The following C program displays "Hello World" in
2 min read
Dynamic Memory Allocation in C using malloc(), calloc(), free() and realloc()
Since C is a structured language, it has some fixed rules for programming. One of them includes changing the size of an array. An array is a collection of items stored at contiguous memory locations.  As can be seen, the length (size) of the array above is 9. But what if there is a requirement to change this length (size)? For example,  If there is
9 min read
C Program to Add Two Integers
Write a C program for the addition of two integers. Adding two integers involves summing their values to produce a single integer result. For example, the addition of two numbers 10 and 5 results in the number 15. Examples Input: a = 5, b = 3 Output: 8 Explanation: The sum of 5 and 3 is 8. Input: a = -2, b = 7 Output: 5 Explanation: The sum of -2 a
6 min read
Prime Number Program in C
A prime number is a natural number greater than 1 and is completely divisible only by 1 and itself. In this article, we will learn how to check whether the given number is a prime number or not in C. Examples Input: N = 29Output: YesExplanation: 29 has no divisors other than 1 and 29 itself. Hence, it is a prime number. Input: N = 15Output: NoExpla
3 min read
Format Specifiers in C
The format specifier in C is used to tell the compiler about the type of data to be printed or scanned in input and output operations. They always start with a % symbol and are used in the formatted string in functions like printf(), scanf, sprintf(), etc. The C language provides a number of format specifiers that are associated with the different
6 min read
Basics of File Handling in C
File handling in C is the process in which we create, open, read, write, and close operations on a file. C language provides different functions such as fopen(), fwrite(), fread(), fseek(), fprintf(), etc. to perform input, output, and many different C file operations in our program. Why do we need File Handling in C?So far the operations using the
15 min read
Substring in C++
The substring function is used for handling string operations like strcat(), append(), etc. It generates a new string with its value initialized to a copy of a sub-string of this object. In C++, the header file which is required for std::substr(), string functions is &lt;string&gt;. The substring function takes two values pos and len as an argument
8 min read
Strings in C
A String in C programming is a sequence of characters terminated with a null character '\0'. The C String is stored as an array of characters. The difference between a character array and a C string is that the string in C is terminated with a unique character '\0'. C String Declaration SyntaxDeclaring a string in C is as simple as declaring a one-
8 min read
Tokens in C
A token in C can be defined as the smallest individual element of the C programming language that is meaningful to the compiler. It is the basic component of a C program. Types of Tokens in CThe tokens of C language can be classified into six types based on the functions they are used to perform. The types of C tokens are as follows: KeywordsIdenti
5 min read
Decision Making in C (if , if..else, Nested if, if-else-if )
The conditional statements (also known as decision control structures) such as if, if else, switch, etc. are used for decision-making purposes in C programs. They are also known as Decision-Making Statements and are used to evaluate one or more conditions and make the decision whether to execute a set of statements or not. These decision-making sta
11 min read
Operators in C
In C language, operators are symbols that represent operations to be performed on one or more operands. They are the basic components of the C programming. In this article, we will learn about all the built-in operators in C with examples. What is a C Operator?An operator in C can be defined as the symbol that helps us to perform some specific math
14 min read
C Pointers
Pointers are one of the core components of the C programming language. A pointer can be used to store the memory address of other variables, functions, or even other pointers. The use of pointers allows low-level memory access, dynamic memory allocation, and many other functionality in C. In this article, we will discuss C pointers in detail, their
14 min read
Converting Number to String in C++
In C++, converting integers to strings or converting numbers to strings or vice-versa is actually a big paradigm shift in itself. In general or more specifically in competitive programming there are many instances where we need to convert a number to a string or string to a number. Let's look at some methods to convert an integer or a number to a s
4 min read
Data Types in C
Each variable in C has an associated data type. It specifies the type of data that the variable can store like integer, character, floating, double, etc. Each data type requires different amounts of memory and has some specific operations which can be performed over it. The data type is a collection of data with values having fixed values, meaning
7 min read
C Variables
A variable in C language is the name associated with some memory location to store data of different types. There are many types of variables in C depending on the scope, storage class, lifetime, type of data they store, etc. A variable is the basic building block of a C program that can be used in expressions as a substitute in place of the value
9 min read
C Arrays
Array in C is one of the most used data structures in C programming. It is a simple and fast way of storing multiple values under a single name. In this article, we will study the different aspects of array in C language such as array declaration, definition, initialization, types of arrays, array syntax, advantages and disadvantages, and many more
15+ min read
C Structures
The structure in C is a user-defined data type that can be used to group items of possibly different types into a single type. The struct keyword is used to define the structure in the C programming language. The items in the structure are called its member and they can be of any valid data type. Additionally, the values of a structure are stored i
10 min read
Bitwise Operators in C
In C, the following 6 operators are bitwise operators (also known as bit operators as they work at the bit-level). They are used to perform bitwise operations in C. The &amp; (bitwise AND) in C takes two numbers as operands and does AND on every bit of two numbers. The result of AND is 1 only if both bits are 1.  The | (bitwise OR) in C takes two n
7 min read
C Language Introduction
C is a procedural programming language initially developed by Dennis Ritchie in the year 1972 at Bell Laboratories of AT&amp;T Labs. It was mainly developed as a system programming language to write the UNIX operating system. The main features of the C language include: General Purpose and PortableLow-level Memory AccessFast SpeedClean SyntaxThese
6 min read
C Program to Swap Two Numbers
Swapping two numbers means exchanging their values. For example, Input: a = 5, b = 10Output: a = 10, b = 5 In this article, we will learn how to swap values of two numbers in a C program. Swap Two Numbers Using Temporary VariableThe easiest method to swap two numbers is to use a temporary variable to hold one of the values, then assign the value of
3 min read
Article Tags :