0

I am solving a problem that reads a set of text lines and prints the longest. The problem is from K&R "The C programming language" section 1.9. The book provides a solution but I tried to solve it in my own way. The code below works but before I get it to work, I was getting a problem due to the line longestStr[i++] = tmpStr[j++] as I have previously used longestStr[i++] = tmpStr[i++] thinking that i would be incremented once the assignment was done. But that was not the case. Is this how postfix operator normally works?

#include <stdio.h>
#define MAXLINE 100


main()
{
    int lineLength = 0, longestLine = 0;
    int c, i, j;
    char longestStr[MAXLINE];
    char tmpStr[MAXLINE];

    while((c = getchar()) != EOF)
    {
        tmpStr[lineLength++] = c;
        if(c == '\n')
        {
            if( lineLength > longestLine)
            {
                longestLine = lineLength;
                i = 0, j = 0;
                while(i < lineLength)
                {
                    longestStr[i++] = tmpStr[j++]; // I tried longestStr[i++] = tmpStr[i++] but it gives wrong result
                }               
            }
            lineLength = 0;
        }       
    }
    printf("Longest line is - %d long\n", longestLine-1);
    for(i = 0; i < longestLine-1; i++)
        printf("%c", longestStr[i]);
    putchar('\n');
}
didierc
  • 14,572
  • 3
  • 32
  • 52
Abraham Guchi
  • 181
  • 1
  • 2
  • 11

4 Answers4

3

The postfix increments or decrements are operators that perform the increment or decrement after the use of the variable, such that if you wish to use a integer value to print out and add it at the same time you would use postfix.

int i = 0;
printf("%d",i++);
//prints out 0

Prefix increment/decrement however works in the opposite manner such that it performs the increment/decrement prior to the use of the variable, such that if you wish to increment/decrement a variable before printing it out, you would use prefix

int i = 0;
printf("%d",++i);
//prints out 1
Mat
  • 202,337
  • 40
  • 393
  • 406
AJ J
  • 539
  • 7
  • 11
1

try to understand how post fix increment works.

f(i++) is equivalent to the operation 'call f on the current value of i, then increment i'

For example, when you use i++ twice, and initial value of i = 1, a(i++) = b(i++) means, a(1)=b(2) and the value of i after the operation is i=3.

If you want to eliminate one variable in what you are trying to do, you have to make sure you use increment only once. Do it like, a(i)=(b(i); i++

codetantrik
  • 315
  • 2
  • 9
  • 1
    That might make intuitive sense (well, I'd expect `a(2) = b(1)` but whatever) but actually it's undefined behavior. – Ismail Badawi Jun 30 '13 at 07:30
  • i know this is undefined behavior. I just wanted the to give the OP an idea of how the post fix increment is happening. My proposed solution is actually a way out of this undefined behavior. – codetantrik Jun 30 '13 at 07:35
  • But that's not necessarily how the postfix increment is happening, because it's undefined behavior. – Ismail Badawi Jun 30 '13 at 07:40
1

The problem you have is not related to how the postfix operator works, but rather to what you intend to do in the code line which gives you a problem.

As asked in the comments, what is the meaning of the line as you wrote it initially?

longestStr[i++] = tmpStr[i++];

Because the C standard does not specify it, this line can be interpreted in several ways:

longestStr[i] = tmpStr[i+1];
i += 2;

or

longestStr[i+1] = tmpStr[i];
i += 2;

or

longestStr[i] = tmpStr[i];
i += 2;

In every case, you end up with a counter incremented twice, which will mess up your algorithm.

The correct way is to increment the counter in a separate line, or use the working solution (you are providing), which should be compiled down to the same code with any decent compiler.

Note that you should probably check for the counter not going beyond the maximum allowed line size MAXLINE (unless the problem states that this cannot happen with the input, but even in that case, it would be helpful for situations like yours, where the code wrongly increment the counter twice).

didierc
  • 14,572
  • 3
  • 32
  • 52
  • Thanks. There are more confusing things in the book actually.For example the author's solution has, ----- "for (i=0; i < lim-1 && (c=getchar())!=EOF && c!=’\n’; ++i) s[i] = c;" ---- I would think this to start at s[1] but it is not! – Abraham Guchi Jun 30 '13 at 07:46
  • strings in C are indexed from `0` upward. This is the case in most programming languages in fact. – didierc Jun 30 '13 at 07:52
  • Thanks @didierc and I know that. I was wondering about the ++i as I feel it should have been i++. – Abraham Guchi Jun 30 '13 at 08:00
  • ah, my bad. In for loops, the three clauses are executed independently of the loop body: either `++i` or `i++` will lead to the same. – didierc Jun 30 '13 at 08:03
0

Whereas the postfix increment operator evaluates the original value, it is understandable to think the actual incrementating doesn't happen until sometime down the road. However this is not the case. You should conceptualize postfix as if the incrementing is happening while the original value is being returned.

The bigger issue though is that your statement increments i twice. This means i will increase by two with each iteration of your loop. Futhermore your index values will be off by one from each other, That's because if a variable changes during one part of statement evaluation, those changes become instantly visible to parts of the statement which have not yet evaluated.

Eric Bond
  • 1
  • 1