FAQ Database Discussion Community


Difference between pre and post decrement in recursive function argument

c++,recursion,post-increment,pre-increment
I have following sample code where i used pre-decrement void function(int c) { if(c == 0) { return; } else { cout << "DP" << c << endl; function(--c); cout << c << endl; return; } } This function give output for input 4 as : DP3 DP2 DP1 DP0...

post and pre increment in for loop. string length without library function in C

c,post-increment,pre-increment
I am trying to find Length of a string w/o using library function. char card[16]; //card number in char array. unsigned int cardno[16]={0}; //card number in int array for calculations. int i,length=0,len; printf("Credit Card Number[without any spaces]: "); gets(card); for(i=0;card[i]!='\0';i++); len=i; length=strlen(card); printf("%d %d",len,length); But if i enter a 16...

Is post-increment operator guaranteed to run instantly?

c,c89,post-increment,ansi-c
Let's say I have the following code: int i = 0; func(i++, i++); The increment is happening right after returning the value? Is it guaranteed that the first argument will be 0, and the second argument will be 1?...

What are the historical reasons C languages have pre-increments and post-increments?

c++,c,post-increment,decrement,pre-increment
(Note: I am not asking about the definitions of pre-increment vs. post-increment, or how they are used in C/C++. Therefore, I do not think this is a duplicate question.) Developers of C (Dennis Ritchie et al) created increment and decrement operators for very good reasons. What I don't understand is...

Why is “ref” not assigning updated value to the next field?

c#,.net,pass-by-reference,ref,post-increment
I've found a really cool feature of a compilator. However, I cannot understand logic of this behaviour. static int IncrementValue(ref int i) { return i++;} and Main method: static void Main(string[] args) { int a = 2; int b = IncrementValue(ref a); Console.WriteLine(a+b); } The output is 5. My question...

Bash arithmetic expression's side effects not executed

bash,post-increment,arithmetic-expressions,side-effects
$ declare -i i=0 $ for j in {0..2}; do echo "${j} $((i++))"; done 0 0 1 1 2 2 $ for j in {0..2}; do echo "$(echo "${j} $((i++))")"; done 0 3 1 3 2 3 $ Why i doesn't get incremented in the 2nd for loop? (Yes, I...

i++ vs. ++i in a JavaScript for loop

javascript,for-loop,post-increment,pre-increment
Because of JSLint, I almost always use i += 1 to increment a JavaScript for loop, but for quick and dirty scripts, I use i++ instead. However, I see a lot of for loops in other people's code in which they increment i by doing ++i instead. As far as...

post and pre increment not working as expected for REPL variable in scala

java,scala,compiler-errors,integer,post-increment
I know that there is no ++ and -- in scala instead I have to use += and -= But when I try scala> var a=2 a: Int = 2 scala> a +=1 scala> a res11: Int = 3 the above works fine but not the below one scala> 5...