{"id":3577,"date":"2024-03-06T11:35:32","date_gmt":"2024-03-06T11:35:32","guid":{"rendered":"https:\/\/www.skillvertex.com\/blog\/?p=3577"},"modified":"2024-03-06T11:35:32","modified_gmt":"2024-03-06T11:35:32","slug":"fgets-and-gets-in-c-language","status":"publish","type":"post","link":"https:\/\/www.skillvertex.com\/blog\/fgets-and-gets-in-c-language\/","title":{"rendered":"fgets() and gets() In C language"},"content":{"rendered":"\n<div class=\"wp-block-rank-math-toc-block\" id=\"rank-math-toc\" id=\"rank-math-toc\"><p>Table of Contents<\/p><nav><ul><li ><a href=\"#fgets-and-gets-in-c-language\">fgets() and gets() In C language<\/a><\/li><li ><a href=\"#fgets\">fgets()<\/a><ul><li ><a href=\"#features-of-fgets\">Features of fgets()<\/a><\/li><\/ul><\/li><li ><a href=\"#example-of-fgets\">Example of fgets()<\/a><\/li><li ><a href=\"#gets\">gets()<\/a><ul><li ><a href=\"#parameters\">Parameters<\/a><\/li><li ><a href=\"#return-value\">Return Value<\/a><\/li><\/ul><\/li><li ><a href=\"#example-of-gets\">Example of gets()<\/a><\/li><li ><a href=\"#faq-fgets-and-gets-in-c-language\">FAQ -fgets() and gets() In C language<\/a><\/li><\/ul><\/nav><\/div>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"fgets-and-gets-in-c-language\">fgets() and gets() In C language<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Both <code>gets()<\/code> and <code>fgets()<\/code> are functions in C used for reading strings with spaces, but they have some key differences:<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"fgets\">fgets()<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">the <code>fgets()<\/code> function reads a line from the specified stream and stores it into the string pointed to by the provided buffer (<code>str<\/code>). It stops reading under the following conditions:<\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li>It reads <code>(n-1)<\/code> characters, where <code>n<\/code> is the size of the buffer. This ensures that the last character in the buffer is reserved for the null terminator (<code>'\\0'<\/code>), making the string properly terminated.<\/li>\n\n\n\n<li>It encounters a newline character (<code>'\\n'<\/code>) in the input, indicating the end of the line.<\/li>\n\n\n\n<li>It reaches the end-of-file (EOF).<\/li>\n<\/ol>\n\n\n\n<p class=\"wp-block-paragraph\">This behavior of <code>fgets()<\/code> is valuable for reading lines of text while providing control over the number of characters read and preventing buffer overflow. Developers commonly use <code>fgets()<\/code> for safe and controlled string input in C programming.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Syntax<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>char *fgets (char *str, int n, FILE *stream);\n<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Parameter<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>str:<\/strong> It&#8217;s where the read string is stored. This is a pointer to an array of characters.<\/li>\n\n\n\n<li><strong>n:<\/strong> This is the maximum number of characters that can be copied into <code>str<\/code>, including the final null character.<\/li>\n\n\n\n<li><strong>stream:<\/strong> It&#8217;s a pointer to a <code>FILE<\/code> object, which points to the source of input, like a file or standard input.<\/li>\n<\/ul>\n\n\n\n<p class=\"wp-block-paragraph\">Return Value<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">The fgets() function will returns a pointer to the string where the input is stored.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"features-of-fgets\">Features of fgets()<\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>Parameters:<\/strong> <code>fgets()<\/code> uses parameters like the maximum length (how many characters to read), a buffer (where the string is kept), and a reference to where the input comes from.<\/li>\n\n\n\n<li><strong>Safety:<\/strong> It&#8217;s safe to use because it checks how much space is available in the buffer, preventing it from grabbing too many characters and causing problems.<\/li>\n\n\n\n<li><strong>Reading Behavior:<\/strong> <code>fgets()<\/code> keeps reading characters until it finds a new line or reaches the maximum limit set by the buffer size. This helps control and manage string input in a safe way.<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"example-of-fgets\">Example of fgets()<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">&nbsp;Consider the maximum number of characters is 15 and the input length is greater than 15 but still fgets() will read only 15 characters and print it.&nbsp;<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\/\/ C program to illustrate fgets()\n#include &lt;stdio.h&gt;\n#define MAX 15\n \nint main()\n{\n    \/\/ defining buffer\n    char buf&#91;MAX];\n \n    \/\/ using fgets to take input from stdin\n    fgets(buf, MAX, stdin);\n    printf(\"string is: %s\\n\", buf);\n \n    return 0;\n}<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>Input:\nHello and welcome to Skillvertex\n\nOutput:\nstring is: Hello and welc<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"gets\">gets()<\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>Functionality:<\/strong> It reads characters from the keyboard and puts them into a C string until it sees a new line or reaches the end of the input.<\/li>\n\n\n\n<li><strong>Safety Concerns:<\/strong> Be cautious using it because it doesn&#8217;t check how much space is available, which can cause problems by using too much space.<\/li>\n\n\n\n<li><strong>Usage:<\/strong> It&#8217;s used to get strings from the user until they press &#8220;Enter.&#8221;<\/li>\n<\/ul>\n\n\n\n<p class=\"wp-block-paragraph\">Syntax<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>char *gets( char *str );\n<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"parameters\">Parameters<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>str<\/strong> is a pointer to a block of memory, typically an array of characters, where the read string is stored as a C string.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"return-value\">Return Value<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">The function  will return a pointer to the string where input is being stored.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"example-of-gets\">Example of gets()<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\"> Consider a character array of 15 characters and the input will be  greater than 15 characters, gets()  reads all these characters and store them into a variable. Since, gets() does not check the maximum limit of input characters, at any time compiler may return buffer overflow error.&nbsp;<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\/\/ C program to illustrate\n\/\/ gets()\n#include &lt;stdio.h&gt;\n#define MAX 15\n \nint main()\n{\n    \/\/ defining buffer\n    char buf&#91;MAX];\n \n    printf(\"Enter a string: \");\n \n    \/\/ using gets to take string from stdin\n    gets(buf);\n    printf(\"string is: %s\\n\", buf);\n \n    return 0;\n}<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>Input:\nHello and welcome to Skillvertex \n\nOutput:\nHello and welcome to Skillvertex<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"faq-fgets-and-gets-in-c-language\">FAQ -fgets() and gets() In C language<\/h2>\n\n\n<div id=\"rank-math-faq\" class=\"rank-math-block\">\n<div class=\"rank-math-list \">\n<div id=\"faq-question-1700127992995\" class=\"rank-math-list-item\">\n<h4 class=\"rank-math-question \">Q1. What is the use of gets () function example?<\/h4>\n<div class=\"rank-math-answer \">\n\n<p>Ans. <code>gets()<\/code> reads a line from the standard input, storing it in a buffer. The line consists of characters until the first newline or EOF. If a newline is encountered, <code>gets()<\/code> replaces it with a null character before returning the line. Note that <code>gets()<\/code> lacks input boundary checking, making it unsafe; it is deprecated in modern C, and safer alternatives like <code>fgets()<\/code> are recommended.<\/p>\n\n<\/div>\n<\/div>\n<div id=\"faq-question-1700127997893\" class=\"rank-math-list-item\">\n<h4 class=\"rank-math-question \">Q2.What are the 4 functions in C?<\/h4>\n<div class=\"rank-math-answer \">\n\n<p>Ans.<strong>Printf(), scanf(), ceil(), and floor()<\/strong>\u00a0are examples of library functions.<\/p>\n\n<\/div>\n<\/div>\n<div id=\"faq-question-1700128010808\" class=\"rank-math-list-item\">\n<h4 class=\"rank-math-question \">Q3.What is the syntax of gets in C?<\/h4>\n<div class=\"rank-math-answer \">\n\n<p>Ans.<strong>char *gets(char *str)<\/strong>\u00a0<\/p>\n\n<\/div>\n<\/div>\n<\/div>\n<\/div>","protected":false},"excerpt":{"rendered":"<p>fgets() and gets() In C language Both gets() and fgets() are functions in C used for reading strings with spaces, but they have some key differences: fgets() the fgets() function reads a line from the specified stream and stores it into the string pointed to by the provided buffer (str). It stops reading under the &#8230; <a title=\"fgets() and gets() In C language\" class=\"read-more\" href=\"https:\/\/www.skillvertex.com\/blog\/fgets-and-gets-in-c-language\/\" aria-label=\"More on fgets() and gets() In C language\">Read more<\/a><\/p>\n","protected":false},"author":4,"featured_media":5424,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[27],"tags":[578],"class_list":["post-3577","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-c-programming","tag-fgets-and-gets-in-c-language","generate-columns","tablet-grid-50","mobile-grid-100","grid-parent","grid-33"],"_links":{"self":[{"href":"https:\/\/www.skillvertex.com\/blog\/wp-json\/wp\/v2\/posts\/3577","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.skillvertex.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.skillvertex.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.skillvertex.com\/blog\/wp-json\/wp\/v2\/users\/4"}],"replies":[{"embeddable":true,"href":"https:\/\/www.skillvertex.com\/blog\/wp-json\/wp\/v2\/comments?post=3577"}],"version-history":[{"count":6,"href":"https:\/\/www.skillvertex.com\/blog\/wp-json\/wp\/v2\/posts\/3577\/revisions"}],"predecessor-version":[{"id":7946,"href":"https:\/\/www.skillvertex.com\/blog\/wp-json\/wp\/v2\/posts\/3577\/revisions\/7946"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.skillvertex.com\/blog\/wp-json\/wp\/v2\/media\/5424"}],"wp:attachment":[{"href":"https:\/\/www.skillvertex.com\/blog\/wp-json\/wp\/v2\/media?parent=3577"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.skillvertex.com\/blog\/wp-json\/wp\/v2\/categories?post=3577"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.skillvertex.com\/blog\/wp-json\/wp\/v2\/tags?post=3577"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}