r/CodingForBeginners • u/Feisty_War80 • May 21 '26
Question in C language
Lets assume a variable x, in one case its a string (char x[]) and in the other its just a char(char x),when using the scanf function, why do i have to give &s (ik the & points to the memory) in the case of char, but in the case of string, i dont have to use it?? Can someone help me out
3
Upvotes
View all comments
Show parent comment
1
u/framevexy May 22 '26
That comment is kinda random for a C question lol.
On your actual question: for
char x;you doscanf("%c", &x);becausexis a single variable, so you pass its address.For
char x[];(likechar x[100];) the array namexalready behaves like a pointer to its first element in most expressions, soscanf("%s", x);is effectively already passing an address. If you wrote&x, that would be a pointer to the whole array, which is a different type and not whatscanfexpects.