[TriLUG] a C question

Greg Brown gregbrown at mindspring.com
Wed Oct 23 09:45:01 EDT 2002


I'm teaching myself C and I'm confused about something (and I'm only on day 3 
of "teach yourself in 21 days" so I'm still doing really basic things).

The following program complies and does what I expect it to (return a number 
to STDOUT):

#include<stdio.h>

int fav_num;

int main(void)
{

	printf("What is your favorite number?\n");
	scanf("%d", &fav_num);
	printf("\n\nYour favorite number is %d\n\n", fav_num);
	return 0;
}

If I try to enter text instead of a number the program returns 0, not the 
text.  The issue seems to be %d gizmo which I'm guessing can only be a 
numeric, not text.  Or is the problem the way I've declared the variable 
fav_num?  Is there a different way to declare a variable when it is text?  Or 
is the issue %d?

Just curious.

And thanks!

Greg





More information about the TriLUG mailing list