Friday, August 31, 2018

Predict output 4 - Answer


Output of following C Programs are not very simple. These are simple but very interesting top c programs asked during viva and interviews.

Predict output of following C Programs:


Program-1

int main()
{
  printf("Hello\\rStudents");
  return 0;
}

Output: Hello\rStudents
Justification: "\" can be printed using "\\" in printf();

Program-2

int main()
{
  printf("12345\r67890");
  return 0;
}

Output: 67890 
Justification: "\r" is a carriage return character; which reset the cursor position to the starting point of current line; hence 12345 printed will be replaced by 67890.

Program-3

int main()
{
  printf("12345678\r000");
  return 0;
}

Output: 00045678
Justification: "\r" is a carriage return character; which reset the cursor position to the starting point of current line; hence 123 will be replaced by 000 keeping remaining character as it is.

Program-4

int main()
{
  printf("123\\\\456");
  return 0;
}

Output: 123\\456
Justification: "\\" in a printf() will print "\"; hence "\\\\" will become "\\" in output.

Program-5

int main()
{
  float n1, n2, total;
  n1 = 5.45;
  n2 = 6.54;
  total = n1 + n2;
  printf("%f", total);
  return 0;
}

Output: 11.990000
Justification: By default floating point numbers are printed with six decimal points precision(after ".")





No comments:

Post a Comment