Questions tagged [post-increment]

For issues relating to defining or performing post increment operations.

Filter by
Sorted by
Tagged with
0 votes
0 answers
40 views

Ambiguity in Pre & Post increment - C Programming [duplicate]

Writing a simple C program to understnad pre and post increment. I could understant case 1 and 3 but not getting how the value of i becoming 15 and 16 in case 2 and 4 resp. Kindly share your thoughts ...
parag's user avatar
  • 1
1 vote
1 answer
138 views

Incrementing rvalue in C++ [duplicate]

Does anyone know why this code does not compile int main() { int i = 7; int j = ++i++; // error: lvalue required as increment operand } while this code compiles perfectly? int main() { Int i{7};...
apostol's user avatar
  • 77
4 votes
3 answers
91 views

Does a variable holding result of signed integer overflow (side effect of post incr.) and, after that, never used it in any expression, result in UB?

Consider this program #include <limits.h> int main (void) { int i = 0; // Assume, user is a fair person, following the instruction strictly.. printf ("Enter a number in the ...
H.S.'s user avatar
  • 11.8k
0 votes
2 answers
49 views

Int pointer cast into char pointer is not deferencing correctly [duplicate]

I am trying to see how endianness applies on my system. Using Python sys module, I found that its little endian, which means that least significant bytes are at lower memory addresses. So, I wanted to ...
user9026's user avatar
  • 854
-4 votes
1 answer
55 views

I am getting the wrong output when trying post-increment operation on a pointer in C

I am testing out pre- and post-increment on an array using pointer. I use the pointers p and q to access the elements of the array but the issue stems from the last result on pointer p. #include <...
Monarch's user avatar
0 votes
0 answers
35 views

Can Overloaded pre increment and decrement, post increment and decrement operators not work well with inheritance?

Start with the COUNTEN2 program in this chapter. It can increment or decrement a counter, but only using prefix notation. Using inheritance, add the ability to use postfix notation for both ...
Avi1223's user avatar
1 vote
0 answers
75 views

Unexpected result in C program with increment operator

This statement int b = (a) + a++; is supposed to assign the value 8 to variable b, where a is initially assigned the value 4. However, the output of the program is 9 instead of the expected 8. I ...
Osmium's user avatar
  • 177
0 votes
0 answers
10 views

Explain the equation and its answer

#include <iostream> using namespace std; int main() { // your code goes here int x=3; int y; y = x+ (--x) + (x++) + x + (++x); cout << y << endl; return 0; } ...
Samiul's user avatar
  • 1
0 votes
2 answers
104 views

How does using a relational operator in an algebraic expression work?

I came across a piece of code where <= is used in an algebraic expression in C. int x=2, y=4, z=5, m=10; m+= x * y + z++ <=m+3; printf("%d, %d", m,z); I've never seen the use of a ...
Anuj Khandelwal's user avatar
0 votes
1 answer
69 views

Is i++ in any immaginable scenario faster than ++i?

(I'm assuming the old value of i is not needed, so i++ and ++i should at most affect performance.) The thing is, I've seen a counter incremented like this in code i++; and it left me wondering: ...
Enlico's user avatar
  • 23.9k
-1 votes
2 answers
60 views

Why is pre and post increment operator not working in recursion?

I have the following: public static void main(String[] args){ Screen.clear(); System.out.println(depth(5,0)); } public static int depth(int n, int depth){ System....
DCR's user avatar
  • 15k
0 votes
0 answers
28 views

how is 1st code working to get 21 while when same value is assigned to **b** the post increment operator doesn't work? [duplicate]

when running the below code the output is 21 #include<stdio.h> int main() {int a=10,b=10; printf("%d",a+(a++)); } whereas running below code gives output 20 int main() {int a=10,b=...
Noobcoder's user avatar
0 votes
2 answers
66 views

Does someone know what is wrong with my program in c? [duplicate]

#define _CRT_SECURE_NO_WARNINGS #include <stdio.h> int sum(int); int main() { printf("%d", sum(3)); return 0; } int sum(i) { int num = 5; if (num == i) { ...
Predrag Avramovic's user avatar
1 vote
3 answers
61 views

for loop through enumerated type in C raising compilation error

I have a code that defines a simple enumerated type, then loop through it to print the corresponding syllable. #include <stdio.h> typedef enum syllable { Do=1, Re=2, Mi=3, Fa=4, So=5, La=6, ...
Kim Minseo's user avatar
2 votes
0 answers
40 views

The below two C codes gives two different outputs, can someone explain why? [duplicate]

The first code is : #include <stdio.h> int main() { int a,b,c; a = 4; b = ++a; c = b + a++; printf("%d",c); return 0; } Output is 10 The second code is : #include &...
Harri's user avatar
  • 29
0 votes
5 answers
321 views

Why is a++=b disallowed, while c[i++]=d is permitted?

Why according to the standard is a++ = b; disallowed, while c[i++] = d; is permitted? (Obviously, a++ = b; would be bad style, but this is a question about a close reading of the C language standard.) ...
Lover of Structure's user avatar
0 votes
0 answers
57 views

Increment int value with more than 1 after evaluation instead of before

Is there any nice way to increment an int variable with more than 1 after evaluation instead of before evaluation? The ++ operator can be used to increment either before or after evaluation, but is ...
Magnus's user avatar
  • 17.4k
-1 votes
2 answers
94 views

Why isn't the ++y part executing? [duplicate]

So here is the question I am given , I need to tell the output : #include <iostream> using namespace std; int main() { int x = 10; int y = 20; if(x++ > 10 && ++y > 20 ){...
Paper PLane's user avatar
0 votes
0 answers
28 views

Different outputs on vs code and replit.com [duplicate]

I am trying to run this code which is written in C language. It is giving different outputs when run on VS code and Replit.com. I am confused. Please explain #include <stdio.h> int main() { ...
Deepanshu Pathak's user avatar
2 votes
5 answers
171 views

Are a[i]=y++; and a[i++]=y; undefined behavior or unspecified in C language?

When I was looking for the expression v[i++]=i; why it is to define the behavior, I suddenly saw an explanation because the expression exists between two sequence points in the program, and the c ...
三六九's user avatar
-1 votes
1 answer
43 views

Prefix and postfix operator

#include <stdio.h> int main() { int x=5, y; y=x+++x; printf("%d", x); printf("%d", y); } What I found is that postfix increment has higher precedence than ...
Subham Bhuyan's user avatar
1 vote
0 answers
52 views

How is the output of this expression 43? [duplicate]

I am learning C language and now I am confused with the output of the below snippet. #include <stdio.h> int main(void) { int p = 20; printf("%d\n",++p + p++); return 0; } ...
varun_2799's user avatar
-2 votes
2 answers
95 views

Whats the order of operations with respect to 'return' in C

I was shocked by the output of this... been coding in C for a few years now. Could someone explain a possible use case? Seems like it should be a compiler warning. #include <stdio.h> int chk(...
Greasyjoe's user avatar
-2 votes
2 answers
135 views

*a++ = *b++ (what does it mean, how it works)

What would be the values after performing this operation? #include <stdio.h> int main() { int *a = 0; int *b = 3; *a++ = *b++; printf("%d", a); printf("%d"...
YellowKing0907's user avatar
-1 votes
1 answer
47 views

How the value of m is 2?

How the value of m is 2 after execution as i is postfix ? int main() { int i,m; int a[5]={8,10,1,14,16}; i=++a[2]; m=a[i++]; cout<<i<<m; return 0; }
Frnaz Akbar's user avatar
0 votes
0 answers
188 views

Why the increment operator is not incrementing the value while assigning [duplicate]

why the Increment operator is working weird? If int b = 2; and b = b++ + b++ + b++; //output is 9 Then why b = b++ + b++; //output is 5 and not 6???
Meezan Malek's user avatar
-1 votes
2 answers
116 views

C language: When [variables++] in array[ ] work? For example, when array[j++] = arr[i]. It is doing j++ first or do = arr[i] first?

disclaimer this is not my code and this code is from Remove Duplicate Elements from an Array in C - Javatpoint What I want to know is in the Example 2 coding part. (I edit code a bit for me or you can ...
LunaticCoder's user avatar
0 votes
0 answers
102 views

Post-increment implementation in MSVC

MSVC 19.33 disagrees with GCC >= 4.8.1 and Clang >= 3.4.1 on the outcome of the following code: int x{0}; x = x++; // 1 with MSVC, 0 with GCC and Clang I know the code is silly. It is auto-...
honey_badger's user avatar
-2 votes
1 answer
28 views

What will this display?

This code was in a quiz of Dart course that I'm taking, please help me solve it. I want to know what it should display. I solved it as 6, but the answer was 1, but I don't know why. int var1 = 5; ...
Amr Emara's user avatar
0 votes
0 answers
26 views

Different Output through gcc and clang complier [duplicate]

so i compline this code in C #include <stdio.h> int main() { int a = 1; printf("%d",a++); printf("%d",a); printf("%d",++a); int b =1; printf(&...
Manvardhan's user avatar
-1 votes
2 answers
108 views

Getting wrong output for a++ +b according to lexical analysis when the program is printed along with a+++b [closed]

I wrote the following C program to find the output for a+++b #include<stdio.h> int main() { int a=5, b=2; printf("%d",a+++b); } And I'm getting the output as 7 which is ...
Siddharth Sengupta's user avatar
-1 votes
1 answer
80 views

Why is the output of the following program showing compile time error? [duplicate]

Why is the output of the following program showing compile time error? Please explain "lvalue required" #include<stdio.h> int main() { int a=5; printf("%d", ++a++); ...
3636 Rewa Kher's user avatar
0 votes
3 answers
192 views

x=*ptr++; is equivalent to x=*ptr; ptr=ptr+1;?

In the below code ptr1--->ptr--->&c=4 when i print *ptr and **ptr1 both prints value of c i.e 4 everything is fine here, but after initialization of x with *ptr++, i read somewhere that x=*...
xan_003's user avatar
  • 11
-1 votes
1 answer
54 views

Increment behaviour using char pointer [duplicate]

When i write copy ( s1++, s2++ ) instead of copy ( ++s1, ++s2 ) nothing happens.What's the problem? #include<stdio.h> void copy ( char *, char * ); int main ( void ) { char st[] = "...
dumb's user avatar
  • 21
1 vote
1 answer
148 views

Java post increment vs opcode iinc

Most of us are familiar with post increment, but when I look to the bytecode instructions, it shows the increment happened before the invocation to the method. Example: int x = 0; aMethod(x++); ...
Abdullah D.'s user avatar
1 vote
1 answer
322 views

Is increment stackable? I.e x++++; or (x++)++;

When me and my friend were preparing for exam, my friend said that x+++; is the same as x+=3; It is not true but is x++++; same as x+=1; or is (x++)++;? Could I generalize it? I.e. x++++++++++++++; or ...
JRBros's user avatar
  • 43
0 votes
2 answers
370 views

prefix operator behaviour in c++ when used multiple times in a statement [duplicate]

I am not able to understand prefix operator behaviour in scenario when it is used multiple times in a statement . Here is an example code to illustrate my problem #include<iostream> using ...
user1371666's user avatar
5 votes
3 answers
200 views

Does i = x[i]++; lead to undefined behavior?

Can someone please explain whether i = x[i]++; lead to undefined behavior? Note: x[i] and i are not both volatile and x[i] does not overlap i. There is C11, 6.5 Expressions, 2 (emphasis added): If a ...
pmor's user avatar
  • 5,593
0 votes
0 answers
19 views

When does the variable increase for using increment operator in C? [duplicate]

When this code run, i in the b[a[i]] already increase before meeting ;. b[a[i]] = i++ Let's suppose i is 0 now. I expect that this would be b[a[0]] = 0++; But, this looks like b[a[1]] = 0++; My ...
Matthew Chae's user avatar
1 vote
3 answers
43 views

Is formatted output equivalent? [duplicate]

When I was doing the practice questions today, I found that the outputs of printf("%d\n",x--); and printf("%d\n",x); are the same. I changed it to printf("%d\n",x++); and ...
huahua sun's user avatar
1 vote
2 answers
81 views

`++` and `+=` operator are behaving differently

I have a code which print pointer to const char, or I mean a string, recursively. My code works fine when I'm using += operator to call print() function. But, when I'm using ++ operator my code goes ...
user avatar
0 votes
0 answers
114 views

Why cant I post-increment a function call c++

Lets say I have this array and function int a[] = {1,2}; const int * bar() { return a; } Then in main int main() { const int * p1 = bar()++; } Why does this throw a compiler error? In my ...
Derithus's user avatar
-2 votes
2 answers
62 views

why doesn't the result of an array member changed when have assigned it? [closed]

when I did something like this: int arr[]={11, 12, 13, 14, 15}; int *p=arr; *(p++) += 100; The result of arr[1] was still 12,why?
HedoesnotDICE's user avatar
-1 votes
1 answer
54 views

b=b++ assignment unexpected result [duplicate]

I wrote this text code int b=5; int main() { b=b++; printf("b = %d\n",b); return 0; } and I expected it to print "b = 6"; however, the result is "b = 5", i.e....
Guille's user avatar
  • 356
2 votes
2 answers
188 views

double free or corruption (out) in realloc

I'm trying to fix a problem in my code since several days but I'm still stuck on it. I want to insert a value in a tab through a realloc but I have a memory leak (or something else) and I don't know ...
Ouahib's user avatar
  • 33
0 votes
1 answer
192 views

Why value of both (x-- +x) and (x-- -x) are same in Java? [duplicate]

Assume int x = 1; System.out.println(x++ + x); ---> Output:3 System.out.println(x++ - x); ---> Output:-1 System.out.println(x-- + x); ---> Output:1 System.out.println(x-- - x); ---> Output:...
Mohamed Arshad's user avatar
-1 votes
1 answer
62 views

Array[i] = i++; [duplicate]

int main() { int arr[5]= {55}, i=0; while (i<5) { arr[i]= i++; } for(i=0; i<5; i++) printf("%d, ", arr[i]); return 0; } why is the output ( ...
Sherif Beshr's user avatar
2 votes
1 answer
254 views

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 ...
SKumar's user avatar
  • 43
0 votes
1 answer
115 views

behavior while loop in c programming

output in this case is 1 int main() { int i = 500; while( (i++) != 0 ); printf("%d\n", i); return; } output in this case is 0 int main() { int i = 500; while( (i=i+1)...
ahmed khaled's user avatar
0 votes
3 answers
68 views

Explanation about Output in C

So I have the following code snippet in C: int a = 25, b = 100, c; if (b++ <= (a * 4)) c = 3; else c = 20; printf ("%f\n", (float) (b / c)); When I ran the code, the output returned ...
Ash's user avatar
  • 3

1
2 3 4 5
12