什么实际上属于 C11中的"字符类型" - char当然除了?
更确切地说,字符类型的特殊例外(例如,任何对象都可以通过字符类型的左值表达式访问 - 参见C11标准中的§6.5/ 7),它们适用于哪些具体类型?他们似乎适用于uint8_t与int8_t从stdint.h,但这个保障?在另一方面GCC并不认为char16_t从uchar.h一个"性格类型".
Harbison/Steele的C引用说完整声明符的结尾是一个序列点.但什么是完整的声明者?
"完整的声明者是不属于另一个声明者的声明者"
...... 什么?
那么作为一个例子,C标准是否可以保证int i = 0, *j = &i内存地址与i指针中变量的值一起存储j?
换句话说,该int i = 0部分是完整的声明者吗?
在关于C的介绍性书籍中,经常声称指针或多或少是数组.充其量只是一个巨大的简化吗?
存在是 C中的数组类型,它可以表现从指针完全不同的,例如:
#include <stdio.h>
int main(int argc, char *argv[]){
int a[10] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9};
int *b = a;
printf("sizeof(a) = %lu\n", sizeof(a));
printf("sizeof(b) = %lu\n", sizeof(b));
return 0;
}
Run Code Online (Sandbox Code Playgroud)
给出输出
sizeof(a) = 40
sizeof(b) = 8
Run Code Online (Sandbox Code Playgroud)
或者作为另一个例子a = b会产生编译错误(GCC:"赋值给带数组类型的表达式").
当然,指针和数组之间存在密切关系,在某种意义上,数组变量本身的内容是第一个数组元素的内存地址,例如int a[10] = {777, 1, 2, 3, 4, 5, 6, 7, 8, 9}; printf("a = %ul\n", a);打印包含777的地址.
现在,一方面,如果你在结构中"隐藏"数组,只需使用=运算符就可以轻松复制大量数据(数组,如果忽略包装结构)(这甚至也很快):
#include …Run Code Online (Sandbox Code Playgroud) 我正在使用Gforth,我寻找一个标准的Forth字来划分两个双整数,或者至少是一个双整数的混合除以一个整数,但结果支持双整数.似乎没有一个.SM/REM,FM/MOD并UM/MOD有所有限制.
我错过了什么吗?为什么这个词不会与Forth内置?该操作定义明确,不会发生算术溢出.是否有必要自己编程?
当进入'a'Gforth时,字符的ASCII编号(通过使用key单词并按下将被放入堆栈的相同数字a)放入堆栈.
例如,这不适用于' '(空格).代替:
' ' ok
.s <1> 34384939008 ok
Run Code Online (Sandbox Code Playgroud)
数字"应该"为32.这个行为有什么解释?除了手动将对应于' '(空间)的ASCII数字放在堆栈上之外,可以做些什么 ?
如果我像这个例子一样跳进一个块,“跳过”声明,
#include <stdio.h>
int main(int argc, char *argv[]){
int counter = 0;
goto jump;
{
static int st = -9;
int au = -9;
jump:
printf("st = %d\n", st);
printf("au = %d\n", au);
au++;
st++;
counter++;
}
if(counter < 10) goto jump;
return 0;
}
Run Code Online (Sandbox Code Playgroud)
我可以用 来编译它gcc --std=c89 -pedantic。
似乎您无法真正“跳过”声明:即使从未到达进行这些声明的行,变量仍然被声明。
但不知何故你可以跳过定义......
st作为静态变量,它被初始化为值 -9 并计数到 0。au使用值 0 进行初始化并计数到 9。1. 和/或 2. 是 C 标准所必需的行为吗?
天真的人可能认为它有,因为通常auto假设没有提供存储类关键字.
但是,对于放在auto它们前面的文件范围变量会产生错误.
#include <stdio.h>
auto int x;
int main(void){
x = 7;
printf("x = %d", x);
}
Run Code Online (Sandbox Code Playgroud)
Clang抱怨说:
3:10: error: illegal storage class on file-scoped variable
auto int x;
Run Code Online (Sandbox Code Playgroud)
声明x没有任何存储类关键字,它编译:
#include <stdio.h>
int x;
int main(void){
x = 7;
printf("x = %d", x);
}
Run Code Online (Sandbox Code Playgroud)
现在我想知道x上面的例子中存储类有什么?它有名字吗?
在这个简单的C99代码:
int main(void){
int a[3][3] = {1};
int m = 3;
int x;
int b[m][m];
x = sizeof(b);
b[0][0] = -1;
return 0;
}
Run Code Online (Sandbox Code Playgroud)
使用GDB,我们在返回行设置一个断点并运行.现在让我们看看以下内容:
(gdb) p a
$1 = {{1, 0, 0}, {0, 0, 0}, {0, 0, 0}}
(gdb) p b
$2 = 0x7fffffffe3a0
(gdb) p sizeof(a)
$3 = 36
(gdb) p sizeof(b)
$4 = 0
(gdb) p x
$5 = 36
(gdb) whatis a
type = int [3][3]
(gdb) whatis b
type = int [][]
(gdb)
Run Code Online (Sandbox Code Playgroud)
我想知道这是怎么发生的.C运行时环境假定b的类型是int …
如果我们使用 ncurses 进行一个非常简单的计数器:
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <ncurses.h>
int main(void) {
struct timespec start;
clock_gettime(CLOCK_REALTIME, &start);
initscr();
cbreak();
nodelay(stdscr, TRUE);
{
int key = -1;
struct timespec delay, now;
do {
clock_gettime(CLOCK_REALTIME, &delay);
delay.tv_sec = 0;
delay.tv_nsec = 1000L * 1000L * 1000L - delay.tv_nsec;
nanosleep(&delay, NULL);
clock_gettime(CLOCK_REALTIME, &now);
mvprintw(1, 1, "%ld\n", (long)(now.tv_sec - start.tv_sec));
refresh();
key = getch();
if (key >= 0)
break;
} while (now.tv_sec - start.tv_sec < 60);
}
endwin();
return 0;
}
Run Code Online (Sandbox Code Playgroud)
按任意键后它会中止(好吧,因为 …