# 字符（char）与字符串

我们可以用`char`类型来专门表示一个字符，例如：

```c
char a='1';
char b='$';
char c='X';
char d=' ';  // 空格也是一个字符
char e='\63';  //也可以使用转义字符的形式
```

char 称为字符类型，只能用单引号`' '`来包围，不能用双引号`" "`包围。而字符串只能用双引号`" "`包围，不能用单引号`' '`包围。

输出字符使用 %c，输出字符串使用 %s。

## 字符与整数

```c
#include <stdio.h>
#include <stdlib.h>
int main()
{
    char a = 'E';
    char b = 70;
    int c = 71;
    int d = 'H';

    printf("a=%c, a=%d\n", a, a);
    printf("b=%c, b=%d\n", b, b);
    printf("c=%c, c=%d\n", c, c);
    printf("d=%c, d=%d\n", d, d);

    system("pause");
    return 0;
}
```

输出结果：

```
a=E, a=69
b=F, b=70
c=G, c=71
d=H, d=72
```

在ASCII码表中，E、F、G、H 的值分别是 69、70、71、72。

字符和整数没有本质的区别。可以给 char 变量一个字符，也可以给它一个整数；反过来，可以给 int 变量一个整数，也可以给它一个字符。

**char 变量在内存中存储的是字符对应的 ASCII 码值。**&#x5982;果以 %c 输出，会根据 ASCII 码表转换成对应的字符；如果以 %d 输出，那么还是整数。

> 完整的ASCII码表请查看： <http://www.asciima.com/>

## 字符串

C语言中没有字符串类型，只能使用间接的方法来表示。可以借助下面的形式将字符串赋值给变量：

```c
char *variableName = "string";
```

`char`和`*`是固定的形式，variableNmae 为变量名称，"string" 是要赋值的字符串。

字符串使用示例：

```c
#include <stdio.h>
#include <stdlib.h>
int main()
{
    char c = '@';
    char *str = "This is a string.";
    printf("char: %c\n", c);
    printf("string1: %s\n", str);
    //也可以直接输出字符串
    printf("string2: %s\n", "This is another string.");

    system("pause");
    return 0;
}
```

运行结果：

```
char: @
string1: This is a string.
string2: This is another string.
```


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://xiaoxiami.gitbook.io/c/ji-chu/zi-fu-ff08-char.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
