0

The code which is given below was asked by my teacher. It is just to test who can figure out the output with out running the code. For compiling Turbo C++ was used.

I am unable to understand how incrementing and decrementing happens in a function call and weather values are passed from left to right or right to left in the below code.

#include<stdio.h>
#include<conio.h>

int main(){
    char c[]="hello";
    char *p;
    clrscr();
    p=&c[0];
    printf("%c %c %c ",++*p,*++p,*p++);
    puts(c);
    getchar();
    return 0;
}

According to me the output is : iel iemlo

but when it compiled and run I got : m l h hemlo Why is this happening is the values are passed from right to left or some thing else ?

Thanks in advance.

Anshu
  • 1,277
  • 2
  • 13
  • 28
Udesh
  • 2,415
  • 2
  • 22
  • 32
  • Would u like to answer why the output is coming like this ? The link which u send went over my head. – Udesh Jun 25 '19 at 17:18
  • 2
    Pazival Because the compiler is allowed to evaluate the arguments in any order –  Jun 25 '19 at 17:23
  • So my output is also correct? – Udesh Jun 25 '19 at 17:25
  • 1
    suggest reading: [operator precedence in C](https://www.tutorialspoint.com/cprogramming/c_operators_precedence.htm) which includes the info on the order of evaluation of the operators – user3629249 Jun 25 '19 at 17:49
  • 1
    regarding *According to me the output is : iel iemlo* there are 3 single character outputs in the statement; `printf("%c %c %c ",++*p,*++p,*p++);` so the output can never be `iel iemlo*` – user3629249 Jun 25 '19 at 17:53
  • I know about operator precedence I am unable to understand the above program output. – Udesh Jun 25 '19 at 17:54
  • ielmo is the output of puts(c). – Udesh Jun 25 '19 at 17:58
  • Suggest reading about [sequence points](https://stackoverflow.com/questions/3575350/sequence-points-in-c) and in the C standard document – user3629249 Jun 25 '19 at 18:03
  • @DevParzival It could even print "99 bottles" and it would be "correct". When you invoke undefined behaviour there are no incorrect results as the program is allowed to do literally anything. "*behavior, [...] for which this International Standard imposes no requirements*" semi-oficcialy in can give you the famous [nasal demons](http://catb.org/jargon/html/N/nasal-demons.html). In that sense your question doesn't make one, unless you are asking what does CPU exactly do in your case i.e. what's the reason behind that particular manifestation. – luk32 Jun 25 '19 at 18:23
  • @user3629249 it's undefined behaviour so anything can happen, including outputting `iel iemlo*` – M.M Jun 26 '19 at 03:39

1 Answers1

1

running the OPs posted code results in:

m l h hemlo

However, the compiler output was:

gcc    -ggdb -Wall -o "untitled" "untitled.c"  -lncurses -lrt -lpthread -lm -lgmp 
untitled.c: In function ‘main’:
untitled.c:11:32: warning: operation on ‘p’ may be undefined [-Wsequence-point]
printf("%c %c %c ",++*p,*++p,*p++);
                              ~^~
untitled.c:11:32: warning: operation on ‘p’ may be undefined [-Wsequence-point]
Compilation finished successfully.

due to the undefined problems, the actual output could be anything

user3629249
  • 16,402
  • 1
  • 16
  • 17