C Tutorial
Let us see how to read multiple line of string from console using gets function and two dimensional array.
how many lines required to be get it from user and gets user inputs for multiple lines and prints the list of lines provided by user.
num_of_lines variable is to have required number of lines.
lines two dimensional character array to have the multiple lines of string.
#include<stdio.h> void main() { int num_of_lines, i; char lines[10][100]; printf("\nEnter number of lines: "); scanf("%d", &num_of_lines); for(i=0;i<num_of_lines;i++) { gets(lines[i]); } printf("\nMultiple lines: "); for(i=0;i<num_of_lines;i++) { printf("%s\n", lines[i]); } }
Output:
$ cc multiple_line_string.c gets_function.c: In function ‘main’: gets_function.c:11:4: warning: implicit declaration of function ‘gets’ [-Wimplicit-function-declaration] gets(lines[i]); ^~~~ /tmp/ccJwNAJT.o: In function `main': gets_function.c:(.text+0x6b): warning: the `gets' function is dangerous and should not be used. $ ./a.out Enter number of lines: 5 testing codingpointer.com testing2 codingpointer.com testing3 codingpointer.com testing4 codingpointer.com Multiple lines: testing codingpointer.com testing2 codingpointer.com testing3 codingpointer.com testing4 codingpointer.com
C Tutorial
Privacy Policy | Copyright2020 - All Rights Reserved. | Contact us
| Report website issues in Github
| Facebook page
| Google+ page