×

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 C and Java

C programming and Java programming are two of the earliest programming languages. C programming follows a procedural approach whereas Java programming follows an object-oriented approach. Java programming is a part...

3 minutes read.

Difference between If and Switch Statement in C

Key Contrast: In the event that assertion is utilizes a Boolean articulation to execute the capability and can frequently be utilized to really look at various circumstances all at once.  The change...

4 minutes read.

Difference between while and do-while loop in C

Introduction Both 'While' and 'Do-While' loops in C mostly have a similar concept, and the code of both loops runs for mainly similar purposes. Both loops process the program/code in the...

3 minutes read.

Static in C

Static is a keyword that is used in the C programming language. It can be used both as variables and as functions. In other words, it can be declared both...

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.

Addition of Matrix in C

The mathematical operation of includingor greater matrices is called the addition of matrices. A matrix is a square series of numbers, symbols, words, letters, and different things. In this article,...

3 minutes read.

Write() function in c

As the name suggests the write () function is used to write the file descriptor. In other words it is used to write any file name without specifying file name,...

3 minutes read.

What are linker and loader in C

Linker and loader are utility programs that have a significant role in executing a program. Linker: A linker is a program that joins the object files produced by the assembler/ compiler...

3 minutes read.

Infix to Postfix program in C

Infix Expression: In infix expression, an operator is placed between the two operands. Example: x + y, here operator + is placed between operands x and y. Postfix Expression: In...

3 minutes read.

How to create a binary file in C?

Creating a binary file in C language is very simple, requiring only some specific functions to be implemented. There are also other binary modes: read, and write, which will be...

7 minutes read.

Multiplication Table in C

Displaying table up to 10 #include <stdio.h> int main() {     int num, i = 1;     printf("     Enter any Number:");     scanf("%d", &num);     printf("Multiplication table of %d: ", num);  ...

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

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.

Garbage in C

Garbage in C The C programming language is a perfect fit for embedded systems as it provides low level control, structured programming, and portability. On the contrary, it does not provide...

3 minutes read.

Expressions in C

Expressions in C: In the C programming language, an expression defines a formula in which the operands are linked to each other by using operators to compute the value. The operand...

4 minutes read.

Prototype in C

A prototype is nothing but a model, a model of initial creation of an intended product. Similarly, in the C programming language, all functions have a prototype. In general, all...

4 minutes read.

gets() function in C

gets() is a pre-define or built-in function present in the stdio.h header file. stdio stands for standard input and output. gets() function is used to read a stream and store...

4 minutes read.

Projects on C language in 2024

Introduction Most aspiring programmers learn the C language as their first high-level programming language. The most flexible language utilized in every industry is, without a doubt, C. Even after 50 years...

9 minutes read.

Big O Notation in C

Big O Notation in C: In the C programming language, we have so many algorithms and solutions to the problems that exist, which have different aspects and purposes to an...

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.