In this C program, we will learn about reading a string input from user and to print a string on screen.
Reading a StringWe can use scanf function with %s format specifier to read string from user. Here is the syntax of scanf to read a string
- Length of the input string should not be more than character array used for storing string. C doesn't perform any array index bound checking, hence it may overwrite something important.
- scanf() cannot use read space separated multi-word string like "Tech Crash Course". However we can use gets() to read multi-word string.
- gets() function can only read one string at a time.
Printing a String
To print a string we can either use printf with %s format specifier or puts() function. While printf can print multiple strings at a time puts can only print one string at a time.
- printf can print multiple string at a time, whereas puts can only print one string at a time.
- After printing string puts places the cursor on next line, whereas printf doesn't move cursor to next line.
C program to read and print string using scanf and printf
This program first takes a string as input from user using scanf function and stores it in a character array inputString. It automatically adds a null terminating character at the end of input string. Then it uses printf function to print inputString on screen.
#include <stdio.h> int main(){ char inputString[100]; printf("Enter a string\n"); /* Read string from user using scanf and store it in inputString char array */ scanf("%s", inputString); /* Print string stored in inputString using printf */ printf("%s\n", inputString); return 0; }Output
Enter a string TechCrashCourse TechCrashCourse
Enter a string Tech Crash Course Tech
C program to read and print string using gets and puts function
This program first takes a string as input from user using gets function and stores it in a character array inputString. The advantage of using gets function is that, it can read string containing white cpace characters but gets can only read one string at a time. Then it uses puts function to print inputString on screen.
#include <stdio.h> int main(){ char inputString[100]; printf("Enter a string\n"); /* Read string from user using gets and store it in inputString char array */ gets(inputString); puts(inputString); return 0; }Output
Enter a string Tech Crash Course Tech Crash Course
C program to read and print string using getchar and putchar function
getchar() function reads one character at a time. We can use getchar function inside a loop to read characters one by one till we don't read newline character (\n). Once we read newline character we break loop and add '\0' character at the end of string.
#include <stdio.h> int main(){ char inputString[100], c; int index = 0; printf("Enter a string\n"); /* Read string from user using getchar inside while loop */ while((c = getchar()) != '\n'){ inputString[index] = c; index++; } inputString[index] = '\0'; index = 0; while(inputString[index] != '\0'){ putchar(inputString[index]); index++; } return 0; }Output
Enter a string C Programming C Programming
Related Topics