Why the result are so strange [closed] - c

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 1 year ago.
Improve this question
int main( )
{
int x = 5;
float y = 3.1f;
printf("int x=%d ,float x=%f ,y=%f, y=%d\n", x, x, y, y); //X86
return 0;
}
I think the answer are 5 , 5.0 , 3.1 , 3.
but the answer is
enter image description here
why?

The %d format specifier requires an argument of type int, and %f requires an argument of type double (possibly promoted from float). If you pass an argument of the wrong type, the behavior is undefined.
The solution: Don't do that.
(The most likely behavior is that the memory or register containing an int value will be displayed as if it were of type double, but anything could happen.)
For most function calls, passing an int to a function expecting a double argument causes the value to be converted, so 42 becomes 42.0. But since printf is variadic, the expected types are determined by the format string, not the parameter type, so the compiler can't in general know to generate a type conversion.

Related

Why does my program repeat forever instead of giving the maximum integer value? [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 5 years ago.
Improve this question
I am currently learning C, and I made a program that I thought would give me the maximum integer value, but it just loops forever. I can't find an answer as to why it won't end.
#include <stdio.h>
int main()
{
unsigned int i = 0;
signed int j = i;
for(; i+1 == ++j; i++);
printf(i);
return 0;
}
Any help would be appreciated, thank you!
Your code has undefined behavior. And that's not because of unsigned integer overflow (it wraps when the value is too big). It is the signed integer overflow that is undefined behavior.
Also note that if your intention is to know the maximum value that an int can hold use the macro INT_MAX.
maximum value for an object of type int
INT_MAX +32767 // 215 - 1
Your should write the printf properly. (Pass the format specifier then the other arguments as specified by format specifier).
I thought would give me the maximum integer value
The maximum signed int value cannot be portably found experimentally with out risking undefined behavior (UB). In OP's code, eventually ++j overflows (UB). UB includes loops forever.
As #coderredoc well answered, instead use printf("%d\n", INT_MAX);
To find the maximum unsigned value:
printf("%u\n", UINT_MAX);
// or
unsigned maxu = -1u;
printf("%u\n", maxu);

Sqrt of a floating point number [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 6 years ago.
Improve this question
Write a C program that asks the user to enter a floating point number from the keyboard and then prints out the square root of that number is the question. What am I doing wrong?
#include <stdio.h>
#include <math.h>
int main(int argc, char *argv[])
{
double x, result;
printf("Enter a positive number.\n");
scanf("&f", &x);
result = sqrt(x);
printf("The square root of %f is %f.\n", x, result);
return 0;
}
The unary '&' operator delivers the reference (to the operand variable's memory address), while the '%' operator in the context of a scanf or printf, for instance, in conjunction with a particular ANSI C symbols for variable type, such as 'lf' for type double, is known as a format specifier. By placing an integer value between the two, as in '%2lf', one can specify the precision to be read or printed. %f specifies a float type variable, and this achieves lower precision than a double. See the docs too. By the way, in C++, precision is specified otherwise.
So:
double x, result;
printf("Enter a positive number.\n");
scanf("%f", &x); //<--- use %lf (for 'long float' ) instead of &f
Use this
scanf("%lf",&x);
instead of
scanf("&f", &x);

Store a long decimal in C [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 6 years ago.
Improve this question
I am trying to do a physics problem and need to store a value around 5 * 10-11;
After trying float, long double and a few others none of them see to be long enough. Is there a data type that will allow me to do so?
Thanks
long double I = 0;
I = 0.01902*pow(0.00318,3)/12;
printf("%Lf\n",I);
Output is 0.000000
long double I = 0;
I = 0.01902*pow(0.00318,3)/12;
At this moment, I's value is approximately 5.096953e-11. Then...
printf("%Lf\n", I);
The sole format specifier in this printf() call is %Lf. This indicates that the argument is a long double (L), and that it should be printed as a floating-point number (f). Finally, as the precision (number of digits printed after the period) is not explicitly given, it is assumed to be 6. This means that up to 6 digits will be printed after the period.
There are several ways to fix this. Two of them would be...
printf(".15Lf\n", I);
This will set the precision to be 15. As such, 15 digits will be printed after the period. And...
printf("%Le\n", I);
This will print the number in scientific notation, that is, 5.096953e-11. It too can be configured to print more digits if you want them.

Why different format specifiers give different outputs with printf? [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 7 years ago.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Improve this question
I wanted to know why the output of this code is 0?
y.beta and p->beta are the same variable, so shouldn't the output be 3?
int main() {
struct MyType {
int alpha;
double beta;
};
static struct MyType y;
struct MyType *p = &y;
y.alpha = 1;
p->alpha = 2;
p->beta = 2.5;
y.beta = 1.5;
printf("%d", y.beta + p->beta);
return 0;
}
You invoke undefined behavior by passing the wrong type of argument to printf:
printf("%d", y.beta + p->beta);
%d expects an int to be passed, y.beta + p->beta is a double value.
Change the code to:
printf("%f\n", y.beta + p->beta);
Undefined behavior means anything can happen; it is vain to try and understand why it prints 0. Something else may happen on a different machine, or at a different time, or even if it is raining or if the boss is coming ;-)
As chqrlie correctly pointed out, printf("%d", somevariable) expects that the variable is passed as an int, whereas your result is a double type value.
%d is called a format specifier. These are required because different data types are passed and interpreted in different ways by the processor, possibly at a different place in memory or in different registers (as is the case for the x86_64 platform). So even the same memory location, with the same bit pattern may be interpreted very differently based on the data type. That's what is happening in this other example:
int main() {
int a = 1048577;;
printf("%d\n", a);
printf("%hd", a); //hd is the format specifier for short int
return 0;
}
output:
1048577
1
As you see, the same variable a is interpreted differently based on what data type you specify.
Why is it so?
This is the binary representation of 1048577 (assuming 32-bit int):
00000000 00010000 00000000 00000001
If the format specifier is short int (%hd) then is shorts are 16 bit wide, only the 16 low order bits from the value are used, hence the output is 1.
Something similar may be happening in your case on some architectures (x86 32 bits) or something worse on other ones where the double value is passed in a different way and printf gets the int from a location where nothing specific was written by the caller and any random value could happen to be there, such as 0.
The solution to this would be to modify your code to
printf("%f", y.beta + p->beta); as pointed out by chqrlie

Floating point to string representation [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 8 years ago.
Improve this question
Consider the following code snippet:
char str[1000];
float b ;
b= 0.0615;
sprintf( &(str[0]), "%1.0e", b);
After the execution of the last statement, I expected the str to contain 6.15e-2. However, I am getting the value as 5e-315.
Where am I going wrong. How to get the expected value?
You cannot get two digits precision with that format string, as you specified only one digit after comma (that is the .0 part after the 1).
What works for me is
#include <stdio.h>
#include <string.h>
main() {
char str[1000];
float b ;
b= 0.0615;
sprintf( &(str[0]), "%.2e", b);
puts(str);
}
prints 6.15e-02
The almighty C/C++ documentation says:
.number:
For a, A, e, E, f and F specifiers: this is the number of digits to be
printed after the decimal point (by default, this is 6).
My bet is you forgot including stdio.h.
There seems to be a mismatch between what type the compiler passes to sprintf and what sprintf actually reads (as described in cmaster's answer).
Apparently your compiler does not realize that sprintf() takes all floating point arguments as doubles. Consequently, it passes only the 32 bits of the float to the function, which erroneously interpretes 64 bits as a double. It should work fine if you cast the float to a double before passing it to sprintf().

Resources