#include <stdio.h>
#include <stdlib.h>
int main(){
char name[100];
int age;
printf("Enter you name:\n");
scanf("%s", name);
printf("Enter you age:\n");
scanf("%d", &age);
printf("You are %s,\nYour age is %d", name, age);
return 0;
}
I apologize in advance if this has been answered before. I'm a beginner to programming, and this is my first post. I'm just learning C, but I don't understand this: Why do we need the '&' for the int variable age, but not for the array variable name?
Due to the history of C, an array is automatically converted to a pointer to its first element, except when it is the operand of sizeof
or a typeof operator or of unary &
or it is a string literal used to initialize an array.
So scanf("%s", name);
is equivalent to scanf("%s", &name[0]);
. It does pass an address to scanf
.
In the early days of the C language, there were no provisions for handling an aggregate objects such as arrays or structures—no operation would act on the entire aggregate object as if it were a single object. Operations were performed only on simple individual items such as char
, int
, float
, and pointers. Computers were much slower and less capable than today’s computers, and programming languages were generally doing very simple things.
Nonetheless, programmers needed to work with arrays somehow. Since the language did not support using the name of an array to refer to the entire array as if it were a single value of an aggregate object, this notion of an automatic conversion of an array to a pointer was designed to make it easier to work with arrays. Writing scanf("%s", name);
instead of scanf("%s", &name[0]);
looks nicer and it easier to type. So this convenience was made part of the language.
You could not even assign structures in C, such as saying a = b
for a structure type. That was added later. Once programming languages and compilers had grown powerful enough to make stronger constructions practical, it was too late to change how arrays behaved in C. To make a = b
work for an array, we would have to take away the automatic conversion of an array to a pointer. So now this is a permanent feature of C.