if we print a float using %d garbage value is printed....but if we scan a float value using %d & int...the value gets truncated...What is the reason behind it...

Dani AI

Generated

Clarification for :

%d and %f are format conversions used by the C I/O functions. They tell the runtime how to interpret the bytes of the corresponding argument. If the format string does not match the actual argument type, the result is undefined for printf (garbage, wrong values, or crashes). For variadic functions like printf, float arguments are promoted to double before being passed; for scanf, %f expects a float * while %lf expects a double *. is correct that these are conversion specifiers; is correct that "lying" to printf/scanf produces the symptoms seen.

A note on sizes and the comment from : on most modern platforms float is 4 bytes and double is 8 bytes; int is commonly 4 bytes but can vary by architecture. That size/representation difference is why passing a floating value where an int is expected (or vice versa) makes the function read the wrong bytes and produce nonsense.

Why scanf("%d",&i) returns 1 when given "3.14": scanf parses as much as matches the conversion. %d accepts an optional sign and a run of digits; the initial "3" is valid, so it is converted and stored and scanf returns 1. If the input started with a non-digit (e.g. "x"), matching fails and scanf returns 0. If an input/read error occurs before any conversion, EOF is returned. Beware: supplying a pointer whose type does not match the conversion (for example, scanf("%f", &someInt)) is undefined behavior and must be avoided.

Practical recommendations: always match format specifier and actual type; enable compiler warnings like -Wall -Wextra -Wformat; for robust user input prefer reading a line with fgets and then strtod/strtol to validate and detect leftovers. Minimal parsing pattern:

char buf[64];
if (fgets(buf, sizeof buf, stdin)) {
    char *end;
    double v = strtod(buf, &end);
    if (end != buf && (*end == '\n' || *end == '\0')) {
        printf("%f\n", v);
    }
}

Recommended Answers

All 6 Replies

float is 8 bytes and int 4 bytes if you say %f you expect 8 bytes and type float

#include <stdio.h>

int main()
{
   int iInt = 1;

   printf("int %d float %f",iInt, (float)iInt);
   return 0;
}

btw i suggest you read char buffer in and then convert it to float or int

Member Avatar for Member #957352

%d %f etc are these are part of format string. They uslually tells the compiler to represnt the value as integer if d is there, as float if f is there. thet are specifiers for the conversions.

What is the reason behind it...

If you lie to printf(), you get what you deserve. When you tell printf() to expect an int, it treats whatever you pass like an int. When you tell printf() to expect a float, it treats whatever you pass like a float. If whatever you pass doesn't have a compatible byte representation, don't be surprised when you get garbage.

then why does scanf returns 1 in following case if we give a floating point variable as input..

int main()
{
int i;
printf("%d",scanf("%d",&i));
return 0;
}

if I give a floating point value as input the output comes 1 but if I give a character as input the value returned is 0...


Return Value
On success, the function returns the number of items successfully read. This count can match the expected number of readings or fewer, even zero, if a matching failure happens.
In the case of an input failure before any data could be successfully read, EOF is returned.

then why does scanf returns 1 in following case if we give a floating point variable as input..

Because scanf() reads until the first invalid character, and if there were valid characters before that such that a conversion can be performed the the conversion will succeed. The first part of a floating-point value up to the radix is a valid integer.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.