'Adding system(cls); to program increases startup time
When I started writing my program (I didn't add system(cls); at that time), it started the console as soon as I doubled clicked on the exe file. But when I added the system(cls); it took 4-5 seconds to open. Why? Is there any way to fix this?
My Code (it's a simple calculator) -
#include <stdio.h> // for printf() and scanf()
#include <stdlib.h> //for exit() and system()
#include <windows.h> // for SetConsoleTitle()
#include <math.h> // for sqrt()
int calculation();
int main()
{
int afterCal;
SetConsoleTitle("My Calculator");
printf("Welcome to my Calculator\n");
calculation();
for(;;)
{
printf("\nAnother Calculation? type 1\n");
printf("Quit? type 0\n");
scanf( "%d", &afterCal);
if (afterCal == 1)
{
system("cls");
calculation();
}
else if (afterCal == 0)
{
exit(0);
}
else
{
system("cls");
printf("\nError, enter 0 or 1\n");
}
}
return 0;
}
int calculation()
{
double num1, num2;
char ope, square = 251;
printf("Enter number 1 -\n");
scanf("%lf", &num1);
printf("Enter number 2 (put 0 if you want square/cube root or cube) -\n");
scanf("%lf", &num2);
if (num2 == 0)
{
printf("Enter operation - s for square root, r for cube root, c for cube\n");
}
else
{
printf("Enter operation - +, -, *, /\n");
}
scanf(" %c", &ope);
switch (ope)
{
case '+':
system("cls");
printf("%g + %g = %g\n", num1, num2, num1 + num2);
break;
case '-':
system("cls");
printf("%g - %g = %g\n", num1, num2, num1 - num2);
break;
case '*':
system("cls");
printf("%g * %g = %g\n", num1, num2, num1 * num2);
break;
case '/':
system("cls");
printf("%g / %g = %g\n", num1, num2, num1 / num2);
break;
case 's':
system("cls");
printf("%c%g = %g\n", square, num1, sqrt(num1));
break;
case 'r':
system("cls");
printf("Cube root of %g = %g\n", num1, cbrtf(num1));
break;
case 'c':
system("cls");
printf("Cube of %g = %g\n", num1, num1 * num1 * num1);
break;
default:
system("cls");
printf("Error, invalid operation %c\n", ope);
break;
}
return 0;
}
Any help would be appreciated!
Solution 1:[1]
1st I was using the MinGW x64 compiler (at that time the startup delay occurred), but now I am using Visual Studio 2022 ide and its compiler, and my exe opens instantly! Problem Solved!
But I am still confused, why did MinGW give that much startup delay...
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Ninad Kulkarni |
