Use number to control the if statement or the ? operator. - C Operator

C examples for Operator:Conditional Operator

Introduction

The expression must simply evaluate to either a true or false (zero or nonzero) value.

The following program reads two integers and displays the quotient.

It uses an if statement, controlled by the second number, to avoid a divide-by-zero error.

Demo Code

#include <stdio.h>

int main(void)
{
   int a, b;/*w ww  .j  av  a 2s.c o  m*/

   printf("Enter two numbers: ");
   scanf("%d%d", &a, &b);

   if (b)
      printf("%d\n", a / b);
   else
      printf("Cannot divide by zero.\n");

   return 0;
}

Result


Related Tutorials