Skip to content
Related Articles

Related Articles

Improve Article
Why “&” is not used for strings in scanf() function?
  • Difficulty Level : Basic
  • Last Updated : 02 Jun, 2017

Below is syntax of Scanf. It requires two arguments:

scanf("Format Specifier", Variable Address);

Format Specifier: Type of value to expect while input
Variable Address: &variable returns the variable's memory address.

In case of a string (character array), the variable itself points to the first element of the array in question. Thus, there is no need to use the ‘&’ operator to pass the address.




// C program to illustrate  not using "&"
// in scanf statement
#include<stdio.h>
int main()
{
    char name[25];
  
    // Syntax to scan a String
    scanf("%s", name);
  
    // Comparing base address of String with adrress
    // of first element of array which must return
    // true as both must be same
    printf("(Is Base address = address of first element)? \n %d",
           (name == &name[0]));
  
}

Output:

(Is Base address = address of first element)?
1

Important Points

  1. ‘&’ is used to get the address of the variable. C does not have a string type, String is just an array of characters and an array variable stores the address of the first index location.
  2. By default the variable itself points to the base address and therefore to access base address of string, there is no need of adding an extra ‘&’

This article is contributed by Ajeet. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.

Want to learn from the best curated videos and practice problems, check out the C Foundation Course for Basic to Advanced C.
My Personal Notes arrow_drop_up
Recommended Articles
Page :