Home »
C programs »
C typedef programs
typedef Example with character array (define an alias to declare strings) in C
Here, we are going to learn how to define an alias for a character array i.e. typedef for character array with given maximum length of the string in C programming language?
By IncludeHelp Last updated : March 10, 2024
Defining an alias for a character array
Here, we have to define an alias for a character array with a given number of maximum characters length to read strings?
In the below-given program, we have defined two alias (typedefs) for character array and unsigned char:
Syntax
typedef char CHRArray[MAXLEN];
typedef unsigned char BYTE;
MAXLEN is also defined with 50 by using define statement #define MAXLEN 50.
Declaring typedef variables
CHRArray name;
CHRArray city;
BYTE age;
Explanation
CHRArray name will be considered as char name[50], CHRArray city will be considered as char city[50] and BYTE age will be considered as unsigned char age.
Note: unsigned char is able to store the value between 0 to 255 (i.e. one BYTE value).
C program to define an alias to declare strings
#include <stdio.h>
#include <string.h>
#define MAXLEN 50
typedef char CHRArray[MAXLEN];
typedef unsigned char BYTE;
int main()
{
CHRArray name;
CHRArray city;
BYTE age;
//assign values
strcpy(name, "Amit Shukla");
strcpy(city, "Gwalior, MP, India");
age = 21;
//print values
printf("Name: %s\n", name);
printf("city: %s\n", city);
printf("Age : %u\n", age);
return 0;
}
Output
Name: Amit Shukla
city: Gwalior, MP, India
Age : 21
C typedef Programs »