Like Tree1Likes
  • 1 Post By qny

Initializer in C

This is a discussion on Initializer in C within the C Programming forums, part of the General Programming Boards category; What does the term 'initializer' in C mean and what are the types of initializers?...

  1. #1
    Registered User
    Join Date
    Nov 2012
    Posts
    50

    Initializer in C

    What does the term 'initializer' in C mean and what are the types of initializers?

  2. #2
    qny
    qny is offline
    Registered User
    Join Date
    Sep 2012
    Posts
    355
    A few examples of initializers (in red)
    Code:
    int a = 42;
    int b[100] = {42, 24, 0, -1};
    a = 17; /* no initializer here */
    const double g = 9.8;

  3. #3
    Registered User
    Join Date
    Nov 2012
    Posts
    50
    Quote Originally Posted by qny View Post
    A few examples of initializers (in red)
    Code:
    int a = 42;
    int b[100] = {42, 24, 0, -1};
    a = 17; /* no initializer here */
    const double g = 9.8;
    thank you but i have heard something about scalar initializer and aggregate initialer what are they?

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,855
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  5. #5
    qny
    qny is offline
    Registered User
    Join Date
    Sep 2012
    Posts
    355
    Quote Originally Posted by Saurabh Mehta View Post
    i have heard something about scalar initializer and aggregate initialer what are they?
    Where have you heard about that? They should have an explanation for the terms ...

    Scalar is when you initialize one "element". Aggregate is when you initialize more than one.
    In the examples above, the 1st and 3rd example are scalar initializers, the 2nd is an aggregate initializer.
    I don't really find the distinction useful.

  6. #6
    Registered User
    Join Date
    Nov 2012
    Posts
    1,220
    The response from qny is correct. However consider this for loop:

    Code:
    for (i=0; i < 30; i++)
      printf(".");
    The purpose of the first part of the for loop syntax is for an initializer, even though it may not be formally an "initializer". So, informally, I would say that an initializer is any statement which assigns an initial value to a variable.

    Code:
    i=0;
    while (i < 30) {
      printf(".");
      i++;
    }
    There is another type of initializer, which is a static initializer. Static initializers happen conceptually at compile time. They can be used to make a function which "remembers" its values in between calls.

    Code:
    void print_foo()
    {
      static int foo=0;
      printf("Foo is %d\n", foo);
      foo++;
    }

  7. #7
    Registered User
    Join Date
    Nov 2012
    Posts
    50
    @ c99tutorial So to sum up in for eg: int a=6; 6 is an scalar initializer right
    or the whole statement is initializer?

  8. #8
    qny
    qny is offline
    Registered User
    Join Date
    Sep 2012
    Posts
    355
    Quote Originally Posted by Saurabh Mehta View Post
    @ c99tutorial So to sum up in for eg: int a=6; 6 is an scalar initializer right
    or the whole statement is initializer?
    The statement is a definition, an initialization, and a declaration all in one. the initializer is 6.

    You can have declarations that are not definitions;
    You can have definitions thar are not initializations;
    All initializatiosn are at the same time definitions and declarations.

    Code:
    extern int a[256]; /* declaration, not definition, not initialization */
    int b[100]; /* declaration and definition, not initialization */
    int c[4] = {1, 2}; /* declaration, definition, and initialization */
    Saurabh Mehta likes this.

  9. #9
    Registered User
    Join Date
    Nov 2012
    Posts
    50
    @qny thank you for simple an useful explanation

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. invalid initializer
    By Milhas in forum C Programming
    Replies: 6
    Last Post: 03-30-2008, 05:11 PM
  2. Using 'this' in Initializer List?
    By Tonto in forum C++ Programming
    Replies: 4
    Last Post: 11-20-2006, 01:34 PM
  3. invalid initializer
    By dr$brown in forum C Programming
    Replies: 8
    Last Post: 01-02-2005, 09:44 AM
  4. initializer?
    By Luigi in forum C++ Programming
    Replies: 0
    Last Post: 11-29-2002, 12:31 PM
  5. Initializer Lists
    By DISGUISED in forum C++ Programming
    Replies: 2
    Last Post: 01-18-2002, 06:12 PM

Tags for this Thread


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21