0

Since I assume that post increment is evaluated first before a relational operator, it will not going inside the second while loop when b=98 under the expression "while (b++ < 99)" in C programming, but it not. The relational operator is evaluted first, then increment. A little bit confusing , so I would ask why it's happened.


#include <unistd.h>

void    ft_putchar(char c)
{
    write(1, &c, 1);
}

void    ft_print_comb2(void)
{
    int a;
    int b;

    a = -1;
    while (a++ < 98)
    {
        b = a;
        while (b++ < 99)
        {
            ft_putchar((a / 10) + 48);
            ft_putchar((a % 10) + 48);
            ft_putchar(' ');
            ft_putchar((b / 10) + 48);
            ft_putchar((b % 10) + 48);
            if (a != 98)
            {
                ft_putchar(',');
                ft_putchar(' ');
            }
        }
    }
}
nyc
  • 1
  • 1
  • 5
    You know the phrase "post-increment", so it's puzzling that you don't know how this is done in C. The way it works is the value is incremented but the previous value is returned. If you want the _new_ value to be returned then use `++b`. – paddy Jul 04 '22 at 04:29
  • 1
    That's what `b++` does, use the old value of `b` first in whatever expression, then increment it. If you want to use the new value of b after increment, use `++b`. – qrsngky Jul 04 '22 at 04:29
  • Now I understand the post-increment returns the previous value of variable, and the pre-increment is used when I want to use the new value of it after increment. Thanks you a lot for your answer!! – nyc Jul 04 '22 at 04:50

0 Answers0