Sunday, 28 September 2014

Printing your name on the screen in C language

Printing your name on the screen 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 : “);
scanf(“%s”,&myname);
printf(“my name is %s“,myname);
getch();
return 0;
}


Explanation of the above program


Preprocessors, header files, main()
We had discussed it in our previous program.

char myname[20] ;
This statement declares a variable myname of type character whose size is of 20. Which means myname variable can store up to 20 characters.

Printf()
This is a kind of a function which is use to display something on the screen
What so ever is written in the double quotes “ ”

Scanf()
This is also a function which is similar to print().
It is used to get the input from the user and stores it into the particular variable

%s
Signifies the character format of values we need to store in the variable
And this is also used to display the value of character variable

Getch()
This function is found in the conio.h library/header file   
This will going to hold the screen until any button is pressed from the keyboard

Return 0
This will going to return the null value to the main() as main() is declared in the int data type so it has to return a integer value to main()



Pictures of program:

Step 1:  Create the program:-


Step 2: compile and run:



Step 3: output 

In the output you need to enter only your first name and press enter



Step 4: run the program again and enter your full name in it



In this case it will only going to show the character entered before space-bar
Next we learn how to overcome this problem

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