I want to write a C program that evaluates the factorials of the integers from 1 to 5 and print them in a tabular format. However, I keep getting a strange number over everything. Here's the code:
#include <stdio.h>
int main()
{
int factorial;
printf("X\t Factorial of X\n");
for(int x=1; x<=5; x++)
{
factorial = 1;
for (int j=1; j<=x; j++)
{
factorial *=j;
}
printf("%d\t %d\n", &x, &factorial);
}
return 0;
}
Here's the result of this code:
X Factorial of X
6356768 6356772
6356768 6356772
6356768 6356772
6356768 6356772
6356768 6356772
What's wrong with my code? The result should be like this:
X Factorial of X
1 1
2 2
3 6
4 24
5 120
Remove & which stands for address of. You are printing address of the variable, not its value.
printf("%d\t %d\n", x, factorial);
When using printf (and related output functions), the %d format specifier expects an int as the corresponding argument. In your printf("%d\t %d\n", &x, &factorial); you are passing the addresses of the x and factorial variables.
So, just remove the two & (address of) operators: printf("%d\t %d\n", x, factorial);!
You are possibly being confused by the fact that, for scanf (and other input functions), the %d specifier requires a pointer to its target variable.
printf("%d\t %d\n", &x, &factorial);
&x and &factorial are addresses of those variables, not the variables themself. Omit the &.
In the statement printf("%d\t %d\n", &x, &factorial); you have used '&' which prints the address of that element.
Unlike scanf, printf() with format %d requires integer values, not addresses (&x is the address containing an integer, so a pointer to integer which type is int *). The correct expression is
printf("%d\t %d\n", x, factorial);
Why was the previous expression wrong?
With printf("%d\t %d\n", &x, &factorial); you are asking to printf to print the decimal representations of the addresses of x and factorial respectively.
For this reason it is not surprising that the values that used to be printed, 6356768 and 6356772:
Are big numbers, multiples of 4 (because they address integers, that have a size 4 bytes, and in 32 bits architectures like yours are aligned to memory locations multiples of 4)
They are memory locations, and their addresses do not vary even if their contents are changed
You can find printf documentation here.
Related
# include<stdio.h>
int main() {
printf("%d \n", 7%9); //integer result
printf("%f", 7%9); //float result
return 0;
}
Above is the code I used to calculate the value of 7%9. I wanted to see both the integral and the float result. But, the values appearing here are different.
printf("%d \n", 7%9); //integer result
printf("%f", 7%9); //float result
Although the comments and the format specifiers indicate your intent, the compiler sees the integer calculation and passes an integer value to printf().
In the 2nd line, you've effectively written:
printf( "%f", 7 );
That is obviously incorrect.
If you wanted to see lots of zeros, you could cast the value to match what you've written as the format specifier:
printf( "%f", (float)( 7 % 9 ) );
What exactly happens when you convert a pointer to a non-pointer type?
For example:
int i = 7;
int *y = &i;
printf("%x %d %x", y, (int)y,7);
The result when compiled and run is:
29ff00 2752256 7
But what actually resulted - where did the number come from? Is it a random number or something related to what the pointer held?
Does it have a mathematical value such as the address or is the value of the pointer converted by some standard?
2752256 is the decimal value of the memory address 29ff00. You'd get a similar result with
printf("%x %d", y, y);
I'm currently learning the C language and I'm having trouble in the double multiplication topic.
I need to to print the original value and then the 2*value of the double.
double num = 34.39;
printf("Original value = %d, 2x original value = %d", num, num*2);
How do I make it so that the 2x value will be really 2x the original value?
Your multiplication is not the problem.
Your printf format string is. %d is not for floating-point values, but for integers, so you're seeing nonsense resulting from your broken contract with the compiler.
double num = 34.39;
printf("Original value = %lf, 2x original value = %lf", num, num*2);
%d - for int
You must use the "%f" for printf
I was going through scope rules questions and all and then got a code snippet, below:
#include <stdio.h>
int main()
{
int x = 1, y = 2, z = 3;
printf(" x = %d, y = %d, z = %d \n", x, y, z);
{
int x = 10;
float y = 20;
printf(" x = %d, y = %f, z = %d \n", x, y, z);
{
int z = 100;
printf(" x = %d, y = %f, z = %d \n", x, y, z);
}
}
return 0;
}
If I change the last print to:
printf("x = %d, y = %d, z = %d \n", x, y, z);
I get the following output, which I don't understand: (Ideone link)
x = 10, y = 0, z = 1077149696
So, could you explain why is z printing that value?
x, y, and z are resolved to most local definitions.
When you use incorrect printf % specifier, the behaviour is undefined.
y is float but you are using %d to print it (in later line).
printf uses varargs and once you corrupt the stack by using incorrect specifier (%d instead of %f in this case), stack is corrupted and incorrect interpretation of stack data (at incorrect offset) would cause many painful surprises.
Decoding This UB
This is what might be happening on your machine (One possible explanation). Because of default argument promotion, bit pattern (in hex) 0x4034000000000000 is being pushed to stack for 20.0f. Sizeof int on your little-endian machine is 4 bytes. When you print float as int your machine 0x00000000 is consumed and interpreted as int which prints first 0, later %d consumes 0x40340000 interpret it as int and prints 1077149696. Final 100 (0x00000064) is left in stack unconsumed and printf returns.
But never rely on this and always write a code for which the behaviour is well defined.
#mohit-jain is correct.
Using the wrong format specifier yields incorrect parameter interpretation on the stack, resulting in undefined and compiler specific behavior.
Note that on a modern compiler, like gcc or clang, it will complain that your format specification is wrong:
$ clang test.c
test.c:12:54: warning: format specifies type 'int' but the argument has type 'float'
[-Wformat]
printf(" x = %d, y = %d, z = %d \n", x, y, z);
~~ ^
%f
1 warning generated.
z = 1077149696
Using %d to print float values is undefined behaviour.
Use "%f" instead
All the variables you have used have storage type "Auto" or "Automatic".
The scope of the automatic variable lies inside the block in which it is declared.
If there are nested blocks, then the variable declared in the outermost block will be visible to all other blocks.
In case if a block has a declared a variable that matches with the one declared in outer blocks, then it will overwrite the outer variable "in its block" i.e.(locally).
To sum up :
Automatic variables are local to the block in which they are declared.
This question already has answers here:
Displaying floating point variable as a hex integer screws up neighbouring integer
(2 answers)
Closed 8 years ago.
OK:
I found that this is a dupe question
For my answer to this question, I tested what would happen if I passed a float to printf, and have it printed out as a hexadecimal value.
The code I used was simply this:
int main()
{
float i = 12.2;
printf("%x == %f\n", i, i);
return 0;
}
The result was somewhat surprizing, to say the least:
60000000 == 26815622268991053043690768862555225929794561970930945383754133458631904088629457662404735694345936594619420127195201411004744101710347755649498829446709248.000000
Now that couldn't be quite true, I do think. If I switch the order of the format string to "%f == %x\n", the float shows up well (12.200000).
Now this intrigued me somewhat. So I did some more experimenting
printf("%f == %x == %f\n", i, i, i);
revealed to me that the %f format fails after the %x has been used. So I tried:
printf("%f == %x == %f\n", 12.2, i, 12.2);
Which had me scratching my noodle even more:
12.200000 == 60000000 == 190359837254612272720121546180914982603183590679420528733621571728996711854558612668142684377801318191569131534073063745617380878171517108433746379972393615159806611654135195336789983232.000000
And, before you ask:
printf("%f == %x == %f\n", 12.2f, i, 12.2f);
yields:
12.200000 == 60000000 == 26815622268991053043690768862555225929794561970930945383754133458631904088629457662404735694345936594619420127195201411004744101710347755649498829446709248.000000
When I print printf("%f\n", i) either before or after the statement(s) I posted above, the output is as I'd expect it to be.
Does anyone have any idea what is going on here?
Your program's behavior is undefined. Wrong conversion specifier invokes undefined behavior.
The %x format is for integers, not floating point. To print a float in hex format, use %a. To print the raw bytes of a float (or other arbitrary object) in hex, which is what I'm guessing you really want, you have to use a union:
union {
float f;
unsigned int i;
} u;
u.f = 1.2;
printf("%x", u.i);
or memcpy the bytes from the float to an int and then printf the int.
Or you can cheat and use *(int *)&some_float