-6

There are actually two ways i can do this. One is to use the increment operator ++ and decrement operator –. For example, the statement “x++” means to increment the value of x by 1. Likewise, the statement “x –” means to decrement the value of x by 1. Another way of writing increment statements is to use the conventional + plus sign or – minus sign. In the case of “x++”, another way to write it is “x = x +1″.

But why am i incrementing like this in my code and what does it mean?

    for(i=0; i < numberOfProducts; ++i){

            printf("Enter Product Name: ");


            scanf("%s", &(pProducts+i)->productName);

            printf("Enter Product Price: ");

            scanf("%f", &(pProducts+i)->price);



}

My question is why did i use ++i for it to work? i tried i++ but could not print.

ALICE
  • 7
  • 3
  • Please clarify your specific problem or add additional details to highlight exactly what you need. As it's currently written, it’s hard to tell exactly what you're asking. See the How to Ask page for help clarifying this question. – too honest for this site Jul 07 '15 at 13:58
  • 1
    You should know what you want to do **before** you start programming. – too honest for this site Jul 07 '15 at 13:59
  • 3
    "*why am i incrementing like this in my code*" What the hell? You mean you wrote code and then you come here to ask us what YOU did and why? – Eregrith Jul 07 '15 at 13:59
  • @Eregrith - you are making an assumption. The OP could have copied it from another student before it was complete and/or debugged – Martin James Jul 07 '15 at 14:13
  • @MartinJames When someone says "Why am I [x] in my [y]" I think it's safe to assume they are saying **they** did [x] in their [y]. – Eregrith Jul 07 '15 at 14:14
  • What does the `x++` is the same as `x = x + 1` explanation have to do with this code? (And the people who are going to answer your question hopefully knows this already, you need not explain fundamental things to them...) – Lundin Jul 07 '15 at 14:14
  • @MartinJames Why would he say *I* and *my* instead of "Why is [that guy] doing [that thing] in *his* code?" – Eregrith Jul 07 '15 at 14:18
  • That code should run the same regardless of whether you pre- or post-increment. The problem must lie elsewhere. – molbdnilo Jul 07 '15 at 14:19
  • I might be bad in asking questions--- but you dont need to talk to me like that... I know it is programming i want. I passed so well in exams and it is my first year in college... looks like this is not the place for first learners, so unfortunate. All this things are new to me. – ALICE Jul 07 '15 at 14:22
  • [Duplicate](http://stackoverflow.com/questions/24853/what-is-the-difference-between-i-and-i). – Lundin Jul 07 '15 at 14:23
  • Why is it that some years back someone asked the same question and got 333 votes up? (In C, what is the difference between using ++i and i++, and which should be used in the incrementation block of a for loop?) this was my question. I have googled but cant get a clear understanding... mostly i loved this site because they offer example codes to look and peruse from.. thats all. – ALICE Jul 07 '15 at 14:25
  • @ALICE I hope that link answers your question. And it gets upvotes because the Stack Overflow community is picky with the question and answer format... it is not a programming forum, so people expect you to be straight to the point and ask specific questions, as described [here](http://stackoverflow.com/help). – Lundin Jul 07 '15 at 14:31
  • @ALICE In general I would recommend looking through the [frequent C questions](http://stackoverflow.com/questions/tagged/c?sort=frequent&pageSize=50) as there is lots of good reading there. I would also recommend the [comp.lang.c FAQ](http://c-faq.com/). – Lundin Jul 07 '15 at 14:32
  • thank you soo much for this. I now know better. – ALICE Jul 07 '15 at 14:38

2 Answers2

0
for(i=0; i < numberOfProducts; ++i)

and

for(i=0; i < numberOfProducts; i++)

are both equivalent as you are not reading the result of the operation (like in a = i++ vs a = ++i). The latter form is more common.

If you have different results, you probably have issues in the way you are testing your program.

ouah
  • 142,963
  • 15
  • 272
  • 331
  • Thank you, I now see why i was getting confused. It is my first year in programming--- i passes my exams very well and this is what i want to do. thank you @ouah – ALICE Jul 07 '15 at 14:20
  • @ALICE You are welcome! – ouah Jul 07 '15 at 14:45
0

++i is a pre-increment operation, meaning that i is first incremented then the incremented value is used in an expression. i++ is a post-increment operation, meaning that the existing value of i is first used in an expression, then it is incremented.

For example:

i=3;
x = 6 - i++;
printf("x=%d\n",x);

Outputs 3.

i=3;
x = 6 - ++i;
printf("x=%d\n",x);

Outputs 2.

dbush
  • 205,898
  • 23
  • 218
  • 273