0

I am studying for an interview and found this question a bit bewildering. Would appreciate your advice.

What is not initialized by default?

  1. The last variable of a static array in which the first variables are explicitly initialized in a statement.
  2. Dynamic array members assigned using a calloc function.
  3. Global variable.
  4. All data provided here is initialized by default.
  5. The current cursor location when a file is open.
  6. A static variable.
  7. The last character in a static character set.

I think the answer is #1 but not sure about the explanation.

paxdiablo
  • 854,327
  • 234
  • 1,573
  • 1,953
jrz
  • 1,213
  • 4
  • 20
  • 54
  • 2
    Static variables are initialized before main is started. I think the answer is 4. – Aleksander Bobiński Aug 02 '20 at 07:27
  • 2
    "The current cursor location when a file is open" what does it mean? Also, this seems completely disjoint from others re. "default initialization". It's possible that the material/question is of dubious nature - provide info where (book/link, etc) you found this, too. – P.P Aug 02 '20 at 07:35
  • I'm guessing that statement is refering to fseek but good point, we shouldn't guess. – Aleksander Bobiński Aug 02 '20 at 07:39
  • 1
    @AleksanderBobiński yeah, but how is related to "default initialization"? OP needs to clarify their source. – P.P Aug 02 '20 at 07:43
  • I suspect "open" should be "opened" (as in `fopen`) - in that case the initial file offset would be initialised. – paxdiablo Aug 02 '20 at 11:13

1 Answers1

2
  1. In a static array (or in any array) when it is explicitly initialized all the non-initialized variables values will be 0.
  2. calloc zero the memory it points to.
  3. Global variables values are 0 by default.
  4. Placeholder (this simply means all other options are initialised).
  5. If not opened in append mode (a in fopen) the cursor location will be 0. In append mode it will be the length of the file.
  6. Static variables values are 0 by default.
  7. Static character set (if I understood it correctly) is a char array, and like any other array, when initialized the last char, if uninitialized, will be 0. In the case of a char array, the last char will be the NULL byte and its intention is to be 0 to mark the end of the string.

As you can see, all the options are initialized by default so the answer is 4.

If you are not sure, always check your questions with a simple code:

#include <stdio.h>
#include <stdlib.h>

void test1()
{
    static int arr[5] = { 1,2,3,4 };
    printf("Last variable is %d\n", arr[4]);
}

void test2()
{
    int* arr = (int*)calloc(5, sizeof(int));
    int b = 1;
    for (int i = 0; b && i < 5; i++)
        if (arr[i])
            b = 0;
    if (b) puts("Calloc zero all elements");
    else puts("Calloc doesn't zero all elements");
}

int test3_arg;

void test3()
{
    printf("Global variable default value is %d\n", test3_arg);
}

void test5()
{
    FILE* file = fopen(__FILE__, "r");
    printf("The current cursor location is %ld\n", ftell(file));
    fclose(file);
}

void test6()
{
    static int arg;
    printf("Static variable default value is %d\n", arg);
}

void test7()
{
    static char arg[] = "hello";
    printf("The last character is %d\n", arg[5]); //last one will be the NULL byte (0)
}

int main()
{
    test1();
    test2();
    test3();
    test5();
    test6();
    test7();

    return 0;

    /*
     * Output:
    Last variable is 0
    Calloc zero all elements
    Global variable default value is 0
    The current cursor location is 0
    Static variable default value is 0
    The last character is 0
     */
}
paxdiablo
  • 854,327
  • 234
  • 1,573
  • 1,953
Roy Avidan
  • 769
  • 8
  • 25
  • Unless you're very 100% (or more) sure your compiler is configured to be very 100% Standard compliant, testing with code is not really a good idea. – pmg Aug 02 '20 at 08:33
  • 4
    `0` is a perfectly valid *garbage* value, ie, something being `0` does not imply it was default initialized. – pmg Aug 02 '20 at 08:34
  • I know `0` can be garbage, but I knew the answers without the code either to know they are `0` and usually `0` is not a garbage value. Also, I didn't initialized the `0` values so I can`t decide them dudes. – Roy Avidan Aug 02 '20 at 08:36
  • @רועיאבידן : when you use `1.`, `2.`, etc in lines, it doesn't matter what the numbers are (I use actually `1` for them all) since SO assigns consecutive numbers. So, even though you had `1,2,3,5,6,7`, they showed up as `1-6`. Added a placeholder to fix that. – paxdiablo Aug 02 '20 at 11:17