Thursday, 21 January 2016

Programs Output(2)

1. Find the output of the following program

#include<stdio.h>
#include<conio.h>
void main(void)
{
    char str[]={"\0play\nhard"};
    printf("%%d %d\n",sizeof(str));
    printf("%s",str);
    getch();
}

Output:--
 printf("%s",str) statement prints NULL because str has NULL as it's first character.










2. What would be the output of the following program ?

#include<stdio.h>
#include<conio.h>
main()
{
    long i=2;
    int j=2;
    float k=2.1;
    double l=2.1;
    if(i==j)
    printf("A");
    else
    printf("B");
    if(k==l)
    printf("C");
    else
    printf("D");
}

OUTPUT:--
 long i=2; is same as long int i=2; therefore i==j is TRUE. While in floating point numbers/variables with relational operators we need to take care, because in the case of floating point numbers the values cannot be predicted easily. This is on account of the number of bytes; the precision varies with the number of bytes. As we know float takes 4 bytes and long double takes 10 bytes. Therefore float stores 0.9 with less precision than long double.

No comments:

Post a Comment