Questions tagged [pre-increment]

For issues relating to defining or performing pre increment operations.

Pre-increment operators increase the value of their operand by 1 (or another specified amount), and the value of the operand in the expression is the operand's value after the increment operation.

342 questions
3
votes
1 answer

Mixed increment operators with logical operators

I have a question concerning pre and post increments with logical operators if I have this code void main() {int i = - 3 , j = 2 , k = 0 , m ; m=++i||++j&&++k; printf("%d %d %d %d",i,j,k,m);} knowing that the increment and the decrement operators…
user2268349
  • 33
  • 1
  • 5
3
votes
4 answers

post increment behaviour

i have small doubt.why the below code is printing value i=2. int i=2; i=i++; System.out.println(i); can someone please explain me what is happening in line no 2. so there is no meaning here of doing ++ here? Thanks
Jayesh
  • 6,047
  • 13
  • 49
  • 81
2
votes
2 answers

Why do textbooks prefer "++x" to "x++" when it is context invariant?

Possible Duplicate: Difference between i++ and ++i in a loop? Is there a performance difference between i++ and ++i in C++? Incrementing in C++ - When to use x++ or ++x? Why use ++i instead of i++ in cases where the value is not used anywhere…
user1202733
  • 623
  • 4
  • 10
2
votes
3 answers

Pre / Post Increment Explanation

Please be easy on me and don't shoot me as I'm still newbie. I'm totally confused and can't for life figure out why when I run this code: int y = 9; cout << "++y = " << ++y << "\n--y = " << --y << "\ny++ = " << y++ << "\ny-- = " << y-- << "\n"; cout…
XO39
  • 481
  • 2
  • 9
  • 22
2
votes
4 answers

Can you change the value of a variable inside of a printf statement in C?

The following two problems I am having trouble with below are from chapter 5 exercise 3 in "C Programming a Modern Approach" by K.N. King. 1) i = 7; j = 8; k = 9; printf("%d ",(i = j) || (j = k)); printf("%d %d…
ChicoTabi
  • 45
  • 4
2
votes
1 answer

Confusion with increment operators in Java in conditional statements

int i = 10; if(i++ == i) System.out.println(i + " is good"); else System.out.println(i + " is bad"); int j = 20; if(++j == j) System.out.println(j + " is good"); else System.out.println(j + " is bad"); So when thinking about the output my thought…
2
votes
2 answers

Java: Order of Operations, Post-Increment Clarification

Why is the output 25? // CODE 1 public class YourClassNameHere { public static void main(String[] args) { int x = 8; System.out.print(x + x++ + x); } } Hi! I am aware that the above code will print 25. However, I would like to…
2
votes
2 answers

Incrementing a 2D array in C

I have just started learning 2D arrays in C and I came across this code where a 2D array is directly pre-incremented like this ++array. I tried to print the matrix in 3 different places(After initializing, in function after increment, in main…
Sum
  • 35
  • 5
2
votes
3 answers

How do Unary operators work in C, you cannot pre increment zero

I am confused about how Unary Operators work in C for Ones Complement, Logical Negation and preincrementing. Ones complement works against 0 like this: int main() { int a; a = ~0; // Ones complement printf("%d",a); // prints as…
jack
  • 145
  • 1
  • 9
2
votes
1 answer

Why am i getting "error: expression is not assignable" for combining increment operators

I am trying to concatenate post and pre-increment operators. I see that for some combinations involving post operators, I get the unassignable error. Is it because the post operator will be evaluated once the j's value is defined. can anyone give me…
Yash Kapoor
  • 93
  • 2
  • 12
2
votes
2 answers

Why does ++(*ptr) increment the Pointer?

So I'm new to C and tested some things with pointers and I have a question about the following printf: char txt[] = "thisIsAQuestion"; char *ptr = &txt[9]; printf("%c\n", ++(*ptr)); printf("%c\n", *ptr); So following my "knowledge", it would go…
2
votes
3 answers

Why isn't j incremented in (++i || ++j)

I don't understand the output of this code: long i=5, j=10; if (++i || ++j) printf("%ld %ld\n", i, j); else printf("Prog1\n"); The output is 6 and 10. I expected 6 and 11. Why wasn't j incremented?
2
votes
1 answer

vb.net preincrement operator in function arg

Sub V(N As Integer) Console.WriteLine(N) End Sub Sub Main() Dim N = 0 For I As Integer = 1 To 5 V(++N) Next End Sub VB.Net does not have preincrement operator, ++N wouldn't work outside of a function argument. Why does this…
user6144226
  • 613
  • 1
  • 8
  • 15
2
votes
5 answers

Strange behavior from a simple C program

If I run the following code, graph[0][0] gets 1 while graph[0][1] gets 4. In other words, the line graph[0][++graph[0][0]] = 4; puts 1 into graph[0][0] and 4 into graph[0][1]. I would really appreciate if anyone can offer reasonable…
2
votes
1 answer

Pre / Post increment operator on structure pointer

I'm new to C didn't get what going on here struct person { int age; }; main () { struct person p , *ptr; ptr = &p; printf ("%d \n" , ++ptr->age ); printf("%d" , ptr++->age); return 0; } How Both printf statements prints 1 ?
Rajat
  • 2,467
  • 2
  • 29
  • 38