What is wrong with this C program? - c

I just want to assign the value of pow to a variable, I've used this
#include<stdio.h>
#include<math.h>
int main( void )
{
int x;
int y;
int z;
x=10;
y=2;
z = pow(x,y);
printf ("%d" , &z);
return 0;
}
but in output I get -1076813284 , I am sorry, but I just started learning C and in every tutorial everyone just print the value of pow, like
printf("Value 3.05 ^ 1.98 = %lf", pow(3.05, 1.98));
and I don't know how to assign it to a variable

printf ("%d" , &z);
prints the address of z (*), not its value. printf("%d", z) prints the value.
(*) Actually, the behavior is undefined and on a 64-bit CPU it will likely print half of the address.

&z is the address of the variable z. If you want to print out the value of z, then the code is simply
printf("%d", z);
You would use &z when reading a value into z, because scanf needs a pointer in order to modify your variable.

pow returns a double (and not a reference), you need to make your print statement:
printf ("%f" , z);
After changing z to a double:
double z;

pow returns double and it takes arguments of type double.
double pow(double x, double y)
You need %f specifier in printf and also remove & from z.
Another way is to cast the return value of pow to int and use %d
int z = (int)pow(x, y);
printf ("%d" , z);

Related

Implicit and explicit casting

Using:
float return1(void);
int main()
{
int x;
x = (float)return1();
printf("%f",x);
return 0;
}
float return1()
{
return 1;
}`
Why is the output -0.000000?
Shouldn't x be implicitly cast to a float and print 1.000000?
Shouldn't x be implicitly cast to a float and print 1.000000?
No, it shouldn't because the compiler may not know what printf does or which format will be used to print x.
x has type int, so %d should be used instead of %f to print it.
Why is the output -0.000000?
because of
printf("%f",x);
and x is int, if you want 1.00:, do
printf("%f", (double) x);
or better change it to:
printf("%i", x);
printf doesn't cast and interpret object according to the flag given in format string (%f currently).
depending of what you want
printf("%d", x);
or
printf("%f", (float)x);
or in C++:
std::cout << x; // or float(x)
No need to cast return1() to float; your x is integer and you want to format it to float in printf
float return1(void);
int main(void)
{
int x;
x = return1();
printf("%d", x);
return 0;
}
float return1()
{
return 1;
}
No, it shouldn't. The compiler does generally not check the correspondence between the format string and the arguments you provide. In your case, it would be able to do so, but what if someone passes a string variable as format string? That is the reason for most compilers not checking the correspondence.

What will be its output and why?

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.

Double turns into nan in C

I have a function that uses an int, say 2488, to store temperature values. I have to call a function getTemp() to get the int. The getTemp function returns a double and uses the int to return the correct number. All the getTemp() does is return (double)x / 100.0 where x is 2488 in this case.
The returned double is then 24.88. This value then is sent to another function that adds the double to an array. Function is called DAaddDouble(double m, int x, int y) where m is the value to add, x and y is the coordinates that specify where to add the double.
Problem is, it turns into nan.
double a = getTemp();
//a is correct, i.e. 24.88
DAaddDouble(a, x, y);
/*-----------inside DAaddDouble----------*/
void DAaddDouble(double m, int x, int y)
{
//at this point, a (or m, same) is 0.nan
cord = x + y*40; //where to put the double
snprintf(DARRAY[cord], 5, "%f",m);
printf(....DARRAY[cord]...);
}
output: -nan
The signature of function DAaddDouble is:
void DAaddDouble(int m, int x, int y)
Note that m is of type int, and inside the function, you have:
snprintf(DARRAY[cord], 5, "%f",m);
in which %f expects type double, it's undefined behavior.
Probably what you need is to have the parameter m as double(as in your words above the code).
void DAaddDouble(double m, int x, int y)
Mis-match prototype.
Usage of DAaddDouble() is not preceded by its declaration/definition, thus the compiler assumes the function is:
int DAaddDouble(int m, int x, int y);
Precede the usage of DAaddDouble() by the function definition or a function prototype.
void DAaddDouble(double m, int x, int y);
A good compiler will warn of this. Insure all warning are enabled.
How do you know m is NAN. If you have infered by the value "printed" to DARRAY[cord], take into account that the format string should be "%lf" and not "%f".
void DAaddDouble(double m, int x, int y)
{
//at this point, a (or m, same) is 0.nan
cord = x + y*40; //where to put the double
snprintf(DARRAY[cord], 5, "%f",m); /* should be "%lf" for printing doubles */
printf(....DARRAY[cord]...);
}

How to multiply float with integers in C?

When I execute this code it returns me 1610612736
void main(){
float a=3.3f;
int b=2;
printf("%d",a*b);
}
Why and how to fix this ?
edit : It's not even a matter of integer and float, if i replace int b=2: by float b=2.0f it return the same silly result
The result of the multiplication of a float and an int is a float. Besides that, it will get promoted to double when passing to printf. You need a %a, %e, %f or %g format. The %d format is used to print int types.
Editorial note: The return value of main should be int. Here's a fixed program:
#include <stdio.h>
int main(void)
{
float a = 3.3f;
int b = 2;
printf("%a\n", a * b);
printf("%e\n", a * b);
printf("%f\n", a * b);
printf("%g\n", a * b);
return 0;
}
and its output:
$ ./example
0x1.a66666p+2
6.600000e+00
6.600000
6.6
Alternately, you could also do
printf("%d\n", (int)(a*b));
and this would print the result you're (kind of) expecting.
You should always explicitly typecast the variables to match the format string, otherwise you could see some weird values printed.

C: Expected output

#include <stdio.h>
int main()
{
long long x = 0x8ce4b16b;
long long y = x<<4;
printf("%lx, %lx, abc\n", x, y);
return 0;
}
I'm getting
8ce4b16b, 0, abc... Is this okay?
However if I change printf like printf("%lld, %lx, abc\n", x, y);
The output becomes:
2363797867, ce4b16b0, abc
Why could have been this behaviour!! :(
Using incorrect format specifier in printf invokes Undefined Behaviour. The correct format specifier for long long is %lld.
Also make sure that you dont have signed integer overflow in your code because that's UB too.
You should use printf("%llx, %llx, abc\n", x, y); in my mind. %lx for long integer.

Resources