Home > Software design >  When I include scanf() in my C code it doesn't work
When I include scanf() in my C code it doesn't work

Time:11-15

Here's the code:

#include <stdio.h>
#include <stdlib.h>

int main()
{
    int age;

    printf("Enter your age: ");
    scanf("%d",&age);
    printf("You are %d years old!",age);

    return 0;
}

When I run the code I get this in the output:

[Running] cd "c:\Users\kathh\Documents\Code\C\" && gcc test.c -o test && "c:\Users\kathh\Documents\Code\C\"test

And when I force stop it I get this in the output:

[Done] exited with code=1 in 6.412 seconds

What should I do?

CodePudding user response:

Try adding fflush(stdout); immediately after each line that calls printf.

CodePudding user response:

Exit Code 1 indicates that the program exited unsuccessful. If you don't even get printf to output "Enter your age" then you're somehow not running the program properly, use and IDE if you're a beginner. Also note you should get userinput with gets() function and then convert to desired datatype.

  • Related