Unsigned int overflow [closed] - c

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question appears to be off-topic because it lacks sufficient information to diagnose the problem. Describe your problem in more detail or include a minimal example in the question itself.
Closed 9 years ago.
Improve this question
I am facing an unsigned integer over flow issue
i.e unsigned int x= < max value * max value >
when I print x it is giving me the -ve value even though it is of unsigned integer
I am eager to understands how the compiler is making that as a negative vale
how do I over come this problem ??
thank you in advance

The compiler itself is not treating it as a signed value, that's almost certainly because you're using the %d format string for outputting it.
Use the %u one for unsigned decimal values and you see it have the "right" value (right in terms of signedness, not right in terms of magnitude, which will be wrong because you've performed an operation leading to overflow).

How are you printing it? Probably using printf. printf prints your unsigned ints as if they were signed (at least if you use %d). But this doesn't change the fact that the number is unsigned and hence positive.
Here's how you can check it: compare it to 0 and see what happens. So add this right after your printf:
if (x>=0) printf("positive\n");
else printf("negative\n");
and see what happens.

Related

Signed long to float and then to signed short [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 14 days ago.
Improve this question
Here is my code:
#include <stdio.h>
typedef signed long int32_t;
typedef signed short int16_t;
int main() {
int32_t var1 = 1200;
int16_t var2;
var2 = (int16_t)((float)var1/1000.0);
printf("Hello World: %d", var2); // prints 1 should print 1.2
return 0;
}
Typecasting between datatypes in C. As a result, I am trying to get the value of 'var2' as 1.2 in the signed short, but I have got value 1. I have to use the 16bit register and I cannot use 32bit float.
printf("Hello World: %d", var2); // prints 1 should print 1.2
No it should not.
(float)var1 converts to float.
(float)var1/ 1000.0 - result 1.2
(int16_t)1.2 - converts to integer and the result is 1
BTW you cant print 1.2 using %d format. To 100% correct you should use %hd format to print short integer.
Casting does not binary copy only converts between the types
var2 is a "signed short" type and it can only contains integer value. If you assign to it a decimal number it truncate the decimal part (0.2) and retains only the integer part (1). I hope I was helpful. Have a nice day!
You already have it in a 16-bit int in var1. Your representation is called "scaled integer". Just do the conversion when you need to print the value.
printf("%f\n", (float)(var1/1000.0));

Why does printing strlens not work for a int value? [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
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.
Closed 2 years ago.
Improve this question
But it works for a long data type?
for example
printf("%i", strlen("test")) -- errors
printf("%li", strlen("test")) -- this works
strlen doesn't return an int or a long, it returns a size_t. It appears that in your system, a size_t is the same size as a long.
But if you're going to print out a size_t with printf, you're supposed to use %zu to do it. That should work (on a new enough compiler to support it) regardless of the relative sizes of int, long and size_t. Before %zu was added to the spec (e.g., C89/90) we typically cast the size_t to an unsigned long before printing it out (and then used %lu, obviously).
strlen returns size_t, not int. size_t is an unsigned type; int is signed.
Passing values that do not match format specifiers is undefined behavior. The correct format specifier for size_t is %zu. (C Standard, ยง 7.19.6.1:6).

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);

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.

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