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.
The getchar() function provide a hardtime for me. I am using GCC 3.2.3.
#include <stdio.h>
int main() {
int c;
c=getchar();
putchar(c);
}
I expects that as soon as i enter a character the execution should proceed with out blocking. However I am forced to hit <enter> after i input a character. Am I doing anything wrong. I don't remember, i had this problem in previous versions of gcc (I can't assertively say it though, because it is long time since i have written something in C. I rather have a bleak memory on how it was before)
The background is if you run the following snippet
#include <stdio.h>
int main() {
int c;
while((c = getchar()) != EOF)
putchar(c);
}
The while loop goes in infinite loops. There is no way to enter an EOF character (which I persume is ^Z). If i press ^Z, the execution stopes. If I hit ^C the execution breaks.
Also check out conio.h or curses.h
If either of these include files are present your compiler will support
the getch() and the getche() function -- just what you want.
Well, I got it worked. getch helped me. However, i was also having problems in getting out of the while loop. In each terminals i think the EOF character is different. CTRL-D is one working for me. My xterm is a gnome terminal. I remember using ctrl-z in dos for EOF.
Youre problem is not with GCC but with how the shell behaves.
If you want to be able to enter a character without having to press
enter afterwards, you are going to have to do some programming of
your own. Also, conio.h does not exist under linux, it is a borland made header. A few people have written or tried to write a conio-like library, but ncurses is still much much better.
If you want, here is my crafted Getchar function that does all the work for you:
Code:
char Getchar(void) {
char x;
//Set terminal to raw mode
system("stty raw");
//Wait for single character
x=getchar();
//Reset terminal to normal "cooked" mode
system("stty cooked");
//And we're out of here
return x;
}
here is a small example that uses it.
Code:
int main()
{
char x;
x=Getchar();
printf("%c",x);
return 0;
}
If you do use the function, dont forget to declare it first.
Opps, it looks like dorian33 did something simliar, or better.
With the help of the post above:
Code:
#include <termios.h>
#include <unistd.h>
#include <stdio.h>
#include <strings.h>
char charget() //I think this thread has the monopoly on 'getchar()' derivatives.
{
char c;
struct termios save_att, set_att; //learned from the post above when accessing multiable variables in a structure
tcgetattr(0, &save_att);
set_att.c_lflag &= ~(ICANON|ECHO|ICRNL);
tcsetattr(0, TCSANOW, &set_att);
c = getchar();
tcsetattr(0, TCSANOW, &save_att);
return c;
}
int main()
{
int i;
i = charget();
putchar(i);
return 0;
}
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.