Jump to content

Got Suck with printf. Please Help

- - - - -

This topic has been archived. This means that you cannot reply to this topic.
22 replies to this topic

#1
nikhilkhullar

nikhilkhullar

    Newbie

  • Members
  • PipPip
  • 20 posts
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

Edited by nikhilkhullar, 12 March 2010 - 04:39 AM.


#2
lintwurm

lintwurm

    Learning Programmer

  • Members
  • PipPipPip
  • 77 posts
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("%c", 2[name])"

Hope that helps... ^_^

#3
abdul.gafur

abdul.gafur

    Learning Programmer

  • Members
  • PipPipPip
  • 42 posts
In ascii, decimal value of 'k' is 107

#4
dargueta

dargueta

    Writes binary right handed and hex left handed

  • Moderators
  • 4,717 posts
Odd syntax...
printf("%d",2[name]);
Should be
printf("%d",name[2]);
Have you been coding in MIPS or Intel AT&T assembly language? :) They do that.

Edited by dargueta, 12 March 2010 - 02:51 AM.

sudo rm -rf /

#5
abdul.gafur

abdul.gafur

    Learning Programmer

  • Members
  • PipPipPip
  • 42 posts
But, Nikhil Khullar wrote, "The output is: 107", he did not say if program had error

#6
dargueta

dargueta

    Writes binary right handed and hex left handed

  • Moderators
  • 4,717 posts
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...

Edited by dargueta, 12 March 2010 - 02:52 AM.
Grammar

sudo rm -rf /

#7
lintwurm

lintwurm

    Learning Programmer

  • Members
  • PipPipPip
  • 77 posts
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...

#8
bobdark

bobdark

    Programmer

  • Members
  • PipPipPipPip
  • 164 posts
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?

#9
dargueta

dargueta

    Writes binary right handed and hex left handed

  • Moderators
  • 4,717 posts
@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...

Edited by dargueta, 12 March 2010 - 03:20 AM.
Typo

sudo rm -rf /

#10
bobdark

bobdark

    Programmer

  • Members
  • PipPipPipPip
  • 164 posts
aa nvm, what I wrote there is nonsense. My bad...

#11
dargueta

dargueta

    Writes binary right handed and hex left handed

  • Moderators
  • 4,717 posts
So I don't suck at English? :D
sudo rm -rf /

#12
abdul.gafur

abdul.gafur

    Learning Programmer

  • Members
  • PipPipPip
  • 42 posts
That is the advantage of sharing, we can know the things that we didn't know before