0

Okay this is actually a very simple code but since I am only starting to learn C, please be patient and help me out. I'll be putting my Questions as comments beside the code so that it easy to relate to which part of the code I have a doubt.

#include <stdio.h>
main()
{
 int first_no, second_no;
 float dec_no, output_no;
 first_no = 75;
 second_no = first_no/2;
 dec_no = 35.3;
 output_no = dec_no/3;
 printf("First No:%d\n", first_no);
 printf("Second No:%d\n", second_no);
 printf("Third No:%d\n",output_no); 
 /*here I wanted to print only the integer part of the output_no */
}

The problem with this is that I had a book and it displayed the value for third no as 0.

And then in another program it says that compile time error is shown.

Second program:

#include <stdio.h>
void main()
{
 int x = 5.3%2;
 printf("Value of x is %d", x);
}

For this program, the book says that a compile time error will be shown. I fail to understand why that is the case. According to me the output should be 1.


If I were to use the following code instead of the previous code:

#include <stdio.h>
main()
{
 int first_no, second_no;
 float dec_no, output_no;
 first_no = 75;
 second_no = first_no/2;
 dec_no = 35.3;
 output_no = dec_no/3;
 printf("First No:%d\n", first_no);
 printf("Second No:%d\n", second_no);
 printf("Third No:%d\n",dec_no); 
}

What output should I expect? Do I still get a zero or some unpredictable output?

S_Learner
  • 13
  • 6
  • "According to me the output should be 1" - compilers operate according to the C Standard, not you – M.M Dec 16 '15 at 00:18
  • if you were to actually compile any of those programs, with a modern compiler, you would see a number of errors/warnings output by the compiler. Note: when compiling, always enable all the warnings. (for gcc, at a minimum use: `-Wall -Wextra -pedantic` ) Fix the warnings before even trying to actually run the programs. – user3629249 Dec 16 '15 at 20:15

2 Answers2

2

The problems with using just

printf("Third No:%d\n",output_no);

is that:

  1. output_no gets converted to a double before being passed to printf.
  2. When printf sees %d as the format specifier, it expects an int. When the object being passed is of type double, the behavior is undefined.

When you want to print a truncated integral value of a floating point number, you can do one of the following.

  1. Create a temporary variable of the integral type and assign to it the floating point number.

     int temp = output_no;
     printf("Third No:%d\n", temp);
    
  2. Explicitly cast the floating point number to an integral type.

    printf("Third No:%d\n", (int)output_no);
    
R Sahu
  • 204,454
  • 14
  • 159
  • 270
  • Why is output_no being converted to a double? – S_Learner Dec 15 '15 at 19:13
  • For any function that has variable number of arguments, `float`s are converted to `double`. There are historical reasons for it but it is now specified in the standard. – R Sahu Dec 15 '15 at 19:14
  • Okay but how does the output in this case become zero? – S_Learner Dec 15 '15 at 19:22
  • 1
    Your program is subject to undefined behavior. It is pointless to explain a certain behavior in situations like that. – R Sahu Dec 15 '15 at 19:24
  • 1
    Okay, so you mean to say that the output can be anything not necessarily zero, as the output is unpredictable? – S_Learner Dec 15 '15 at 19:25
  • And if I were to change the code to : printf("Third No: %d\n", dec_no); What kind of output would be displayed then? – S_Learner Dec 15 '15 at 19:28
0

printf("Third No:%d\n",dec_no);

What output should I expect? Do I still get a zero or some unpredictable output? As of the printf function is concerned,

When you try to print an integer value with format specifiers that are used for float (or) double and vice the versa the behaviour is unpredictable. But it is possible to use %c to print the character equivalent of the integer value. Also using of %d to print ASCII value (integer representations) of character is acceptable.

Second program: For this program, the book says that a compile time error will be shown.

According to C Reference manual

7.3.3 expression % expression

The binary % operator yields the remainder from the division of the first expression by the second. Both operands must be int or char, and the result is int. In the current implementation, the remainder has the same sign as the dividend.

Here in your case you are providing one value 5.3 so it is neither char nor int so that is why it generates compilation error.

If you still want to run that program you can do that by using fmod() function.

Try this code :

#include<stdio.h>
#include<math.h>
void main()
{
        float x=5.3;
        int c =2;
        printf("Value of xremainer is %lf",fmod(x,c));
}

Compile it as :

$gcc test.c -lm
Punit Vara
  • 3,744
  • 1
  • 16
  • 30