Sunday, 5 October 2014

Printing your name on the screen using gets() in C language

Printing your name on the screen using gets() in C language



Let’s see how to write this C program

#include<conio.h>
#include<stdio.h>
int main()
{
char myname[20];
printf(“enter your name : “);
gets(myname);
printf(“my name is %s“,myname);
getch();
return 0;
}


Details about scanf() and gets()

As you can see this program is almost similar to the previous program we have done.

The difference is that in this program we have used the gets() instead of scan() function to get input from the user.

Gets() is the function under the stdio.h header file.

Gets() can also be capable of receiving blank space inputs as scanf() is not

It is specially made for character type inputs on other hand scanf() is better for receiving the numeric inputs .
                            


Explanation of the program


As it is similar to the previous program so. I start explaining it from little ahead

Whatever user is going to type it will be store in the variable myname as a string or character.

And then we going to display it on the screen similarly as we have done it into our previous program using printf() function.


Pictures of the program


Step1: write a program




Step2: compile and run
Now I am not going to show how to run the program I have tell all that in my previous program or you can move to my first program for that.




Step3: output




Now here you can enter any of the string may be with or without blank space

With the help of gets() function we can also receive the character inputs like blank space.

You may increase the size of myname variable to hold the string of larger size

Example:
char myname[I];
//here I = any positive integer        

Note:  if any of the programmer is facing the problem of reappearing of the                      previous screen again and again
           He can use the function clrscr();
           It present in the conio.h header file
           After declaring the variables


Example in this program

……
………..
char myname[20];
clrscr();
printf(“enter your name : “);
…………
……………….


No comments:

Post a Comment