-6
#include<stdio.h>
int main()
{
    switch(*(1+"AB""CD"+1))
    {
        case'A':printf("Pulp Fiction");
              break;
        case'B':printf("12 Angry Man");
              break;
        case'C':printf("Casabance");
              break;
        case'D':printf("Blood Diamond");
    }
    return 0;

}

This is one C code and it gives output as per the third character in the given string . "AB""CD" gives Casabance , "AB""DD" gives Blood Diamond . I would like to know how the expression in the switch statement calculated ?

While , I tried another code,

#include<stdio.h>
int main()
{
    const char ch=*(1+"AB""AD"+1);
    printf("%c",&ch);
    return 0;
}

This code gives a output 'f' for any changes i make to the string . I would like to know how this expression is evaluated ?

And why do both above codes differ ??

Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335
jeet
  • 9
  • 3

2 Answers2

2

There is an important mistake here

#include <stdio.h>
int main()
{
    const char ch = *(1 + "AB""AD" + 1);
    printf("%c", &ch);
    /*           ^ WRONG ... */
    return 0;
}

You are passing char * where char is expected, that would invoke undefined behaviour. The printf() family of function does not define a behaviour when you pass a parameter that does not match the corresponding specifier.

In this case the parameter is a pointer, so

printf("%p", (void *) &ch);

would print the address of ch, but if you want to print the character do

printf("%c", ch);

Also, 1 + "AB""CD" + 1 is and forgive me a ridiculous way of writing "ABCD" + 2 it's not wrong and it's 100% valid but it makes you think why, why someone would waste time doing this?

Iharob Al Asimi
  • 52,653
  • 6
  • 59
  • 97
  • Can you point out the mistake?? And how is the expression evaluated ? – jeet May 29 '16 at 15:11
  • @jeet the mistake has nothing to do with the expression, it's pretty much the same as `const char *sting = "ABCD"; switch (string[2])`. – Iharob Al Asimi May 29 '16 at 15:14
  • I had written (1 + "AB""CD" + 1) just for the purpose of checking that the numbers are added normally not . In Java the expressions are type-casted according to the string . Like "A"+1 in a out.print() statement will convert 1 to String type . – jeet May 29 '16 at 15:22
  • Whell in [tag:c] they're not. Java and c are very different languages, you need to read about pointer arithmetic. – Iharob Al Asimi May 29 '16 at 15:27
2

In this switch statement

switch(*(1+"AB""CD"+1))

first of all string literals "AB" and "CD" are concatenated by the compiler and as result it looks like

switch(*(1+"ABCD"+1))

String literals are stored as zero-terminated character arrays. For example in C string literal "ABCD" has type char[5] (In C++ string literals have types of constant character arrays. So in C++ string literal "ABCD" has type const char[5]).

In expressions arrays are implicitly converted to pointers that point to the first characters of the arrays.

Thus in this expression 1+"ABCD"+1 that can be written simpler like

2+"ABCD" 

or

"ABCD" + 2

there is used the so-called pointer arithmetic. The result of the expression is pointer that points to the third element of the string literal and has type char *.

Thus applying dereferencing you get this third character of the string literal.

In this code snippet

const char ch=*(1+"AB""AD"+1);
printf("%c",&ch);

there is used the same string literal and object ch of type const char that is initialized by the same third character of the string literal.

However in the statement

printf("%c",&ch);

you are trying to output the address of the object as a character. It would be correctly just to write

printf( "%c", ch );
Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335