1

I recently started taking a C course and I am having some difficulties running a simple I/O program:

#include <stdio.h>

int main()
{
    int age;
    printf("Please enter your age: ");
    scanf("%d\n", age);
    printf("Your age is: %d", age);
    return 0;
}

the problem is that the program does start running, but it never stops and it doesn't allow me to type in the age and doesn't even print out the "Please enter your age: " sentence. I have a gcc, g++ and a gdb installed and everything works fine if I remove the "scanf".

I would really appreciate some help! Thank you!

eitan102
  • 53
  • 6
  • Unrelated to your problem, but don't have trailing white-space characters (like `\n`) in your `scanf` format string. It will almost never work as expected. – Some programmer dude Sep 08 '21 at 10:28
  • And you probably have a *crash*, as `scanf` expects *pointers* as arguments. For `scanf` the `%d` format specifier expects an `int *` argument. Mismatching format specifier and argument type leads to *undefined behavior*. – Some programmer dude Sep 08 '21 at 10:29
  • ok, thank you for this information! but i think i have a problem with running the code in general, maybe something in the compiler, don't really know... – eitan102 Sep 08 '21 at 10:34
  • 2
    In many systems, `stdout` is line buffered. That means the output is not flushed to your terminal unless a `\n` is printed or `stdout` is flushed manually. As a result the program might be ready to take your input but you just cannot see any text asking you to do so. Try adding a `\n` at the end of your `printf`s. For a quick test you can just start typing your age without waiting for the prompt message. – Gerhardh Sep 08 '21 at 10:51
  • it doesn't work.. i thin something is wrong with the setup because i didn't setup the tasks.json file and it's very confusing – eitan102 Sep 08 '21 at 10:54
  • But you wrote, it starts running... – Gerhardh Sep 08 '21 at 11:00
  • it starts running but doesn't show anything and doesn't stop running – eitan102 Sep 08 '21 at 11:01
  • 1
    You might add the current version of your code after you adapted to comments and the existing answer. Just edit your question and add the code below. – Gerhardh Sep 08 '21 at 11:04
  • 1
    How do you build? How do you run? What commands are involved? Do you have any output at all, from e.g. Visual Studio Code, in any tab anywhere? Does a new window open when you try to run the program (but perhaps minimized or hidden behind other windows)? Please [edit] your question to include as much details as possible. – Some programmer dude Sep 08 '21 at 11:07
  • 1
    Also please try calling `fflush(stdout)` after each `printf` call. – Some programmer dude Sep 08 '21 at 11:07

2 Answers2

3

You need to add an & symbol before the variable name while scanning. Check why here.

scanf("%d\n", &age);

Muhammed Jaseem
  • 782
  • 6
  • 18
1

Working Code

#include <stdio.h>

int main()
{
    int age;
    printf("Please enter your age: ");
    fflush(stdout); // Prints to screen or whatever your standard out is
    scanf("%d", &age);
    printf("Your age is: %d\n", age);
    fflush(stdout); // Prints to screen or whatever your standard out is
    return 0;
}

I run this and it is working as expected. If you are using linux os, please follow below command to compile and run.

gcc age.c
./a.out

Please visit here to know more about scanf function in c.

vhdl
  • 58
  • 6
  • @eitan102 please visit https://stackoverflow.com/questions/12450066/flushing-buffers-in-c if output is not flushing to terminal. – vhdl Sep 08 '21 at 11:19