ECE DEPARTMENT : NOTICE BOARD

ADVISORY TO STUDENTS (Answer to "Sir, College is there today ?")

Quote :
"If egg is broken by outside force, life ends. If broken by inside force, life begins. Great things always begin from inside force, trust yourself."

Batchwise blogs: -2008 | 2009 | 2010 | 2011 | 2012

Internal Marks Sem-II : | 2011 | 2010 | 2009 |

Intro | Case 1 | Case 2 | Case 3 | Case 4 | Case 5 |

Previous Posts

Friday, July 09, 2010

Today's C Program

When we run this program with the data 3 7 3 0 2,
the results are ?


#include "stdio.h"
char line[100] ; /*line of input */
int seven_count; /* number of 7s in the data */
int data[5]; /* the data to count 3 and 7 in */
int three_count;/*the number of 3s in the data*/
int index;/*index into the data*/

int main ( ) {

     seven_count = 0;
     three_count = 0;


          printf("Enter 5 numbers\n");
          fgets(line, sizeof(line), stdin);


          sscanf(line, "%d %d %d %d %d",
               &data[1], &data[2], &data[3],
                     &data[4], &data[5]);


          for (index = 1 ; index <= 5 ; ++index) {
                if (data[index] == 3)
                      ++three_count;
                if (data[index] == 7)
                      ++seven_count;
          }            
           printf("Threes %d evens %d\n",
               three_count, seven_count);


                return(0);


}







2 comments:

  1. ans: Threes 3 sevens 3

    Exp:
    (Note:This is specific to Turbo c++ Compiler!!! if we use a 32 bit compiler under windows or linux then o/p is as expected 2 1.)

    In this program when we enter the data as 3 7 3 0 2 the fgets() function will take that data into the string line

    then the sscanf() function is used to copy this data into array data[]

    now in this function the addresses of elements of the array is given from 1st element, it should have been from 0th element so the function sscanf() copies the data
    into array data[] as

    data[1]=3;
    data[2]=7;
    data[3]=3;
    data[4]=0;
    data[5]=2;

    as we know array elements are indexed from 0 so data[0] is left unused here. data[0] will have the value 0 because the array data[] is declared globally to which the compiler allocates the extern class storage which when uninitialized has the default value as 0.

    Now as i said array elements begin with 0 subscript then when we say data[5] we are exceeding the boundary of the array we've actually declared it to contain 5 elements but we are accessing the 6th element which is illegal!

    unfortunately this 6th element i,e data[5] happens to be the variable seven_count
    because the compiler arranges the variables declared globally in continuous locations in this program it arranges the variables in memory as

    mem location variable
    3000 index
    3002 three_count
    3004 data[0]
    3006 data[1]
    3008 data[2]
    3010 data[3]
    3012 data[4]
    3014 seven_count
    3016-3116 line[100]

    clearly when we say data[5] it's address would be 3014 which happens to be the address of seven_count hence the value 2 is copied into the variable seven_count. by sscanf()

    so before the execution of for loop seven_count has 2 ;when the loop is executed
    first time:
    index=1; data[1] is 3 so three_count is incremented
    2nd time:
    index=2 ; data[2] is 7 so seven_count is incremented
    3rd time:
    index=3; data[3] is 3 so three count is incremented
    4rth time:
    index=4; data[4] is 0 so none is incremented
    5th time :
    index=5; data[5] is 3 as seen earlier data[5] is the variable seven_count whose value now is 3
    so three_count is incremented

    hence we get the op as 3 3!!!!

    ya had the variables declared like this then there would not hav been the conflict b/w data[5] and seven_count
    int data[5];
    char line[100];
    int seven_count;
    int three_count;
    int index;
    or it should be declared inside main in which case the compiler allocates register class to some variables like seven_count or three_count or possibly index,because variables declared locally persists till the control is within its scope once control goes out of its scope they are destroyed! but the global variables persist till the program execution comes to end! so they are not given the register class b'cuz after all x86 processors have only 14 registers!

    from this prog one has to learn the consequences of exceeding the bounds when accessing the array elements specially in c/ c++ bcuz these compilers leave this responsibility on the programmer but high level counterparts like java,c# take it as their responsibility and throw u run time exception!!!!!

    ReplyDelete
  2. Did any one else get ANY OTHER output ? How can we fix this program so as to get the correct output all the time ?

    ReplyDelete