0
#include<stdio.h>
#include<conio.h>
void main() 
{
   char cr[] = "Yash";
   clrscr();
   printf("%p\n",(void*)cr);
   printf("%p\n",cr);
   printf("%p\n",&cr);
   printf("%p\n",&cr[0]);
   printf("%p\n",cr[0]);
   printf("%p\n",(void*)cr[0]);

   printf("%c\n",&cr[0]);
   printf("%s\n",&cr);
   printf("%c\n",(void*)cr[0]);
   printf("%s\n",(void*)cr);
   getch();
}

Output:

FFEE
FFEE
FFEE
FFEE
0059
0059
ε
Yash
Y
Yash

Question: I fail to understand exactly the output which I get in this code. Can somebody please explain each output why is it the way it is. Especially why does printf("%c",&cr[0]); give some sought of ε this weird or may be be null symbol? And why are outputs of (void*)cr[0] and cr[0] different from the other 3 in the half of %p? If they are just addresses then why different?(I am really really sorry for the last minute changes I made:/)

  • 0059 isn't an address, it's the value in hex of the character 'Y'. – jsheeran Mar 01 '18 at 15:22
  • 2
    In short: Undefined behavior. See https://stackoverflow.com/questions/16864552/what-happens-when-i-use-the-wrong-format-specifier – Support Ukraine Mar 01 '18 at 15:26
  • 3
    Using %p for something else than a pointer is UB. – Jabberwocky Mar 01 '18 at 15:30
  • 1
    OT: Since the code have undefined behavior, the output doesn't need to make any sense at all. In the real world the output often makes some kind of sense. Looking at your code and the output, I'm wondering whether the output is really from executing the posted code. Is it or did you do some last minute changes? – Support Ukraine Mar 01 '18 at 15:35
  • Is that 16 bit Turbo C? – Jean-François Fabre Mar 01 '18 at 15:35
  • @MichaelWalz Doesn't %p require not just a pointer but a void pointer? – Support Ukraine Mar 01 '18 at 15:39
  • Yes,Its TurboC3 – Yash Javeri Mar 01 '18 at 15:39
  • `printf("%s",&cr);` --> `printf` expects `&cr` to be a valid pointer to a _string_. It is not. Results: _undefined behavior_ - anything may occur. – chux - Reinstate Monica Mar 01 '18 at 15:39
  • @4386427 you're right – Jabberwocky Mar 01 '18 at 15:41
  • I can't understand why people still use this crappy antique piece of software. Turbo C was great 25 years ago when the average PC had 4 Mb of memory, but today there are better (and also free) alternatives. – Jabberwocky Mar 01 '18 at 15:45
  • @MichaelWalz If one is using a 25 year old computer, Turbo C makes sense. Ask the OP what computer they are using. Agree about many good alternatives, – chux - Reinstate Monica Mar 01 '18 at 16:03
  • @chux good point, but if he is able to use a modern web site such as stackoverflow, his computer is most likely not that old. – Jabberwocky Mar 01 '18 at 16:05
  • Yeah I know Turbo C is really a very crappy software, I have already switched to code blocks but i just wanted to understand all about void*,*,& etc in C. That is why this code and I wish to know the reason behind each output if someone could help OR some really good link for understanding the same.I am once again sorry for the last minute changes. – Yash Javeri Mar 03 '18 at 17:07

1 Answers1

3

explain each output

UB is undefined behavior: Anything may happen. "%p" expects a void*. pointers to characters and void* have same representation.

char cr[] = "Yash";
printf("%p\n",(void*)cr);     // OK:  address of CR[0]
printf("%p\n",cr);            // OK:  address of CR[0]  
printf("%p\n",&cr);           // UB,  &cr is neither a void * or pointer to a character
printf("%p\n",&cr[0]);        // OK:  address of CR[0]  
printf("%p\n",cr[0]);         // UB   cr[0] is neither a void * or pointer to a character
printf("%p\n",(void*)cr[0]);  // UB,  conversion to void* from arbitrary integer

"%s" expects a char* to a valid string

printf("%s\n",&cr);           // UB,  Not a `char *`
printf("%s\n",(void*)cr);     // OK

"%c" expects a int, converts that to a unsigned char and prints that character.

printf("%c\n",&cr[0]);        // UB, pointer passed as int
printf("%c\n",(void*)cr[0]);  // UB, pointer passed as int

why does printf("%s",&cr); give ....

printf("%s",&cr); --> printf expects &cr to be a valid pointer to a string. It is not. Results: undefined behavior - anything may occur.

why are outputs of (void*)cr[0] and cr[0] different from the other 3 in the half of "%p

printf("%p\n",(void*)cr[0]); and printf("%p\n",cr[0]); are UB.


Explaining output when UB occurs is not productive until one clearly understands why explaining UB is not generally useful.

chux - Reinstate Monica
  • 143,097
  • 13
  • 135
  • 256
  • "why does printf("%c",&cr[0]); give some sought of ...". As all ready answered, it is [undefined behavior](https://en.wikipedia.org/wiki/Undefined_behavior). Anything may happen, You seem to want to still want know why this _undefined behavior_ results in an `ε`. There are numerous possible explanations including the address had a byte in it corresponding to the character `ε`. Yet this is not a efficient use of time. One needs to know not to code `printf("%c",&cr[0]);` in the first place and 2nd, use a compiler that provides warnings to such trivial coding mistakes. – chux - Reinstate Monica Mar 04 '18 at 16:13
  • I wrote the program to understand the concept. That's it!But I agree with compiler thing as I should use Codeblocks or something. – Yash Javeri Mar 05 '18 at 10:16