Click here to Skip to main content
15,888,984 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,
I'm new to programming and I'm learning the C language.

I use the most current DevCpp. When I click compile + run the codes, they work. However, when I open the .exe files the software only works until the data is entered. When I hit 'enter' to continue processing, they suddenly close.

I leave here one of the scripts so that, if possible, someone can help me.

Thank you!

What I have tried:

#include<stdio.h>
main()
{
int num;
printf("Digite um numero inteiro: ");
scanf("%d",&num);
if (num % 2 == 0) {
printf("Este numero e par!");
}
else {
printf("Este numero e impar!");
}
}
Posted
Updated 21-Feb-22 21:20pm

At the end of the main function the program terminates and control is back to OS.
A workaround to keep the program alive is asking the user for another input, try

C
#include<stdio.h>
int main()
{
  int num;
  printf("Digite um numero inteiro: ");
  scanf("%d",&num);
  if (num % 2 == 0)
  {
    printf("Este numero e par!\n");
  }
  else
  {
    printf("Este numero e impar!\n");
  }

  printf("program is paused. Please enter a key to exit\n");
  scanf("%d", &num);

  return 0;
}
 
Share this answer
 
Comments
Lyudmila Pavlichenko 28-Feb-22 19:42pm    
Thank You, friend!!!!!!!!!
CPallini 1-Mar-22 2:01am    
You are welcome.
Another approach using getch()

C
#include<stdio.h>
#include <conio.h> // for getch()

int main()
{
  int num;
  printf("Digite um numero inteiro: ");
  scanf("%d",&num);
  if (num % 2 == 0)
  {
    printf("Este numero e par!\n");
  }
  else
  {
    printf("Este numero e impar!\n");
  }

  printf("program is paused. Please enter a key to exit\n");
  getch();

  return 0;
}
 
Share this answer
 
Comments
Lyudmila Pavlichenko 28-Feb-22 19:42pm    
Thank You, Asif!!!!!!!!!!!!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900