By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,418 Members | 1,084 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,418 IT Pros & Developers. It's quick & easy.

what is wrong with void ungets(char *s)

P: 8
hello everyone.I am new here,and also new to c;I got new problem

Expand|Select|Wrap|Line Numbers
  1. #include <stdio.h>
  2. #include <conio.h>
  3.  
  4. void ungets(char *s)
  5. {
  6.     while (*s != '\0')
  7.         ungetc(*s++,stdin);
  8.     ungetc('\0',stdin);
  9. }
  10.  
  11. void main()
  12. {
  13.     unsigned char c;
  14.  
  15.     char *s = "i am hero!";
  16.     ungets(s);
  17.  
  18.     while ((c = getchar()) != EOF)
  19.         putchar(c);
  20. }
I think this should print "i am hero!",but thers is only a 'i' !what is wrong?i can not figure out
Jun 2 '07 #1
Share this Question
Share on Google+
5 Replies


AdrianH
Expert 100+
P: 1,251
hello everyone.I am new here,and also new to c;I got new problem

Expand|Select|Wrap|Line Numbers
  1. #include <stdio.h>
  2. #include <conio.h>
  3.  
  4. void ungets(char *s)
  5. {
  6.     while (*s != '\0')
  7.         ungetc(*s++,stdin);
  8.     ungetc('\0',stdin);
  9. }
  10.  
  11. void main()
  12. {
  13.     unsigned char c;
  14.  
  15.     char *s = "i am hero!";
  16.     ungets(s);
  17.  
  18.     while ((c = getchar()) != EOF)
  19.         putchar(c);
  20. }
I think this should print "i am hero!",but thers is only a 'i' !what is wrong?i can not figure out
This makes sense as what I remeber, but when I looked it up, it appears that what you did is valid.

What compiler are you using and what version is it?

If it is an older compiler, it may be that it is using the old standard which can only push back only one character. If you push back multiple ones, all you will get is the first one you pushed back when you read from the stream again.

Take a look at the return value from ungetc().


Adrian
Jun 2 '07 #2

AdrianH
Expert 100+
P: 1,251
I've just tested the code. You are definatly using an old compiler based on your syntax.

Expand|Select|Wrap|Line Numbers
  1. #include <stdio.h>
  2.  
  3. void ungets(char *s)
  4. {
  5.     while (*s != '\0')
  6.         ungetc(*s++, stdin);
  7.     //ungetc('\0', stdin); // This line is unnecessary, only ment for c-strings
  8. }
  9.  
  10. // MUST return int according to standard.  void is nonstandard and old extention.
  11. int main()  
  12. {
  13.     char c;
  14.  
  15.     char *s = "i am hero!";
  16.     ungets(s);
  17.  
  18.     while ((c = getchar()) != EOF)
  19.         putchar(c);
  20.  
  21.     return 0;  // REQUIRED in C, optional in C++.
  22. }
What I get back is '!oreh ma i' from a gcc compiler v 3.4.4 cygwin special. This is correct because this acts like a stack, first in, last out (FILO).


Adrian
Jun 2 '07 #3

P: 8
Thanks for your help.I am using vc6.0 compiler,maybe it is a litter older.
I am reading c programming lanuage,this is a function in Exerise 5-10.
I will try another complier.
Or may be there is a soultion!
Expand|Select|Wrap|Line Numbers
  1. #define BUFSIZE 100   
  2.  
  3. char buf[BUFSIZE];    //buffer for ungetch
  4. int bufp = 0;               //next free position in buf
  5.  
  6. int getch(void )         //get a (pushed back )charater
  7. {
  8.     return (bufp > 0) ? buf[--bufp] : getchar();
  9. }
  10. void ungetch(int c)   //push character back on input
  11. {
  12.     if (bufp > BUFSIZE)
  13.         printf("error : too many characters");
  14.     else
  15.          buf[bufp++] = c
  16. }
  17.  
so this a stream of my own.am i right?
Jun 3 '07 #4

P: 8
And it did!!
Expand|Select|Wrap|Line Numbers
  1. #include <stdio.h>
  2. //#include <conio.h>
  3.  
  4. #define    BUFSIZE     100
  5.  
  6. char buf[BUFSIZE];
  7. int bufp = 0;
  8.  
  9. //get a pushed back character
  10. int getch(void)
  11. {
  12.     return (bufp > 0) ? buf[--bufp] : getchar();
  13. }
  14.  
  15. //ungetch: push charater back on input
  16. void ungetch(unsigned char c)
  17. {
  18.     if (bufp > BUFSIZE)
  19.     {
  20.         printf("ungetch: too many characters\n");
  21.     }
  22.     else
  23.     {
  24.         buf[bufp++] = c;
  25.     }
  26. }
  27. //ungets: push back a string
  28. void ungets(char *s)
  29. {
  30.     while (*s != '\0')
  31.         ungetch(*s++);
  32.     ungetch('\0');
  33. }
  34.  
  35. void main()
  36. {
  37.     unsigned char c;
  38.  
  39.     char *s = "i am hero!";
  40.     ungets(s);
  41.  
  42.     while ((c = getch()) != EOF)
  43.         putchar(c);
  44. }
  45.  
The result is exactly what you told.Thanks again Adrian!!
Jun 3 '07 #5

AdrianH
Expert 100+
P: 1,251
And it did!!

The result is exactly what you told.Thanks again Adrian!!
Glad to hear it.


Adrian
Jun 3 '07 #6

Post your reply

Sign in to post your reply or Sign up for a free account.