NO

Author Topic: problem with gets() function....  (Read 8303 times)

hamsam

  • Guest
problem with gets() function....
« on: November 17, 2012, 01:34:16 PM »
I get the following warning everytime I use the gets() function in my code:

warning #2018: Undeclared function 'gets'; assuming 'extern' returning 'int'.

I have included the stdio.h library but it still givesthis warning. I thought that gets() is suppose to output a char * but it requires an int instead and gives an error

error #2168: Operands of '=' have incompatible types 'char *' and 'int'

if you output a char *.

How can I make Pellec C recognize the gets()?

Offline Stefan Pendl

  • Global Moderator
  • Member
  • *****
  • Posts: 582
    • Homepage
Re: problem with gets() function....
« Reply #1 on: November 17, 2012, 01:50:43 PM »
Starting with PellesC 7 the default standard is ANSI C11, which has the gets() function dropped.

Instead use the fgets() or gets_s() functions as described in the help file.

Excerpt from the stdio.h file:
Code: [Select]
#if __POCC_STDC_VERSION__ < 201101L
extern _CRTBAD char * __cdecl gets(char *);
#endif /* __POCC_STDC_VERSION__ < 201101L */
---
Stefan

Proud member of the UltraDefrag Development Team

CommonTater

  • Guest
Re: problem with gets() function....
« Reply #2 on: November 17, 2012, 02:07:13 PM »
Hi Stephan...
 
Starting with PellesC 7 the default standard is ANSI C11, which has the gets() function dropped.

Instead use the fgets() or gets_s() functions as described in the help file.

If he's not using any other C11 features, he could also set
Project -> Project Options -> Compiler -> C Standard = C-99

Although...  as you point out gets(string) is really just an alias for fgets(string, stdin) anyway...

To avoid  buffer overruns, the best option would be getline() which allocates it's own memory. (which he has to free() when he's done with it)

Offline Stefan Pendl

  • Global Moderator
  • Member
  • *****
  • Posts: 582
    • Homepage
Re: problem with gets() function....
« Reply #3 on: November 17, 2012, 02:56:48 PM »
To avoid  buffer overruns, the best option would be getline() which allocates it's own memory. (which he has to free() when he's done with it)

I fully agree to using this function, since it is as safe as using fgets() or gets_s(), but with the advantage to get the whole input without the need to check for truncation.
---
Stefan

Proud member of the UltraDefrag Development Team

CommonTater

  • Guest
Re: problem with gets() function....
« Reply #4 on: November 17, 2012, 04:37:41 PM »
To avoid  buffer overruns, the best option would be getline() which allocates it's own memory. (which he has to free() when he's done with it)

I fully agree to using this function, since it is as safe as using fgets() or gets_s(), but with the advantage to get the whole input without the need to check for truncation.

:D Especially when you have someone like me at the keyboard  :D




hamsam

  • Guest
Re: problem with gets() function....
« Reply #5 on: November 28, 2012, 02:58:02 PM »
Thanks for both of your great replies and help  :)

migf1

  • Guest
Re: problem with gets() function....
« Reply #6 on: November 28, 2012, 06:18:24 PM »
I think it is worth mentioning that getline() does not comply to the ANSI/ISO standards of the C language. It was initially a GNU extension in gcc, which was later adopted by the Posix standard. Some compilers also provide it, but as an extension.

Here is a very informative link about a backwards portable way of getting interactive input in C. It actually ends up to a custom but portable implementation of getline() ;)

PS. I've also made a rather primitive inputting library for C, which only operates in plain char (no wide-char support). Unfortunately, the documentation is all in Greek, but the source code is pretty straight forward. Anyone interested will find it here: http://x-karagiannis.gr/prg/c-prog/c-misc/prompt-for-library/
« Last Edit: November 28, 2012, 06:26:12 PM by migf1 »

CommonTater

  • Guest
Re: problem with gets() function....
« Reply #7 on: November 28, 2012, 06:27:12 PM »
It's defined in TR 24731-2 ... and included with stdio.h ... I see no reason not to use it.

IMO, standards compliance is made way too much of these days.  It's good to know if it's non-standard or not, but frankly I'd suggest that if it's available, why not use it?  Heck... I'll use my great grandmother's code if it makes my life easier.

migf1

  • Guest
Re: problem with gets() function....
« Reply #8 on: November 28, 2012, 06:39:35 PM »
It's defined in TR 24731-2 ... and included with stdio.h ... I see no reason not to use it.

TR 24731-2 describes suggested extensions.

Quote
IMO, standards compliance is made way too much of these days.  It's good to know if it's non-standard or not, but frankly I'd suggest that if it's available, why not use it?  Heck... I'll use my great grandmother's code if it makes my life easier.

I guess it could be due to your needs not including portable code, or a different compiler?
« Last Edit: November 28, 2012, 06:41:40 PM by migf1 »

CommonTater

  • Guest
Re: problem with gets() function....
« Reply #9 on: November 28, 2012, 09:42:28 PM »
I guess it could be due to your needs not including portable code, or a different compiler?

Partly ... but mostly I just don't care about that stuff.  :D

In all my years programming, I've never yet seen any of my source code adapted to any other system.  Quite honestly it's just never been an issue.