小编vv1*_*133的帖子

宏"#define STR(x)#x"和"#define STR(x)VAL(x)"与"#define VAL(x)#x"之间有什么区别?

当我使用这段代码时:

#include <stdio.h>
#define STR(x) #x

int main(void)
{
    printf(__FILE__ STR(__LINE__) "hello!\n");
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

它打印

hello.c__LINE__hello!
Run Code Online (Sandbox Code Playgroud)

但是当我使用它时:

#include <stdio.h>
#define STR(x) VAL(x)
#define VAL(x) #x

int main(void)
{
    printf(__FILE__ STR(__LINE__) "hello!\n");
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

它打印

hello.c7hello!
Run Code Online (Sandbox Code Playgroud)

有什么区别

#define STR(x) #x
Run Code Online (Sandbox Code Playgroud)

#define STR(x) VAL(x)
#define VAL(x) #x
Run Code Online (Sandbox Code Playgroud)

c

16
推荐指数
1
解决办法
1600
查看次数

为什么cygwin这么慢

我在Ubuntu上运行一个脚本,并测试它的时间:

$ time ./merger
./merger  0.02s user 0.03s system 99% cpu 0.050 total
Run Code Online (Sandbox Code Playgroud)

它花了不到1秒钟.但如果我使用cygwin:

$ time ./merger
real    3m22.407s
user    0m0.367s
sys     0m0.354s
Run Code Online (Sandbox Code Playgroud)

它花了超过3分钟.为什么会这样?我该怎么做才能提高cygwin的执行速度?

linux cygwin

8
推荐指数
1
解决办法
6847
查看次数

为什么basicConfig中的python日志记录级别没有效果?

import logging

# root logger
root = logging.getLogger()      # root
ch = logging.StreamHandler()
ch.setLevel(logging.WARN)
formatter = logging.Formatter('[root] %(levelname)s - %(message)s')
ch.setFormatter(formatter)
root.addHandler(ch)

# logging as child
c = logging.getLogger('mod')
c.setLevel(logging.DEBUG)
ch = logging.StreamHandler()
ch.setLevel(logging.DEBUG)
formatter = logging.Formatter('[mod] - %(levelname)s - %(message)s')
ch.setFormatter(formatter)
c.addHandler(ch)

c.error('foo')
c.warning('foo')
c.info('foo')
c.debug('foo')
Run Code Online (Sandbox Code Playgroud)

输出:

[mod] - ERROR - foo
[root] ERROR - foo
[mod] - WARNING - foo
[root] WARNING - foo
[mod] - INFO - foo
[mod] - DEBUG - foo
Run Code Online (Sandbox Code Playgroud)

没关系.root的级别是WARN,因此INFO …

python logging

8
推荐指数
1
解决办法
2423
查看次数

为什么这(i = ++ i%3)会产生警告:"可能未定义"?

int main(void)
{
    int i = 0;
    i = ++i % 3;
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

我这样编译:

$ gcc -Wall main.c -o main
main.c: In function ‘main’:
main.c:4: warning: operation on ‘i’ may be undefined
Run Code Online (Sandbox Code Playgroud)

为什么编译器说i可能未定义?

c

5
推荐指数
3
解决办法
333
查看次数

我如何得到gcc -s的解释?

我通过搜索引擎得到了"gcc -s"的含义.这意味着在链接时剥离符号.

但是如何使用"man gcc"或"gcc --help"获取此信息?它似乎没有相关信息:

root@desktop:~# gcc --help
Usage: gcc [options] file...
Options:
  -pass-exit-codes         Exit with highest error code from a phase
  --help                   Display this information
  --target-help            Display target specific command line options
  --help={target|optimizers|warnings|params|[^]{joined|separate|undocumented}}[,...]
                           Display specific types of command line options
  (Use '-v --help' to display command line options of sub-processes)
  --version                Display compiler version information
  -dumpspecs               Display all of the built in spec strings
  -dumpversion             Display the version of the compiler
  -dumpmachine             Display the compiler's target processor
  -print-search-dirs       Display …
Run Code Online (Sandbox Code Playgroud)

gcc

5
推荐指数
1
解决办法
7149
查看次数

函数调用的双括号?

请考虑以下代码:

#include <stdio.h>

int aaa(char *f, ...)
{
    putchar(*f);    
    return 0;
}

int main(void)
{
    aaa("abc");
    aaa("%dabc", 3); 
    aaa(("abc"));
    aaa(("%dabc", 3));
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

我想知道为什么以下几行:

    aaa("abc");
    aaa("%dabc", 3); 
    aaa(("abc"));
Run Code Online (Sandbox Code Playgroud)

运行没有错误,但第四行(见下文):

    aaa(("%dabc", 3));
Run Code Online (Sandbox Code Playgroud)

生成以下错误:

main.c:15:2:警告:传递'aaa'的参数1使得整数指针没有强制转换

main.c:3:5:注意:预期'char*'但参数类型为`int'

c syntax gcc

4
推荐指数
1
解决办法
1867
查看次数

为什么强制转换编译失败?

struct AAA
{
    char a_1;
    int a_2;
};

struct BBB
{
    char b_1;
    int b_2;
};


int main(void)
{
    struct AAA a1 = {2, 4};
    struct BBB b1;
    b1 = (struct BBB)a1;
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

如上所示,"b1 =(struct BBB)a1;"使complie说"错误:转换为请求的非标量类型".struct AAA和struct BBB具有相同类型的成员,为什么强制转换失败?

谢谢

c

2
推荐指数
2
解决办法
764
查看次数

使用#include <>时如何修改.h文件的路径?

当我们在linux平台中使用#include <stdio.h>时,编译器将在/ usr/include中搜索stdio.h.如何改变使用#include <>的路径?谢谢.

我之所以问这个问题:当我使用C标准函数isdigit()时,如果没有添加"#include <ctype.h>",程序会生成警告但没有错误.但是如果添加"#include <ctype.h>",链接时会产生错误.(我的编译器不是标准的gcc.)

我想知道为什么?

c gcc

1
推荐指数
1
解决办法
195
查看次数

标签 统计

c ×5

gcc ×3

cygwin ×1

linux ×1

logging ×1

python ×1

syntax ×1