无法将'#'字符作为命令行参数传递 [英] Unable to pass '#' character as a command-line argument

查看:68
本文介绍了无法将'#'字符作为命令行参数传递的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不能将以#开头的字符串作为命令行参数.

I can't pass strings starting with # as command-line arguments.

这是一个简单的测试:

#include <stdio.h>

int main(int argc, char *argv[])
{
    for (int i = 1; i < argc; i++)
        printf("%s ", argv[i]);

    putchar('\n');

    return 0;
}

如果我按如下所示输入参数:

If I input the arguments as follows:

2 4 # 5 6

argc 的值是 3 ,而不是 6 .它读取#并停在那里.我不知道为什么,在 C编程语言 C Primer Plus 的副本中也找不到答案.

The value of argc is 3 and not 6. It reads # and stops there. I don't know why, and I can't find the answer in my copies of The C Programming Language and C Primer Plus.

推荐答案

#在Unix shell中开始注释,就像在C语言中的//一样.

# begins a comment in Unix shells, much like // in C.

这意味着当shell将参数传递给程序时,它将忽略#之后的所有内容.用反斜杠或引号将其转义将意味着它被视为其他参数,并且程序应按预期运行.

This means that when the shell passes the arguments to the progam, it ignores everything following the #. Escaping it with a backslash or quotes will mean it is treated like the other parameters and the program should work as expected.

2 4 \# 5 6

2 4 '#' 5 6

2 4 "#" 5 6

请注意,#仅在单词开头处是注释字符,因此这也应该起作用:

Note that the # is a comment character only at the start of a word, so this should also work:

2 4#5 6

这篇关于无法将'#'字符作为命令行参数传递的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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