183 questions
-2
votes
1
answer
38
views
how --x works fine in this code but x-- gets StackOverflowError [duplicate]
private static int fib(int zero, int one, int x, int s) {
if(x==0) return s;
zero = one;
one = s;
s= zero + one;
return fib(zero,one,x--,s);
}
private static int fib(int zero,...
-1
votes
1
answer
62
views
Postfix increment operator in JAVA [duplicate]
I'm studying about postfix increment operator in JAVA, and curious about why the result is 6 when coding like this.
int b = 3;
b += b++;
System.out.println(b);
I think that due to the += ...
1
vote
5
answers
254
views
How is pointer ++*ptr++ evaluated
So I'm quite confused as to how the post-increment is not evaluated first. (According to precedence post-increment comes first.) Can someone please explain this?
Consider the code given below:
int ...
0
votes
0
answers
50
views
C++ Postfix Operator Strange Behaviour in BorlandC++ [duplicate]
Overloading problem.
I have tried to overload Postfix++ operator in a class then I saw a strange behaviour whenever I try to do this
cout << oldValue <<" "<< oldValue++;...
0
votes
0
answers
67
views
How does printf() execute its arguments with prefix and postfix unary operators?
I wrote a simple code for printf in C with unary prefix operator as the following:
#include <stdio.h>
int main() {
int i=5;
printf("%d %d %d",++i,i,i+1);
return 0;
}
And ...
0
votes
2
answers
67
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 ...
1
vote
4
answers
164
views
Why does the following code print 8,5,2 instead of 7,4,1?
I just came across this C question with the following code:
int fun();
int main()
{
for (fun(); fun(); fun())
printf("%d\n", fun());
return 0;
}
int fun()
{
int static n = ...
0
votes
3
answers
95
views
StackOverFlow in Postfix Decrement in JAVA
This works fine because we are using the prefix decrement operator in the recursive call !
public static void main(String[] args) {
doubt1(5);
}
Prefix Decrement:
static void doubt(int num)
{
...
1
vote
3
answers
78
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, ...
0
votes
1
answer
48
views
Java arithmetic increment operator [duplicate]
int a = 6;
int b = a + a++;
answer is 12
int a = 6;
int c = a++ + a;
answer is 13
why are the answers for b and c are different ?
since postfix has higher a precedence value, then the answer ...
3
votes
3
answers
294
views
Will this expression evaluate to true or false (1 or 0) in C?
#include<stdio.h>
int main()
{
int a=4;
int b=4;
int c= a++ < ++b? 1 : 0;
printf ("%d",c);
}
It is known that there is a sequence point at ?, which means that ...
-1
votes
1
answer
53
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 ...
0
votes
0
answers
49
views
c++ operation precedence bug in g++ compiler vs clang++ compiler [duplicate]
the output of the code below are different between g++ and clang++ !
I think clang++ is correct, what do you think?
g++ output: 11 4 13 7
clang++ output: 11 4 11 8
#include <iostream>
struct A
{ ...
-1
votes
1
answer
72
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;
}
0
votes
1
answer
500
views
Evaluate postfix (Stack python)
what I have so far is:
Stack = ArrayStack()
def evaluate(postfix_str, Stack):
for c in postfix_str:
if c not in '+-*/()':
Stack.push(int(c))
else:
c....
2
votes
1
answer
83
views
Why does jshell show this number?
I am learning java and this logic makes me feel confused.
Isn't here i=20(+1)+20(+1)?
Why 41 instead of 42?
jshell> int i = 20
i ==> 20
jshell> i=i++ + i++
i ==> 41
See this code run at ...
0
votes
2
answers
952
views
Infix to Postfix Converter in C++ gives no output
I made a function infixToPostfix() which converts an infix expression to a postfix expression. But my program generates no output on running. Please point out the errors in my program.
Code:
#include &...
0
votes
2
answers
767
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 ...
1
vote
3
answers
50
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 ...
0
votes
1
answer
124
views
How to overload decrement(--) operator for functions which consist of other structure/classes
I found a lot information how to overload decrement(--) operators if your class has an int value.
But I can not understand how to do the same with classes which contain structures which contain int ...
0
votes
2
answers
848
views
how to apply operator overloading for unary postfix operator
below is code for operator overloading for unary operator ++
#include <iostream>
using namespace std;
class Distance {
private:
int feet; // 0 to infinite
int inches;...
1
vote
1
answer
63
views
Why does code compile when you attempt to access a value in 0 as an array? [duplicate]
Let's say you have the code as follows:
float arr[] = {
0.0f,
1.0f,
62.0f,
400.0f
};
Then I print this out as follows:
printf("%lu\n", sizeof(0[arr]));
Why does it return 4,...
1
vote
1
answer
156
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)...
0
votes
0
answers
20
views
Precedency of Postfix and prefix operator in C [duplicate]
I am finding difficulty to understand how precedency works in the following case. Can anyone explain why the answer is different in below two printf functions?
int main()
{
int a=10,b=20,c;
c=...
0
votes
0
answers
122
views
Operator overloading: X = X++ doesn't work [duplicate]
#include <iostream>
using namespace std;
class A {
int x, y;
public:
A(int a, int b)
{
x = a;
y = b;
}
void out()
{
cout << "\nx="...
1
vote
1
answer
172
views
precedence operator in java 8 - postfix operator first
Following the precedence operator in java 8 it is clear that the postfix operator (expr++ expr--) has a higher precedence that the unary operator, pre-unary operator (++expr --expr).
However when ...
0
votes
7
answers
535
views
Could you please explain why the value of i variable is 3 here after getting executed?
The semicolon has been added after the first while loop, but why is the value of the i variable 3 here, where j is 2?
#include<stdio.h>
int main()
{
int i=1;
while(i++<=1);
...
0
votes
3
answers
215
views
How do you evaluate z = x- - == y + 1;
given that
int w = 1;
int x = 6;
int y = 5;
int z = 0;
z = !z || !x && !y;
printf("%d\n", z);
z = x-- == y + 1;
printf("%d\n", z);
Could someone explain how the line ...
3
votes
4
answers
3k
views
What happens here: sum += i++;?
I have this simple piece of code but I don't understand this part: sum += i++ .
int num1 = 5;
int sum = 0;
if (num1 < 100) {
for (int i = 0; i < num1; i++)
sum += i++; //?
...
-3
votes
3
answers
123
views
Different and odd results when using prefix and postfix operators in function arguments [duplicate]
Code:
#include <stdio.h>
int main()
{
int i = 3;
printf("%d %d %d %d %d\n",i = 7,--i,i = 18, i+5, i = 0);
printf("%d %d %d %d %d\n",i = 7,i--,i = 18, i+5, i = 0);...
1
vote
1
answer
93
views
Confusion in assignment in c
#include<stdio.h>
int main()
{
int a=0, b=1, c=3;
*((a) ?&b :&a) = a ? b : c;
printf("%d %d %d\n", a, b, c);
return 0;
}
Output:3 1 3
The associativity for assignment ...
1
vote
1
answer
46
views
NSExpresseion, determine operating from left-associative
func postFix(_ expr: String) -> Int {
// write your code here
let nums = expr.components(separatedBy:CharacterSet.decimalDigits.inverted)
.joined()
let set = CharacterSet(...
0
votes
0
answers
22
views
Order of evaluation with assignment operator [duplicate]
Can anyone explain to me why the following code:
#include <iostream>
using namespace std;
int main() {
int y=3;
int z = (--y) + (y=10);
printf("%d \n", z);
}
prints out z=...
0
votes
1
answer
116
views
Prefix operators are not working as expected in C [duplicate]
#include <stdio.h>
int main()
{
int a = 1;
int b = 1;
int c = a || --b;
int d = a-- && --b;
printf("a=%d, b= %d, c= %d, d= %d",a,b,c,d);
return 0;
}
In ...
-2
votes
3
answers
134
views
Pre and Postfix Increment
a = 1;
int a2 = a++;
System.out.println("----------Test 3 ----------");
System.out.println("The value of a is " + a);
System.out.println("The value of a2 is " + a2);
...
1
vote
1
answer
219
views
Using postfix/prefix increment operators in compound literals in C99
There is an example borrowed from CARM (C A Reference Manual, Samuel P. Harbison III, Guy L. Steele Jr., 2002, Prentice Hall), page 218-219.
I write all of three code-chunk in one source:
#include <...
1
vote
1
answer
66
views
unary operator prefix execution in recursion is not working as expected
I am trying to count number of combinations in given range and length of combinations. Here is following code.
function generateCombination() {
const perm = [];
const permLength = 2;
const ...
13
votes
3
answers
316
views
Different results when using increment operator (arr[i++] vs arr[i]; i++;)
I can't get my head around why the code below is not working as expected:
#include <stdio.h>
int main() {
int i = 0, size = 9, oneOrZero[] = {1,1,1,1,1,1,1,1,0};
while (i < size &...
1
vote
1
answer
67
views
Why are expressions not processed after a postfix increment and a conjunction operator?
I bumped into a problem which prompted me to do some research. I have found that a piece of code like this:
#include <stdio.h>
int main(void)
{
char i = 0;
i++ && puts("Hi!&...
-2
votes
1
answer
242
views
How do I convert an infix expression to a postfix expression and I am not able to get the expected output?
I am trying to write a C++ program to convert infix expressions to prefix and postfix. Where did I go wrong?
How can I change code to delete parentheses at the last form (postfix)?
Thanks for any help!...
4
votes
1
answer
103
views
Does C++ compiler treat all postfix operator overloading as same (postfix version of - and --)?
#include <cstdio>
#include <iostream>
using namespace std;
class Int32 {
int num;
public:
Int32(int num = 0) : num(num) {}
~Int32() {}
int value() { return num; }
...
3
votes
0
answers
77
views
Different results for postfix operator (x++) in PHP and JavaScript
I'm learning web development, and I got puzzled by the strange behavior of a certain code in JavaScript and PHP.
JavaScript
n = 10
n = n - n++
console.log(n) // outputs 0
PHP
$n = 10;
$n = $n - $n++;
...
-2
votes
1
answer
239
views
Prefix vs Postfix and table made of array?
Can you please explain this code, keeping in mind that the person who wrote it was using it to teach me the difference between prefix and postfix incrementations
int main()
{
const int ROWS = 3;`...
-1
votes
1
answer
121
views
Why there is output difference for give code in c++ 11 and c++ 17?
I have a piece of code
#include <bits/stdc++.h>
using namespace std;
typedef long long ll ;
int main()
{
ios_base::sync_with_stdio(0);cin.tie(0);...
1
vote
1
answer
75
views
Postfix has high precedence than prefix so value of sml2 in given code should be 2 but it's 0. Why?
namespace Randomedits
{
class Program
{
static void Main(string[] args)
{
int x = 2;
int sml2 = ++x - (x++) ;
Console.WriteLine(sml2);
...
-1
votes
1
answer
175
views
Function to split the input expression into token units(python)
def get_token_list(expr):
token_list_initial = expr.split(' ')
token_list=[]
for token in token_list_initial:
if token in '+-/*^()':
token_list.append(token)
elif ...
6
votes
1
answer
346
views
Difference between &++x and &x++
Although this question might be answered somewhere and I could not find it.
Below written first statement work whereas second does not? WHY?
int main() {
int x = 1, y = 2;
int *p = &++x; // ...
-3
votes
1
answer
179
views
Why is this java postfix expression returning this result? [duplicate]
Given:
int r=1;
r=r++ + r++ + r++;
System.out.println("r:" + r);
Why is this returning 6 instead of 7.
I can get the order of how the above is evaluated e.g. 1 + 2 + 3 = 6 but not sure why the last ...
0
votes
1
answer
169
views
Why aren't unary functions usable in postfix notation?
I see those two main advantages to postfix over prefix notations for unary functions:
nice formatting, no nesting
maps better the Input -> Function -> Ouput way of thinking about data ...
0
votes
1
answer
306
views
In Java, is a++ a postfix operator or an unary operator?
In Java, is a++ a postfix operator or an unary operator? In the Operator Precedence from Oracle, it is separated. Reference