-2

i write code in c and i want to terminate if statement i use.

exit();

function it works but it will create new problem.

getch();

function stop working in my code and the output wont stay on screen it just flash and disappeared. here is my code

#include<stdio.h>
#include<cono.h>
#include<iostream.h>
#include<stdlib.h>
void main(void)
{
 int a,b;
 printf("enter a");
 scanf("%d",&a);
 printf("enter b");
 scanf("%d",&b);
 if(a==1,b<=8)
 {
  printf("you");
  exit(0);
  }
 if(a==2,5<b<=10)
 {
  printf("you");
 else
 printf("me");
 }
 getch();
 } 
StoryTeller - Unslander Monica
  • 165,132
  • 21
  • 377
  • 458
shoaib sabir
  • 605
  • 2
  • 9
  • 23
  • 2
    That's because the [`exit`](http://en.cppreference.com/w/c/program/exit) function terminates the process immediately. It doesn't return. – Some programmer dude Jun 25 '17 at 09:57
  • 1
    `exit()` terminates the running process. How do you expect `getch` being able to do something, if the very process that would call it gets terminated long before. Just use structured programming or (god forbid) a goto. – datenwolf Jun 25 '17 at 09:58
  • 2
    Also, your conditions are wrong. With `a==1,b<=8` do you mean `a==1 && b<=8`? Perhaps you should [find a good beginners book](http://stackoverflow.com/questions/562303/the-definitive-c-book-guide-and-list) and start all over. – Some programmer dude Jun 25 '17 at 09:59
  • 3
    `if(a==1,b<=8)` and `if(a==2,5 – A.S.H Jun 25 '17 at 09:59
  • 1
    place a `getch()` before `exit()`. – GAURANG VYAS Jun 25 '17 at 09:59
  • 3
    **How do these questions even receive upvotes!** – Am_I_Helpful Jun 25 '17 at 09:59
  • i am beginner in the case i write if(a==1,b<=8) in my code i think the control first check the value of a and then b and then run if condition but i think i did not know the accurate syntax to write that can anyone give me correct syntax – shoaib sabir Jun 26 '17 at 04:38

1 Answers1

1

I think you misunderstood the concept of the function exit(int). The exit function terminates the current calling process and returns control to the Operating System, which in your case means that it terminates the execution of main.

Bremsstrahlung
  • 686
  • 1
  • 9
  • 23
  • 3
    **"exit() can be used to stop a loop, but it cannot be used to stop an if condition, since it does not constitute a calling process unit"**. - What do you mean by this ? – GAURANG VYAS Jun 25 '17 at 10:13
  • Reading the beginning of the question: "i write code in c and i want to terminate if statement i use." it seemed to me that what he intended to do with the `exit()` command was to stop running the rest of commands inside the scope of the if statement. – Bremsstrahlung Jun 26 '17 at 12:38
  • 1
    I think you are confused between `break` and `exit`. Is it ? – GAURANG VYAS Jun 26 '17 at 12:45
  • You are right, the example I used would be true for `break`, not `exit`. I will edit it. – Bremsstrahlung Jun 26 '17 at 15:06