How to handle overflow of unsigned int in C? [closed] - c

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 12 months ago.
Improve this question
How can I change line 6 to handle the integer overflow so that this code will never terminate (while still increasing i in each loop iteration)?
1 int main() {
2 unsigned int i;
3 i = 1;
4 while (i > 0) {
5 printf("%u \n", i);
6 i *= 2;
7 }
8 printf("%u \n", i);
9 }

Because i is unsigned, it is never less than zero, but it may at some point be zero.
I might try to guarantee it is always at least 1 with something like this:
i = i*2? i*2 : 1;
That is:
If i*2 is non-zero, then that is the new value of i.
Otherwise, i*2 would be zero, so instead set i = 1;

Related

In C language, how can I print 1234567890 to create a position/location ruler? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 1 year ago.
Improve this question
basically, I want to create a Position/location ruler which shows each numeric position starting at 1. let's say, if the characters in the above statement are 20 then in the next line 12345678901234567890 should be printed. similarly, if the characters are 30 then 123456789012345678901234567890 should be printed.
how can I do that?
Let's say n is the length you need to match
int n = 20; // or 30, or whatever
for (int i = 0; i < n; i++) printf("%d", (i + 1) % 10);
puts(""); // add newline

Relational operator and the precedence of operators [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 2 years ago.
Improve this question
I executed a code in the C language, However I am unable to understand its output.
#include <stdio.h>
int main()
{
int a=5;
int b= ++a + 0!=0;
printf("%d %d",++a, b);
return 0;
}
The output for the above program is
7 1
I am unable to understand why it is so.
Order of operations causes this to be treated as:
int b = (((++a) + 0) != 0);
Therefore:
int b = (6 != 0);
6 isn't 0, so that has a value of true aka 1.
int b = 1;

Try to multiple huge int but get product of 1 [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 4 years ago.
Improve this question
I watched a tutorial which explain int:
but I just get 1:
Enter a:999999999999999999999999999999999999999999
Enter b: 9999999999999999999999999999999999999999999999999999999
a * b = 1
$ ./a.out
Enter a:87999999999999999999999999999999999999999999999999999999999
Enter b: 89999999999998798774334999999999994378969869869869458639534934578365
product = 1
What's the problem?
The reason for you getting 1 instead of -129542144 is that you multiply
999999999999999999999999999999999999999999 and 89999999999998798774334999999999994378969869869869458639534934578365
instead of 300000 and 200000.
Try the following short program you will get the result in the example, -129542144.
#include <stdio.h>
int main()
{
int a = 300000;
int b = 200000;
printf("%d\n", a*b);
return 0;
}
This is assuming that the datatype is int and that it is at least 32 bits.

How to get the suffix of a int variable in C? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 6 years ago.
Improve this question
I have a int variable in which I am storing the integer value. Now I want to get only the suffix. e.g:
int num = 12;
int num = 17;
etc
from the above num variables, I want to store the suffix like 2, 7 etc.
What is the efficient way to do that?
Thanks in advance...
The modulo (%) gives you the remainder when divided by a number which will be 10.
int num1 = 12;
int num2 = 17;
int suffix1 = num1 % 10;
int suffix2 = num2 % 10;

How to solve using bitwise operators [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
I wanted to find out efficiently which drawers are Full. But the output has to be a number corresponding to the binary representation. For example if only the second Drawer is full (from left to right), then the output is 4:
8 4 2 1
0 1 0 0 (Drawer Two is Full)
So I used this approach.
int DrawersFull[4] = {0,0,0,0}; //Initially all are empty
for(i=0;i<4;i++)
{
if(IsDrawerFull) // the api was provided by the interviewer
DrawerFull[i]=1;
}
I am not sure how to generate the output. Any suggestions will be helpful. Thanks.
Interviewer gave me hint that it can be done using bitwise operators but I am not sure.
This is the same as converting binary number to decimal. Try this one:
int res = 0;
for (i = 4; i >= 1; i--) {
res = res * 2 + DrawerFull[i]; // Assuming DrawerFull will contain only 1 or 0.
}
char fullDrawers = 0;
for( char i = 0; i < 4; ++i )
{
if( IsDrawerFull )
fullDrawers &= 1;
fullDrawers <<= 1;
}

Resources