-1

Can anybody explain the variations in printf syntax..i am confused in some of it like

 printf(5+"hello my friend");//i have problem this addition of 5 
 printf("hello""my""friend");//i have problem with double apostrophe

what kind of printf prototype do these follows ? Is this have anything to do with dynamic linking? Can anybody show some other weird printfs and explain them.

Andy G
  • 19,232
  • 5
  • 47
  • 69
mrigendra
  • 1,472
  • 3
  • 19
  • 33
  • You really have no idea what you are doing. (Prototypes? Dynamic linking? What gives?) Before trying to be clever, just read a good beginner C tutorial. –  Sep 08 '13 at 17:34
  • @RahulTripathi Actually, both calls are perfectly legal. –  Sep 08 '13 at 17:35
  • You need to learn how strings work. – SLaks Sep 08 '13 at 17:35
  • 1
    Read the question to the right under `Linked` http://stackoverflow.com/questions/381542/in-c-arrays-why-is-this-true-a5-5a?lq=1 – dcaswell Sep 08 '13 at 17:35
  • @H2CO3:- Yes I knew that. I was about to update the same but before that I got the downvotes!!! – Rahul Tripathi Sep 08 '13 at 17:36
  • @RahulTripathi You can still edit an undelete your question in this case. –  Sep 08 '13 at 17:37

1 Answers1

1

A string in C is accessed via a pointer to a char (see H2CO3's comment for a more precise definition). If you add 5 to a pointer to a char, you start the string 5 characters later. So 5+"hello my friend" points to " my friend", skipping "hello".

When a C compiler sees two strings with nothing (except possibly whitespace) in between, it treats them as a single string. This makes it easier to break long strings in multiple lines. So "hello""my""friend" compiles into exactly the same thing as "hellomyfriend" or

"hello"
"my"
"friend"

None of these has anything to do with printf, and a lot to do with strings.

Amadan
  • 191,408
  • 23
  • 240
  • 301
  • 3
    A string in C is not a pointer to char. It's an array of `char` that decays into a pointer to its first element in certain contexts. –  Sep 08 '13 at 17:39
  • i usually forget some of the rules of c everyday. – mrigendra Sep 08 '13 at 17:56