如何为C中的变量指定特殊字符?

时间:2021-05-30 00:24:05

How to assign a special character to a variable in C? I am trying to assign variable symbol a character like ~~~~~ or @@@@@ or ***** or ;;;;;;. What I did waschar symbol = "~~~~~~~~~~"; but this did not work, so I tried to change double quotes to single like this:

如何为C中的变量赋一个特殊字符?我正在尝试为变量符号分配一个字符,如~~~~~或@@@@@或*****或;;;;;;。我做了什么waschar symbol =“~~~~~~~~~~”;但这不起作用,所以我试图将双引号改为单引号,如下所示:

char symbol = '@@@@@@@@@';

but this did not work either;

但这也不起作用;

2 个解决方案

#1


4  

It works, but you are trying to put multiple character into a single one which doesn't make sense.

它有效,但你试图将多个角色放入一个没有意义的角色。

Try:

尝试:

char symbol = '@';
const char symbols[] = "@@@@@@";
const char* symbols2 = "@@@@@@@";

Note that you need to use single quotes ' for single characters and double quotes " for strings.

请注意,对于字符串,您需要对单个字符和双引号使用单引号。

Also note that char is usually defined as 8-bit integer so you can only hold the usual ASCII characters. If you need Unicode you should use char arrays to accommodate all the bits needed and preferably use a library that takes care of handling, conversions, and any other operation you might want to do.

另请注意,char通常定义为8位整数,因此您只能保留常用的ASCII字符。如果您需要Unicode,您应该使用char数组来容纳所需的所有位,并且最好使用一个库,它负责处理,转换以及您可能想要执行的任何其他操作。

#2


1  

You should use a pointer to char if you want to assign multiple values.

如果要分配多个值,则应使用指向char的指针。

const char *smth = "@@@@";

If you need only one, then simply char smth = '@';

如果你只需要一个,那么只需要char smth ='@';

#1


4  

It works, but you are trying to put multiple character into a single one which doesn't make sense.

它有效,但你试图将多个角色放入一个没有意义的角色。

Try:

尝试:

char symbol = '@';
const char symbols[] = "@@@@@@";
const char* symbols2 = "@@@@@@@";

Note that you need to use single quotes ' for single characters and double quotes " for strings.

请注意,对于字符串,您需要对单个字符和双引号使用单引号。

Also note that char is usually defined as 8-bit integer so you can only hold the usual ASCII characters. If you need Unicode you should use char arrays to accommodate all the bits needed and preferably use a library that takes care of handling, conversions, and any other operation you might want to do.

另请注意,char通常定义为8位整数,因此您只能保留常用的ASCII字符。如果您需要Unicode,您应该使用char数组来容纳所需的所有位,并且最好使用一个库,它负责处理,转换以及您可能想要执行的任何其他操作。

#2


1  

You should use a pointer to char if you want to assign multiple values.

如果要分配多个值,则应使用指向char的指针。

const char *smth = "@@@@";

If you need only one, then simply char smth = '@';

如果你只需要一个,那么只需要char smth ='@';