Printf containing i++ [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 1 year ago.
Improve this question
In this piece of code:
int main()
{
int i=0;
while(i<10)
printf("%d",i++)
return 0;
}
If I am not wrong, the printf will always print the value +1 of i, so the first print will not be 0 but actually 1, am I right?

You are wrong. The post-increment i++ is evaluated to the value of i before incrementing, so what is printed in the first iteration is 0.

The expression x++ evaluates to x before incrementing.
int x = 5;
printf("%d\n", x++);
// Output: 5
printf("%d", x);
// Output: 6
On the other hand, ++x evaluates to x + 1, or x after incrementing:
int x = 5;
printf("%d\n", ++x);
// Output: 6
printf("%d", x);
// Output: 6

When you compile code it is read from left to right like text,
so for example your line 6
printf("%d",i++)
would first print the decimal and then add to variable.

Related

How to handle overflow of unsigned int 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 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;

How to calculate values for -- / ++ [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 3 years ago.
Improve this question
I am trying to get the value for the last equation
int a = 0, b = 0, c = 0, x = 0, y = 0, z = 0;
a = b++ + ++c;
printf("a=%d\n", a);
x = y + 1 + ++z;
printf("x=%d\t", x);
printf("b=%d\t", --b);
printf("b=%d\t", b++);
printf("c=%d\t", c+1);
printf("c=%d\t", 2-c);
whats the value of the last equation and why? how do I calculate it
These statements
printf("c=%d\t", c+1);
printf("c=%d\t", 2-c);
do not change the value of the variable c.
The variable was changed only in this statement
a = b++ + ++c;
^^^
If you want to change the variable c in the calls of printf then you should write at least
printf("c=%d\t", c = c+1);
printf("c=%d\t", c = 2-c);
Then the output will look like
a=1
x=2 b=0 b=0 c=2 c=0
^^^
Without these changes the output is
a=1
x=2 b=0 b=0 c=2 c=1
^^^

Print two strings [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 want to print two strings in console
int k = 3;
for (int i = 0; i < k; i++)
{
printf("\ra: %d\n\rb: %d", i*2, i*3);
}
I want to get this result:
a: 4
b: 6
But I get:
a: 0
a: 2
a: 4
b: 6
The reason why you are getting that output is that you have just one \r before a, but \n\r between a and b in the:
printf("\ra: %d\n\rb: %d", i*2, i*3);
Every cycle, the cursor returns to the beginning of line where b is and it writes OVER it, with new value for a :
1st 2nd 3rd run
a=0 a=0 a=0
b=0 a=2 a=2
b=3 a=4
b=6
since that is quick, you can see only the last one. It's really not clear what you wanna do, but if you need only the output of the last run, you can write the printf after the cycle:
for (int i = 0; i < k; i++)
{
...
}
printf("\r\na: %d\n\rb: %d", i*2, i*3);
If you change the loop to
for (int i = 2; i < k; i++)
this will print the result you asked for.
a: 4
b: 6
You could do a if inside the for like:
if (i * 2 == 4)
printf ...

How do i increment statement or decrement statement 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 7 years ago.
Improve this question
There are actually two ways i can do this. One is to use the increment operator ++ and decrement operator –. For example, the statement “x++” means to increment the value of x by 1. Likewise, the statement “x –” means to decrement the value of x by 1. Another way of writing increment statements is to use the conventional + plus sign or – minus sign. In the case of “x++”, another way to write it is “x = x +1″.
But why am i incrementing like this in my code and what does it mean?
for(i=0; i < numberOfProducts; ++i){
printf("Enter Product Name: ");
scanf("%s", &(pProducts+i)->productName);
printf("Enter Product Price: ");
scanf("%f", &(pProducts+i)->price);
}
My question is why did i use ++i for it to work? i tried i++ but could not print.
for(i=0; i < numberOfProducts; ++i)
and
for(i=0; i < numberOfProducts; i++)
are both equivalent as you are not reading the result of the operation (like in a = i++ vs a = ++i). The latter form is more common.
If you have different results, you probably have issues in the way you are testing your program.
++i is a pre-increment operation, meaning that i is first incremented then the incremented value is used in an expression. i++ is a post-increment operation, meaning that the existing value of i is first used in an expression, then it is incremented.
For example:
i=3;
x = 6 - i++;
printf("x=%d\n",x);
Outputs 3.
i=3;
x = 6 - ++i;
printf("x=%d\n",x);
Outputs 2.

post increment about an expression [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 8 years ago.
Improve this question
I was asked the output of following expression :-
I=10;
I++;
I++;
I++;
What will be the value of I at the end of this code. As per my knowledge, post increment in c means that first perform any other task like assignment ,printf etc and after it incteement the value of variable. Similarly in this case , first I should be 10, then I should be 10 and then it would be 11. But final answer came out to be 10 . how ?
The awnser should be 13.
int i = 10;
i++;
i++;
i++;
printf("%d", i);
test it urself
It would have been a nine-line program to demonstrate what happens to I in your question. In an expression containing I++;, I is used then incremented. In your example, since I is not used in any statement, you might have well used ++I to pre-increment it. But if a statement uses or tests I more than once, or contains a part that might not be executed, you must increment I afterwards.
#include<stdio.h>
int main() {
int I = 10;
I++;
I++;
I++;
printf ("%d\n", I); // prints 13
if (1 || I++) // I++ is not executed
printf ("%d\n", I); // prints 13
if (1 && I++) // I++ is executed
printf ("%d\n", I); // prints 14
return 0;
}
lets create an int i = 10;
i++;
is essentially the same as:
i = i + 1;
There is a slight difference though:
If you printf("%d", i++);
the printed value will be 10 since a ++ postfix will increment the value only after the value is used and i will equal 11 only on the next line, when:
printf("%d", i + 1);
will print 11 since it will be calculated before printf runs and i it self wont be changed since we didn't assign a value to it, we only used it to calculate a new value.
If you want behavior exactly the same as i = i + 1; you can use a ++ prefix like:
printf("%d", ++i);
in which case 11 will be printed and the value of i will increment by 1.
In your code you increment i using a ++ postfix 3 times without actually using i so all the code dose is increments i by one, there times. So at the end of the code i is equal to 13.
You can find more information on operators here:
http://www.tutorialspoint.com/cprogramming/c_operators.htm

Resources