Home / Programs / Read string from user using C Functions
Programming Example

Read string from user using C Functions

👁 381 Views
💻 Practical Program
📘 Step by Step Learning
In this program we will use gets() function to take the string value in the array variable.

Strings handling functions are defined under "string.h" header file.

  #include <string.h> 

Program Code

#include<stdio.h>
#include<string.h>
int main()
{
int i;

char arrayName[100];
gets(arrayName);

   for(i = 0; arrayName[i] != 0; i++)
    {
    printf("%c", arrayName[i]);
    }
}

Output

Hello
Hello

Explanation

You can user this code for the same program

Note: Though, gets() and puts() function handle strings, both these functions are defined in "string.h" header file.



#include<stdio.h>
#include<string.h>
int main()
{
int i;
char arrayName[100];
gets(arrayName);
puts(arrayName);
}


How to learn from this program

First read the algorithm, then study the program code line by line. After that, compare the code with the output and finally go through the explanation. This approach helps learners understand both the logic and the implementation properly.