[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: help with C program



Meidan Zemer <meidanze@internet-zahav.net.il> writes:

> I have a small C program wich i compile with GNU C Compiler 2.8.1 and
> 2.7.

This is not a linux question. It has nothing to do with linux.
Next time please RTFM and then post to comp.lang.c.

I am not sure what you are trying to do. You do not say what your
input is. I don't understand why you think that getchar() should
prompt you for input. I still think I can provide you some hints 
about what you do wrong:

1) Use int main(void); main should return an integer. Throw away
   any text that says otherwise. 

2) You don't need string.h

3) Don't use scanf() - it is a recipe for terrible bugs and clumsy
   code. Use fgets().

4) Don't read more than your string can hold, and don't forget the
   null-terminator.

5) What do you think your getchar() will read?

With as little changes to your code as possible, here is what
you can do:

#include <stdlib.h>
#include <stdio.h>

int main(void) /* sic! */
{
 char str[10];
 int num;
 scanf("%9s",str);    /* only read as much as you can hold */
 str[10] = '\0';      /* terminate the string */
 printf("scanf accept %s\n",str);
 num=getchar();       /* pray there is more input */
 printf("getchar accept %c\n",num);
 return EXIT_SUCCESS; /* main return a value */
}

Compile it and run:

$ ./a.out
1234567890
scanf accept 123456789
getchar accept 0
$ 

Hope it helps.

-- 
Oleg Goldshmidt          goldshmt@netvision.net.il   
BLOOMBERG L.P. (BFM)     oleg@bfr.co.il

=================================================================
To unsubscribe, send mail to linux-il-request@linux.org.il with
the word "unsubscribe" in the message body, e.g., run the command
echo unsubscribe | mail linux-il-request@linux.org.il