当我使用这段代码时:
#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) 我在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的执行速度?
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 …
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可能未定义?
我通过搜索引擎得到了"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) 请考虑以下代码:
#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'
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具有相同类型的成员,为什么强制转换失败?
谢谢
当我们在linux平台中使用#include <stdio.h>时,编译器将在/ usr/include中搜索stdio.h.如何改变使用#include <>的路径?谢谢.
我之所以问这个问题:当我使用C标准函数isdigit()时,如果没有添加"#include <ctype.h>",程序会生成警告但没有错误.但是如果添加"#include <ctype.h>",链接时会产生错误.(我的编译器不是标准的gcc.)
我想知道为什么?