2
#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?

2 Answers2

1

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.

Daniel Giger
  • 2,023
  • 21
  • 20
0

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.

  • "If a side effect on a *scalar object* is unsequenced relative to another side effect on the same scalar object, the behavior is undefined." — So this doesn't apply here in the case of the statement ```scanf("%4s%4s", b, b);``` since ```b``` is not a scalar object, right? –  May 07 '20 at 07:22
  • 1
  • supports Monica Celio Thanks. –  May 07 '20 at 07:25
  • C99 Standard: "Between the previous and next sequence point an object shall have its stored value modified at most once by the evaluation of an expression" — So was this definition by the C99 standard incomplete and hence it was updated in C11 since it only contains the term ```object``` and not ```scalar object```? –  May 07 '20 at 08:51
  • @NirajRaut *"object"* is to be understood as single entity here, because an array does not have "*its stored value"*. Nonetheless, An array is compound of single objects with different values. - At the end, You modifying single objects with `scanf()` statement, thus the behavior shall be undefined. - But this is such a huge and intersting question on its own, that it can´t be answered and shouldn´t only reside here. - Ask a separate focused question about that. I´m very curious about the answers there, too. – RobertS supports Monica Cellio May 07 '20 at 09:17
  • Please tell me whether ```%c``` expects a ```char``` or an ```int```? –  May 07 '20 at 11:04
  • @NirajRaut I was wrong before. I apologize. `%c` expects an argument of type `char*`. - The `EOF` case with storing characters in `int` suprisingly doesn´t have side influence on here. But `char` is still compatible with `int`. – RobertS supports Monica Cellio May 07 '20 at 11:53
  • I feel that you were right before. https://stackoverflow.com/a/55009814/13469230 –  May 07 '20 at 11:58
  • @NirajRaut Things are sometimes different for `scanf()` than for `printf()`. This is one of those cases. `scanf()` really expects `char*` for `%c`. Here is a test case: https://godbolt.org/z/dBpd8- – RobertS supports Monica Cellio May 07 '20 at 12:02
  • That's true for ```scanf()```. But my question is can I pass a negative ```int``` in the case of ```printf``` since while printing it's converted into an ```unsigned char``` — like ```printf("%c", -65);``` — I tried it on GCC but not getting the correct output. –  May 07 '20 at 12:07
  • @NirajRaut Where is the relation to this question? – RobertS supports Monica Cellio May 07 '20 at 12:55
  • Don't ask for a relation. Just answer it as I cannot ask more questions now. I did not even know that I can ask only 6 questions in 24 hours. –  May 07 '20 at 12:58
  • 1
    @NirajRaut You cannot print negative ASCII codes in `char` notation because there are none. All ASCII values are positive. -> https://stackoverflow.com/questions/4690415/negative-ascii-value What do you want to achieve with that statement? – RobertS supports Monica Cellio May 07 '20 at 13:13
  • Then how does ```%c``` expects an ```int``` in ```printf```. Shouldn't it expect an ```unsigned int```? –  May 07 '20 at 13:15
  • 1
    @NirajRaut The magic happens here just because of `EOF`. It is not a character, rather an symbol to indicate an error returned by a function. - To differentiate the error from common characters it had to be a negative value. The exact negative value of `EOF` is implementation-defined. – RobertS supports Monica Cellio May 07 '20 at 13:19
  • Hey, if there are no negative values what is ```signed char``` then? –  May 07 '20 at 14:40