EDUCBA

EDUCBA

MENUMENU
  • Free Tutorials
  • Free Courses
  • Certification Courses
  • 600+ Courses All in One Bundle
  • Login

typedef in C

Home » Software Development » Software Development Tutorials » C Programming Tutorial » typedef in C

typedef in C

Introduction to typedef in C

typedef is a predefined keyword in C language. This typedef keyword tells the C compiler that “please assign a user given keyword to the already existing type”. It Means typedef gives an alternative user-friendly keyword for existing C language data types like unsigned int, long, int, char, float, etc. This concept is very useful when the existing data types a little bit handy to use then we will use this typedef concept.

Real-Time Example: Let’s take if we want to declare some variables like unsigned int then we have to write unsigned int always throughout the program is a very lengthy process. So, instead of that, we can assign a new name to the already existing data type then it is easy to use in the code. For this, we can use typedef keyword.

Start Your Free Software Development Course

Web development, programming languages, Software testing & others

How does typedef Work in C Language?

This keyword works with typedef followed by existing data type and user wanted name to the data type. Then compiler will assume existing keyword name becomes user givenname for the entire application.

Syntax:

typedef<existing data type or keyword in C><user required name for the data type or keyword>;

Examples of typedef in C

Below given are the practical examples of typedef:

Example #1 – Typdef unsigned int ui;

Code: StructTypedef.c

#include <stdio.h>//Add all the basic C language libraries
#include <string.h>//Add the String library to perform string actions
//typedef for give struct keyword to user wanted keyword as like below (Courses)
typedef struct Courses {
char courseName[60];//declare character variable
float CourseFee;//declare float variable
char companyName[100];//declare character variable
int loginID;//declare integer variable
} Courses; //To make work user defined keyword we have call the keyword from here
//main method to execute application code
int main( ) {
//Taken Courses name as course( alias name)
Courses course;
//Copying character values into varaible
strcpy(course.courseName, "C Programming");
strcpy(course.companyName, "EDUCBA");
//Initailize float values into varaible
course.CourseFee = 5000.00;
//Initailize integer values into varaible
course.loginID=2452;
//display the output of all the declared variable below
printf( "Course Name : %s\n", course.courseName);
printf( "Company Name : %s\n", course.companyName);
printf( "Course Fee : %f\n", course.CourseFee);
printf( "Login ID : %d\n", course.loginID);
return 0;
}

Output:

typedef in C-1.1

Example #2 – Typedef union keyword

Code: TypedefUnion.c

#include <stdio.h>//Add all the basic C language libraries
#include <string.h>//Add the String library to perform string actions
//typedef for give struct keyword to user wanted keyword as like below (Employee)
typedef union Employee
{
inteID;//declare integer variable
float salary;//declare float variable
char company[30];//declare character variable
}Employee;//To make work user defined keyword we have call the keyword from here
//main method to execute application code
int main()
{
//Taken Courses name as course( alias name)
Employee e1, e2, e3, e4;
//Initailize float values into varaible
e1.salary = 18314912111343777091682304.000000 ;
//Initailize integer values into varaible
e1.eID=1769104726;
//Copying character values into varaible
strcpy(e1.company,"Verinon Technologies Private Limited");
//displaying employee details
printf("Details of First Employee\n");
printf("Employee ID : %d\n", e1.eID);
printf("Employee Salary : %f\n", e1.salary);
printf("Company Name : %s\n", e1.company);
//Initailize integer values into varaible
e2.eID = 1667330639 ;
//Initailize float values into varaible
e2.salary = 4158754218828133040128.000000;
//Copying character values into varaible
strcpy(e2.company,"Oracle Technologies Private Limited");
//displaying employee details
printf("Details of Second Employee\n");
printf("Employee ID : %d\n", e2.eID);
printf("Employee Salary : %f\n", e2.salary);
printf("Company Name : %s\n", e2.company);
//Initailize integer values into varaible
e3.eID = 1919117645;
//Initailize float values into varaible
e3.salary = 4504345476014339048099257778176.000000;
//Copying character values into varaible
strcpy(e3.company,"Microsoft Technologies Private Limited");
//displaying employee details
printf("Details of Third Employee\n");
printf("Employee ID : %d\n", e3.eID);
printf("Employee Salary : %f\n", e3.salary);
printf("Company Name : %s\n", e3.company);
//Initailize integer values into varaible
e4.eID = 1735356231 ;
//Initailize float values into varaible
e4.salary = 1130698294087203659186176.000000;
//Copying character values into varaible
strcpy(e4.company,"Google Technologies Private Limited");
//displaying employee details
printf("Details of Fourth Employee\n");
printf("Employee ID : %d\n", e4.eID);
printf("Employee Salary : %f\n", e4.salary);
printf("Company Name : %s\n", e4.company);
return 0;
}
course.loginID=2452;
//display the output of all the declared variable below
printf( "Course Name : %s\n", course.courseName);
printf( "Company Name : %s\n", course.companyName);
printf( "Course Fee : %f\n", course.CourseFee);
printf( "Login ID : %d\n", course.loginID);
return 0;
}

