Like Tree2Likes
  • 1 Post By msh
  • 1 Post By std10093

How does this program work??

This is a discussion on How does this program work?? within the C Programming forums, part of the General Programming Boards category; Recently, i came across a c language code snippet. That is: Code: #include<stdio.h> #include<conio.h> int main() { int a=3, b ...

  1. #1
    Registered User
    Join Date
    Aug 2012
    Posts
    2

    How does this program work??

    Recently, i came across a c language code snippet. That is:

    Code:
    #include<stdio.h>
    #include<conio.h>
    int main()
    { 
        int a=3, b = 5; 
        printf(&a["Ya!Hello! how is this? %s\n"], &b["junk/super"]); 
        printf(&a["WHAT%c%c%c %c%c %c !\n"], 1["this"], 2["beauty"],0["tool"],0["is"],3["sensitive"],4["CCCCCC"]); 
        getch();
    }
    This program produced output:

    Hello! how is this? super
    That is C !

    I can't under stand how this works? I've used Microsoft Visual C++ compiler. Please explain this code.

    Also suggest some websites where can I learn such type of advanced programming.

    Thanks in advance.

  2. #2
    msh
    msh is offline
    Novice
    Join Date
    Jul 2009
    Posts
    568
    kcuF this code; you don't want to program like this.

    It works because pointers are integral types and addition is commutative for them, and, for example, `3["this"]` is the same `("this") + 3`.
    Last edited by msh; 08-27-2012 at 05:32 AM. Reason: Censored? Seriously... & one ''&" too many.
    AndiPersti likes this.
    Disclaimer: This post shows my ignorance at the time of its making. I claim ownership of but not responsibility for all errors in it. Reference at your own peril.

  3. #3
    Registered User
    Join Date
    Aug 2012
    Posts
    41
    refer to the following link, you will find the solution

    C Puzzle : Level 3

  4. #4
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Nice, France
    Posts
    2,678
    I am not sure but i'll give it a try.
    1st printf
    Mind that a has value 3.So what it does is to retrieve from string "Ya!Hello..." the substring from third position,but you have to imagine the string as an array.So element at zero position is Y.Then the %s is replaced by the substring of where b points to which again (with same logic) points to s,so %s will be replaced from s till the end of the string,thus with super.

    2nd printf
    Same logic.But now prints characters instead of string.The first %c is replaced by the a-rd(3rd) element of WHAT which is T and so on..
    Dushyant likes this.

  5. #5
    Registered User
    Join Date
    Aug 2012
    Posts
    2
    Ya i referred the link and it was a bit explanatory. But i still don't understand how the printf prints the output in this program. Can u please xplain it in detail??

  6. #6
    Registered User
    Join Date
    May 2012
    Location
    India
    Posts
    12
    Yup ur logic seems to be correct....well but it is obvious that one doesn't code like this but these snippets throw some good mind games......

  7. #7
    Registered User
    Join Date
    Aug 2012
    Posts
    41
    From what i understood....

    "Ya!Hello! how is this? %s\n" is stream of characters which is equivalent to char *p="Ya!Hello! how is this? %s\n".
    Here you are writing p+3 which means discard first three characters which will be "Hello! how is this? %s\n". Same logic applies to every other part of code.
    On first printf it is %s and on second it is %c so you will be having only one character from each word.

  8. #8
    Registered User
    Join Date
    Jun 2005
    Posts
    6,261
    Quote Originally Posted by msh View Post
    It works because pointers are integral types and addition is commutative for them, and, for example, `3["this"]` is the same `("this") + 3`.
    Addition of a pointer and an integral value is commutative, but pointers are not integral types. The result of adding a pointer to an int is a pointer, not an int.

    There is also the quibble that a string literal is not a pointer either. It is a (const) array of char - which can be converted to a pointer, despite not being a pointer.
    Right 98% of the time, and don't care about the other 3%.

  9. #9
    msh
    msh is offline
    Novice
    Join Date
    Jul 2009
    Posts
    568
    Quote Originally Posted by grumpy View Post
    Addition of a pointer and an integral value is commutative, but pointers are not integral types. The result of adding a pointer to an int is a pointer, not an int.
    You're right.

    There is also the quibble that a string literal is not a pointer either. It is a (const) array of char - which can be converted to a pointer, despite not being a pointer.
    Yes, it's not a pointer in a strictly C sense, but, and correct me if I'm wrong, that is what it will become during compilation -- an address in the data segment where the string literal is stored. Hance why this is possible in the first place.
    Disclaimer: This post shows my ignorance at the time of its making. I claim ownership of but not responsibility for all errors in it. Reference at your own peril.

  10. #10
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Nice, France
    Posts
    2,678
    Quote Originally Posted by Dushyant View Post
    Yup ur logic seems to be correct....well but it is obvious that one doesn't code like this but these snippets throw some good mind games......
    Glad you agree

    Quote Originally Posted by sana.iitkgp View Post
    From what i understood....

    "Ya!Hello! how is this? %s\n" is stream of characters which is equivalent to char *p="Ya!Hello! how is this? %s\n".
    Here you are writing p+3 which means discard first three characters which will be "Hello! how is this? %s\n". Same logic applies to every other part of code.
    On first printf it is %s and on second it is %c so you will be having only one character from each word.
    Yes,you understood correct.I could not call it however stream of characters but a sequence or characters(or a string )

  11. #11
    Registered User
    Join Date
    Jun 2005
    Posts
    6,261
    Quote Originally Posted by msh View Post
    Yes, it's not a pointer in a strictly C sense, but, and correct me if I'm wrong, that is what it will become during compilation -- an address in the data segment where the string literal is stored. Hance why this is possible in the first place.
    Well, it's not a pointer in any sense. A string literal is a different thing from a pointer. "Able to be converted to a pointer" is not the same as "is a pointer".

    Take out the words "in the data segment" (since not all executable file formats have a "data segment") and you're roughly right. The compiler does the array-to-pointer conversion, within certain expressions ..... which is a more technically formal way of what you are saying. The name of an array is still not a pointer (and a lot of compilers use different instructions to access something_known_to_be_an_array[index] than they do to access some_pointer[index]). But the compiler can recognise an expression of the form "array_name + integer", and do appropriate conversions (say of array_name to pointer). The thing is, the standard describes the observable effect required, it doesn't mandate how a compiler achieves that effect.
    Right 98% of the time, and don't care about the other 3%.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Can not get this program to work?
    By BLG in forum C Programming
    Replies: 9
    Last Post: 09-09-2009, 09:28 AM
  2. Why can't I get this program to work?
    By nadeni0119 in forum C++ Programming
    Replies: 2
    Last Post: 03-23-2003, 03:59 PM
  3. help getting program to work
    By jlmac2001 in forum C Programming
    Replies: 2
    Last Post: 11-13-2002, 10:04 PM
  4. how do i get this program to work, anyone, anyone?
    By correlcj in forum C Programming
    Replies: 5
    Last Post: 07-04-2002, 10:28 PM
  5. how does this program work
    By ssjnamek in forum C++ Programming
    Replies: 2
    Last Post: 01-02-2002, 12:48 PM

Tags for this Thread


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21