CrazyEngineers
  • Find the output of the following C Apptitude (2)

    rengaraj

    Member

    Updated: Oct 26, 2024
    Views: 1.1K
    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.}
    0
    Replies
Howdy guest!
Dear guest, you must be logged-in to participate on CrazyEngineers. We would love to have you as a member of our community. Consider creating an account or login.
Replies
  • sarveshgupta

    MemberDec 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?
    Are you sure? This action cannot be undone.
    Cancel
  • gaurav.bhorkar

    MemberDec 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).
    Are you sure? This action cannot be undone.
    Cancel
  • dipen30

    MemberDec 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.
    Are you sure? This action cannot be undone.
    Cancel
  • rengaraj

    MemberDec 6, 2009

    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
    Are you sure? This action cannot be undone.
    Cancel
  • gaurav.bhorkar

    MemberDec 7, 2009

    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?
    Are you sure? This action cannot be undone.
    Cancel
  • sarveshgupta

    MemberDec 7, 2009

    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?
    Are you sure? This action cannot be undone.
    Cancel
  • dipen30

    MemberDec 7, 2009

    I am using 16-bit compiler. so it gives output 2?
    Are you sure? This action cannot be undone.
    Cancel
  • Saandeep Sreerambatla

    MemberDec 7, 2009

    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.
    Are you sure? This action cannot be undone.
    Cancel
  • gaurav.bhorkar

    MemberDec 7, 2009

    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.
    Are you sure? This action cannot be undone.
    Cancel
  • sarveshgupta

    MemberDec 7, 2009

    Ok gaurav.. thanks
    Are you sure? This action cannot be undone.
    Cancel
  • dipen30

    MemberDec 8, 2009

    thanx gaurav & E-S
    Are you sure? This action cannot be undone.
    Cancel
  • rengaraj

    MemberDec 8, 2009

    Sir,
    Have anybody tested on the compiler ???
    I hope the solution will be 6.
    R.Rengaraj
    Are you sure? This action cannot be undone.
    Cancel
  • aishwarya mk

    MemberJul 10, 2016

    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)
    Are you sure? This action cannot be undone.
    Cancel
Home Channels Search Login Register