Is b a scalar object in this case?

不打扰是莪最后的温柔 提交于 2020-06-27 18:24:08

问题


#include <stdio.h>

int main(void) 
{
    char b[5];
    scanf("%4s%4s", b, b);
    printf("%s", b);
}

What is the exact definition of a scalar object? Is b a scalar object in this case?


回答1:


Quote from ISO/IEC 9899:2018 (C18), 6.2.5 (Types)/21:

"Arithmetic types and pointer types are collectively called scalar types. Array and structure types are collectively called aggregate types.47)"

47) "Note that aggregate type does not include union type because an object with union type can only contain one member at a time."


"What is the exact definition of a scalar object?"

A scalar object is an object which only consists of a single entity, such as pointers and objects of arithmetic types.

"Is b a scalar object in this case?"

b isn´t a scalar object as a scalar object hold only one single entity. Arrays such as b are "aggregates". The array to pointer decay in scanf("%4s%4s", b, b); and printf("%s", b); doesn´t change that b is still of array type.




回答2:


According to the c11 standard, "Arithmetic types and pointer types are collectively called scalar types. Array and structure types are collectively called aggregate types"

So no, b isn't a scalar because it is an array. If it were a number or a pointer (like char* b), it would be a scalar type.



来源:https://stackoverflow.com/questions/61651790/is-b-a-scalar-object-in-this-case

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!