Anyone 'Help' me ...y my code is showing error?? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

Anyone 'Help' me ...y my code is showing error??

#include <stdio.h> main() { char name[20]; gets(name); printf("Your name is : %s", name); }

25th Nov 2020, 4:24 AM
Rohit Anand
10 Answers
+ 8
Not really showing an error, but some warnings. 1. You should have the return type of main() as an int 2. Be sure to return an int at the end of main. 3. Use fgets() instead of gets() for better safety against overflow during input. Code will look like; #include <stdio.h> int main() { char name[20]; fgets(name, sizeof(name), stdin); printf("Your name is : %s", name); return 0; }
25th Nov 2020, 4:39 AM
ChaoticDawg
ChaoticDawg - avatar
+ 5
The fgets() function takes in 3 arguments. The first is the pointer to the char array. The second is the max number of characters that is allowed for the first argument. This is so as not to overflow the array. This could also just be an int for the length of the array (20), but sizeof(char) = 1 so in this case it will return 20 for the char array. The third is a pointer to the stream to be used. In this case the standard input stream (stdin). https://www.google.com/amp/s/www.geeksforgeeks.org/fgets-gets-c-language/amp/
25th Nov 2020, 4:55 AM
ChaoticDawg
ChaoticDawg - avatar
+ 5
LastSecond959 I see... However, your phrasing implied that ChaoticDawg misrepresented sizeof as a function, which he did not. Perhaps you meant to write something like this: ---- "Building upon what ChaoticDawg explained, it should be noted that sizeof is an operator, not a function."
26th Nov 2020, 8:08 AM
David Carroll
David Carroll - avatar
+ 3
thanks
25th Nov 2020, 5:01 AM
Rohit Anand
+ 3
LastSecond959 ??? I never said it was a function. I am very aware that sizeof is an operator. Your comment is baseless. Maybe re-read what I wrote.
26th Nov 2020, 12:42 AM
ChaoticDawg
ChaoticDawg - avatar
+ 3
ChaoticDawg LOL... I too was puzzled by the clarification from LastSecond959. 🤷‍♂️
26th Nov 2020, 4:25 AM
David Carroll
David Carroll - avatar
+ 2
LastSecond959 OK, So, why name me in the post? Next time instead just post it to the OP as an FYI? Otherwise, it makes it seem like you are calling me out for giving bad or incorrect information, which clearly was not the case.
26th Nov 2020, 8:08 AM
ChaoticDawg
ChaoticDawg - avatar
+ 1
ChaoticDawg ok...but y is "sizeof(name)" and "stdin" used...explain me...
25th Nov 2020, 4:44 AM
Rohit Anand
0
ChaoticDawg sizeof is an operator, not a function. So typing sizeof name is not misleading, EXCEPT that you need parentheses for sizeof of the name of the data type (e.g. sizeof(int)).
26th Nov 2020, 12:27 AM
LastSecond959
LastSecond959 - avatar
0
ChaoticDawg The reason I said it because in my college, it was said to be a function, I'm just trying to prevent Rohit Anand from misunderstanding it.
26th Nov 2020, 7:45 AM
LastSecond959
LastSecond959 - avatar