xorl %eax, %eax

C Quiz No. 1

with 6 comments

This is a new category that I’m starting. You’re all welcome to express your ideas for the behavior of the programs. The first one is going to be really easy one.. So.. I have this nice prog.:

#include <stdio.h>

  int *c;
  c = (int [4]){1,*c,3};
  printf("%d\n", c[3]);
  return 0;

But why do I get this output when I’m running it?

sh-3.1$ gcc quiz1.c -std=c99 -pedantic -Wall -o quiz1
sh-3.1$ ./quiz1

I believe that most C developers already know the answer.. Let’s see. In any case, I’m going to answer this question in about 36 hours from now :) Have fun!

Written by xorl

January 27, 2009 at 15:30

Posted in C programming, fun

6 Responses

Subscribe to comments with RSS.

  1. The answer is that since you provide initialization in a brace-enclosed list, and omit to initialize the last element of the array (the one with index 3), it is implicitly initialized to zero.

    As the C standard states in Section 6.7.8 Initialization:

    “21 If there are fewer initializers in a brace-enclosed list than there are elements or members of an aggregate, or fewer characters in a string literal used to initialize an array of known size than there are elements in the array, the remainder of the aggregate shall be
    initialized implicitly the same as objects that have static storage duration.”

    This means that the above implicit initialization will be the same as the one imposed on static variables. Static variables are initialized at compilation time according to the following rule:

    “10 If an object that has automatic storage duration is not initialized explicitly, its value is indeterminate. If an object that has static storage duration is not initialized explicitly, then:
    — if it has pointer type, it is initialized to a null pointer;
    — if it has arithmetic type, it is initialized to (positive or unsigned) zero;
    — if it is an aggregate, every member is initialized (recursively) according to these rules;
    — if it is a union, the first named member is initialized (recursively) according to these rules.”


    January 27, 2009 at 23:26

  2. Exactly! And regarding the static variables you are right about the compile time initialization. Here is a part of the GCC(1) man page which clearly defines .BSS segment:

    If the target supports a BSS section, GCC by default puts variables
    that are initialized to zero into BSS. This can save space in the
    resulting code.

    This option turns off this behavior because some programs explic-
    itly rely on variables going to the data section. E.g., so that
    the resulting executable can find the beginning of that section
    and/or make assumptions based on that.

    The default is -fzero-initialized-in-bss.


    January 28, 2009 at 12:30

  3. I came across this post and I tried your program, but what I have is

    :~$ gcc quiz1.c -std=c99 -pedantic -Wall -o quiz1
    quiz1.c: In function ‘main’:
    quiz1.c:6: warning: ‘c’ is used uninitialized in this function

    :~$ ./quiz1
    Segmentation fault

    :~$ gcc –version
    gcc (Ubuntu 4.4.1-4ubuntu8) 4.4.1

    Maybe the compiler version has some effect!

    Just to let you know



    January 8, 2010 at 08:11

  4. Try to merge it in one line like:

    int *c = (int [4]){1,*c,3};

    Even though I’m not really sure if this is the problem.
    In a Debian with GCC 4.3.2-1.1 that I just tested it, it works as expected.


    January 8, 2010 at 09:06

  5. No way, The problem seems to be dereferencing c. If I use this instead

    int *c;
    c = (int [4]){1,c,3};

    it works as it should and as you say.

    Anyway, I learned about compound literals in C99, which I have never heard of =)

    Thanks again and bye


    January 8, 2010 at 11:45

  6. He belief that she would similar to the patch (obviously, or one wouldn\’t purchase wet now would ya?) but when the patch arrived she certainly was wempressed by the detail shen the patch. Wet\’s a little littleer that he concept itt would be even though i had a ruler and sketched it out – no mas, it would suggested this patch to anyone!
    the shipping and service was great – i wasn\’t going to ups a patch so you procure what you quiz, for there.


    June 5, 2013 at 05:33

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s