Closed Thread
Page 1 of 3 123 LastLast
Results 1 to 10 of 23

Thread: Got Suck with printf. Please Help

  1. #1
    nikhilkhullar is offline Newbie
    Join Date
    Nov 2009
    Location
    Jannat
    Posts
    20
    Rep Power
    0

    Got Stuck with printf. Please Help

    Hello friends,

    Please explain the output of the following code:

    #include <stdio.h>

    int main(void)
    {
    char *name = "Nikhil";
    printf("%d",2[name]);

    getchar();
    return 0;
    }



    The output is: 107.



    Regards,
    Nikhil Khullar
    Last edited by nikhilkhullar; 03-12-2010 at 04:39 AM.

  2. CODECALL Circuit advertisement
    Join Date
    Always
    Location
    Advertising world
    Posts
    Many

     
  3. #2
    lintwurm's Avatar
    lintwurm is offline Learning Programmer
    Join Date
    Mar 2010
    Location
    Pretoria
    Posts
    81
    Rep Power
    0

    Re: Got Suck with printf. Please Help

    Hi,

    What happens here is that you are displaying the integer value(ascii) of a char. The character you are displaying is the 'k' in Nikhil, because you are working with an array of characters. In other words: N = 0 in the array. i = 1 in the array etc...
    To change the output so that it displays the character instead of the integer value, just change your printf to:"printf("&#37;c", 2[name])"

    Hope that helps... ^_^

  4. #3
    abdul.gafur's Avatar
    abdul.gafur is offline Learning Programmer
    Join Date
    Mar 2010
    Location
    Salo, Riau, Indonesia
    Posts
    42
    Rep Power
    0

    Re: Got Suck with printf. Please Help

    In ascii, decimal value of 'k' is 107

  5. #4
    Join Date
    Oct 2007
    Location
    /dev/null
    Posts
    4,513
    Blog Entries
    8
    Rep Power
    59

    Re: Got Suck with printf. Please Help

    Odd syntax...
    Code:
    printf("&#37;d",2[name]);
    Should be
    Code:
    printf("%d",name[2]);
    Have you been coding in MIPS or Intel AT&T assembly language? They do that.
    Last edited by dargueta; 03-12-2010 at 02:51 AM.
    sudo rm -rf /

  6. #5
    abdul.gafur's Avatar
    abdul.gafur is offline Learning Programmer
    Join Date
    Mar 2010
    Location
    Salo, Riau, Indonesia
    Posts
    42
    Rep Power
    0

    Re: Got Suck with printf. Please Help

    But, Nikhil Khullar wrote, "The output is: 107", he did not say if program had error

  7. #6
    Join Date
    Oct 2007
    Location
    /dev/null
    Posts
    4,513
    Blog Entries
    8
    Rep Power
    59

    Re: Got Suck with printf. Please Help

    True. But I've never seen that syntax before. It's really weird.

    EDIT: WTF? It works?! I've been programming in C/C++ for going on seven years now and I've never seen anyone do that before. But apparently it works...
    Last edited by dargueta; 03-12-2010 at 02:52 AM. Reason: Grammar
    sudo rm -rf /

  8. #7
    lintwurm's Avatar
    lintwurm is offline Learning Programmer
    Join Date
    Mar 2010
    Location
    Pretoria
    Posts
    81
    Rep Power
    0

    Re: Got Suck with printf. Please Help

    Don't worry man... ^_^
    This surprised me the first time too... was about to post the same as you, but tested first ^_^
    I guess you learn something new everyday...

  9. #8
    bobdark's Avatar
    bobdark is offline Programmer
    Join Date
    Jan 2010
    Location
    Haifa, Israel
    Posts
    164
    Rep Power
    9

    Re: Got Suck with printf. Please Help

    On one hand it logical that it works because eventually its just another way to calculate the address, I mean lets call the original offset start address and the origial start address now will be the offset. On the other hand, in the calculation the index is multiplied by the size of the type, which is derevied by the compiler from the identifier. But here there is no identifier from which the type can be derived... So maybe its some default value by which the offset is multiplied? Or the size of the type is determined by some other means?

  10. #9
    Join Date
    Oct 2007
    Location
    /dev/null
    Posts
    4,513
    Blog Entries
    8
    Rep Power
    59

    Re: Got Suck with printf. Please Help

    @lintwurm: Yeah, really. Only reason why I questioned it was because I've seen the same in Intel assembly language with AT&T syntax, and MIPS assembly language, but not in C. I dunno, it's kinda weird and less readable in my opinion.

    @bobdark: Huh? My English is failing me...
    Last edited by dargueta; 03-12-2010 at 03:20 AM. Reason: Typo
    sudo rm -rf /

  11. #10
    bobdark's Avatar
    bobdark is offline Programmer
    Join Date
    Jan 2010
    Location
    Haifa, Israel
    Posts
    164
    Rep Power
    9

    Re: Got Suck with printf. Please Help

    aa nvm, what I wrote there is nonsense. My bad...

Closed Thread
Page 1 of 3 123 LastLast

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. Replies: 12
    Last Post: 01-13-2010, 07:01 PM
  2. C and C++ suck big time
    By freestyler in forum C and C++
    Replies: 6
    Last Post: 07-16-2009, 08:17 PM
  3. Printf
    By mhm1984 in forum C and C++
    Replies: 36
    Last Post: 11-24-2008, 01:38 PM
  4. Why vista won't suck! Will it rock?
    By TcM in forum The Lounge
    Replies: 23
    Last Post: 07-29-2008, 10:46 AM
  5. C printf()
    By broncoslb in forum C and C++
    Replies: 2
    Last Post: 03-06-2008, 11:54 AM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts