ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
i have a very silly problem with c programming in linux.i have red hat 6.2 linux version.and whenever i am writing a code for accepting a single character its not accepting it, rather it is skiping the value.i also used fflush(stdin),but problem not solved.the same code runs perfectly in TC(dos based).what can be the problem can any one tell me?
i used scanf and getchar both but these both never worked
Originally posted by abi_sh i have a very silly problem with c programming in linux.i have red hat 6.2 linux version.and whenever i am writing a code for accepting a single character its not accepting it, rather it is skiping the value.i also used fflush(stdin),but problem not solved.the same code runs perfectly in TC(dos based).what can be the problem can any one tell me?
Distribution: Red Hat Enterprise Linux 3.0 Advanced Server
Posts: 34
Rep:
hi abi_sh,
yes it is the problem in linux. fflush(stdin) doesn't work in linux.
the reason why it is skipping that getting of char is there might be something garbage in stdin buffer. the garbage is due to previous scanf()'s or gets()'s before getchar(). so always use fgets() instead of scanf()'s and getchar()'s.
now try to replace to scanfs()'s and gets()'s with fgets();
if you are in need of formatted input , now try to format the input using atoi() or strtod() or something else.
now use getchar(), i hope there won't be problem.
hi edreddy
i tried the fgets() function also but it doesn't work it is giving me a error saying that missing parameters or something,well as far as i know fgets is used with FILE pointer right ,it takes file pointer.
if u don't mind can u write a small code with this fgets() and show how to use it?
Here is a code example of how to use getchar(). It is pretty much the same as the fgetc example above.
Code:
#include <stdio.h>
#include <unistd.h>
main()
{
char c = (char) NULL;
while ( 1 )
{
c = getchar();
if ( c != -1 )
printf("Hey there\n");
usleep(100);
} // end while
}
The problem when you have to press enter to get single character input is actually a terminal problem and not a fgetc or getchar error. Most terminals by default are in canonical mode, meaning that you have to press enter to send the input line. You can turn off canonical mode into ways.
Using stty:
Code:
stty -icanon
Run the first example above, before and after running stty.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.