Click here to Skip to main content
15,901,122 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
C
#include <stdio.h>
#include<string.h>
int main()
{
    char a[100];
    printf("enter name:");
    fgets(a,strlen(a),stdin);
    printf("%d",strlen(a));
}


output:
enter name:abc
4


What I have tried:

shouldn't the lenght be 3 instead of 4?
Posted
Updated 30-Apr-24 16:07pm
v2

C++
char a[100];
printf("enter name:");
fgets(a,strlen(a),stdin); // at this point a contains any random set of characters

You are calling strlen on an unitialised array, so the result will be anything from zero to the largest integer value. A better way to do this is to use either a #define like this:
C++
#define MAX_BUFFER 100
char a[MAX_BUFFER];
printf("enter name:");
fgets(a,MAX_BUFFER,stdin);

Or use the sizeof operator like this:
C++
char a[100];
printf("enter name:");
fgets(a,sizeof(a),stdin);


[edit]
As Rick York suggest below, the size values in the call to fgets shoud be one less than the size of the buffer. Interestingly, the example in the Microsoft documentation fails to take that into account.

So in case 1 it should be:
C++
fgets(a, MAX_BUFFER - 1, stdin);

and in case 2:
C++
fgets(a, sizeof(a) - 1, stdin);

[/edit]
 
Share this answer
 
v4
Comments
Rick York 30-Apr-24 17:34pm    
That works until someone enters 100 characters and then they have an unterminated string. This is why I prefer the #define (or a const int) and the declaration to be char a[MAX_BUFFER+1]={0}; The +1 leaves room for the null character and the rest of the code doesn't change. Also, initializing it adds the null every time, including when building in release mode. If sizeof is used then a -1 is needed to be safe.
merano99 30-Apr-24 22:14pm    
Exactly. I would also like to add that the missing end of the string becomes a problem for all entries with 100 or more characters. In addition, one could also think about what should happen with the rest of the line.
Richard MacCutchan 1-May-24 3:49am    
You are right of course. I was just trying to keep it simple.
merano99 30-Apr-24 22:31pm    
The comments that fgets() requires the specification of the maximum buffer size and not the length of an uninitialized string are correct, but do not answer the question of why the result is 4 and not 3 when entering "abc".
First of all, fgets() expects the maximum buffer size and not the length of an uninitialized string as already read in answer 1. When asked why the result is 4 and not 3, it should be noted that fgets() also reads the end-of-line character. When "abc" is entered, the result is a length of 4. In the case of hexadecimal output, you can see that the buffer contains the sequence 31 32 33 0a.
 
Share this answer
 
Comments
k5054 1-May-24 8:06am    
My 5
To to OP: you can confirm this by changing your printf statement to:
printf("strlen(%s) is %d\n", a, strlen(a))"
which will then print out:
strlen(abc
) is 4

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900