So I've been looking into c programming and bought a book. There's a problem at the end of a chapter that reads:
write a program that defines five integer variables and initializes them to 1, 10, 100, 1000, 10000. It then prints them on a single line separated by space characters using the decimal conversion code (%d), and on the nest line with the float conversion code (%f). Note the differences between the results. How do you explain to them?
Now, my question is how do you use the float conversion code for a number that is defined as an int?
This is what I have written so far:
#include <stdio.h>
int main(void)
{
int one = 1;
int ten = 10;
int hundred = 100;
int thousand = 1000;
int tenThous = 100000;
printf("%d %d %d %d %d \n", one, ten, hundred, thousand, tenThous);
printf("%f %f %f %f %f \n", one, ten, hundred, thousand, tenThous);
return 0;
}
In the way they describe the problem, I think I have to use the int variables that I had defined. It would be easy to make a new set of numbers and declare them as floats, but I don't think this is exactly what they're asking.
The error message that I get when compiling is:
numPrint.c:12:33: warning: format specifies type 'double' but the argument has
type 'int' [-Wformat]
printf("%f %f %f %f %f \n", one, ten, hundred, thousand, tenThous);
~~ ^~~
%d
and it goes through all the int variables, saying that the format specifies for a 'double' and the variable used is 'int'
Is there a way to do this?
Is there a way to do this?
You have done it. The messages are warnings; they will not have aborted compilation.
Or maybe you're asking whether you can do it without eliciting warnings? Sure you can. The message tells you what warning flag that particular diagnostic is associated with: -Wformat. You can disable those warnings by passing -Wno-format as a command-line option.
But you shouldn't.
The warnings are telling you about a bona fide problem in your code. Just as it explains, the %f descriptor expects the corresponding argument to have type double. Alternatively, float will work, too, since it is automatically converted to double in this particular context, but if the argument has a different type then the behavior is undefined. You could cast the arguments to type double, but that's not what I interpret the text to be asking you to do.
What should you do instead? Burn the book and get a better one. Really. There is no justification for a textbook on C to suggest doing what yours seems to have suggested. Even if it's just poorly worded, you have to wonder what other stinkers are waiting for you there. Burning the book instead of returning it or giving it away constitutes a community service.
You can cast the printf() arguments to floats like this:
printf("%f %f %f %f %f \n", (float)one, (float)ten, (float)hundred, (float)thousand, (float)tenThous);
Related
void main()
{
clrscr();
float f = 3.3;
/* In printf() I intentionaly put %d format specifier to see
what type of output I may get */
printf("value of variable a is: %d", f);
getch();
}
In effect, %d tells printf to look in a certain place for an integer argument. But you passed a float argument, which is put in a different place. The C standard does not specify what happens when you do this. In this case, it may be there was a zero in the place printf looked for an integer argument, so it printed “0”. In other circumstances, something different may happen.
Using an invalid format specifier to printf invokes undefined behavior. This is specified in section 7.21.6.1p9 of the C standard:
If a conversion specification is invalid, the behavior is
undefined.282) If any argument is not the correct type for the
corresponding conversion specification, the behavior is undefined.
What this means is that you can't reliably predict what the output of the program will be. For example, the same code on my system prints -1554224520 as the value.
As to what's most likely happening, the %d format specifier is looking for an int as a parameter. Assuming that an int is passed on the stack and that an int is 4 bytes long, the printf function looks at the next 4 bytes on the stack for the value given. Many implementations don't pass floating point values on the stack but in registers instead, so it instead sees whatever garbage values happen to be there. Even if a float is passed on the stack, a float and an int have very different representations, so printing the bytes of a float as an int will most likely not give you the same value.
Let's look at a different example for a moment. Suppose I write
#include <string.h>
char buf[10];
float f = 3.3;
memset(buf, 'x', f);
The third argument to memset is supposed to be an integer (actually a value of type size_t) telling memset how many characters of buf to set to 'x'. But I passed a float value instead. What happens? Well, the compiler knows that the third argument is supposed to be an integer, so it automatically performs the appropriate conversion, and the code ends up setting the first three (three point zero) characters of buf to 'x'.
(Significantly, the way the compiler knew that the third argument of memset was supposed to be an integer was based on the prototype function declaration for memset which is part of the header <string.h>.)
Now, when you called
printf("value of variable f is: %d", f);
you might think the same thing happens. You passed a float, but %d expects an int, so an automatic conversion will happen, right?
Wrong. Let me say that again: Wrong.
The perhaps surprising fact is, printf is different. printf is special. The compiler can't necessarily know what the right types of the arguments passed to printf are supposed to be, because it depends on the details of the %-specifiers buried in the format string. So there are no automatic conversions to just the right type. It's your job to make sure that the types of the arguments you actually pass are exactly right for the format specifiers. If they don't match, the compiler does not automatically perform corresponding conversions. If they don't match, what happens is that you get crazy, wrong results.
(What does the prototype function declaration for printf look like? It literally looks like this: extern int printf(const char *, ...);. Those three dots ... indicate a variable-length argument list or "varargs", they tell the compiler it can't know how many more arguments there are, or what their types are supposed to be. So the compiler performs a few basic conversions -- such as upconverting types char and short int to int, and float to double -- and leaves it at that.)
I said "The compiler can't necessarily know what the right types of the arguments passed to printf are supposed to be", but these days, good compilers go the extra mile and try to figure it out anyway, if they can. They still won't perform automatic conversions (they're not really allowed to, by the rules of the language), but they can at least warn you. For example, I tried your code under two different compilers. Both said something along the lines of warning: format specifies type 'int' but the argument has type 'float'. If your compiler isn't giving you warnings like these, I encourage you to find out if those warnings can be enabled, or consider switching to a better compiler.
Try
printf("... %f",f);
That's how you print float numbers.
Maybe you only want to print x digits of f, eg.:
printf("... %.3f" f);
That will print your float number with 3 digits after the dot.
Please read through this list:
%c - Character
%d or %i - Signed decimal integer
%e - Scientific notation (mantissa/exponent) using e character
%E - Scientific notation (mantissa/exponent) using E character
%f - Decimal floating point
%g - Uses the shorter of %e or %f
%G - Uses the shorter of %E or %f
%o - Signed octal
%s - String of characters
%u - Unsigned decimal integer
%x - Unsigned hexadecimal integer
%X - Unsigned hexadecimal integer (capital letters)
%p - Pointer address
%n - Nothing printed
The code is printing a 0, because you are using the format tag %d, which represents Signed decimal integer (http://devdocs.io).
Could you please try
void main() {
clrscr();
float f=3.3;
/* In printf() I intentionaly put %d format specifier to see what type of output I may get */
printf("value of variable a is: %f",f);
getch();
}
Consider the following C-program:
int main() {
int a =2;
float b = 2;
float c = 3;
int d = 3;
printf("%d %f %d %f %d %f %d %f\n", a/c, a/c, a/d, a/d, b/c, b/c, b/d, b/d);
printf("%d\n", a/c);
}
The output of this is:
0 0.666667 0 0.666667 2 0.666667 0 0.666667
539648
I can't make sense of this at all. Why does printing a/c as an integer give 0, while b/c gives 2? Aren't all integers promoted to floats in computations involving both floats and integers? So the answer should be 0 in both cases.
In the second line of the output I'm simply printing a/c as an integer, which gives a garbage value for some reason (even though it gives 0 when I print it in the first compound printf statement). Why is this happening?
You have undefined behaviour:
printf("%d %f %d %f %d %f %d %f\n", a/c, a/c, a/d, a/d, b/c, b/c, b/d, b/d);
The format specifier for printf must match the type of the provided parameter. As printf doesn't provide a parameter list with types, but only ... there is no implicit type conversion apart from standard type conversion.
If you have UB, basically anything can happen.
What is likely to happen is the following:
Depending on the format specifier, printf consumes a certain number of bytes from the calling parameters. This number of bytes matches the specified format type. If the number of bytes does not match the number of bytes passed as an argument, you are out of sync for all successive parameters.
And of course you do incorrect interpretation of the data.
For starters according to the C Standard the function main without parameters shall be declared like
int main( void )
If you have for example the following declarations
int a =2;
float c = 3;
and then call the function printf the following way
printf( "%d", a / c );
then behind the hood the following events occur.
The expression a / c has the type float due to the usual arithmetic conversions.
As the function printf is declared with the ellipsis notation then to the expression a / c of the type float there are applied the default argument promotions that convert the expression to the type double.
As result in this call there is an attempt to output an expression of the type double using conversion specifier %d designed for the type int. Hanse the call has undefined behavior.
From the C Standard (7.21.6.1 The fprintf function)
9 If a conversion specification is invalid, the behavior is
undefined.275) If any argument is not the correct type for the
corresponding conversion specification, the behavior is undefined.
As a new C programming learner, I'm trying to understand which specifier I should use in printf to display the maximum and minimum values of a long double type. Per the following StackOverflow answer, I can use %Lg: https://stackoverflow.com/a/18797417/1536240
But that doesn't seem to do the trick. I've tried %e, %f, %Lf, %g and %Lg, but it's not working. Here's the relevant part of my code I'm having issues with (the rest of the stuff I'm doing's working fine):
printf("**long double**\nStorage size: %d bytes \t Minimum value: %Lg
\t Maximum value: %Lg\n", sizeof(long double), LDBL_MIN, LDBL_MAX);
I've included these header files at the top:
#include <stdio.h>
#include <limits.h>
#include <float.h>
I'm kinda lost here.
EDIT: Sorry for not explaining what the output is. I'm using %Lg right now, and I'm getting this in the terminal.
While you should provide the obtained results in your question, your mistake likely is to use %d to print the result of sizeof, which should be printed with %zu.
This causes undefined behavior. For some platforms, the rest of printf's arguments, on the stack, will be misinterpreted because the width of int does not match the width of size_t.
You should use "%Lf" or "%Lg" for a long double, and %zu (since and including C99) for the sizeof return type size_t.
Currently the behaviour of your program is undefined: %d is not appropriate for a size_t type.
As of the printf function is concerned, I understand the following from few references and experiments.
When we 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.
Similarly, what is the behaviour of scanf, if there is a mismatch of format specifier and the arguements passed to scanf. Does the standards define it?
Variadic arguments (those matching the ellipsis, ...) are default-promoted. That means that all shorter integral types are promoted to int (or unsigned, as appropriate). There's no difference between integers and characters (I believe). The difference between %d and %c in printf is merely how the value is formatted.
scanf is a different kettle of fish. All the arguments you pass are pointers. There's no default-promotion among pointers, and it is crucial that you pass the exact format specifier that matches the type of the pointee.
In either case, if your format specifier doesn't match the supplied argument (e.g. passing an int * to a %p in printf), the result is undefined behaviour, which is far worse than being "unpredictable" -- it means your program is simply ill-formed.
printf and scanf are confusing, because they're pretty special and unusual functions. Both of them accept a variable number of arguments. And this means that the rules for matching the types of the arguments, and the automatic conversions that might happen, are different than for other functions.
For most functions, the compiler knows exactly what type(s) of arguments(s) are expected. For example, if you call
#include <math.h>
int i = 144;
printf("%f\n", sqrt(i));
it works fine. The sqrt function expects an argument of type double, but the compiler knows this, so it can insert an automatic conversion, just as if you had written
printf("%f\n", sqrt((double)i));
(Footnote: The compiler knows about sqrt's expected argument type from the function prototype found in <math.h>.)
But printf accepts a variable number of arguments, of any types. You can pass just about anything, as long as the format specifiers match:
int i1 = 12, i2 = 34;
float f1 = 56.78, f2 = 9.10;
printf("%d %d %f %f\n", i1, i2, f1, f2);
printf("%f %f %d %d\n", f1, f2, i1, i2);
But that's the key: as long as the format specifiers match. In a call to a function with a variable number of arguments, like printf, the compiler does not attempt (it's not even allowed to attempt) to convert each argument to the type expected by the corresponding format specifier. So if there are gross mismatches, like trying to use %f to print an integer, it doesn't work, and in fact crazy things can happen.
But, just to keep things interesting, when you call a function that takes a variable number of arguments, like printf, there's another set of conversions that are automatically performed. They're called the default argument promotions. Basically, anything of type char or short is automatically converted ("promoted") to int, and anything of type float automatically converted to double. So you can get away with some mismatches: you can print a char or a short using %d, and you can print either a float or a double using %f. And since char is always promoted to int, that means %c is actually going to receive an int, and this means you can pass a regular int and print it with %c, also.
But it's easy to get this stuff wrong, especially since we're used to the compiler converting everything properly for us when we call other functions, like sqrt. So, a good compiler will peek at the format string, and use it to predict what types of arguments you ought to pass, and if you're passing something wrong, the compiler can issue a warning. If your compiler isn't issuing such warnings, it would be a good idea to figure out how to enable them, or if not, perhaps get a better compiler.
And then we come to scanf. scanf also accepts a variable number of arguments, which are supposed to match the specifiers on the format string. However, for scanf, all of the arguments you pass are pointers, to variables of yours which you're asking scanf to fill in with the values it reads. And it turns out this means that no automatic conversions are possible, and you must pass all arguments as pointers of exactly the right type. Here is a table listing some of them:
specifier
corresponding argument
%c
pointer to char
%hd
pointer to short
%d
pointer to int
%ld
pointer to long
%f
pointer to float
%lf
pointer to double
%s
pointer to char [note]
So there's no automatic conversion from char and short to int like there is for printf, and there's no automatic conversion from float to double If you're reading a float you have to use %f, and if you're reading a double you have to use %d.
Again, though, a good compiler will warn you about any mismatches, and it's a very good idea to seek out and pay attention to those warnings!
There is one "conversion" that happens automatically when you call scanf, but it has nothing to do with scanf. If you're using %s to read a string, you're supposed to pass an argument of type pointer-to-char, although it has to be a pointer to some number of contiguous characters, because scanf probably isn't going to read a single character; it's probably going to read a string of several characters. So it's pretty common to pass scanf an array of characters, and this is okay, because whenever you use an array in an expression in C, like when you pass it to a function (like scanf), the value that gets used is a pointer to the array's first element, which works just fine, and is what scanf is expecting. That is, both
char *p = malloc(10);
scanf("%9s", p);
and
char a[1-];
scanf("%9s", a);
will work. (And note that this is the one case where you do not need to use a & on a variable you pass to scanf.)
I ran this simple program, but when I convert from int to double, the result is zero. The sqrt of the zeros then displays negative values. This is an example from an online tutorial so I'm not sure why this is happening. I tried in Windows and Unix.
/* Hello World program */
#include<stdio.h>
#include<math.h>
main()
{ int i;
printf("\t Number \t\t Square Root of Number\n\n");
for (i=0; i<=360; ++i)
printf("\t %d \t\t\t %d \n",i, sqrt((double) i));
}
Maybe this?
int number;
double dblNumber = (double)number;
The problem is incorrect use of printf format - use %g/%f instead of %d
BTW - if you are wondering what your code did here is some abridged explanation that may help you in understanding:
printf routine has treated your floating point result of sqrt as integer. Signed, unsigned integers have their underlying bit representations (put simply - it's the way how they are 'encoded' in memory, registers etc). By specifying format to printf you tell it how it should decipher that bit pattern in specific memory area/register (depends on calling conventions etc). For example:
unsigned int myInt = 0xFFFFFFFF;
printf( "as signed=[%i] as unsigned=[%u]\n", myInt, myInt );
gives: "as signed=[-1] as unsigned=[4294967295]"
One bit pattern used but treated as signed first and unsigned later. Same applies to your code. You've told printf to treat bit pattern that was used to 'encode' floating point result of sqrt as integer. See this:
float myFloat = 8.0;
printf( "%08X\n", *((unsigned int*)&myFloat) );
prints: "41000000"
According to single precision floating point encoding format.
8.0 is simply (-1)^0*(1+fraction=0)*2^(exp=130-127)=2*3=8.0 but printed as int looks like just 41000000 (hex of course).
sqrt() return a value of type double. You cannot print such a value with the conversion specifier "%d".
Try one of these two alternatives
printf("\t %d \t\t\t %f \n",i, sqrt(i)); /* use "%f" */
printf("\t %d \t\t\t %d \n",i, (int)sqrt(i)); /* cast to int */
The i argument to sqrt() is converted to double implicitly, as long as there is a prototype in scope. Since you included the proper header, there is no need for an explicit conversion.