0

Question 1.

#include <stdio.h>
int main(void)
{

  int c;
  while((c=getchar())!='\0')
   {
     putchar(c);
   } 
}

Input

Hello C.

Tell me about you.

Output

Hello C.

Tell me about you.

ÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿ

and it continues with status-time limit exceeded.

Question 2.

#include <stdio.h>
int main(void)
{
  float a;
  a=46.43253;
  printf("\n%d",a);
  printf("\n%f",a);
  return 0;
}

Output

536870912

46.432529

Imtiaz Raqib
  • 315
  • 2
  • 20
Archit
  • 91
  • 6

2 Answers2

3

Output- 536870912

46.432529

In general using incorrect format specifier triggers undefined behavior - which is what you have when you use %d in printf for printing float. In this case, you can expect any output usually.

However, it may also be the case that since you have specified to read the float number as integer (e.g. by using %d specifier), it simply interpreted the result as integer - hence the strange number (since floats and integers are stored differently).

If you are interested why the second printf prints a number slightly different from yours, this may help you.

Community
  • 1
  • 1
Giorgi Moniava
  • 27,046
  • 9
  • 53
  • 90
0

This block is fine:

float a;
a=46.43253;

This block is also fine:

printf("\n%f",a);

The problem is with this block:

printf("\n%d",a);

Particularly this part:

"\n%d"

Please keep in mind you declared a float and using the integer syntax to output it. That's why you are getting the negative output If it is a case where you don't want to change the "%d," then simply cast it as a float before output

Javy26
  • 375
  • 1
  • 7
  • 22