-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathInputtingString.c
More file actions
41 lines (32 loc) · 921 Bytes
/
InputtingString.c
File metadata and controls
41 lines (32 loc) · 921 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
#include <stdio.h>
void method1()
{
//You can use the scanf() function to read a string.
//The scanf() function reads the sequence of characters
//until it encounters whitespace (space, newline, tab, etc.)
char name[20];
printf("Enter your name: ");
scanf("%s", name); //Notice that we have used the code name instead of &name with scanf()
//This is because name is a char array, and we know that
//array names decay to pointers in C.
printf("Your name is %s", name);
}
void method2()
{
//fgets() - to read a line of string.
//puts() - display the string.
char name[30];
printf("Enter your name: ");
fgets(name, sizeof(name), stdin); //read String
printf("Name: ");
puts(name); //display String
/*Or
printf("Name: %s", name);
*/
}
int main()
{
//method1();
method2();
return 0;
}