Gentoo Forums
Gentoo Forums
Gentoo Forums
Quick Search: in
C programming: Why doesn't it work?
View unanswered posts
View posts from last 24 hours

 
Reply to topic    Gentoo Forums Forum Index Portage & Programming
View previous topic :: View next topic  
Author Message
X-SoCiaL
Apprentice
Apprentice


Joined: 15 Jul 2002
Posts: 160
Location: Filipstad/Sweden

PostPosted: Tue Feb 11, 2003 3:49 pm    Post subject: C programming: Why doesn't it work? Reply with quote

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
View user's profile Send private message
abergou
Tux's lil' helper
Tux's lil' helper


Joined: 12 Jun 2002
Posts: 85
Location: Ithaca, NY

PostPosted: Tue Feb 11, 2003 4:21 pm    Post subject: Reply with quote

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
View user's profile Send private message
emil
n00b
n00b


Joined: 10 Feb 2003
Posts: 44
Location: Ireland

PostPosted: Tue Feb 11, 2003 4:23 pm    Post subject: Reply with quote

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
View user's profile Send private message
jingram
n00b
n00b


Joined: 09 Feb 2003
Posts: 12
Location: Urbana, IL

PostPosted: Tue Feb 11, 2003 4:28 pm    Post subject: Re: C programming: Why doesn't it work? Reply with quote

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
View user's profile Send private message
X-SoCiaL
Apprentice
Apprentice


Joined: 15 Jul 2002
Posts: 160
Location: Filipstad/Sweden

PostPosted: Tue Feb 11, 2003 6:34 pm    Post subject: Reply with quote

Thanx for all the answers ...

/Roger
_________________
~'¨'~'¨'~'¨'~'¨'~'¨'~'¨'~'¨'~'¨'~'¨'~'¨'~'¨'~'¨'~
: Would I do it if I couldnt do it with a computer?
~'¨'~'¨'~'¨'~'¨'~'¨'~'¨'~'¨'~'¨'~'¨'~'¨'~'¨'~'¨'~
Back to top
View user's profile Send private message
Display posts from previous:   
Reply to topic    Gentoo Forums Forum Index Portage & Programming All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum