字符串文字是否在C编译时自动转换为char *? [英] Are string literals automatically being casted to char* at compile time in C?

查看:107
本文介绍了字符串文字是否在C编译时自动转换为char *?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我要执行以下操作:

If I were to do something like:

printf("The string is: %s\n", "string1");

在编译时是否已完成以下操作:

Is the following done at compile time:

printf("The string is: %s\n", (unsigned char*) "string1"); 

还是类似的?

推荐答案

标准定义字符串文字的类型为char 1 的数组,并且该数组自动衰减为指针,即char*.使用%s说明符时,无需将其作为参数传递给printf时显式进行强制转换.

It is defined by the standard that the type of string literals is an array of char1 and arrays automatically decay to pointers, i.e. char*. You don't need to cast it explicitly while passing it as an argument to printf when %s specifier is used.

侧面说明:在C ++中为const char* 2 .

Side note: In C++ it's const char*2.

[1] C99 6.4.5:字符串文字是由零个或多个多字节字符括起来的序列, 双引号,例如"xyz" ... 静态存储持续时间和长度的数组 足以包含序列.对于字符串文字,数组元素具有 输入char"

[1] C99 6.4.5: "A character string literal is a sequence of zero or more multibyte characters enclosed in double-quotes, as in "xyz"... an array of static storage duration and length just sufficient to contain the sequence. For character string literals, the array elements have type char"

[2] C ++ 03 2.13.4§1:"普通字符串文字的类型为"n个数组 const char"和静态存储持续时间"

[2] C++03 2.13.4 §1: "an ordinary string literal has type "array of n const char" and static storage duration"

这篇关于字符串文字是否在C编译时自动转换为char *?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