Use of typedef keyword in Structure C Language
Categories: C language
Let's consider a program to use the typedef keyword in structure to provide a new name to the struct and initialize the structure variables, as follow:
Struct.c
#include <stdio.h>
#include <string.h>
typedef struct Students {
int roll_no;
char name[50];
char Subject[60];
char teacher_name[80];
} Stud;
int main( ) {
Stud S1;
S1.roll_no = 30;
strcpy( S1.name, "Lockie Ferguson");
strcpy( S1.Subject, "Mathematics");
strcpy( S1.teacher_name, "Jasmine");
printf( "Student Roll No: %d\n", S1.roll_no);
printf( "Student Name: %s\n", S1.name);
printf( "Student Subject: %s\n", S1.Subject);
printf( "Student Teacher Name: %s\n", S1.teacher_name);
return 0;
}
Output:
Student Roll No: 30
Student name: Lockie Fergusion
Student Subject: Mathematics
Student Teacher Name: Jasmine
define
A #define is the pre-processor used to represents the constant aliases for various data. It is used to define the constant variable for different data types in C. It is defined outside of the main program.
Syntax
#define token value
In the above syntax, #define is a pre-processor that defines the token as a constant variable name, and the value represents the token value. After initializing the constant variable in the pre-processor, we can use the value using the variable in a program.