0

screenshot

I'm fairly new to all of this, so if it is possible could help be really dumbed down for me thank you :).

So I've had about 1 semester of coding at Uni, and we were learning C. Wanting to practice during my holiday I've done some research and opted to download Atom, installed the Gpp-compiler packages, MinGw and created a path, installed git bash etc. according to all the online instructions I could find.

However when I finally thought I was done with all the set up, a problem occured. As I was testing things out, I realised I couldn't run things properly like I was doing on the uni terminal and gedit.

Shown in my screenshot, shouldn't "hello world" be printed first after I try to run it, then I could enter a value and it should print the value or something.

However after running it would come up blank, until I enter something random and then it would print all at once.

Not sure what's happening here or what I did wrong, any guidance would be great thank you.

Edit: this only seems to be a problem, when I use scanf. Without it, everything prints out in order.

My code:

#include <stdio.h>

int main (void) {

    int value;

    printf("hello world\n");
    scanf("%d", &value);
    printf("%d", value);
    return 0;
}

when I try to call, it comes out blank until I input 45, then it prints out "hello world" "45"

FNATIC P1@PC MINGW64 ~/OneDrive/Documents/CPP
$ gcc -o main main.c

FNATIC P1@PC MINGW64 ~/OneDrive/Documents/CPP
$ ./main
45
hello world
45

Another example:

#include <stdio.h>
#include <math.h>

int main(void) {
    double sideA, sideB, sideC;
    double s, area;

    printf("Please enter 3 sides of your triangle: \n");
    scanf("%lf %lf %lf", &sideA, &sideB, &sideC);

    s = (sideA + sideB + sideC) / 2;
    area = sqrt(s * (s - sideA) * (s - sideB) * (s - sideC));

    printf("%lf\n", area);
    return 0;
}

Output: nothing came out until I entered (2 3 4) and (5.31 4.2 7.77)

FNATIC P1@PC MINGW64 ~/OneDrive/Documents/CPP
$ ./heron
2 3 4
Please enter 3 sides of your triangle:
2.904738

FNATIC P1@PC MINGW64 ~/OneDrive/Documents/CPP
$ ./heron
5.31 4.2 7.77
Please enter 3 sides of your triangle:
10.542172

JXTN
  • 11
  • 1
  • 1
    Welcome to stackoverflow, you should post your code in text, not images. – anastaciu Feb 06 '20 at 10:34
  • Possibly without a newline the output doesn't get flushed to screen; try `"hello world\n". – stijn Feb 06 '20 at 11:33
  • @stijn Thanks for replying :), I tried and unfortunately it didn't work. after running ./main it sill came up blank (instead of printing "hello world " first) until I entered a value. – JXTN Feb 06 '20 at 11:40
  • 1
    you're right. However, your question is actually a duplicate of this: https://stackoverflow.com/questions/1716296/why-does-printf-not-flush-after-the-call-unless-a-newline-is-in-the-format-strin and the solutions presented there work for me in a similar environment. – stijn Feb 06 '20 at 11:57
  • Welcome to SO. Please include the code in your question. With code I mean plain text formatted as code, not a picture of that code. For your output the same applies.- Please just add it as text. – Gerhardh Feb 06 '20 at 11:57
  • Concerning compilation, never compile without warnings. For GCC, just add `-Wall` to the flags on the commandline. Check the manpage for this and further options as well. – Ulrich Eckhardt Feb 06 '20 at 13:29

0 Answers0