鱼C论坛

 找回密码
 立即注册
查看: 1746|回复: 4

[已解决]%s 两边可以用双引号吗?

[复制链接]
发表于 2020-2-24 23:06:35 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x

求助求助!为什么这个代码编译不了,代码从小甲鱼那复制来的。

#include <stdio.h>
#include <string.h>

int main()
{
        char str1[] = "love";
        char str2[] = "FishC";

        if (strlen(str1) - strlen(str2) < 0)
        {
                printf("字符串"%s"比字符串"%s"短!\n", str1, str2);
        }                //删除上面%s两边双引号就是正常编译
        else
        {
                printf("字符串"%s"比字符串"%s"长!\n", str1, str2);
        }

        return 0;
}
最佳答案
2020-2-25 00:16:09
本帖最后由 major_lyu 于 2020-2-25 00:18 编辑

printf()函数里面只允许出现一对双引号表示格式化输出,如果你想在输出的字符中包含双引号,所要显示的双引号要用\进行转义
  1. #include <stdio.h>
  2. #include <string.h>

  3. int main()
  4. {
  5.     char str1[] = "love";
  6.     char str2[] = "FishC";

  7.     if (strlen(str1) - strlen(str2) < 0)
  8.     {
  9.         printf("字符串\" % s \"比字符串\" % s \"短!\n", str1, str2);
  10.     } //删除上面%s两边双引号就是正常编译
  11.     else
  12.     {
  13.         printf("字符串\" % s \"比字符串\" % s \"长!\n", str1, str2);
  14.     }

  15.     return 0;
  16. }
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2020-2-24 23:19:11 | 显示全部楼层

  1. #include <stdio.h>
  2. #include <string.h>

  3. int main()
  4. {
  5.         char str1[] = "love";
  6.         char str2[] = "FishC";

  7.         if (strlen(str1) - strlen(str2) < 0)
  8.         {
  9.                 printf("字符串%s比字符串%s短!\n", str1, str2);
  10.         }                //删除上面%s两边双引号就是正常编译
  11.         else
  12.         {
  13.                 printf("字符串%s比字符串%s长!\n", str1, str2);
  14.         }

  15.         return 0;
  16. }
复制代码

printf里面多了引号了
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2020-2-25 00:08:46 | 显示全部楼层
正常不需要双引号,如果你一定要显示双引号的效果,可以用转义字符\"
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2020-2-25 00:16:09 | 显示全部楼层    本楼为最佳答案   
本帖最后由 major_lyu 于 2020-2-25 00:18 编辑

printf()函数里面只允许出现一对双引号表示格式化输出,如果你想在输出的字符中包含双引号,所要显示的双引号要用\进行转义
  1. #include <stdio.h>
  2. #include <string.h>

  3. int main()
  4. {
  5.     char str1[] = "love";
  6.     char str2[] = "FishC";

  7.     if (strlen(str1) - strlen(str2) < 0)
  8.     {
  9.         printf("字符串\" % s \"比字符串\" % s \"短!\n", str1, str2);
  10.     } //删除上面%s两边双引号就是正常编译
  11.     else
  12.     {
  13.         printf("字符串\" % s \"比字符串\" % s \"长!\n", str1, str2);
  14.     }

  15.     return 0;
  16. }
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2020-2-25 11:05:29 From FishC Mobile | 显示全部楼层
major_lyu 发表于 2020-2-25 00:16
printf()函数里面只允许出现一对双引号表示格式化输出,如果你想在输出的字符中包含双引号,所要显示的双引 ...

谢谢了
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2025-7-17 20:37

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表