Output:

 typedef in C-1.2

Example #3 – Typedef unsigned char

Code: TypedefUnsignedChar.c

#include <stdio.h>//Add all the basic C language libraries
int main()
{
//typedef for give struct keyword to user wanted keyword as like below
typedef unsigned char uchar;
//declare character variable with user defined keyword
uchar alphabet = 'a';
//declare character variable with user defined keyword
uchar a='P';
//declare character variable with user defined keyword
uchar b='b';
//declare character variable with user defined keyword
uchar c = 'C';
//declare character variable with user defined keyword
uchar d='d';
//declare character variable with user defined keyword
uchar e = 'E';
//declare character variable with user defined keyword
uchar f='f';
//declare character variable with user defined keyword
uchar g='g';
//Displaying output of the user
printf("alphabet inside main() : %c\n", alphabet);
printf("alphabet inside main() : %c\n", a);
printf("alphabet inside main() : %c\n", b);
printf("alphabet inside main() : %c\n", c);
printf("alphabet inside main() : %c\n", d);
printf("alphabet inside main() : %c\n", e);
printf("alphabet inside main() : %c\n", f);
printf("alphabet inside main() : %c\n", g);
return 0;
}

Popular Course in this category
C Programming Training (3 Courses, 5 Project)3 Online Courses | 5 Hands-on Projects | 34+ Hours | Verifiable Certificate of Completion | Lifetime Access
4.5 (5,570 ratings)
Course Price

View Course

Related Courses
C++ Training (4 Courses, 5 Projects, 4 Quizzes)Java Training (40 Courses, 29 Projects, 4 Quizzes)

Output:

typedef in C-1.3

Conclusion

Typedef is used to declare predefined C data types or keywordswith user defined names. It reduces the repetition same data type again and again. We can conclude by this we can change any c data type name with any user wanted name.

Recommended Articles

This is a guide to typedef in C. Here we also discuss the Introduction and how does typedef work along with examples and its code implementation. You may also have a look at the following articles to learn more –

  1. Constants in C
  2. Bubble Sort in C#
  3. Queue in C
  4. Null pointer in C

C Programming Training (3 Courses, 5 Project)

3 Online Courses

5 Hands-on Projects

34+ Hours

Verifiable Certificate of Completion

Lifetime Access

Learn More

