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!

10
  • 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. Commented Sep 8, 2021 at 10:28
  • 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 printfs. For a quick test you can just start typing your age without waiting for the prompt message. Commented Sep 8, 2021 at 10:51
  • 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. Commented Sep 8, 2021 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. Commented Sep 8, 2021 at 11:07
  • 1
    Also please try calling fflush(stdout) after each printf call. Commented Sep 8, 2021 at 11:07

2 Answers 2

3

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

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

Sign up to request clarification or add additional context in comments.

1 Comment

try removing \n
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.

1 Comment

@eitan102 please visit stackoverflow.com/questions/12450066/flushing-buffers-in-c if output is not flushing to terminal.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.