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 字符串