UMBC CS 201, Fall 06
UMBC CMSC 201
Fall '06

CSEE | 201 | 201 F'06 | lectures | news | help

scanf2

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: R. Chang * Modified by: Sue Evans * Date: 3/5/04 * Section: 01XX & 02XX * EMail: bogar@cs.umbc.edu * * This program demonstrates the use of the * scanf function - misuse - forgetting the & **********************************************/ #include <stdio.h> 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] %


CSEE | 201 | 201 F'06 | lectures | news | help

Tuesday, 22-Aug-2006 07:14:18 EDT