norixia
norixia

Reputation: 13

What is the difference between char array[100]; and char array[100] = "";?

I am trying to write a C program that loads a file, reads it and outputs the longest line from the file together with the number of symbols. The result is written in another file. The code seems to be working like it is, but I would like to understand why I get wrong results once I slightly change the definition of the arrays, to remove the equals to empty quotes (= "") from the maxLine definition. For example, if I write the following:

char currentLine[100]; 
char maxLine[100]; 

then I get unwanted results.

Here is the whole function:

#define MAX_FILE_NAME 50

void maxCharRow()
{
    FILE *fptr;
    errno_t err;

    char fileNameRead[MAX_FILE_NAME] = "test.txt";
    char fileNameWrite[MAX_FILE_NAME] = "results.txt";
    char currentLine[100]; 
    char maxLine[100] = "";  

    if ((err = fopen_s(&fptr, fileNameRead, "r")) != NULL) {
        printf("Could not open the file: %s\n", fileNameRead);
        exit(1);
    }

    while (fgets(currentLine, sizeof(currentLine), fptr) != NULL)
    {
        if (strlen(maxLine) < strlen(currentLine))
        {
            strcpy_s(maxLine, currentLine);
        }
    }
    printf("\nLongest line in file has %i symbols\nand its content is:%s", strlen(maxLine), maxLine);

    ((err = fopen_s(&fptr, fileNameWrite, "w")) != NULL); {
        fprintf(fptr, "%s", maxLine);
        exit(1);
    }
    fclose(fptr);
}

Upvotes: 0

Views: 120

Answers (2)

nekineki
nekineki

Reputation: 38

In c "" is an empty string terminated by null character 0.

Since char currentLine[100] is a local variable it is by default not initialized to zero and is filled with indeterminate values.

But if you initialize zero-th element to 0 or "" then c will initialize whole array with zeros.

example:

#include <stdio.h>

int main()
{
    char arr0[100];
    char arr1[100] = "";
    char arr2[100] = {0};

    for(int i=0; i<100; ++i){
        printf("%d ", arr0[i]);
    }
    printf("\n");
    for(int i=0; i<100; ++i){
        printf("%d ", arr1[i]);
    }
    printf("\n");
    for(int i=0; i<100; ++i){
        printf("%d ", arr2[i]);
    }
    printf("\n");
}

Output:

0 0 0 0 0 0 0 0 -1 -75 -16 0 0 0 0 0 -62 0 0 0 0 0 0 0 -89 -82 55 -96 -4 127 0 0 -90 -82 55 -96 -4 127 0 0 85 -105 -84 -34 -92 127 0 0 1 0 0 0 0 0 0 0 101 88 74 28 -111 85 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 32 88 74 28 -111 85 0 0 -48 85 74 28 -111 85 0 0 -80 -81 55 -96 
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0

Edit:

Also note that even if you set first eg. 3 elements to non-zero values char arr0[100] = {1,2,3} the rest of the elements will still be initialized to zero.

Upvotes: 2

S.S. Anne
S.S. Anne

Reputation: 15576

Assigning "" to an array null-terminates the array so it becomes a string.

It's like doing this:

char array[100] = { '\0' };

Note that this isn't needed at global scope because variables at global scope are already zero-initialized.

Upvotes: 1

Related Questions