0

I am using Code::Blocks for programming and yeah i am a beginner but everytime i write a program it pauses in IDE but does not pause while executing directly.

What is the possible reason ? Can anyone explain me ?

My code goes as follows :

#include <stdio.h>
void main()
{
    float length,breadth,Area;
    printf("Enter the value of length and breadth \n\n");
    scanf("%f %f",&length,&breadth);
    printf("You entered length=%f and breadth=%f \n\n",length,breadth);
    Area= length * breadth;
    printf("The area of the rectangle is %f\n\n",Area);
    return 0;
}
11
  • 8
    It's a console application. It's meant to be run from the console. And main must return int.
    – chris
    Jul 16, 2014 at 12:08
  • Please use the appropriate formatting next time. Instead of typing <br> after each line of code (which still doesn't format it as code), you can just highlight the code and click the "Code" button. Jul 16, 2014 at 12:09
  • That's how console applications are supposed to work! If I typed ls and it listed the files in the current directory and then waited for input, I'd be pretty annoyed. Jul 16, 2014 at 12:10
  • It's the IDE that pauses once your program have finished. It has nothing to do with your program. Jul 16, 2014 at 12:10
  • 1
    @mistgeek, Open up a terminal and type ./<program name>. Voila, no closing at the end.
    – chris
    Jul 16, 2014 at 12:17

4 Answers 4

6

You have to tell your program to wait for input at the end otherwise it will execute, do exactly what you wrote in your code and exit. The "good" way would be to execute it from a terminal (cmd if you are on windows)

#include <stdio.h>
int main()
{
    float length,breadth,Area;
    printf("Enter the value of length and breadth \n\n");
    scanf("%f %f",&length,&breadth);
    getchar(); // catch the \n from stdin
    printf("You entered length=%f and breadth=%f \n\n",length,breadth);
    Area= length * breadth;
    printf("The area of the rectangle is %f\n\n",Area);
    getchar(); // wait for a key
    return 0;
}

Why do you need a getchar() after your scanf()?

When you enter your numbers you finish it with a press of enter. Let's see what you are reading: a float whitespaces and another float. The \n is not consumed by scanf(), but left in the input buffer (stdin). The next time you use a function that reads from stdin, the first sign this function sees is a \n (Enter). To remove this \n from the input buffer you have to call getchar() after scanf() which reads 1 character from the input buffer. I'm sure you will encounter this behaviour more often in future.

7
  • still not solved :( the console exits immidiately ?? Jul 16, 2014 at 12:14
  • 2
    this is probably caused by your scanf(). scanf() most likely leaves the \n (Enter) in the input buffer which is then catched by the last getchar. I made an edit to my post. try it this way.
    – robin.koch
    Jul 16, 2014 at 12:17
  • yeah you helped me Thank You but can you please explain why you used getchar() there in the middle and the last ?? and will this not cause any problem in execution on versions of windows ?? Jul 16, 2014 at 12:22
  • @mistgeek thats mentioned in the comments of his code
    – Spikatrix
    Jul 16, 2014 at 12:26
  • yeah you did but exactly why is it necessary and why does it make a difference ?? Jul 16, 2014 at 12:32
3

As a really, REALLY bad practise, you can just use a getch call to stop the excecution (or any function that generates a small pause, getch is not a standard function).

4
  • yeah thanks ill keep that in mind :) but please help me resolve this issue ?? Jul 16, 2014 at 12:16
  • 2
    @mistgeek ,you can use a getch(); at the end of your program before the return
    – Spikatrix
    Jul 16, 2014 at 12:21
  • Alternatively, you can run the program from the standard command line. It should run, and then return to the prompt. Jul 16, 2014 at 12:28
  • uh thanks for your help but someone helped me but thank you man :) Jul 16, 2014 at 12:29
3

A program is not supposed to pause after execution and this is a feature added by the IDE. If you want execution to pause and wait for some input you should instruct it to do so. For instance if you are on windows you can add a line:

system("pause");

Right before return 0;. This is not advisable, but may help you for debugging in some cases. Also the standard requires that your main function is int, not void. So you better get used to writing int main instead of void main.

5
  • thanks you helped me :) but i have one question ?? does system("PAUSE") works on all Windows OS if i execute ?? Jul 16, 2014 at 12:19
  • @mistgeek ,yes it does
    – Spikatrix
    Jul 16, 2014 at 12:21
  • @mistgeek yes it works. However this will not work on a linux system Jul 16, 2014 at 12:25
  • yeah thanks man p.s i dont have any knowledge about linux systems :) Jul 16, 2014 at 12:26
  • 1
    It does not reliably work on Windows. The user could easily have replaced the pause utility or have another pause program that is found first.
    – chris
    Jul 16, 2014 at 12:27
-2

You need to use getch(); at the end of program (Before return statement) Getch holds down the output screen.

1
  • 1
    -1 This has already been posted as an answer by NightwalkerUY.
    – Spikatrix
    Jul 16, 2014 at 15:09

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.