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
0
votes
1 answer

Preincrement operator resulting in different output than post increment operator in C code

In the code snippet below the pre-increment operator used in the main function results in values starting from 2 while the post increment values start from 1 when inserting to the list. I am unable to figure out why. #include #include…
Mukul Mehra
  • 91
  • 1
  • 9
0
votes
1 answer

Segmentation fault core (Core dumped) in C

I am running a code which is throwing me segmentation fault (Core dumped) error. I have a function which returns an unsigned integer data type of strlen which accepts a char datatype of a de-referenced variable pointer or a variable of char datatype…
Revanth Tv
  • 63
  • 8
0
votes
1 answer

How does the value get assigned in the array in that specific index?

When I run the following code, I get the output: 0 0 2 0 0 int main(){ static int var[5]; int count=0; var[++count]=++count; for(count=0;count<5;count++) { printf("%d ",var[count]); } return 0; }
0
votes
1 answer

Output is 23.Please explain

This is my code snippet. class Example{ public static void main(String[] args) { int a=10; int x; x= ++a + ++a; System.out.println(x); } } Output is 23. Need little help.
sha sha
  • 115
  • 9
0
votes
2 answers

Undefined behavior while using increment operator

I am new to C, i have an Increment operator program in C #include main(){ int a, b; a = 2; b = a + ++a + ++a; printf("%d", b); getchar(); } The output is 10, can someone explain me how the output will be 10 .
useCase
  • 1,265
  • 6
  • 22
  • 41
0
votes
0 answers

Pre and Post increment in C++ in cout statement

#include using namespace std; int main(){ int t; t=1; cout<<"--1--"<<"\n"; cout<<++t + ++t<<"\n"; t=1; cout<<++t <<"\t"<< ++t<<"\n"; t=1; cout<<"--2--"<<"\n"; cout<<++t + t++<<"\n"; t=1; …
Aakash Shivanshu
  • 11
  • 1
  • 1
  • 3
0
votes
0 answers

Post and Pre increment

The output of the following code int k=10; int g=5; int s=g+k++; cout<
ZubinShah
  • 11
  • 1
0
votes
1 answer

Incrementing pointer on string results in pointer to char not on string

I've got this code to get familiar with C: char txt[] = "Thanksfor4lltheFish"; char *ptr = &txt[2]; printf("%c\n", ++*ptr++); I expected to increase the pointer by 2 and print "k" but I get "b" which isn't even in this string. Why?
0
votes
0 answers

How ++x and x++ function TOGETHER in an equation in C++

I've been trying to make sense of the functionalities of ++x and x++, but the minute I feel like I've figured it out, something new comes up that doesn't make sense with the previous rules. So as far as I know, when using (++x),first the value of x…
mitrafrz
  • 51
  • 6
0
votes
2 answers

Increment expression for loop

I don't understand, why in for loop incrementation(i++) is executed on second time and ignored on the first iteration? I tried searching this question but can not understand. for(int i=1; i<=10; i++) { cout<<"hello, World"); } your answer would…
0
votes
2 answers

Unary increment operator madness

Could someone give me please a way to debug the following examples: public class Example1 { public static void main(String[] input) { int i = 0; // i = i++ + i++; // prints 1 // i = i++ + i++ + i++; // prints 3 …
0
votes
1 answer

Pre increment operator and dereference operator resulting in segmentation fault, can't seem to understand why

Found the following piece of code given in a test which asked to figure out the output. #include int gate(char *P) { char *q = P; q++; *q++; ++*q; return(q-P); } int main() { char *s = "gateexam"; int x =…
0
votes
3 answers

Why values of i and j are 2 after the execution of statement " j= i++ + ++i"?

The code I used is int i=0, j=0; j=i++ + ++i; And the output I got is i=2 and j=2 Could anyone explain how this happens!
Jahnavi
  • 11
  • 3
0
votes
1 answer

Why is my code running even though I'm assigning 4 values on a 3 sized array?

Btw, im fairly new to coding :) Basically I'm trying to do a program which asks the student how many grades he has (div), and calculates the overall grade (nota=grade, im portuguese). Note that in every try I always input '3' and the value for div…