gets() function of stdio.h in C

In this article, we are going to learn about the gets() function of stdio.h header file in C programming language, and use it get string and then print it on console.
Submitted by Abhishek Sharma, on April 12, 2018

The function gets() is used to scan a string from the user without any \n or till the \n character. This function is from the string.h class doing the jobs related to strings.

Example:

    gets(str) ;
    User input: "I AM A STRING"

    Output:
    I AM A STRING

You can also use scanf() which shave more options then this function.

stdio.h - gets() function Example in C

#include <stdio.h>

int main()
{
    //initializing the type of variables
    char str[50];
    char c;

    //message for user
    printf("Please enter a string : ");
    gets(str);

    //printing the result
    printf("Your String: %s\n\n", str);

    //message for user
    printf("Please enter a character: ");
    c = getchar();

    //printing the result
    printf("Your Character: ");
    putchar(c);

    return (0);
}

Output

stdio.h - gets() in c language

C stdio.h Library Functions Programs »





Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.