0
main()
{
  char *x="girl";
  int n,i;
   n=strlen(x);
  *x=x[n];
  for(i=0;i<n;i++)
  {
   printf("%s \n",x);
    x++;
  }
}

What is the output?
Please explain the output.......................
o/p is :

irl
rl
l
3
  • 2
    Result = UB = Undefined Behavio(u)r Commented May 7, 2011 at 14:03
  • Change char*x to char x[] to avoid undefined behaviour. Commented May 7, 2011 at 14:04
  • @missingno: there are more UB issues with the code; the modifying a string literal is not alone Commented May 7, 2011 at 14:10

4 Answers 4

4

The output is undefined behaviour. You modified a string literal.

Sign up to request clarification or add additional context in comments.

2 Comments

... and called a variadic function without a prototype in scope; and didn't return a value from main (the code is not C99 (which would accept a main without a return) because C99 forbids implicit int).
@Adi: You only changed the word in the string. That doesn't change the UB.
1

As others have pointed out, the program as written has undefined behaviour.

If you make a small alteration:

char x[] = "girl";

then I believe it is legal, and possible to explain. (EDIT: Actually there are still problems with it. It's int main(), and you should return 0; at the end. You also need to #include <string.h> because you are using strlen, and #include <stdio.h> because you are using printf.)

The line

*x = x[n];

sets x[0] (i.e. *x) to x[4] (which happens to be the string terminator '\0'). Thus the first string to be printed is the empty string, because the very first character is the string terminator.

We then loop through the string, one character at a time, printing the substrings:

irl
rl
l

Comments

1

While the result is undefined behavior, as DeadMG said, let's assume you declared x as char x[] = "girl".

You assign 4 to n (since the length of the word "girl" is 4), and you assign the value in x[4] to *x (which is x[0]), but this value is '\0' (null terminator)

Now you loop and print the word from x to the next null terminator, but the first time, the first char is the null terminator, so you get nothing. after that you print the word from incrementing index.

g   i    r   l   \0

*x = x[4]:

\0   i    r    l   \0
^    ^    ^    ^
it1  it2  it3  it4      // << Where does x points to in each iteration of the for loop

Comments

0

The code is distictly suspect. *x=x[n] attempts to overwrite the literal "girl", and the effect will vary between platforms and compilers. More correctly it should be declared as:

const char *x = "girl";

and then it will not (should not) compile.

3 Comments

More correctly he should be making it safe to modify the string, not forcing his entire program to break by making the original non-mutability of the string compiler-checked.
Huh? How is it more correct to write code which will not compile?
@Simon: it's "more correct" in the sense that it won't allow him to modify a string literal; IOW, the compiler will catch those kinds of errors for him.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.