-3
#include<stdio.h>

void main(){
    char p[20];
    char *s="string";
    int i;
    int length= strlen(s);
    for(i=0;i<length;i++)
        p[i]=s[length-i];  // it shows expected values when checked here, character by character.

    //But, if it's printed as string, it shows blank.
    printf("%s",p);

    //And now, again checking it character by character, it behaves like nothing is stored.
    for(i=0;i<strlen(p);i++)
        printf("%c",p[i]);
    }
}

It prints nothing. When traversing the for loop and checking variable p's value, it shows expected values; but it doesn't hold anything when checked outside the for loop. And then Why?

Will
  • 24,082
  • 14
  • 97
  • 108
  • 2
    `void main()` is implementation-defined in C and is illegal in global namespace in standard C++. You should use standard `int main(void)` unless you have some special reason to use special form of `main`. – MikeCAT Jun 02 '16 at 15:17
  • 1
    @MikeCAT: `void main()` is not a valid signature in C, not just _implementation defined_. The standard is quite clear about this. And there is not just `int main(void)`, but also `int main(int argc, char **argv)`. – too honest for this site Jun 02 '16 at 15:23
  • @Olaf No, he is correct, `void main()` can be perfectly fine in some implementations, most notably all freestanding implementations. The standard is certainly not clear at all, it is quite vague. [See this](http://stackoverflow.com/a/31263079/584518). I have some references to the C99 rationale etc to show why the standard is vague. – Lundin Jun 02 '16 at 15:30
  • 1
    @Lundin: I know very well, but I strongly doubt this is a _freestanding environment_. But agreed, I should have been more clear about that. – too honest for this site Jun 02 '16 at 15:33
  • @Olaf The C standard is not even clear about which forms that can exist on a hosted system. Check the link I posted. The C99 rationale contradicts sections in the standard. This part of the standard is simply very poorly written. – Lundin Jun 03 '16 at 06:19
  • @Lundin: Agreed. But OS/Environment may add additional restrictions, e.g. POSIX. Typically, they allow additional parameters, and the `void` form (never saw `int main(int argc)` actually using `argc`, becauseit makes little sense without `argv`, too. I tend to accept omitting arguments from the right, but not different arguments. But a return value of `void` is not acceptable in any variant IMO. – too honest for this site Jun 03 '16 at 16:06
  • @Lundin: Another hint the standard does not mean "all is allowed" is that it explicitly states it for freestanding environments. – too honest for this site Jun 03 '16 at 16:45

3 Answers3

2

Basically, you are copying s[length - 0], which copies the string terminator character \0 at the beggining of your new string. So when it is going to be printed, it will stop at the first character.

EDIT:

And, as Sourav Ghosh pointed out, the second strlen() will return 0

Nadir
  • 1,799
  • 12
  • 20
2

The first assignment p[i]=s[length-i] sets p[0] to the null-terminator that finishes the string s.

Hence attempting to use printf on the array p will not output anything.

Bathsheba
  • 231,907
  • 34
  • 361
  • 483
1

s[length-i] will be '\0', which is at the end of string, when i = 0.

Seeing it, printf("%s",p); will print zero-character string and strlen(p) will return zero, so the body of second loop will be executed zero times.

MikeCAT
  • 73,922
  • 11
  • 45
  • 70