I have just installed visual studio code & after watching some basics, I have written a simple C code to calculate area of rectangle. But, when I run the code its taking too long to execute. My system configuration are 4gb RAM, i3 - 5th gen (poor boi) Here is the code:
#include <stdio.h>
int main()
{
int l;
int b;
printf("enter the length of rectangle in an integer value");
scanf("%d ", &l);
printf("enter the breadth of rectangle in an integer value");
scanf("%d ", &b);
printf("the area of rectangle is %d", l * b);
return 0;
}
CodePudding user response:
Change scanf("%d ", &l);
to scanf("%d", &l);
and scanf("%d ", &b);
to scanf("%d", &b);
.
A space in a format string tells scanf
to read from input until a non-white-space character is read. So, after scanf("%d ", &l);
, the program continues reading input until you type something such as the next number. If you do type that next number, then scanf("%d ", &b);
reads it and then continues reading until it sees another character that is not a space. As long as you do not type anything, or type only spaces and returns/enters and other keys that generate white space characters, the program continues waiting.
Removing the spaces eliminates this.
The program is not “taking too long to execute.” In fact, the program is waiting for user input, so it is just waiting, not executing. Because it did not complete, you concluded it was taking too long to execute. Instead of reporting a conclusion, you should have reported the observed behavior: After typing input, there was no visible activity by the program.
Skipping spaces before performing a conversion is built into most scanf
conversions, such as %d
. Only include a space character in a format string where you want to skip spaces that would not be skipped normally, such as before a %c
conversion, which does not have the automatic skipping built in.
Also, in printf("the area of rectangle is %d", l * b);
, add a new-line character at the end, printf("the area of rectangle is %d\n", l * b);
. (This ensures the prompt printed by a command-line shell after the program finishes executing is on a new-line, so it will not be confused with the program output. C is designed to end lines of output with new-line characters, so you should make it a regular practice.)
CodePudding user response:
Alright guys, I found the solution. The first thing I want to apologize is that the code is not taking too long to execute. The code is actually executing but there was no visible activity. Everything in the code is just perfect. The mistake I made is I didn't change one setting in VS Code. I use code runner extension to run my codes [that's the most popular]. I have to go in File --> Preferences --> Settings --> Type 'code runner' in the search bar --> check the setting given in this image - https://drive.google.com/file/d/1QwB2NjKpYX7M0b5w55sqy4oMVAOk_IK2/view?usp=sharing
Lot of people were trying to figure out a mistake in the code, but there is nothing wrong with it. Code runner was not able to run in the terminal, so there was no activity to take any user input - due to read only text editor. After changing that setting I was able to give user input directly in the terminal.
Credit for the solution - https://www.youtube.com/watch?v=Si8rN5J249M
EDIT: There is a mistake in my code also lol, which is mentioned in another answer - Really appreciate it Eric Postpischil