warning: multi-character character constant

本文探讨了C语言中使用单引号括起多个字符而非字符串所带来的问题,并通过示例代码展示了如何处理这类非标准用法导致的编译警告。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

That's a somewhat obscure feature which has been in C since long before ANSI/ISO; 'xxxx' is not a null-terminated character string like "xxxx" which would be stored byte-wise.  It depends on your system being capable of storing that group of characters in a way similar to that suggested by your quoted usage, so it's non-portable.  Of course, nowadays, 4 characters are expected to fit in a long, but there's also the possibility that a string was meant rather than a multi-character constant.

编译以下代码时出现的问题。

#include <getopt.h>
#include <stdio.h>
#include <stdlib.h>
#include "args.h"

int main (int argc, char * argv[])
{
        int next_opt;
        const char * const short_opts = "hl:";
        const struct option long_opts [] = {
                {"help", 0, NULL, 'h'},
                {"load", 1, NULL, 'l'},
                {NULL, 0, NULL, 0}
        };

        prgm_name = argv[0];

        do {
                next_opt = getopt_long(argc, argv, short_opts, long_opts, NULL);
                switch (next_opt) {
                case 'h' :
                        prt_usage(stdout, 0);
                case 'l' :
                        break;
                case '?' :
                        prt_usage(stderr, 1);
                case '-1' :

                 /*不同机器对被单引号引起的多个字符的处理方式会有所不同。故而,编译器会报 

                    出以上警告信息。*/                        
                        break;
                default :
                        abort();
                }
        } while (next_opt != -1);
        return 0;
}

void prt_usage (FILE * stream, int exit_code)
{
        fprintf (stream, "Usage : %s options.\n", prgm_name);
        fprintf (stream, "-h --help Displays this usage information.\n" \
                "-l int dur --load int dur Set interval and duration of average load.\n");
        exit (exit_code);
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值