// fgets vs scanf

#include <stdio.h>
int main()
{
    char str[100];
    char *p;

    printf("Enter any string: \n");
    fgets(str, 5, stdin);

    /* Assigning the base address str[0] to pointer
     * p. p = str is same as p = str[0]
     */
    p=str;
    printf("%s\n",p);
    printf("%x\n",*p);

    printf("The input string is: ");
    //'\0' signifies end of the string
    while(*p!='\0')
        printf("%c",*p++);

    return 0;
}

/*

Fgets is normally necessary to read the line of data that is given 
in the majority of circumstances. When bounds checking is done, 
Fgets is a superior choice as well as a better performance, 
allowing the assessment technique.

The term “fgets” usually refers to a type of C library function. Since its
introduction, the use of Fgets has spread to other libraries. Fgets has a lot 
of potentials when it comes to reading any file. The file must, however, be open.

Parameters of Comparison            Scanf	                 Fgets

Focuses on	                The specific type of pattern	A line from the given set of files
Limit on character	        No limits	                    A maximum limit exists
Limited To	                Standard inputs	Open files
What it is?	                Scan formats	                C library
Use	Dissection of the given piece of data	Read the line of data that is given.
*/

Embed on website

To embed this program on your website, copy the following code and paste it into your website's HTML: