scanf

Using fgets after scanf [duplicate]

别等时光非礼了梦想. 提交于 2021-01-10 03:30:18
问题 This question already has answers here : fgets doesn't work after scanf [duplicate] (9 answers) Closed 18 days ago . I want to get rid of buffer overflow and I am using fgets instead of scanf to limit the characters. But whenever I enter something with scanf before fgets , it doesn't work correctly anymore. This code works correctly #include <stdio.h> int main() { char name[10]; printf("Who are you? \n"); fgets(name,10,stdin); printf("Good to meet you, %s.\n",name); return(0); } This code

Using fgets after scanf [duplicate]

*爱你&永不变心* 提交于 2021-01-10 03:29:25
问题 This question already has answers here : fgets doesn't work after scanf [duplicate] (9 answers) Closed 18 days ago . I want to get rid of buffer overflow and I am using fgets instead of scanf to limit the characters. But whenever I enter something with scanf before fgets , it doesn't work correctly anymore. This code works correctly #include <stdio.h> int main() { char name[10]; printf("Who are you? \n"); fgets(name,10,stdin); printf("Good to meet you, %s.\n",name); return(0); } This code

Array of strings won't print

醉酒当歌 提交于 2021-01-07 02:33:39
问题 I'm making a program where a teacher can enter the number of students and their full name. I don't know what I'm doing wrong because this is the first time I'm trying to print an array of strings. This is the part of my program that I'm having trouble with: #include <stdio.h> int main() { int n_students,i,b=1; char surname[20],first_name[20]; printf("number of students:"); scanf("%d",&n_students); for(i=0;i<n_students;i++) { printf("%d. ",b); scanf("%s %s",&surname[i],&first_name[i]); } for(i

Why is the C program giving weird output using Scanf?

我是研究僧i 提交于 2020-12-11 04:56:29
问题 I'm currently learning C programming and I have come across this weird output. // Program will try functionalities of the scanf function #include<stdio.h> #include<stdlib.h> int main(int argc, char const *argv[]) { char array[40]; scanf("Enter your address: %39s",array); //39 + 1/o null character at the end of the character array. printf("The address is : %s", array); return 0; } It should accept input address and output the same. PS C:\Users\G\Documents\C programs> gcc scanff.c -o scanff