typedef 字符串
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):
typedef char CHRArray[MAXLEN];
typedef unsigned char BYTE;
MAXLEN is also defined with 50 by using define statement #define MAXLEN 50.
MAXLEN还与50通过定义语句的#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.
CHRArray名称将被视为char name [50] , CHRArray city将被视为char city [50] , BYTE age将被视为unsigned char age 。
Note: unsigned char is able to store the value between 0 to 255 (i.e. one BYTE value).
注意: unsigned char能够存储0到255之间的值(即一个BYTE值)。
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
翻译自: https://www.includehelp.com/c-programs/typedef-example-with-character-array-define-an-alias-to-declare-strings.aspx
typedef 字符串