I'm very new to c so..
scanf ("%d", &num1 ) ;
printf ( "The square of %d is %d \n", num1, num1*num1) ;
getchar();
This code is not printing the answer until the next time i run it (I'm using turboC++ ) i.e after it asks for a value it doesnt do anything even though I type a value. And if I press enter it just closes. But I can see the printed answer next time if i dont clear the screen.
But this code..
scanf ("%d", &num1 ) ;
getchar();
printf ( "The square of %d is %d \n", num1, num1*num1) ;
getchar();
works!
And I don't get why. In the first one the getchar is after the printf so it should print before closing. If we assume it happens really fast, why does the compiler take the scanf value and then print it AFTER i press enter? Doesn't that make the sequence wrong?
And in the second one it takes the scanf value after enter too. But it just prints the value after this enter (and quits usually after another enter).Why?
Also why isn't the printf executed after the scanf immediately in the first code? Should I do somethinf after entering the value?
This is the complete code for the second one
#include <stdio.h>
#include <conio.h>
int main ()
{ clrscr();
int num1 ;
printf ( "The Integer Squarer \n Type the Integer you would like to square \n");
scanf ( "%d",&num1 );
getchar();
printf ( "The squrae of %d is %d \n", num1, num1*num1);
getchar();
return 0;
}