0

So I have this code running on sublime text

#include <stdio.h>

int main()
{
    int c;

    printf("Type a letter: \n");
    c = getchar();
    printf("You typed '%c'. \n",c);

    return(0); 
}

When I try run this however, it just doesn't show anything at the bottom. Nothing seems to be wrong when I do a simply HelloWorld code but when using the getchar function, I always get a blank output as shown in this screenshot: enter image description here

Any idea?

David C. Rankin
  • 81,885
  • 6
  • 58
  • 85
bigfocalchord
  • 553
  • 1
  • 7
  • 14
  • 2
    Possible duplicate of [Sublime Text 2 console input](https://stackoverflow.com/questions/10604409/sublime-text-2-console-input) – Keith Hall Jul 10 '17 at 05:54
  • Have you tried [flushing](http://en.cppreference.com/w/c/io/fflush) the output? – Some programmer dude Jul 10 '17 at 05:56
  • @KeithHall That works for Python, but I use C? – bigfocalchord Jul 10 '17 at 06:01
  • 2
    Provided screenshot does not show how your program was compiled, as well as how you run and input data - I suppose problem not in the source code (it is extremely simple) – VolAnd Jul 10 '17 at 06:06
  • @VolAnd Do you use sublime text for input and output? – bigfocalchord Jul 10 '17 at 06:10
  • I do not use sublime text, but I suppose it is just editor with additional features, so I suggest you to compile program by `gcc` (do you use Linux or something like that?) in the command line, e.g.: `gcc CharIO.c -o hello` and then run as `.\hello` ... if it is problem of sublime text you will see the difference – VolAnd Jul 10 '17 at 06:18
  • Any particular reason you want to use sublime? There is always [**Geany**](https://github.com/geany/geany/) or [**CodeBlocks**](http://www.codeblocks.org/downloads/26) both good *open-source* editor/IDE's – David C. Rankin Jul 10 '17 at 06:23
  • @DavidC.Rankin Seemed nice and aesthetic :D – bigfocalchord Jul 10 '17 at 06:26
  • Another alternative would be [eclipse](https://www.eclipse.org/cdt/). – Aconcagua Jul 10 '17 at 06:27
  • @dydxx Yes, but it comes with licensing requirements, etc.. Give either one of those others a try. Both a solid code editors. Geany is a bit lighter, CodeBlocks a bit more configurable and both run on just about anything. – David C. Rankin Jul 10 '17 at 06:28
  • 2
    A simple question: Have you pressed 'enter' after doing some input? – Aconcagua Jul 10 '17 at 06:28
  • The reason I asked: getchar() does not take input before you press enter, similar as gets or fgets (with stdin) do. If this is not your intention, you might prefer getch() if on windows (header: conio.h); on linux, a little more difficult, see e. g. [here](https://stackoverflow.com/questions/7469139/what-is-equivalent-to-getch-getche-in-linux). – Aconcagua Jul 10 '17 at 06:43
  • Again, have you tried *flushing* the output? What happens if you call [`fflush`](http://en.cppreference.com/w/c/io/fflush) on `stdout` after the first `printf` call? – Some programmer dude Jul 10 '17 at 07:34

1 Answers1

0

c is an integer not a character.

Use -

1) char c instead of int c.

or

2)Change %c to %d in printf statement.

yash darak
  • 368
  • 4
  • 17