View previous topic :: View next topic |
Author |
Message |
X-SoCiaL Apprentice
Joined: 15 Jul 2002 Posts: 160 Location: Filipstad/Sweden
|
Posted: Tue Feb 11, 2003 3:49 pm Post subject: C programming: Why doesn't it work? |
|
|
Hi there ...
Im working trough the C Primer Plus 4th edition to get up to speed with my C programming. On page 122(ch4) there is an example code snippet that I dont get to work as it should? Here is the code:
Code: | /*
skip2.c --- skips over first two integers of input
*/
#include <stdio.h>
int main(void)
{
int n;
printf("Please enter three integers:\n");
scanf("%*d %*d %*d", &n);
printf("The last integer was %d\n",n);
return 0;
} |
I've compiled the code but I dont get the expected output? When I run it and gives it say '1 4 5' as an argument it should return 5, right.
Code: | Please enter three integers:
1 4 5
The last integer was 1075090796
|
At least that is what the book says. That only the last number would be red by scanf(). If it is of any interest Im using GCC 3.2.1 r6 at the moment. Ya, I know the version of GCC got its problems as Ive followed the thread about it.
/Roger _________________ ~'¨'~'¨'~'¨'~'¨'~'¨'~'¨'~'¨'~'¨'~'¨'~'¨'~'¨'~'¨'~
: Would I do it if I couldnt do it with a computer?
~'¨'~'¨'~'¨'~'¨'~'¨'~'¨'~'¨'~'¨'~'¨'~'¨'~'¨'~'¨'~ |
|
Back to top |
|
|
abergou Tux's lil' helper
Joined: 12 Jun 2002 Posts: 85 Location: Ithaca, NY
|
Posted: Tue Feb 11, 2003 4:21 pm Post subject: |
|
|
replace your scanf line with:
Code: |
scanf("%*d %*d %d", &n)
|
%*d will say match a number there, but ignore it... you're ignoring all three integers therefore what you see when you output n is whatever was at that memory location before (garbage).
Hope it helps . |
|
Back to top |
|
|
emil n00b
Joined: 10 Feb 2003 Posts: 44 Location: Ireland
|
Posted: Tue Feb 11, 2003 4:23 pm Post subject: |
|
|
Remove the third '*' in the format string; you only want to skip _two_ assignments, not three.
thus
Code: |
/*
skip2.c --- skips over first two integers of input
*/
#include <stdio.h>
int main(void)
{
int n;
printf("Please enter three integers:\n");
scanf("%*d %*d %d", &n);
printf("The last integer was %d\n",n);
return 0;
}
|
gives the expected output.
With the original format string, the variable is uninitialized, and whatever garbage is there is printed. |
|
Back to top |
|
|
jingram n00b
Joined: 09 Feb 2003 Posts: 12 Location: Urbana, IL
|
Posted: Tue Feb 11, 2003 4:28 pm Post subject: Re: C programming: Why doesn't it work? |
|
|
Looks like 2 people beat me to it. Here's another way that would work:
Code: | scanf("%d %d %d", &n, &n, &n); |
That way the result of each conversion is stored, but the second and third conversions overwrite the preceding ones. It probably isn't as efficient since there are 3 assignments instead of just 1, but there is still the same number of conversions.
I hope this helps. |
|
Back to top |
|
|
X-SoCiaL Apprentice
Joined: 15 Jul 2002 Posts: 160 Location: Filipstad/Sweden
|
Posted: Tue Feb 11, 2003 6:34 pm Post subject: |
|
|
Thanx for all the answers ...
/Roger _________________ ~'¨'~'¨'~'¨'~'¨'~'¨'~'¨'~'¨'~'¨'~'¨'~'¨'~'¨'~'¨'~
: Would I do it if I couldnt do it with a computer?
~'¨'~'¨'~'¨'~'¨'~'¨'~'¨'~'¨'~'¨'~'¨'~'¨'~'¨'~'¨'~ |
|
Back to top |
|
|
|