Quick links
Latest articles
Internship
Members
New...
Algorithms
Discrete Mathematics
Big data
Languages
C
C++
C++ STL
Java
Data Structure
C#.Net
Android
Kotlin
SQL
Web
PHP
Python
JavaScript
CSS
Ajax
Node.js
Web prog.
Programs
C
C++
DS
Java
C#
Python
Aptitude
C
C++
Java
DBMS
Interview
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

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





Quick links:
C FAQ(s) C Advance programs C/C++ Tips & Tricks Puzzles JavaScript CSS Python Linux Commands PHP Android Articles More...

Featured post:
Introduction to Linux (Its modes, Safety, Most popular Applications)
Linux Best Distribution Software (Distros) of 2018

Was this page helpful? Please share with your friends...

Are you a blogger? Join our Blogging forum.

Comments and Discussions



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 (2015-2018), Some rights reserved.