我发现C代码打印从1到1000没有循环或条件:但我不明白它是如何工作的.任何人都可以通过代码解释每一行吗?
#include <stdio.h>
#include <stdlib.h>
void main(int j) {
printf("%d\n", j);
(&main + (&exit - &main)*(j/1000))(j+1);
}
Run Code Online (Sandbox Code Playgroud) 我在ac程序中使用scanf从STDIN读取一个int:
scanf("%d", &n);
Run Code Online (Sandbox Code Playgroud)
当我编译启用优化的c程序时,我收到一些警告:
gcc main.c -lm -lpthread -O2 -o main
main.c: In function ‘main’:
main.c:45: warning: ignoring return value of ‘scanf’, declared with attribute warn_unused_result
main.c:50: warning: ignoring return value of ‘scanf’, declared with attribute warn_unused_result
Run Code Online (Sandbox Code Playgroud)
但是当我删除优化选项时,为什么不收到这些警告?
gcc main.c -lm -lpthread -o main
Run Code Online (Sandbox Code Playgroud)
PS:我没有使用-Wall或类似的东西.
可能重复:
为什么C字符文字的内部而不是字符?
int main(void)
{
printf("%d %d\n", sizeof('c'), sizeof(char));
return 0;
}
Run Code Online (Sandbox Code Playgroud)
为什么sizeof('c')返回4而不是1?
我想知道是否有办法在运行时获取内存中c函数的大小.
我已经使用过这段代码,但它不起作用:
#include <stdio.h>
#include <stdlib.h>
#include <stddef.h>
int main(void)
{
int t[10];
char c;
offsetof(t, p);
p:
return 0;
}
Run Code Online (Sandbox Code Playgroud) gcc 4.6.2 c89
为2D阵列分配内存并填充字符.
但是,我似乎没有填写,因为我打印什么都没有显示.
我在这里做错了吗?
char **attributes = NULL;
/* TODO: Check for memory being allocated */
attributes = malloc(3 * sizeof(char*));
int i = 0;
int k = 0;
for(i = 0; i < 3; i++) {
for(k = 0; k < 5; k++) {
sdp_attributes[i] = malloc(5 * sizeof(char));
sdp_attributes[i][k] = k;
}
}
for(i = 0; i < 3; i++) {
for(k = 0; k < 5; k++) {
printf("attributes[i][k] [ %c ]\n", attributes[i][k]);
}
} …Run Code Online (Sandbox Code Playgroud)