Page 1 of 5 1234 ... LastLast
Results 1 to 10 of 46

Thread: C Puzzle

  1. #1

    Default C Puzzle


    ayaw ipa run ... predict lang unsa output
    naa ra ninyo kng mo cheat mo or dli

    =============

    Write the output of this program

    #include

    main()
    {
    int *a, *s, i;

    s = a = (int *) malloc( 4 * sizeof(int));

    for (i=0; i<4; i++) *(a+i) = i * 10;

    printf("%d\n", *s++);
    printf("%d\n", (*s)++);
    printf("%d\n", *s);
    printf("%d\n", *++s);
    printf("%d\n", ++*s);
    }

  2. #2

    Default Re: C Puzzle

    actually di ni mo run kai sayop imong #include heeheh sakto?

  3. #3

    Default Re: C Puzzle

    Quote Originally Posted by M@rv|n
    ayaw ipa run ... predict lang unsa output
    naa ra ninyo kng mo cheat mo or dli

    =============

    Write the output of this program

    #include

    main()
    {
    int *a, *s, i;

    s = a = (int *) malloc( 4 * sizeof(int));

    for (i=0; i<4; i++) *(a+i) = i * 10;

    printf("%d\n", *s++);
    printf("%d\n", (*s)++);
    printf("%d\n", *s);
    printf("%d\n", *++s);
    printf("%d\n", ++*s);
    }
    d mo run kay sayop ang include pero cge try lng gihapon....................liboga oi. kapoy trace. ngano gud imanual ni oi.

  4. #4

    Default Re: C Puzzle

    ahehe sagdi nalang gud na ang include ... nya aron daghan nata mag kutaw utok bah ... kaila kag ang level ani is easy lang? ahehe unsa nalang kaha ang hard noh?

  5. #5

    Default Re: C Puzzle

    Sige ko tutok ana nga program, the title is puzzle so i am trying to see where's the catch or trick there. But this is just plain program.

    main()
    {
    int *a, *s, i;

    s = a = (int *) malloc( 4 * sizeof(int)); // allocating 4 int bytes; 'a' and 's' vars points for the 1st address of the allocated memory

    for (i=0; i<4; i++) *(a+i) = i * 10; // assigning 0, 10, 20, 30 to the address of (a + 1, a + 2 and so on);

    printf("%d\n", *s++); // dereferencing or get the value of the first address, the value is=0; increment the address at the same time
    // it's the same as (*s) then s = s + 1;
    printf("%d\n", (*s)++); //dereference the 2nd address, the value is 10; increment it by 1
    // it's the same as (*s) = (*s) + 1;
    printf("%d\n", *s); // dereference the 2nd address which was increased 1, the value now is 11
    printf("%d\n", *++s); // increments the address then dereference, s = s + 1, s now is pointing the 3rd element, then dereference *s, value is 20
    printf("%d\n", ++*s); // deference s (*s), the value of the 3rd element, then increment it by 1, value is 20 + 1=21
    }


    the output should be 0, 10, 11, 20, 21

    Good exercise though.



  6. #6

    Default Re: C Puzzle

    koyawa gloryhunter oi! ahehe sakto bhay! hmmm murag dli naman tingali na explain kay naa naman explaination c gloryhunter

  7. #7

    Default Re: C Puzzle

    Next question

    Write a program to swap two integers without using 3rd integer (ie. Without using any temporary variable)

  8. #8

    Default Re: C Puzzle

    using xor swap.

    ----------
    #include <stdio.h>

    void swap(int *x, int *y);

    main()
    {
    int x = 10, y = 20;
    swap(&x, &y);
    printf("%d, %d", x, y);
    return 0;
    }

    void swap(int *x, int *y)
    {
    *x ^= *y;
    *y ^= *x;
    *x ^= *y;
    }

  9. #9

    Default Re: C Puzzle

    sakto bhay! ahehe pero ang ako answer is this :d
    hilig jud ko ug one liner ahehe
    ============

    int x, y;

    printf("enter x y: ");
    scanf("%d %d", &x, &y);

    x^=y^=x^=y;

    printf("%d, %d",x, y);

  10. #10

    Default Re: C Puzzle

    one liner is fine but i wont do it if compromise readability.

  11.    Advertisement

Page 1 of 5 1234 ... LastLast

Similar Threads

 
  1. Puzzle...
    By Sinyalan in forum Humor
    Replies: 18
    Last Post: 07-10-2009, 04:06 AM
  2. Replies: 17
    Last Post: 04-14-2007, 07:46 PM
  3. SHOCK PUZZLE - You're warned. Try to solve it...
    By ralf23 in forum Websites & Multimedia
    Replies: 2
    Last Post: 09-01-2005, 11:19 AM
  4. C Puzzle (bag o)
    By darkhero in forum Programming
    Replies: 12
    Last Post: 08-17-2005, 08:02 AM

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
We are the first Cebu Online Media.

iSTORYA.NET is Cebu's Biggest, Southern Philippines' Most Active, and the Philippines' Strongest Online Community!
follow us
#top