-3
char nation[100];

printf("Enter Your Name : ");
gets(nation);

int size = strlen(nation);

printf("Output = %f\n", size);

I need to solve the question, please.

Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
  • 3
    Whoever told you to use `gets`, ignore all of that advice and delete this code. Do a proper C course. – Cheatah Nov 29 '22 at 17:49
  • The format `%f` is for *floating point* values, not integers. Use `%d` or `%i` for integer values. – Some programmer dude Nov 29 '22 at 17:50
  • 3
    See [Why the `gets()` function is too dangerous to be used — ever!](https://stackoverflow.com/questions/1694036/why-is-the-gets-function-dangerous-why-should-it-not-be-used) It has the dubious distinction of being the only function removed from Standard C — and you should pretend it does not exist. If your teacher teaches you to use it, you should try to get a better teacher, or point them to the linked question. The first Internet worm, from 1988, thrived because a program used `gets()`. – Jonathan Leffler Nov 29 '22 at 17:53
  • 4
    If your compiler is not complaining that `%f` is the wrong format for printing an `int`, you need to turn on more compiler warnings or get a better compiler. – Jonathan Leffler Nov 29 '22 at 17:55

1 Answers1

0
char nation[100];

printf("Enter Your Name : ");
fgets(nation, sizeof(nation), stdin);  //gets is depreciated use fgets instead

size_t size = strlen(nation);    //strlen returns size_t not int

printf("Output = %zu\n", size);  //%f is to print floats

You need to tell printf what type you want to print:

%c  character
%d  decimal (integer) number (base 10)
%e  exponential floating-point number
%f  floating-point number
%i  integer (base 10)
%o  octal number (base 8)
%s  a string of characters
%u  unsigned decimal (integer) number
%x  number in hexadecimal (base 16)
%%  print a percent sign
0___________
  • 60,014
  • 4
  • 34
  • 74