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?
Submitted by IncludeHelp, on September 11, 2018

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:

    typedef char CHRArray[MAXLEN];
    typedef unsigned char BYTE;

MAXLEN is also defined with 50 by using define statement #define MAXLEN 50.

Declaring 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).

Program:

#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





Comments and Discussions

Ad: Are you a blogger? Join our Blogging forum.
Learn PCB Designing: PCB DESIGNING TUTORIAL







Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing » Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates

© https://www.includehelp.com some rights reserved.