0 Shares
Share
Tweet
Share
Primary Sidebar
C Programming Tutorial
  • Advanced
    • Constructor in C
    • Encapsulation in C
    • C Storage Classes
    • Static Keyword in C
    • File Handling in C
    • Queue in C
    • Hexadecimal in C 
    • typedef in C
    • Memory Allocation in C
    • Linked List in C
    • Volatile in C
    • Tokens in C
    • Expression in C
    • Regular Expression in C
    • Error Handling in C
    • Types of Errors in C
    • Preprocessor in C
    • Preprocessor Directives in C
    • fscanf() in C
    • #Pragma in C
    • #ifndef in C
    • #undef in C
    • Macros in C
  • Basic
    • Introduction to C
    • What is C
    • Career in C Programming
    • Advantages of C
    • How to Install C
    • Best C Compilers
    • Data Types in C
    • Variables in C
    • C Keywords
    • C Command
    • Command Line Arguments in C
    • C Literals
    • Constants in C
    • Unsigned Int in C
    • String in C
  • Pointers
    • Pointers in C
    • Null pointer in C
    • Function Pointer in C
    • Double Pointer in C
    • Void Pointer in C
    • Const Pointer in C
    • Dangling Pointers in C
    • Pointer Arithmetic in C
  • Operators
    • C Operators
    • Arithmetic Operators in C
    • Relational Operators in C
    • Assignment Operators in C
    • Logical Operators in C
    • Conditional Operator in C
    • Modulus Operator in C
    • Ternary Operator in C
    • Address Operator in C
    • Unary Operator in C
    • Operators Precedence in C
    • Left Shift Operator in C
  • Control Statement
    • Control Statements in C
    • If Statement in C
    • If-else Statement in C
    • Else if Statement in C
    • Nested if Statement in C
    • #else in C
    • Structure Padding in C
    • Nested Structure in C
    • Continue Statement in C
    • Break Statement in C
    • Switch Statement in C
    • Goto Statement in C
  • Loops
    • Loops in C
    • For Loop in C
    • While Loop in C
    • Do While Loop in C
    • Nested Loop in C
    • Infinite Loop in C 
  • Function
    • Math Functions in C
    • Hashing Function in C
    • Recursive Function in C
    • Power Function in C
    • fputs in C
    • C puts() Function
    • fprintf() in C
    • fseek() in C
    • Stderr in C
    • ASCII Value in C
    • strcat() in C
    • Inline Function in C
    • sizeof() in C
    • Function Prototype in C
    • C ftell()
  • Array
    • Arrays in C Programming
    • 2-D Arrays in C
    • 3D Arrays in C
    • Multidimensional Array in C
    • Array Functions in C
    • Strings Array in C
  • Sorting
    • Sorting in C
    • Heap Sort in C
  • C programs
    • Patterns in C Programming
    • Star Patterns in C
    • Number Patterns in C
    • Swapping in C
    • Reverse Number in C
    • Palindrome in C Program
    • Factorial in C
    • Fibonacci Series in C
    • Square Root in C
    • Random Number Generator in C
    • Prime Numbers in C
    • Escape Sequence in C
    • Reverse String in C
    • Leap Year Program in C
    • Anagram Program in C
    • Strong Number in C
    • String Concatenation in C
    • C Programming Matrix Multiplication
    • Decimal to Octal in C
    • Expression Evaluation in C
    • Decimal to Hexadecimal in C
  • Interview question
    • C Programming Interview Questions

Related Courses

C Programming Training Course

C++ Training Course

Java Training Course

Footer
About Us
  • Blog
  • Who is EDUCBA?
  • Sign Up
  • Corporate Training
  • Certificate from Top Institutions
  • Contact Us
  • Verifiable Certificate
  • Reviews
  • Terms and Conditions
  • Privacy Policy
  •  
Apps
  • iPhone & iPad
  • Android
Resources
  • Free Courses
  • Java Tutorials
  • Python Tutorials
  • All Tutorials
Certification Courses
  • All Courses
  • Software Development Course - All in One Bundle
  • Become a Python Developer
  • Java Course
  • Become a Selenium Automation Tester
  • Become an IoT Developer
  • ASP.NET Course
  • VB.NET Course
  • PHP Course

© 2020 - EDUCBA. ALL RIGHTS RESERVED. THE CERTIFICATION NAMES ARE THE TRADEMARKS OF THEIR RESPECTIVE OWNERS.

EDUCBA
Free Software Development Course

Web development, programming languages, Software testing & others

*Please provide your correct email id. Login details for this Free course will be emailed to you
Book Your One Instructor : One Learner Free Class

Let’s Get Started

This website or its third-party tools use cookies, which are necessary to its functioning and required to achieve the purposes illustrated in the cookie policy. By closing this banner, scrolling this page, clicking a link or continuing to browse otherwise, you agree to our Privacy Policy

EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you
EDUCBA Login

Forgot Password?

EDUCBA
Free Software Development Course

Web development, programming languages, Software testing & others

*Please provide your correct email id. Login details for this Free course will be emailed to you

Special Offer - C Programming Training (3 Courses, 5 Project) Learn More