Thursday 19 June 2014

OUTPUT1

     
Predict the output of following C programs.

Question 1
#include <stdio.h>
 
int main(void)
{
    int i;
    int power_of_ten[5] = {
                            00001,
                            00010,
                            00100,
                            01000,
                            10000,
                        };
     
    for (i = 0; i < 5; ++i)
        printf("%d ", power_of_ten[i]);
    printf("\n");
     
    return 0;
}
In the above example, we have created an array of 5 elements, whose elements are power of ten (i.e. 1, 10, 100, 1000 and 10,000) and we are printing these element by using a simple for loop. So we are expecting output of above program is ” 1 10 100 1000 and 10000″, but above program doesn’t show this output, instead it shows
"1 8 64 512 10000" 
Let us discuss above program in more detail. Inside array we declared elements which starts with “0″, and this is octal representation of decimal number .
That’s why all these numbers are in octal representation. “010″ is octal representation of decimal “8″, “0100″ is octal representation of decimal “64″ and so on.
Last element “10000″ which doesn’t start with “0″, that’s why it is in decimal format and it is printed as it is.


Question 2
#include <stdio.h>
 
int main(void)
{
 
http://http://sunburn.in/
 
     printf("Hello, World !!!\n");
 
     return 0;
}

Output : Hello, World !!!
At first sight, it looks like that the above program will give compilation error, but it will work fine (but will give compilation warning). Observe the above program carefully, in this program “http:” will be treated as goto label and two forward slashes will act as single line comment. That’s why program will work work correctly.


Question 3

#include <stdio.h>
 
#define ARRAY_SIZE(arr) (sizeof(arr) / sizeof(arr[0]))
 
int main(void)
{
    int i;
    int arr[] = {1, 2, 3, 4, 5};
 
    for (i = -1; i < ARRAY_SIZE(arr) - 1; ++i)
        printf("%d ", arr[i + 1]);
 
    printf("\n");
 
    return 0;
}
Output : nothing
In above program, “ARRAY_SIZE” macro substitutes the expression to get the total number of elements present in array, and we are printing these element by using a for loop. But program doesn’t print anything. Let us see what is wrong with code.
Value returned by the substituted expression is in “unsigned int” format, and inside for loop we are comparing unsigned 5 with signed -1. In this comparison -1 is promoted to unsigned integer. -1 in unsigned format is represented as all it’s bit are set to 1 (i.e. 0xffffffff), which is too big number
After substituting value of macro, for loop look like this.
for (i = -1; 0xffffffff < 5; ++i)
In above loop indeed 0xffffffff is not less than 5, that's why for loop condition fails, and program exits from loop without printing anything.

Question 4
#include <stdio.h>
 
int main()
{
    char a = 30;
    char b = 40;
    char c = 10;
    char d = (a * b) / c;
    printf ("%d ", d);
 
    return 0;
}
Output : 120
At first look, the expression (a*b)/c seems to cause arithmetic overflow because signed characters can have values only from -128 to 127 (in most of the C compilers), and the value of subexpression ‘(a*b)’ is 1200. For example, the following code snippet prints -80 on a 32 bit little endian machine.
    char d = 1200;
    printf ("%d ", d);
Arithmetic overflow doesn’t happen in the original program and the output of the program is 120. In C, char and short are converted to int for arithmetic calculations. So in the expression ‘(a*b)/c’, a, b and c are promoted to int and no overflow happens.

No comments:

Post a Comment