Our second example shows the consequence of forgetting the
& in front of the variable passed to scanf: you
get garbage and although it didn't happen this time, you can and sometimes
do get a segmentation fault - core dump.
The Program
/**********************************************
* File: scanf2.c
* Author: Sue Bogar
* Date: 2/2/98
* Section: 101
* SSN: 123-45-6789
* EMail: bogar@cs.umbc.edu
*
* This program demonstrates the use
* of the scanf function.
**********************************************/
#include
int main ( )
{
int n ;
printf("Enter an integer: ") ;
scanf("%d", n) ;
printf("n = %d\n", n) ;
return 0;
}
The Sample Run
linux3[88] % gcc -Wall -ansi scanf2.c
scanf2.c: In function `main':
scanf2.c:21: warning: format argument is not a pointer (arg 2)
linux3[89] % a.out
Enter an integer: 5
n = 134517984
linux3[90] % !a
a.out
Enter an integer: 7
n = 134517984
linux3[91] %