Results 1 to 5 of 5

Thread: C Code Snippet

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Junior Member
    Join Date
    May 2008
    Answers
    1

    Thumbs up C Code Snippet

    [FONT='Times New Roman','serif']Which is the output produced by the following program
    main()
    {
    int n=2;
    printf("%d %d\n", ++n, n*n);
    }
    a) 3,6 b) 3,4 c) 2,4 d) cannot determine[/FONT]


  2. #2
    Junior Member
    Join Date
    May 2008
    Answers
    1

    Re: C Code Snippet

    c)2,4
    printf first evaluates n*n and then n++.


  3. #3
    Junior Member
    Join Date
    Jun 2008
    Answers
    1

    Re: C Code Snippet

    The C language, by default, uses the CDECL calling convention, but most compilers allow the programmer to specify another convention via a specifier keyword. These keywords are not part of the ISO-ANSI C standard, so you should always check with your compiler documentation about implementation specifics.

    In the CDECL calling convention the following holds:
    1.Arguments are passed on the stack in Right-to-Left order.
    2. The calling function cleans the stack. This allows CDECL functions to have variable-length argument lists (aka variadic functions).

    In the STDCALL calling convention the following holds:
    1. STDCALL passes arguments right-to-left.
    2. The called function cleans the stack, unlike CDECL. This means that STDCALL doesn't allow variable-length argument lists.


    There is another convention FASTCALL, This uses the arguments to be stored in registers to some extent and on to stack after that.


    main()
    {
    int n=2;
    printf("%d %d\n", ++n, n*n);
    }

    it is equivalent to
    printf("%d %d\n", 3, 2*2);
    So output is 3, 4


  4. #4
    Junior Member
    Join Date
    May 2008
    Answers
    8

    Re: C Code Snippet

    answer is 3,4.....
    coz the execution starts from right...
    hence n*n is 2*2=4...
    then ++n is ++2=3 ..ie..preincrement...
    so getting o/p as 3,4...


  5. #5
    Expert Member
    Join Date
    Dec 2007
    Answers
    138

    Re: C Code Snippet

    The answer is b option, (3,4)


Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
About us
Applying for a job can be a stressful and frustrating experience, especially for someone who has never done it before. Considering that you are competing for the position with a at least a dozen other applicants, it is imperative that you thoroughly prepare for the job interview, in order to stand a good chance of getting hired. That's where GeekInterview can help.
Interact