×

How to use Typedef Struct in C

The “typedef” is a predefined keyword in C programming language which is used to declare the new name to an existing type of variable.

For better understanding, if you declare a variable using typedef function with int datatype, then for declaring another variable with integer data type we can use that user declared variable as datatype for the new created variable.

Syntax:

typedef <old_name or keyword> <new name for datatype>

Example:

typedef int NEW;

//In the above line we are declared a user defined variable “NEW” with data type int.

NEW a, b;

By using the user defined variable “NEW” we are declaring a and b variables. Here the NEW is acting like datatype to the variables a and b.

Here is the Example C program for typedef

#include<stdio.h>
#include<conio.h>
void main (void)
{
	typedef int INTEGER;
	INTEGER a=1 , b=2;
	int c;
	c = a +b;
	printf(“Total=%d”,c);
}

Output:

3

In the above program, we are declared the variable “INTEGER” using typedef. By using the user defined variable INTEGER we are declaring another variable like a and b and printing the sum of them. The output of the program is 3.

Differences between #define and typedef.

#define:

The #define is used to assign the values / give an alias for the variables. This method is done in the preprocessor section. For example, we declare a pie as 3.14 using the #define then we can be able to use the pie values throughout out the program. In this we cannot be able to overwrite the value of the pie in program.

Example (program for #define):

#include<stdio.h>
#include<conio.h>
#define p 100
void main (void)
{
	int a = 1, b;
	b = p*a;
	printf(b);
}

Output:

100

Typedef:

In the typedef we are declaring the variable with a datatype, then we can able to use that variable as the datatype for declaring another variable. The typedef is written inside the code and processed by the compiler.

Example (program for typedef):

//program for arrays using typedef
#include<stdio.h>
#include<conio.h>


void main(void)
{
	typedef int array[5];
	int a;
	array i = {1, 2, 3, 4, 5};
	for (a=0 ; a<5; a++)
	{
		printf(“Array Elements =%d\n”,i[a]);
}
}

Output:

Array Elements = 1
Array Elements = 2
Array Elements = 3
Array Elements = 4
Array Elements = 5

Using Typedef in Structures

The general structure declaration is given below

Example:

struct demo
{
int a;
};
struct demo d;

In the above example we have created a struct body and a variable d.To create a variable d we have to use another line, so to reduce the lines of code and complexity of code we use the typedef in structures.

Syntax

Syntax for Using typedef struct:

typedef struct
{
//statements
} variable;

Example (program for structures using typedef):

#include<stdio.h>
#include<conio.h>
void main (void)
{
	typedef struct studentmarks
{
	int rollno;
	int marks;
}mk; 
mk m1;
m1.rollno = 21;
m1.marks = 98;
printf(“the marks of %d is %d”, m1.rollno,m1.marks);


}

Output:

The marks of 21 is 98

Conclusion

The typedef keyword is used to declare a variable and use that variable as datatype for declaring another variable. The typedef in structures are used to reduce the lines of code and the complexity of the code for faster execution.


Related Topics

Difference between rand() and srand() function in C

What is the rand()? The rand() means random function. This function is used in C. It generates random numbers in the range of 0 to the RAND_MAX. Suppose we generate a...

3 minutes read.

Length of an Array Function in C

In C, there is no built-in function to get the length of an array. However, there are a few ways you can determine the length of an array. It is necessary...

15 minutes read.

String in C

String is a collection of character or group of characters. In array, string of character is terminated by a null value “\0” and enclose between double quote. We can declare...

2 minutes read.

C String Manipulation programs without using Library Functions

C string manipulation programs without using Library Functions The string is the character list, and typically we operate with library functions to read and print the entire string, but here you...

6 minutes read.

Volatile in C

Introduction A volatile keyword is a qualifier in C. Qualifiers are nothing but keywords which are used to modify the properties of a variable. Qualifiers are of two types: 1) Const The const type...

3 minutes read.

C program to compare the two strings

C program to compare the two strings Strings can be compared either by using the string function or without using string function. First, we will look at how we can compare the strings with...

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

Find out Power without using POW function in C

Introduction: In this discussion, we will discuss how we find out power without using the POW function in C. In this article, you'll understand how to compute integer powers in...

3 minutes read.

Union Program in C

 How should a union be defined?  In C programming, a Union is a method used to organize different data types into groups in the same way that Structures are used. We...

4 minutes read.

Dynamic memory allocation

Dynamic memory allocation is used to allocate the memory at runtime. It has following function within <stdlib.h> header file. Function Syntax malloc() malloc (number *sizeof(int)); calloc() calloc (number, sizeof(int)); realloc() realloc (pointer_name, number * sizeof(int)); free() free (pointer_name); malloc() function The process...

2 minutes read.

What is 2s Complement in C?

The complement of 2s in C is generated from the 1s in C. As we know, the 1s complement of a binary number is generated by converting bit 1 to...

4 minutes read.

Simple Programs in C Language

In this article, we will discuss the basic programs in C language. Addition of two values in C. Swapping two values. Finding the odd or even values. Finding vowels and consonants in C. Finding the...

11 minutes read.

Multi-dimensional Arrays in C

Multi-dimensional Arrays in C The C programming allows the concept of multi-dimensional arrays. The data within the multidimensional arrays are stored in the tabular form, that is, in a row significant...

3 minutes read.

Flow Chart of For loop in C

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

3 minutes read.

DSA Program in C

How is a Data Structure Implemented? The foundational terms of a data structure are the following terms. Data may be arranged using data structures to make it easier to use. Each data...

20 minutes read.

Integer Promotions in C

As we know that some of the data types such as char, short int, Enum takes a smaller number of bytes than compared to int. When an operation is applied...

3 minutes read.

Comma Operator in C

What is a comma operator? The comma operator in the C programming language has the least priority. The comma operator is essentially a binary operator that operates on the first operand...

4 minutes read.

Const vs Volatile in C

Introduction Qualifiers are nothing but keywords which are used to modify the properties of a variable. Const and Volatile keywords are qualifiers in C. The Const qualifier is applied to the...

4 minutes read.

How to use atoi() function in C

Introduction: The atoi() is a function used in C programming language. This function converts string characters to the integer value. The atoi() function converts the input string to the return type of...

4 minutes read.

Queue implementation in C

In the C programming language, the queue is the abstract concept that is similar to stacks. However, it is the part of the data structures that work opposite to that...

4 minutes read.