typedef 字符串_typedef在C中使用字符数组(定义别名来声明字符串)的示例

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

  • 2
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值