Home Ask Login Register

Developers Planet

Your answer is one click away!

Syed Rumman February 2016

I get 4 when I use scanf_s but 1 when I use fgets()?

#include "stdafx.h"
#include <stdio.h>
#include <string.h>

void main()
    char s[200];
    int count = 0, i;

    printf("enter the string\n");
    //scanf_s("%[^\n]s", s, sizeof(s));
    fgets(s, sizeof*(s), stdin);
    for (i = 0;s[i] != '\0';i++)
        if (s[i] == ' ')
    printf("number of words in given string are: %d\n", count + 1);


Jay Kumar R February 2016

fgets(s, sizeof*(s), stdin); -- is wrong. it should be sizeof(s)

The *(s) is character (first element of array), while s is array. Because single character is of size 1, you got 1.

Post Status

Asked in February 2016
Viewed 2,141 times
Voted 7
Answered 1 times


Leave an answer

Quote of the day: live life