When should I use ampersand with scanf()

in c program '&' is used in 'scanf' to indicate
scanf in c
how to use scanf and printf in c
char input using scanf
does scanf null terminate
scanf without
scanf pointer
scanf string c++

What are the rules for using ampersand in c while using scanf()?

struct Student 
{
  char name[20];
  int id;
};

int main(void)
{
  struct Student std1;
  printf("enter name and id of std1\n");
  scanf("%s %d", std1.name, &(std1.id));
}

Why for String do I not need to use the the ampersand and for int I have to use it?

Is there a rule on when to use the ampersand sign?

scanf reads particular types of data into addresses which are passed as second, third, fourth and so on arguments.

int var;
scanf("%d",&var);

Here var is value and &var is address. The above statement says: read %d (integer) type of data into &var address.

char s[20];
scanf("%s",s);

Here s is address not the value because s is a character array (string). An array name itself indicates its address. s == &s[0], these are both the same.

The above statement says: read %s (array of characters) type of data into address location starting from s.

#include<stdio.h>
#define MAX 5

int main()
{
    int a[MAX];
    int i;
    printf("Enter Values of array\n");
    for(i=0;i<MAX;i++)
    {
        printf("Enter a[%d] =  ",i);
        scanf("%d",&a[i]); // reading each time single integer value starting index with 0 and ending index MAX-1.
    }
}

There is no single format specifier to scan group of integers at a time as like scanning group of characters at time with the help of %s.

And here a=&a[0]; you can scan single integer value directly to the address which is pointed by a.

scanf("%d",a);
printf("a[0]=%d\n",a[0]);

if you enter 10 then prints a[0]=10.

Usage of Pointers:

if you use pointers as shown below, then you will come to know how to increment the pointer and get the values into different locations of array.

You can move the pointer location to read arrays. You can read arrays without moving pointer location.

  1. Reading arrays by moving pointer locations

    #include<stdio.h>
    #define MAX 5
    
    int main()
    {
        int a[MAX];
        int i;
        int *ptr;
        ptr = &a[0];
        printf("Enter Values of array\n");
        for(i=0;i<MAX;i++)
        {
            printf("Enter a[%d] =  ",i);
            scanf("%d",ptr);
            ptr++; //moving pointer 
        }
    }
    
  2. Reading arrays with out moving pointer locations.

    #include<stdio.h>
    #define MAX 5
    
    int main()
    {
        int a[MAX];
        int i;
        int *ptr;
        ptr = &a[0];
        printf("Enter Values of array\n");
        for(i=0;i<MAX;i++)
        {
            printf("Enter a[%d] =  ",i);
            scanf("%d",ptr+i); //we are not moving ptr position we scaning each time into next location by incrementing i 
        }
    }
    

    When a pointer is incremented then the increment is dependent on the type of pointer. Here ptr is an integer pointer so ptr+1 will increment ptr+sizeof(int) locations.

int a[5][5];

This is two dimensional array so you require 5 pointers to scan so I was declared pointer array.

#include<stdio.h>
#define MAX 5

int main()
{
    int a[MAX][MAX],i,j;
    int *pointer[MAX];

    for(i=0;i<MAX;i++)
        pointer[i]=&a[i][0]; //initializes the pointers 

    printf("Enter elements :\n");
    for(i=0;i< MAX;i++)
    {   
        for(j=0;j<MAX;j++)
        {
            printf("Enter the a[%d][%d] element: ",i,j);
            scanf("%d",pointer[i]+j); //each time you will move like 00 01 02 03 04 and second time 10 11 12 13 14 and so on...
            //printf("%u or %x",pointer[i]+j,pointer[i]+j);//un comment this line and see the addresses how the address incrementing for each element
        }
    }

    printf("The Given Matrix:\n\n");
    for(i=0;i<MAX;i++)
    {
        for(j=0;j<MAX;j++)
        {
            printf("%d",*(pointer[i]+j));
            printf("\t\t");
        }
        printf("\n\n");
    }
}

Direct scanning

printf("Enter elements :\n");
for(i=0;i< MAX;i++)
{   
    for(j=0;j<MAX;j++)
    {
        printf("Enter the a[%d][%d] element: ",i,j);
        scanf("%d",&a[i][j]); //we can't do like this a++ or ++a or a+i this is illegal in C. for that purpose we are using pointers  
    }
}

You will found most of the above stuff in The C Programming Language (Second edition) by Brian W. Kernighan and Dennis M. Ritchie.

Why do we use an ampersand in the scanf in the C language?, Why do we use & in scanf and not in printf? If the function scanf didn't receive a pointer to a variable and it only received the value, it wouldn't know where to save that value. In code form, if scanf looked like this: Code: int scanf () { int bird; // read input stream // save next int into variable bird // return number of items read }.

Because C strings have a type of char []. The array name has the value of it's address, but an int variable doesn't, you need to use &

And it's wrong to write void main, you should always use int main.

Use of & in scanf() but not in printf(), means to pass by reference. * means the dereference of a pointer variable, meaning to get the value of that pointer variable. When should I use ampersand with scanf() Ask Question Asked 6 years, 1 month ago. Active 17 days ago. Viewed 25k times 18. 10. What are the

In C, strings are arrays of characters (terminated with \0 character).

Array name returns the pointer of the first element of a array (memory location where array is stored), and name of the scalar variable returns the value of the scalar, so you need to use & operator to get the memory location of scalar in which you need to write the value.

Why is the ampersand not required in case of reading a string in C , The ampersand is the address-off operator. The scanf function is going to fill a buffer, and it needs to know its address so that it can write to it. If you want to scan using %s then your input should be char buffer like char buf[10]. for e.g. char a[10]; scanf("%s",a); u don't need to put &(ampersand) sign in printf when u use %c or %d ? no need to provide address & to printf() as printf() job is to print not to scan. for e.g

Pointers in C: when to use the ampersand and the asterisk?, In a way, you could argue that scanf() is the input version of the printf() function. variable with an ampersand in the scanf() function; when using scanf() to read  You don’t need to prefix a char array variable with an ampersand in the scanf () function; when using scanf () to read in a string, just specify the string variable name. The scanf () function stops reading text input at the first white space character, space, tab, or Enter key.

Basics of the scanf() Function in C Programming, n = %d\n", n); } int main() { int i = 5; printf("before function call, i = %d\n", i); square_it( i ); printf("after function call, The usage of & is explain in detail scanf write a value to a variable and need the variable adres to do so. 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.

Why we dont use ampersand(&) in printf and why we use it in scanf, I want to know about ampersand(&) that why used in scanf() method. stored as pointers, you use the reference operator so you can store the  The ampersand usage rules are simple. First Rule: Generally, don’t use the ampersand in regular text, headings or titles as a replacement for and. Just don’t—except as noted in the second rule. Second Rule: Limit ampersand use to these few situations: Proper nouns like company names, e.g., “Crown & Co.”

Comments
  • Read a tutorial on C strings
  • @PP might sound a bit condescending in his comment but he's absolutely right : read a good C programming book first. Kernighan & Ritchie's The C programming language is an excellent resource.
  • @PP is absolutely right. You can't begin to write C until you understand why & is needed for one variable and not the other. It's like trying to drive a car without knowing the difference between the throttle and brake.