Pages

Tuesday, May 23, 2017

Predict C Program Output-2 Answer



Program-1
#include<stdio.h>
int main()
{
  printf("Hello");
  printf("\rIndia");
  return 0;
}
Output: India
Justification: \r is linefeed character.

Program-2
#include<stdio.h>
int main()
{
  int number = - -5;
  printf("number = %d",number);
  return 0;
}
Output: number = 5
Justification: Here, unary minus sign used twice. Hence minus * minus becomes plus.

Program-3
#include<stdio.h>
#define float char
int main()
{
  float num=65;
  printf("Size of(num) = %d",sizeof(num));
  return 0;
}
Output: Size of(num) = 1
Justification: #define replaces the string float by char, hence num becomes char type variable.

Program-4
#include<stdio.h>
enum colors {RED, BLUE, GREEN};
int main()
{
  printf("%d, %d, %d",BLUE, RED, GREEN);
  return 0;
}
Output: 1, 0, 2
Justification: By default, enum assigns numbers starting from 0 to its variables.

Program-5
#include<stdio.h>
void display(void);
int main()
{
  int i=1;
  for(i=0; i<=5; i++)
  {
     printf("%d",i);
     if (i>2)
        goto ABC;
  }
}
display()
{
  ABC:
  printf("Hello World");
}
Output: Compile time error.
Justification: goto and Label(ABC) is not defined in same function.

1 comment: