Find the output of the following C Apptitude (2)

rengaraj
@rengaraj-89Rrct โ€ข Oct 26, 2024

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

Welcome, guest

Join CrazyEngineers to reply, ask questions, and participate in conversations.

CrazyEngineers powered by Jatra Community Platform

  • sarveshgupta

    @sarveshgupta-txtmu5 Dec 6, 2009

    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

    @gauravbhorkar-Pf9kZD Dec 6, 2009

    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-hGOPpa Dec 6, 2009

    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-89Rrct Dec 6, 2009

    sarveshguptaCan 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

    @gauravbhorkar-Pf9kZD Dec 7, 2009

    dipen30compile 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-txtmu5 Dec 7, 2009

    rengarajSir,
    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-hGOPpa Dec 7, 2009

    I am using 16-bit compiler. so it gives output 2?

  • Saandeep Sreerambatla

    @saandeep-sreerambatla-hWHU1M Dec 7, 2009

    dipen30I 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

    @gauravbhorkar-Pf9kZD Dec 7, 2009

    dipen30I 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-txtmu5 Dec 7, 2009

    Ok gaurav.. thanks

  • dipen30

    @dipen30-hGOPpa Dec 8, 2009

    thanx gaurav & E-S

  • rengaraj

    @rengaraj-89Rrct Dec 8, 2009

    Sir,
    Have anybody tested on the compiler ???
    I hope the solution will be 6.
    R.Rengaraj

  • aishwarya mk

    @aishwarya-hnVUq3 Jul 10, 2016

    dipen30compile 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)