How i will get this output

This is a discussion on How i will get this output within the C Programming forums, part of the General Programming Boards category; Dear Allo, Recently i was attended one interview i got one question like this Code: #include<stdio.h> #define MAX_STR1_LEN 15 #define ...

  1. #1
    Registered User
    Join Date
    Aug 2007
    Posts
    33

    How i will get this output

    Dear Allo,
    Recently i was attended one interview i got one question like this
    Code:
    #include<stdio.h>
    #define MAX_STR1_LEN 15
    #define MAX_STR2_LEN 16
    void main()
    {
        char a,b,c,d,e,f,g,h,j,k,l,m,n,o,count;
        char i=4;
        char str2[MAX_STR2_LEN]="0123456789abcdef";
        chat str1[MAX_STR1_LEN]="0123456789abcde";
        str2[MAX_STR2_LEN]=0;
        str1[MAX_STR1_LEN]=0;
        printf("1.str1:%s  str2:%s\n",str1,str2);
        strcpy(str1,str2);
       printf("2.str1:%s str2:%s\n",str1,str2);
       for(count=0;count<i;count++);
       str2[MAX_STR2_LEN]='a';
       str1[MAX_STR1_LEN]=0;
       strcpy(str1,str2);
       printf("3. str1:%s str2:%s\n", str1,str2);
       for(; count<i;count++);
       printf("4. count:%d\n",count);
       return;
    }

    The o/p i got in my compiler

    Code:
    1. str1:0123456789abcde str2:0123456789abcdef
    2. str1:0123456789abcdef str2:
    3. str1:    str2:
    4. count:97
    can anybody explain step by step procedure how i got this output... what is the concept beyond this program..........I am advance thankful to you
    Last edited by Salem; 08-10-2007 at 11:36 PM. Reason: Put code tags around only those things which need code tags

  2. #2
    Woof, woof! zacs7's Avatar
    Join Date
    Mar 2007
    Location
    Australia
    Posts
    3,459
    I don't see any question? What question did they give you?
    Unless that's for an embedded system, don't use void main(), see the FAQ.
    Try and indent it... and put a few line breaks in.horrid.

    Code:
        char str2[MAX_STR1_LEN]="0123456789abcdef";
        chat str1[MAX_STR2_LEN]="0123456789abcde";
        str2[MAX_STR2_LEN]=0;
        str1[MAX_STR1_LEN]=0;
    declares and initializes str2 and str1, then sets the last character to NUL, ie NUL to NUL (pointless).

    prints them, blah blah blah - rest is straight forward (and somewhat error some, use of strcpy() etc).

    Other than that, it's full of pointlessness.
    Code:
    for(count=0;count<i;count++);
    Is just moronic, why not count = i; ? Duh.

    As for the output, count should not be 97, Really it's just:
    Code:
    i = 4;
    count = i;    /* 4 */
    Therefore count is 4, as
    Code:
    for(; count<i;count++);
    is the same as,
    Code:
    for(; 4 < 4;);
    And last time I checked, 4 was not less than 4.

    PS: try not to put your whole post in code tags :s
    Last edited by zacs7; 08-10-2007 at 11:33 PM.

  3. #3
    Registered User
    Join Date
    Aug 2007
    Posts
    33
    Quote Originally Posted by zacs7 View Post
    I don't see any question? What question did they give you?
    Unless that's for an embedded system, don't use void main(), see the FAQ.
    Try and indent it... and put a few line breaks in.horrid.

    Code:
        char str2[MAX_STR1_LEN]="0123456789abcdef";
        chat str1[MAX_STR2_LEN]="0123456789abcde";
        str2[MAX_STR2_LEN]=0;
        str1[MAX_STR1_LEN]=0;
    declares and initializes str2 and str1, then sets the last character to NUL, ie NUL to NUL (pointless).

    prints them, blah blah blah - rest is straight foward.

    Other than that, it's full of pointlessness.
    Code:
    for(count=0;count<i;count++);
    Is just moronic, why not count = i; ? Duh.

    As for the output, count should not be 97, Really it's just:
    Code:
    i = 4;
    count = i;    /* 4 */
    Therefore count is 4, as
    Code:
    for(; count<i;count++);
    is the same as,
    Code:
    for(; 4 < 4;);
    And last time I checked, 4 was not less than 4.

    PS: try not to put your whole post in code tags :s

    I got output as follows in my compiler

    1. str1:0123456789abcde str2:0123456789abcdef
    2. str1:0123456789abcdef str2:
    3. str1: str2:
    4. count:97

    First line output is correct but remaining i am not able to analyse..could you explain me now how i wil get lines 2,3 and 4

  4. #4
    Woof, woof! zacs7's Avatar
    Join Date
    Mar 2007
    Location
    Australia
    Posts
    3,459
    eh? What compiler.
    And how the hell did you get count = 97?

    Stripped down:

    Code:
    #include <stdio.h>
    
    int main(void)
    {
        int count, i = 4;
        
        for(count = 0; count < i; count++);
        printf("Count = &#37;d\n", count);
        
        for(; count < i; count++);
        printf("Count = %d\n", count);
        return 0;
    }
    Which is
    Code:
    Count = 4
    Count = 4
    As I said before.

    Either:
    * you're making it up
    * you're compiler is old skool
    * you're stepping past the end of an array and setting it's value. (Possibly from your bad use of strcpy(), str1 is not big enough to hold str2)

    Code:
    str2[MAX_STR2_LEN]='a';
    
    strcpy(str1,str2);
    Will remove the NUL terminator from str2, which is bad considering you then use strcpy() (Which will read from str2 until the NUL character) and copy it into str1, and this is probably one place where you set the value of count mistakingly, and you're probably unlucky enough to get a segfault.

    If this was the actual question, it's moronic...
    Last edited by zacs7; 08-10-2007 at 11:45 PM.

  5. #5
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,822
    97 = 'a'
    That's because the whole code is riddled with buffer overflows.

    > str2[MAX_STR2_LEN]=0;
    All these operations are out of bound memory accesses, so I guess at some point, the counter variable got trashed with some of the string manipulation.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  6. #6
    Woof, woof! zacs7's Avatar
    Join Date
    Mar 2007
    Location
    Australia
    Posts
    3,459
    Quote Originally Posted by Salem
    so I guess at some point, the counter variable got trashed with some of the string manipulation.
    Code:
    str2[MAX_STR2_LEN]='a';
    
    strcpy(str1,str2);
    Like there

  7. #7
    Registered User
    Join Date
    Aug 2007
    Posts
    33
    Quote Originally Posted by Salem View Post
    97 = 'a'
    That's because the whole code is riddled with buffer overflows.

    > str2[MAX_STR2_LEN]=0;
    All these operations are out of bound memory accesses, so I guess at some point, the counter variable got trashed with some of the string manipulation.

    Yes Salam you are right i will get the value of count because of that assignement...It is not application program..just to test the skills in C...i don't want to correct my source code i want only how i will get this output..thats all.
    could you now explain me how i will achieve this output? thanks

  8. #8
    Woof, woof! zacs7's Avatar
    Join Date
    Mar 2007
    Location
    Australia
    Posts
    3,459
    He already has.

  9. #9
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,822
    Exactly what skills are you looking to test?

    The only skill you need here is the ability to recognise a buffer overflow when you see one (eg, a variable contains an unexpected value).

    > could you now explain me how i will achieve this output?
    What good will that do?
    At the point where the code became undefined (in this case, it was with the 'void main'), exactly what the code does is just a matter of chance.

    If you want to argue that
    str2[MAX_STR2_LEN]='a';
    is really
    i = 'a';
    because they're declared next to each other, then that's fine by me. There are several 'a' characters to begin with, maybe another one is responsible, or maybe it's none of them and it gets there some other way.

    But that 'explanation' is valid ONLY for this program, with the current compiler, with the current compiler options.

    But the only way to be sure would be to manually inspect the generated machine code.

    Change anything at all, say the lengths of the arrays, remove some unused variables, change the options, change the compiler, change blah blah blah and you're likely to be scrabbling around for another explanation.

    The problem is, whatever set of 'rules' you can come up with to explain such things, it will always be a subset. Sooner or later, a new bit of undefined code will force you to think again. Then you change the compiler, or patch your current one, or change the flags and you have to start over.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. code output...
    By roaan in forum C Programming
    Replies: 6
    Last Post: 07-03-2009, 03:22 AM
  2. Help for my output array
    By qwertysingh in forum C Programming
    Replies: 1
    Last Post: 02-17-2009, 03:08 PM
  3. Replies: 4
    Last Post: 11-30-2005, 04:44 PM
  4. Formatting output into even columns?
    By Uncle Rico in forum C Programming
    Replies: 2
    Last Post: 08-16-2005, 06:10 PM
  5. Output problems with structures
    By Gkitty in forum C Programming
    Replies: 1
    Last Post: 12-16-2002, 05:27 AM

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