Trying to understand this printf formatting output [duplicate] - c

This question already has answers here:
Why does printf print wrong values?
(7 answers)
Closed 7 years ago.
I don't understand why this code produces the below output:
#include <stdio.h>
#define LOWER 0
#define UPPER 300
#define STEP 20
float c_to_f(float c);
int main(void) {
for (int c = LOWER; c <= UPPER; c += STEP) {
printf("%3.0f %6.1f\n", c, c_to_f(c));
}
return 0;
}
float c_to_f(float c) {
return c * (9.0/5.0) + 32;
}
with output:
0 5144477247317086170901765440027035767837163293591161256351693248184965237877467107389389528872273154691913581744607058050215827488351921876414407003384176234234181468372580859505320314312544948225387164490993094256968227227818959640206687395851530141696.0
0 5144477248223936133773425621301895611375254354002848388186656757998856300277376171732587490599187934010444965285645525890922748188992798901374640654313592657351174354641522290319226294263523595109393871854132336451448805097328901373303486131449817464832.0
0 5144477248704033172940775129035644940307184915397270987393402145547386862724386852855456999748731052473196874219136479453649941501096792620471234940099754293118994117960373636044235342472865231695044481634617817613232640440010635232001791344413616635904.0
0 5144477249104114038913566385480436047750460383225956486732356635171162331430229087124514924040016984525490131663712274089255935927850120719718396844921555656258843920726083090815076215980649928849753323118355718581385836558912080114250379021883449278464.0
0 5144477249344162558497241139347310712216425663923167786335729328945427612653734427685949678614788543756866086130457750870619532583902117579266693987814636474142753802385508763677580740085320747142578628008598459162277754230252947043599531628365348864000.0
0 5144477249584211078080915893214185376682390944620379085939102022719692893877239768247384433189560102988242040597203227651983129239954114438814991130707717292026663684044934436540085264189991565435403932898841199743169671901593813972948684234847248449536.0
I do understand that c should be a float, just not the output. Any help would be greatly appreciated.

The problem here, as I see it is
printf("%3.0f %6.1f\n", c, c_to_f(c));
c is an int and you're trying to print it's value using %f, which is undefined behavior.
Using an inappropriate type of argument for a format specifier is undefined behavior.

When using printf and floats you have the right token %f. But your using it wrong. To printf out 2 decimal places you would use %.2f, 3 would be %.3f and so on. I think the console is bugging out because you are using %3.0f and %6.1f

Related

Unexpected output in expression that uses macros [duplicate]

This question already has answers here:
The need for parentheses in macros in C [duplicate]
(8 answers)
Closed 2 years ago.
My code:
#include <stdio.h>
#define PRODUCT(x) (x * x)
int main()
{
int i = 3, j, k, l;
j = PRODUCT(i + 1);
k = PRODUCT(i++);
l = PRODUCT(++i);
printf("%d %d %d %d", i, j, k, l);
return 0;
}
I am not able to comprehend why the output is:
7 7 12 49.
Is there any error in macro or some other problem?
Your code has undefined behavior, operations in i:
k=PRODUCT(i++);
l=PRODUCT(++i);
lack a sequence point.
As for:
j=PRODUCT(i+1);
It expands to i+1*i+1 which is i+i+1 which is 7. I assume it's not the expected result, in the future also include that in your question.
Your macro is incorrect. The following expression:
PRODUCT(i+1)
will expand to
(i+1 * i+1)
which is 2*i+1.
Your macro should be:
#define PRODUCT(x) ((x)*(x))
I strongly suggest you stop using macros for this sort of thing. You could easily write this as a function:
int product(int x)
{
return x * x;
}
Note that this will only work for the example I gave. If you try
PRODUCT(i++)
you will get
( (i++) * (i++) )
which invokes undefined behaviour, as this expression lacks a sequence point between the 2 increments.

C arguments from the C primer plus 6e should effect print [duplicate]

This question already has answers here:
What can happen if printf is called with a wrong format string?
(5 answers)
Closed 4 years ago.
C written in Emacs
// print1.c --displays some properties of printf()
#include <stdio.h>
int main(void)
{
int ten = 10;
int two = 2;
printf("Doing it right: ");
printf("%d minus %d is %d\n", ten, 2, ten - two);
printf("Doing it wrong: ");
printf("%d minus %d is %d\n", ten); // forgot 2 arguments
return 0;
}
Welcome to the Emacs shell
Output:
~ $ ./a.exe
Doing it right: 10 minus 2 is 8
Doing it wrong: 10 minus 2 is 8
It should do nothing of the sort.
The behaviour of your program is undefined.
One manifestation of undefined behaviour is the compiler figuring out what you really wanted to do. Don't ever rely on that though.

Why am I getting 0 as the output? Shouldn't it be 40? [duplicate]

This question already has answers here:
Why printf() isn't outputting this integer as float number? [duplicate]
(8 answers)
Closed 4 years ago.
Why does the following code output 0 instead of 40?
#include <stdio.h>
int main()
{
int volume;
int length = 5;
int width = 8;
volume = length * width;
printf("%f", volume);
return 0;
}
The variable "volume" is Integer , you need in the printf function to change from %f that is for float variable, to %d that is for print Integer variable.
Declaration of a variable is int volume; and you are printing it by format specifier %f which belongs to float type of variable. Type casting requires (float)volume. In C programming it happens a lot because compiler dependency comes in picture.

Using modulus operator on a char in C [duplicate]

This question already has answers here:
Simple Caesar shift in C
(2 answers)
Closed 6 years ago.
I am trying to mod a char in c by using the following:
int shift = 1;
c = (c + shift ) % 26;
printf("c= %c \n",c);
The variable c is a lowercase english character between a-z. When I try to print out the char c using the above code, I get the following:
c=
Could someone please tell me what I am doing wrong?
Shift it by 'a' or 'A'. Then you'll always get a character that's part of the alphabet:
int main(){
unsigned i;
for(i=0;;i++)
printf("i=%c\n", 'a'+i%26);
}
prints:
i=a
i=b
i=c
i=d
i=e
i=f
i=g
i=h
i=i
i=j
...

Not understanding the output when Converting Fahrenheit to Celsius [duplicate]

This question already has answers here:
C Temperature Conversion Program Keeps Outputting 0 For Fahrenheit to Celsius [duplicate]
(2 answers)
Closed 8 years ago.
I have following C program, that takes a temperature in Fahrenheit and convert in to Celsius value. But, every time I input a value it always gives me0.00 as output. I'm not understanding where is the problem.
#include <stdio.h>
int main()
{
float cel_out, fht_in;
printf("Enter a temperature in farenheit: ");
scanf("%f", &fht_in);
cel_out = (fht_in - 32) * (5/9);
printf("Temperature in celcious: %.2f", cel_out);
return 0;
}
In this euqation
cel_out = (fht_in - 32) * (5/9);
When you use 5/9 it is integer data type which results 0. so
cel_out = (fht_in - 32) * 0;
Results 0 only!
You need to use-
cel_out = (fht_in - 32) * (5.0f/9.0f);
here it is treated as float values and gives the actual result of 5/9.

Resources