0

I have a question in strings. In the given program: c is used after scanf ( which indicates address) and the same c is used after printf (which means value), then what should we consider c as a memory location or value? . Can anyone explain this in detail?

#include<stdio.h>
int main()
{
char c[50];
printf("enter the string");
scanf("%s",c);
printf("the value of string is %s",c);
return 0;
}
soumya
  • 21
  • 4
  • 2
    The variable `c` is an array, nothing more and nothing less. It's contents is the data stored in the array. It can *decay* to a pointer to its first element (i.e. when you use `c` in the calls to `scanf` and `printf` it's actually the same as doing `&c[0]`). – Some programmer dude Sep 15 '16 at 09:26
  • 2
    It's an array (so a value) which decays to a pointer (so a memory location) when passed to those functions as an argument. If you're asking for an essay on what pointers and arrays are, that's a whole essay and probably too big for an answer here. – Useless Sep 15 '16 at 09:27
  • Garf365 my question is different – soumya Sep 15 '16 at 09:39
  • But answers of this question are same as yours. Joachim Pileborg has summarized them into its comment – Garf365 Sep 15 '16 at 09:40
  • Joachim and Useless thanks for responding but now also I am not getting in strings after scanf and printf how can we use the same variable. It will be really helpful if you you can explain in detail. – soumya Sep 15 '16 at 09:41
  • 1
    `scanf` reads the characters in (to the array `c`) and `printf` prints things out - we can use the same variable for different things and usually do. – doctorlove Sep 15 '16 at 09:48
  • Garf365 my question is not about array and pointer... My question is can a variable store both address and value within the same function? – soumya Sep 15 '16 at 11:54
  • The compiler knows. – vz0 Sep 15 '16 at 12:06

0 Answers0