LinuxQuestions.org
Review your favorite Linux distribution.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 05-08-2021, 09:09 AM   #1
Parmanand Motiramani
LQ Newbie
 
Registered: Oct 2020
Location: Bharat
Distribution: Linux Lite
Posts: 11

Rep: Reputation: Disabled
What is wrong in this c program?


I've attached a screenshot of the program and error generated by the compiler.
Attached Thumbnails
Click image for larger version

Name:	Screenshot_2021-05-08_19-35-09.png
Views:	37
Size:	15.5 KB
ID:	36331   Click image for larger version

Name:	Screenshot_2021-05-08_19-34-36.png
Views:	25
Size:	108.1 KB
ID:	36332  
 
Old 05-08-2021, 09:24 AM   #2
rtmistler
Moderator
 
Registered: Mar 2011
Location: USA
Distribution: MINT Debian, Angstrom, SUSE, Ubuntu, Debian
Posts: 9,316
Blog Entries: 13

Rep: Reputation: 4372Reputation: 4372Reputation: 4372Reputation: 4372Reputation: 4372Reputation: 4372Reputation: 4372Reputation: 4372Reputation: 4372Reputation: 4372Reputation: 4372
gets() is deprecated

Here's a manual page for it https://www.man7.org/linux/man-pages/man3/gets.3.html
 
1 members found this post helpful.
Old 05-08-2021, 09:30 AM   #3
michaelk
Moderator
 
Registered: Aug 2002
Posts: 21,491

Rep: Reputation: 4106Reputation: 4106Reputation: 4106Reputation: 4106Reputation: 4106Reputation: 4106Reputation: 4106Reputation: 4106Reputation: 4106Reputation: 4106Reputation: 4106
Quote:
What is wrong in this c program?
As per the compiler warning message
Quote:
the `gets' function is dangerous and should not be used.
gets has no limit on the number of input characters and therefore will overwrite whatever is after your buffer. Many exploits are based on buffer overruns due to insufficient checks.

Use fgets instead:

Code:
#include <stdio.h>
#include <string.h>

void main () {
  char nm[21];
  puts("Enter your name");
  fgets(nm,sizeof(nm),stdin);
  puts(nm);
}
 
1 members found this post helpful.
Old 05-08-2021, 09:50 AM   #4
onebuck
Moderator
 
Registered: Jan 2005
Location: Summer Midwest USA, Central Illinois, Winter Central Florida
Distribution: SlackwareŽ
Posts: 13,673
Blog Entries: 37

Rep: Reputation: 2862Reputation: 2862Reputation: 2862Reputation: 2862Reputation: 2862Reputation: 2862Reputation: 2862Reputation: 2862Reputation: 2862Reputation: 2862Reputation: 2862
Moderator Response

Moved: This thread is more suitable in <Programming> and has been moved accordingly to help your thread/question get the exposure it deserves.
 
1 members found this post helpful.
Old 05-09-2021, 12:14 AM   #5
NevemTeve
Senior Member
 
Registered: Oct 2011
Location: Budapest
Distribution: Debian/GNU/Linux, AIX
Posts: 4,170
Blog Entries: 1

Rep: Reputation: 1536Reputation: 1536Reputation: 1536Reputation: 1536Reputation: 1536Reputation: 1536Reputation: 1536Reputation: 1536Reputation: 1536Reputation: 1536Reputation: 1536
Kindly don't attach images instead of actual source code.
 
2 members found this post helpful.
Old 05-09-2021, 07:27 AM   #6
GazL
LQ Veteran
 
Registered: May 2008
Posts: 5,923

Rep: Reputation: 3899Reputation: 3899Reputation: 3899Reputation: 3899Reputation: 3899Reputation: 3899Reputation: 3899Reputation: 3899Reputation: 3899Reputation: 3899Reputation: 3899
Quote:
Originally Posted by michaelk View Post
Use fgets instead:
Code:
#include <stdio.h>
#include <string.h>

void main () {
  char nm[21];
  puts("Enter your name");
  fgets(nm,sizeof(nm),stdin);
  puts(nm);
}
Just remember to make nm at least 2 characters bigger than your largest input line, convert the trailing '\n' to a '\0', and check for and deal with any truncation.

Alternatively, if you're writing to POSIX-1.2008, use getline(), it's much easier and has the advantage that it returns strlen into the bargain, possibly saving you a strlen() call later.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
how to use web to invoke php program, and the php program call c program bellhuang Linux - Server 10 01-13-2014 06:09 AM
Fortran - G95 - Wrong Syntax or wrong compiler settings laucian Programming 1 03-21-2008 10:18 AM
2domain but backingup the wrong one .. what am i doing wrong salimmeethoo Linux - Server 3 10-17-2007 10:43 AM
What is wrong with reiserfs? wrong free space mesh2005 Linux - General 1 05-03-2007 07:21 AM
my time is wrong and calender is also wrong Paxmaster Linux - General 6 12-16-2004 12:46 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 12:37 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration