Why is the result of -5/2 being printed as 0? [duplicate] - c

This question already has answers here:
What happens when I use the wrong format specifier?
(2 answers)
Closed 1 year ago.
Can you tell why this statement in C gives 0 as output
printf("Hello %f",-5/2);
Whereas
printf("Hello %d",-5/2);
is giving output as -2

Division of two integers produces a integer result (int in this case). The %f format specifier expects an argument of type double. Using the wrong format specifier for a given argument triggers undefined behavior which in this case gives you an incorrect result.
If you want a floating point result, at least one of the operands to / must have a floating point type, i.e.
printf("Hello %f",-5.0/2);

Related

About the division of floating point numbers in C language。 [duplicate]

This question already has answers here:
What happens when I use the wrong format specifier?
(2 answers)
Closed 5 months ago.
I am a university student. When learning C language, I encountered the problem of floating point division. Can anyone help me to see why the results are different every time I run it. thank you very much.( I am running in a Linux)
My English is not good, hope you can understand the meaning of my question。
#include <stdio.h>
int main()
{
double a = 1;
double b = 2;
printf("%d\n",b/a);
return 0;
}
You're using the wrong format specifier.
The %d former specifier for printf expects an int as an argument, but you're passing in a double. Using the wrong format specifier triggers undefined behavior.
You should instead use %f which is for printing a double.
printf("%f\n",b/a);

what is the “%1$d” output meaning? [duplicate]

This question already has answers here:
How do positional arguments like "1$" work with printf()?
(4 answers)
Closed 3 years ago.
im using ubuntu to run this code. but what does the code mean?
running ubuntu code:
gcc name.c -o name
/name
#include <stdio.h>
int main(void)
{
printf("%d %1$d %2$d",5);
}
the output is "5 5 random number ". So, what meaning of %2$d?
The 1$ and 2$ specifies the position of the argument. It's only specified by POSIX, it is not in C standard.
According to the C standard the behavior of printf here is undefined anyway, as the $ is not a valid printf conversion specification.
According to fprintf posix:
[...] In format strings containing the "%n$" form of a conversion
specification, a field width or precision may be indicated by the
sequence "*m$", where m is a decimal integer in the range
[1,{NL_ARGMAX}] giving the position in the argument list (after the
format argument) of an integer argument containing the field width or
precision, for example:
printf("%1$d:%2$.*3$d:%4$.*3$d\n", hour, min, precision, sec);
So:
printf("%d %1$d %2$d",5);
%d - specifies that the next int argument should be converted to a signed decimal.
%1$d - specifies that the first after format string int argument should be converted to a signed decimal
%2$d - specifies that the second after format string int argument should be converted to a signed decimal
You gave only two arguments to the printf function (the format string and 5). The result is undefined, because 2$ expects another argument.

Why does converting a certain short to double equal a 4004 digit number? [duplicate]

This question already has answers here:
What happens when casting floating point types to unsigned integer types when the value would overflow?
(2 answers)
Closed 5 years ago.
When I run the following program a 4004 digit number is printed:
double d;
short s;
d = 23234564568788.5;
d = s = d;
printf("%lf\n", d);
Why?
This is UB. On my machine, for example, the result is -32768.0.
My gcc outputs this warning: warning: conversion to 'short int' from 'double' may alter its value [-Wfloat-conversion] meaning you shouldn't do this as it is not the way C should be written (if one expects code to run properly).

Printing floating point using integer type specifier [duplicate]

This question already has answers here:
How is conversion of float/double to int handled in printf?
(7 answers)
Closed 7 years ago.
I have the below C code.
int main (void)
{
printf("%d\n",5/(float)4.7);
printf("Size of int = %d",sizeof(int));
return(1);
}
But the result is 1610612736
I was expecting the result to be 1 since we are tying to print an integer. Why this is the case? How float value is getting converted to a large value in printf statement?
I am using codeblock + mingw
as you're trying to print an integer you have to cast the result to int
printf("%d\n",(int)(5/(float)4.7));
otherwise 5/(float)4.7 is a float
You see 1610612736 because the float value is "interpreted" as and int leading to and odd result.

Why the division of two integers return 0.00? [duplicate]

This question already has answers here:
printf("%f", aa) when aa is of type int [duplicate]
(2 answers)
Closed 7 years ago.
Every time I run this program I get different and weird results. Why is that?
#include <stdio.h>
int main(void) {
int a = 5, b = 2;
printf("%.2f", a/b);
return 0;
}
Live Demo
printf("%.2f", a/b);
The output of the division is again of type int and not float.
You are using wrong format specifier which will lead to undefined behavior.
You need to have variables of type float to perform the operation you are doing.
The right format specifier to print out int is %d
In your code, a and b are of type int, so the division is essecntially an integer division, the result being an int.
You cannot use a wrong format specifier anytime. %f requires the corresponding argument to be of type double. You need to use %d for int type.
FWIW, using wrong format specifier invokes undefined behaviour.
From C11 standard, chapter §7.21.6.1, fprintf()
If any argument is not the correct type for the corresponding conversion specification, the behavior is undefined.
If you want a floating point division, you need to do so explicitly by either
promoting one of the variable before the division to enforce floating point division, result of which will be of floating point type.
printf("%.2f", (float)a/b);
use float type for a and b.
You need to change the type as float or double.
Something like this:
printf("%.2f", (float)a/b);
IDEONE DEMO
%f format specifier is for float. Using the wrong format specifier will lead you to undefined behavior. The division of int by an int will give you an int.
Use this instead of your printf()
printf("%.2lf",(double)a/b);

Resources