-1

I'm a University student and I'm trying to verify different printf output through the following code:

#include <stdio.h>
int main()
{
    int i=3,j=0;
    float x=5,y;
    printf("1: %d\n", i);  
    printf("2: %d\n", i/j);
    printf("3: %d\n", i*i);
    printf("4: i = \n");   
    i=i + j;               
    printf("6: %f\n", x/y);
    printf("7: x = %f\n"); 
    printf("8: %d\n", i/2);
    printf("9: %f", x/2) ; 
    return 0;
}

In the execution phase, after the first output printf("1: %d\n", i);, I don't get anything in the terminal. Any idea? (no compiling error)

bersi
  • 75
  • 1
  • 8

2 Answers2

1

i / j is 3 / 0, and integer division by 0 in C is Undefined Behavior, which means that literally anything can happen. In your case, "literally anything" meant "go back in time and make the previous printf not work, then crash" (this actually happened due to how buffering works, but that's just an implementation detail).

  • As a minor nit, its not integer division by zero that's undefined behaviour, but having the right operand of `/` be 0 (anywhere, for any purpose) that's UB. – kopecs Dec 27 '19 at 18:51
  • @kopecs Isn't `float f = 1.5 / 0` well-defined as infinity? – Joseph Sible-Reinstate Monica Dec 27 '19 at 18:54
  • From C99 (the 2007 working copy at least) 6.5.5.5: "The result of the `/` operator is the quotient from the division of the first operand by the second; the result of the `%` operator is the remainder. In both operations, if the value of the second operand is zero, the behavior is undefined." (I'll admit, I'm a bit surprised it isn't defined for floating point numbers, but I don't think the standard necessitates IEEE 754 or equivalent). – kopecs Dec 27 '19 at 18:59
  • @kopecs In "pure" C99 you'd be right, but Annex F overrides that: [The behaviour of floating point division by zero](https://stackoverflow.com/q/42926763/7509065) – Joseph Sible-Reinstate Monica Dec 27 '19 at 19:12
  • and it seems that may be why the standard appeared to conflict with my common sense. Thanks! – kopecs Dec 28 '19 at 01:27
1

What did you expect to get for the line printf("2: %d\n", i/j); when i = 3 and j = 0?

Do you see a reason why your program might interrupt at that point?

Followup questions:

What output do you expect from these lines?
Explain why for each one!

printf("6: %f\n", x/y);
printf("7: x = %f\n"); 
printf("9: %f", x/2) ; 

Since you provided your answers, You're wrong on the first two expectations.

printf("6: %f\n", x/y); <-- "y" as you said is not declared, so I suppose to have a random float number.

y is declared, but it is not initialized.
Division by an uninitialized value, in practice will produce an unknown value.
But by the spec, it is Undefined Behavior, and anything can happen.


printf("7: x = %f\n"); <-- this is one other question, because the terminal gives me a random value either.

Because you did not provide a parameter to go into the %f slot, it is undefined behavior, and you might get Nasal Demons.

Community
  • 1
  • 1
abelenky
  • 63,815
  • 23
  • 109
  • 159
  • printf("6: %f\n", x/y); <-- "y" as you said is not declared, so I suppose to have a random float number. printf("7: x = %f\n"); <-- this is one other question, because the terminal gives me a random value either. printf("9: %f", x/2) ; <-- 2.500000 – bersi Dec 27 '19 at 18:59