What are the input and output for strings in C language?

CServer Side ProgrammingProgramming

An array of characters (or) collection of characters is called a string.

Input and output for strings

Example

Following is the C program for input and output for strings −

#include<stdio.h>
main ( ){
   char a[30];
   printf("enter your name");
   scanf ( "%s",a);
   printf ("your name is %s",a);
   getch ( );
}

Output

When the above program is executed, it produces the following result −

1. Enter your name : Lucky 2. Enter your name : Lucky Lol
Your name is Lucky Your name is Lucky

Note

  • ‘&’ is not used for accepting strings because name of the string itself specifies the base address of the string.

  • Space is not accepted as a character by scanf( ).

  • ‘\0’ is placed by the compiler at the end.

Example

Following is the C program for using gets ( ) and puts ( ) for reading and writing strings −

#include<stdio.h>
main ( ){
   char a[30];
   printf ( "enter your name");
   gets (a);
   printf("Your name is");
   puts (a);
}

Output

When the above program is executed, it produces the following result −

1. Enter your Name : Lucky 2) Enter your name : Lucky Lol
Your name is Lucky Your name is Lucky Lol

Note − Space is also accepted as a character by gets ( ).

raja
Published on 08-Mar-2021 10:27:24
Advertisements