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.
Related
C program takes float value as command line argument, so need to format from string to float and then to integer. Using round() from math.h, and then want to cast to int.
Casting by declaring (int) before value, but value type does not change.
Code below:
double *f = (double *) malloc(sizeof(double));
*f = (int)roundf(atof(argv[1]));
printf("Testing f: %d", *f);
make gives this error message:
format specifies type 'int' but the argument has type 'double'
[-Werror,-Wformat]
printf("Testing f: %d", *f);
You're putting int into double. The f variable should be of type int.
int f;
f = (int)roundf(atof(argv[1]));
C has a direct way to round and convert a float to an integer type
long int lround(double x);
The lround and llround functions round their argument to the nearest integer value, rounding halfway cases away from zero, regardless of the current rounding direction. ... C11 7.12.9.7 2
#include <math.h>
#include <stdlib.h>
long i = lround(atof(argv[1]));
// or
float f = atof(argv[1]);
long i = lroundf(f);
// Use %.0f to print a FP with no factional digits.
printf("%.0f\n", f);
// Use %ld to print a `long`
printf("%ld\n", i);
The error is in the %d format specifier in the printf function:
printf("Testing f: %d", *f); /* %d expects an integer but *f is a double */
It doesn't matter that *f contains a rounded number, it is still a double as far as printf is concerned.
Try this:
printf("testing f: %d", (int)*f);
N.B. why you are going to the trouble of using malloc to allocate a single double? If you need to pass just one double to some other program, you could just have:
double f;
... stuff ...
foo(&f);
Write a C program that takes in two arguments from the user and stores them as integers. Print the quotient of the two integers as a float. Print the two arguments themselves as integers. Constraints: You can only declare two variables int var1, var2; and you CANNOT use type casting. You must use the string formatting to cast the types. For example in printf("%lf", somefloat) the %f takes a float and returns a double.
Again: Only two variables. Print quotient as float, print variables as integers.
Something like this - without type casting:
#include <stdio.h>
int main() {
int v1 = 0;
int v2 = 0;
scanf("%d %d", &v1, &v2);
printf("qot %d %d %f", v1, v2, (float) v1 / v2); // NO TYPE CASTING !!!
}
Is this OK?
#include <stdio.h>
int main() {
int var1, var2;
scanf("%d %d", &var1, &var2);
printf("qot %d %d %f", var1, var2, 1.0f * var1 / var2); // implicit conversion
}
I also noticed "You must use the string formatting to cast the types" in your question. What do you mean by saying that?
#include <stdio.h>
int main() {
int v1 = 0;
int v2 = 0;
scanf("%d %d", &v1, &v2);
printf("qot %d %d %f", v1, v2, ( 1.0 * v1 / v2));
}
Use a compound:
printf("%f", ((double){v1})/v2);
You can store the answer in an intermediate float, in spite of the restrictions on variables, by using a compound literal such as (float){v1}.
The notation introduced here seems suspiciously cast-like, but this is not a cast. The curly braces distinguish it from casts; the expression inside the curly braces forms an initialiser for an anonymous object.
We can objectively prove that (float){v1} isn't a cast; by observing footnote 99 of the section on compound literals it becomes clear that casts don't form lvalues and compound literals do. We can't modify the results of casts, though we can modify the results of compound literals:
((float) v1 ) = 42; // ERROR! (float) v1 isn't an lvalue.
((float){v1}) = 42; // SUCCESS! (float){v1} is an lvalue.
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);
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.
#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.