1

I am new to C programming and working through a book called " Sam's Teach Yourself C Programming in One Hour A Day"

One of the Exercise programs in chapter 2 is giving me an error I am too novice to understand. A little help and clear explanation without pompous sarcasm would be much obliged! Thank you for your time and consideration.

Exercise Program

Sumner Evans
  • 8,951
  • 5
  • 30
  • 47
  • 2
    `fgets(buffer);` --> `fgets(buffer, sizeof buffer, stdin);`, `%d` --> `%zu` – BLUEPIXY Nov 18 '16 at 05:33
  • 2
    For future questions please copy/paste the code and error messages as text (indented by 4 columns so it gets formatted correctly) instead of using a screenshot. Screenshots can be difficult to read and it is impossible to copy/paste the example code from if someone wants to try out the code locally. – Michael Burr Nov 18 '16 at 05:41
  • Thank you for the advice. I will follow those instructions for posting from now on. I truly appreciate your time and consideration. Cheers! – Christopher Pettit Nov 18 '16 at 06:04
  • @ChristopherPettit totally unrelated, what text editor is that? – RoadRunner Nov 18 '16 at 06:05
  • You should accept the answer that solved your problem by clicking the check mark. – Keith Thompson Nov 20 '16 at 04:59

1 Answers1

1

The error says it all: fgets() expects three arguments. You are giving it one.

So, call it like this:

fgets(buffer, 256, stdin)

buffer is where the input is to be stored, 256 is the size of the buffer, stdin is the stream to read from.

Also, use %lu instead of %d as the format specifier for unsigned long.

Edit: Use the z modifier as %zu for the value returned by strlen, which is of type size_t

Community
  • 1
  • 1
Bijay Gurung
  • 1,094
  • 8
  • 12