Sunday 18 October 2020

C program to read a name from keyboard and display it on the screen

October 18, 2020 Posted by Knowledge Bite , No comments

 Hello Friends

Today i will tell you about the String's in the 'C' Language. String's are basically Character Array. 

Examples of of string constants

char name[]={'K','N','O','W','L','E','D','G','E','\0'};

char name[]="KNOWLEDGE";

It is a string constant of one dimensional array of characters which is terminated by null ('\0');

here '\0' is called the null character in it \n indicates that what it follows is something special. In string's null character is most important because it is the only one which know where the string ends. Null character i.e. '\0' need not to be entered in the declaration 'C' automatically insert it at the end of the string's so we can declare them as in the above 2nd example

Each character in the string which is stored in an array occupy one byte of memory.

let's try one example of printing the string

//Program to print the string

#include<stdio.h>

#include<conio.h>

    {

        char name[]="KNOWLEDGE BITE";        

        int t=0;

        clrscr();

            while (t<=13)

                {    

                    printf("%c",name[t]);

                    t++;

                }

    getch();    

    }


Output will be:

KNOWLEDGE BITE

NOW LET'S TRY ANOTHER C PROGRAM TO READ A NAME FROM KEYBOARD AND DISPLAY IT ON THE SCREEN.

#include<conio.h>

#include<stdio.h>

void main()

    {

        char name[30];

        clrscr();

        printf(" Hi! What is your Full Name. \n User Enter Name from Keyboard ");

        scanf(%[^\n]s",name);

        printf("O Hi! My Full Name is %s ",name);

        getch();

    }

Output of the given program is

Hi! What is your Name.

User Enter Name from Keyboard #Knowledge_Bite

O Hi! My Full Name is #Knowledge_Bite