问题
#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