Notices
Results 1 to 5 of 5

Thread: amazing output

  1. #1 amazing output 
    Forum Freshman
    Join Date
    Jul 2006
    Location
    Hyderabad
    Posts
    10
    wht is the out put of the below C program which was execuited in CPP compiler under DOS enverionment


    main()
    {
    printf("%s",printf("hello"));
    }


    I am reddaiah.
    Reply With Quote  
     

  2.  
     

  3. #2  
    New Member
    Join Date
    Jan 2007
    Posts
    3
    why don't you try it?
    anyway, you know %s is a string and printf returns an integer, don't you?


    Reply With Quote  
     

  4. #3  
    New Member
    Join Date
    Feb 2007
    Posts
    1
    duh



    i think it will print hallo
    coz the output for the second printf is word as first prinf excpict character(s) as second parameter
    Shena Soul of The Computer
    Reply With Quote  
     

  5. #4  
    Forum Freshman
    Join Date
    Feb 2007
    Location
    Heraklion - Greece
    Posts
    5
    First we must know that the printf function, when the input is a char array,
    prints each char one by one until it reaches the special char '\0' (end of string).

    The following program will do the following:
    First it will print the "hallo" of the inside printf(), and then it will try to print the
    argument of the outside printf (=> the return value of the inside) which is an integer. So, it will start to print 4 bytes (an integer is 4 chars) but it will not stop, since there is no '\0' char in the integer. If you're lucky enough it will print some non human readble characters. But in the most cases (if the OS is safe and strict) will drop a Segmentation Fault (in Windows Access Violation) and terminate.
    Reply With Quote  
     

  6. #5  
    Forum Freshman
    Join Date
    Feb 2007
    Location
    Heraklion - Greece
    Posts
    5
    I am so sorry, I have to correct something I said.
    As we know, if we want to access a string (char[]) we need to specify its starting address. As a result, in the printf(), if we want ot print a string (%s) we must give as an argument its starting address. So, in this case the argument of the oustide printf() is the return value of the inside printf(). It will return an integer. Then this integer we be an address number for the oustide printf(), but the address will be outside the limits of the process's memory space and so it will drop a Segmentation Fault. Even if it is inside the process's limits, the address may be not a multiple of 4 (perhaps it will be 1) and thus it will drop again Segmention Fault (for missaligned address).
    Reply With Quote  
     

Bookmarks
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
  •