How does scanf function work in C?

Why do you need an ampersand (&) in the scanf function. What will be the result or type of error (compilation or runtime) in the following C code?

 #include <stdio.h> void main() { int a; printf("enter integer:"); scanf("%d", a); } 
+6
c scanf
source share
7 answers

& in C is an operator that returns the address of an operand. Think of it this way, if you just specify scanf variable without & , it will be passed to it by value, which means that scanf will not be able to set the value for you to see. Passing it by reference (using & , in fact passes a pointer to a ), allows scanf set it so that calling functions can also see the change.

As for the specific error, you cannot say. The behavior is undefined. Sometimes it can calmly continue to work, not knowing that scanf changed some value somewhere in your program. Sometimes this can cause the program to crash immediately, as in this case:

 #include <stdio.h> int main() { int a; printf("enter integer: "); scanf("%d",a); printf("entered integer: %d\n", a); return 0; } 

The compilation shows this:

 $ gcc -o test test.c test.c: In function 'main': test.c:6: warning: format '%d' expects type 'int *', but argument 2 has type 'int' 

And execution shows a segmentation error:

 $ ./test enter integer: 2 Segmentation fault 
+11
source share

In C, all function arguments are passed by value; any changes to the formal parameter of the function are not reflected in the actual parameter. For example:

 void foo(int bar) { bar = bar + 1; } int main(void) { int x = 0; printf("x before foo = %d\n", x); foo(x); printf("x after foo = %d\n", x); return 0; } 

The output of the program will be

  x before foo = 0
 x after foo = 0

because bar gets the value of x (0), not a reference to x . Changing bar does not affect x .

In C, the way around this is to pass a pointer to a variable:

 void foo(int *bar) { *bar = *bar + 1; } int main(void) { int x = 0; printf("x before foo = %d\n", x); foo(&x); printf("x after foo = %d\n", x); return 0; } 

Now exit program

  x before foo = 0
 x after foo = 1

This time, the formal parameter bar not int, but a pointer to int and receives the address x (given by the expression &x in the call to foo ), not the value contained in x. The expression *bar means "get the value in the location bar," so *bar = *bar + 1 corresponds to x = x + 1 .

Since scanf() needs to be written in its arguments, it expects these arguments to be entered as pointers. The conversion specifier "% d" expects the corresponding argument to be a pointer to int ( int * ), the conversion specifier "% u" expects a pointer to unsigned int ( unsigned * ), "% s" expects a pointer to char ( char * ), " % f "expects a pointer to float ( float * ), etc. In your example, since a printed int , you need to use the &a expression to get the pointer.

Note that if a already a pointer type, you would not need to use the & operator in the scanf() call:

 int main(void) { int a, *pa; // declare pa as a pointer to int ... pa = &a; // assign address of a to pa scanf("%d", pa); // scanf() will write to a through pa ... } 

Note also that when passing an array to a function (for example, when using the% s conversion specifier to read a string) you do not need to use the & ; an array expression will be implicitly converted to a pointer type:

 int main(void) { char name[20]; ... scanf("%19s", name); // name implicitly converted from "char [20]" to "char *" ... } 
+3
source share

If you ask such a question, I would recommend only the training at the moment "he just does."

You will find out that you need an ampersand because scanf accepts one or more pointer arguments. If a is an int variable, it is not a pointer. & a ("address a") is a pointer, so it will work with scanf .

+2
source share

This is due to the fact that in C the parameters of the functions are passed by value . In order for the scanf() function to scanf() variable ' a ' in your main () function, the address from a 'must be assigned to scanf() , therefore, use the ampersand (address).

+2
source share

Because scanf requires a pointer to a variable (i.e. a reference), which will include the value.

+1
source share

You do not always need to use & with scanf . What you need to do is pass pointers. If you are new to C, you should spend some time reading the comp.lang.c FAQ:

http://c-faq.com/

In particular:

+1
source share

"&" in scanf is only required to get the address of a variable. You can use scanf without '&' with pointers:

 int myInt; int * pointer_to_int; pointer_to_int = &myInt; scanf("%d", pointer_to_int); 

In general, using '&' is often easier than creating a pointer so as not to use &.

0
source share

All Articles