2
printf("Enter name: ");
scanf("%s", first);
int s = sizeof(first);
for(i = 0; i < s; i++){
    if(s < 2 || s > 20){
        printf("improper array size");
    }
}
fprintf(fptr,"NAME: %s \n",first);

code checks if array size is less than 2 or greater than 20 but it does not seem to work. why is that?

Wasi Ahmad
  • 35,739
  • 32
  • 114
  • 161
Rgoat
  • 85
  • 1
  • 1
  • 7

3 Answers3

3

Example

char first[100];
printf("Enter name: ");
scanf("%s", first);
printf("%zu\n", sizeof(first));
printf("%zu\n", strlen(first));

Output

Enter name: Wasi
100
4
  • printf("%zu\n", sizeof(first)); - prints size of the char array which is 100.
  • printf("%zu\n", strlen(first)); - prints the length of the input string which is 4.

Update

There is no problem in your if condition. In the example given above, if I add the following code snippet -

for(int i = 0; i < s; i++){
    if(s < 2 || s > 20){
        printf("improper array size\n");
    }
}

It prints the message - improper array size 100 times which is the size of the array first.

Wasi Ahmad
  • 35,739
  • 32
  • 114
  • 161
2

You should be using strlen(3) to get the length of the input. sizeof will return the machine size of the type.

Ryan
  • 14,392
  • 8
  • 62
  • 102
2

Their are a couple of issues with your code:

  • scanf("%s", first) will not suffice, as it does not check for buffer overflow. If your char array is first[100], then you need to use scanf("%99s", first). the format specifier must be one less than the destination array, to account for the '\0' at the end. It would be helpful if we could also see the declaration of first.
  • sizeof(first) will only get the number of bytes in your array. If you again use char first[100], sizeof(first) will evaluate to 100.

    sizeof returns the number of bytes requires to store the type or variable supplied.

    Note: sizeof is an operator, not a function. Using no brackets is acceptable.

    As others have said, you can use strlen(), or simply loop until '\0' is found, which is the null-terminator that marks the end of the array. Here is an example:

    char first[100];
    printf("Enter name: ");
    scanf("%99s", first);
    for(size_t i = 0; first[i] != '\0'; i++) { /* Or i < strlen(first) */
    
  • Your if statement seems strange, and I don't see how it ties in with the loop. Additionally, Why are you checking s < 2 || s > 20 for valid array size? If you declare an array of size 10, then that is the valid array size. If you have used s like this:

    int s = sizeof(first);
    

    Then s will only be the size of the destination array. I can only assume that you want to check that 2 or more characters must be present, and no more than 20. Although, it depends on how many bytes first occupies. If this is the case, then you need to change your code slightly. Something like this would work:

    size_t slen = strlen(first);
    if (slen < 2 || slen > 20) {
        printf("invalid name size\n");
    }
    

    Which compares the amount of characters found in terms of your bounds. It verifies that the name can only be between 2 and 20 characters long. In order for a check like this to be suitable, then the size of first will have to be bigger than 20. Otherwise, checking slen < 2 would be enough.

RoadRunner
  • 25,803
  • 6
  • 42
  • 75