Find the output of the following C Apptitude (2)

Sir / madam,
My question is

Find the output of the following

void main()
{
char *s="\12345s\n";
printf("%d",sizeof(s));
}
Advance Thanks,
R.Rengaraj
{Java Certification Quiz}
{On this forum my 22nd Question.}

Replies

  • sarveshgupta
    sarveshgupta
    Can you please tell that don't we specify char with ' ' rather than " "

    is it because we have taken it as a char pointer

    seeing as if it can be said that the output will be 6

    Is it the right answer?
  • gaurav.bhorkar
    gaurav.bhorkar
    Can you please tell that don't we specify char with ' ' rather than " "

    is it because we have taken it as a char pointer

    seeing as if it can be said that the output will be 6

    Is it the right answer?
    The variable s is a pointer to a string. (its because of "").

    The answer may be 4 as any pointer is 4 bytes long (correct me if I'm wrong).
  • dipen30
    dipen30
    compile this code and run it.

    It gives the output 2.

    can anyone explain how the output is 2? char contain 1 byte.
  • rengaraj
    rengaraj
    sarveshgupta
    Can you please tell that don't we specify char with ' ' rather than " "

    is it because we have taken it as a char pointer

    seeing as if it can be said that the output will be 6

    Is it the right answer?
    Sir,
    How you say the output is 6 ? can you please explain.
    R.Rengaraj
  • gaurav.bhorkar
    gaurav.bhorkar
    dipen30
    compile this code and run it.

    It gives the output 2.

    can anyone explain how the output is 2? char contain 1 byte.
    The output depends on the compiler you use. 16 bit compilers store the pointers as 2 byte entity.

    Which compiler you used?
  • sarveshgupta
    sarveshgupta
    rengaraj
    Sir,
    How you say the output is 6 ? can you please explain.
    R.Rengaraj
    Actually i counted the characters in the string

    As we are calculating the length of the string so i counted the length of the string

    but if we see for a normal char we know that in java char occupies 2 bytes

    so if it has to consider char the output should be 2

    Am I right?
  • dipen30
    dipen30
    I am using 16-bit compiler. so it gives output 2?
  • Saandeep Sreerambatla
    Saandeep Sreerambatla
    dipen30
    I am using 16-bit compiler. so it gives output 2?

    Yes the pointer is 2 bytes irrespective of the type it is pointing to.

    Depends on the compiler though.

    Yes Gaurav said 16 bit gives 2 bytes.

    32 bit gives 4 bytes.
  • gaurav.bhorkar
    gaurav.bhorkar
    dipen30
    I am using 16-bit compiler. so it gives output 2?
    E_S is exactly right. A pointer is 2 bytes long in 16-bit compilers, and 4 bytes long in 32-bit compilers.

    @sarveshgupta: Remember, it is not a character string, its a pointer to a character string.
  • sarveshgupta
    sarveshgupta
    Ok gaurav.. thanks
  • dipen30
    dipen30
    thanx gaurav & E-S
  • rengaraj
    rengaraj
    Sir,
    Have anybody tested on the compiler ???
    I hope the solution will be 6.
    R.Rengaraj
  • aishwarya mk
    aishwarya mk
    dipen30
    compile this code and run it.

    It gives the output 2.

    can anyone explain how the output is 2? char contain 1 byte.
    answer is 2
    because char data type 1 byte for * and another byte for s(address of array)

You are reading an archived discussion.

Related Posts

Hello Everyone, I just wanted to introduce myself. My name is Bill and I'm from the Northeast US. I'm currently studying civil engineering and I work with a firm that...
i found a video in utube on an episode of planet mechanics where these people use wind power to run a moped, the episode was really interesting. compressed air cylinder...
i'm an ECE 6th sem stdnt.....i want to know how is it possible to interface PC to LED/LCD in order to form a graphic display...
Twitter co-founder Jack Dorsey announced his new company on Tuesday in appropriate fashion: with a tweet on the network he created in 2006. His new company, Square, allows merchants and...
The draw says it will be South Africa playing hosts to the FIFA world cup next year The 32 teams participating at next year's FIFA World Cup™ finals discovered their...