NO

Author Topic: gets' is marked as deprecated  (Read 11946 times)

AMD

  • Guest
gets' is marked as deprecated
« on: October 07, 2011, 01:30:28 pm »
I was trying to comile this code:

/* EX2-5.c */
#include <stdio.h>
#include <string.h>

int main ()
{
   char buffer[256];

    printf( "Enter your name and press <Enter>: \n");
    gets( buffer );

    printf( "\Your name has %d characters and spaces!",
               strlen ( buffer ));
    return 0;
}

Please help i get the following errors:

C:\Users\AMD\Documents\Road to Lite Hacker\C Language\Day 2\Exercises\Q5\5.c(10): warning #2241: The function 'gets' is marked as deprecated.
C:\Users\AMD\Documents\Road to Lite Hacker\C Language\Day 2\Exercises\Q5\5.c(12): warning #2176: Unrecognized character escape sequence '\Y'.
C:\Users\AMD\Documents\Road to Lite Hacker\C Language\Day 2\Exercises\Q5\5.c(13): warning #2234: Argument 2 to 'printf' does not match the format string; expected 'int' but found 'unsigned long long int'.
Done.

CommonTater

  • Guest
Re: gets' is marked as deprecated
« Reply #1 on: October 07, 2011, 01:38:20 pm »
yes... you should use fgets() instead because it's buffer safe...

Code: [Select]
char buffer[256];

prinf("type something\n");
fgets(buffer,255,stdin);

printf("You entered %d characters\n",strlen(buffer));

The advantage is that only the first 255 characters are loaded into the buffer... with gets() you could easily overflow a small buffer causing memory access violations.

AMD

  • Guest
Re: gets' is marked as deprecated
« Reply #2 on: October 09, 2011, 10:04:01 am »
I corrected my code as follows:

/* EX2-5.c */
#include <stdio.h>
#include <string.h>

int main ()
{
   char buffer[256];

    printf( "Enter your name and press <Enter>: \n");
    fgets( buffer , 255, stdin);

    printf( "\nYour name has %d characters and spaces!",
               strlen( buffer ));
    return 0;
}

And i am still getting the following error please help:

Building 5.obj.
C:\Users\AMD\Documents\Road to Lite Hacker\C Language\Day 2\Exercises\Q5\5.c(13): warning #2234: Argument 2 to 'printf' does not match the format string; expected 'int' but found 'unsigned long long int'.
Done.

Offline Stefan Pendl

  • Global Moderator
  • Member
  • *****
  • Posts: 566
    • Homepage
Re: gets' is marked as deprecated
« Reply #3 on: October 09, 2011, 10:28:32 am »
Try the following
Code: [Select]
/* EX2-5.c */
#include <stdio.h>
#include <string.h>

int main ()
{
   char buffer[256];

    printf( "Enter your name and press <Enter>: \n");
    fgets( buffer , 255, stdin);

    printf( "\nYour name has %d characters and spaces!", (int)strlen( buffer ));
    return 0;
}
You could change %d to match the result of the function strlen() too.
---
Stefan

Proud member of the UltraDefrag Development Team

CommonTater

  • Guest
Re: gets' is marked as deprecated
« Reply #4 on: October 09, 2011, 10:41:25 am »
Try the following
Code: [Select]
/* EX2-5.c */
#include <stdio.h>
#include <string.h>

int main ()
{
   char buffer[256];

    printf( "Enter your name and press <Enter>: \n");
    fgets( buffer , 255, stdin);

    printf( "\nYour name has %d characters and spaces!", (int)strlen( buffer ));
    return 0;
}
You could change %d to match the result of the function strlen() too.

If strlen() is returing a 64 bit integer (64 bit project), it's probably better to change the format string to %lld ...

@AMD ... just a little tip here... if you are doing exercises from class or textbooks, your best bet is to compile them as 32 bit (x86) projects.  64 bit (amd64) brings in some differences that may make it more difficult for you to follow the texts...  Concentrate on learning C... You can always to the "big new thing" afterwards...

« Last Edit: October 09, 2011, 10:45:05 am by CommonTater »