gets' is marked as deprecated

Started by AMD, October 07, 2011, 01:30:28 PM

Previous topic - Next topic

AMD

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

yes... you should use fgets() instead because it's buffer safe...


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

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.

Stefan Pendl

Try the following
/* 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

#4
Quote from: Stefan Pendl on October 09, 2011, 10:28:32 AM
Try the following
/* 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...