C-Questions

Discussion in 'C Programming' started by aarklon@gmail.com, Nov 19, 2007.

  1. Guest

    1) why this program gives o/p as variable = 123 value = 0
    does it mean that if a variable is not known the pre processor treats
    it as zero


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

    #if var == 0
    int variable = 123;
    #endif

    int main (void)
    {
    int value=0;

    printf("\n variable = %d value = %d",variable,value);

    puts ("");
    return(EXIT_SUCCESS);
    }

    2)

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

    int main(void)
    {

    struct st
    {
    int i;
    struct st *j;
    }t1,t2;

    t1.i = 123;
    t2.i = 456;

    t1.j = &t2;
    t2.j = &t1;

    printf("\n %d %d",*t2.j,*t1.j);

    puts("");
    return(EXIT_SUCCESS);
    }


    123 -1079205560

    why i am not getting o/p as 123 456
    or why i am not getting 456 in the o/p.

    3) printf("\n sizeof(volatile) === %d",(int)sizeof(volatile));
    printf("\n sizeof(const) === %d",(int)sizeof(const));

    why both these questions are giving o/p as 4 on my machine..???.
    does this mean that it is printing the size of int on my
    machine....????
    , Nov 19, 2007
    #1
    1. Advertising

  2. writes:

    > 1) why this program gives o/p as variable = 123 value = 0
    > does it mean that if a variable is not known the pre processor treats
    > it as zero


    Yes.

    <snip example of that>

    > 2)
    >
    > #include<stdlib.h>
    > #include<stdio.h>
    >
    > int main(void)
    > {
    >
    > struct st
    > {
    > int i;
    > struct st *j;
    > }t1,t2;
    >
    > t1.i = 123;
    > t2.i = 456;
    >
    > t1.j = &t2;
    > t2.j = &t1;
    >
    > printf("\n %d %d",*t2.j,*t1.j);


    Undefined behaviour. *t2.j is a structure. %d expects an int (ditto
    the other one).

    > puts("");
    > return(EXIT_SUCCESS);
    > }

    <snip>

    > 3) printf("\n sizeof(volatile) === %d",(int)sizeof(volatile));
    > printf("\n sizeof(const) === %d",(int)sizeof(const));
    >
    > why both these questions are giving o/p as 4 on my machine..???.
    > does this mean that it is printing the size of int on my
    > machine....????


    When you have only specifiers and qualifiers the default is
    assumed to be int.

    --
    Ben.
    Ben Bacarisse, Nov 19, 2007
    #2
    1. Advertising

  3. Guest


    >
    > > 3) printf("\n sizeof(volatile) === %d",(int)sizeof(volatile));
    > > printf("\n sizeof(const) === %d",(int)sizeof(const));

    >
    > > why both these questions are giving o/p as 4 on my machine..???.
    > > does this mean that it is printing the size of int on my
    > > machine....????

    >
    > When you have only specifiers and qualifiers the default is
    > assumed to be int.
    >
    > --
    > Ben.


    then what about printf("\n sizeof(NULL) = %d",(int)sizeof(NULL));
    giving o/p as :-
    sizeof(NULL) = 4

    is it because NULL is defined as (void*)0, so it sizeof(NULL)
    is actually giving the size of memory address at location 0
    , Nov 19, 2007
    #3
  4. On Mon, 19 Nov 2007 11:34:05 -0800, aarklon wrote:
    > then what about printf("\n sizeof(NULL) = %d",(int)sizeof(NULL));
    > giving o/p as :-
    > sizeof(NULL) = 4
    >
    > is it because NULL is defined as (void*)0, so it sizeof(NULL) is
    > actually giving the size of memory address at location 0


    *If* NULL is defined as ((void*)0) -- this is not always the case -- then
    sizeof(NULL) is equal to sizeof(void *). The value is irrelevant, only
    the type matters.
    Harald van Dij殺俎k, Nov 19, 2007
    #4
  5. Ian Collins Guest

    wrote:
    >>> 3) printf("\n sizeof(volatile) === %d",(int)sizeof(volatile));
    >>> printf("\n sizeof(const) === %d",(int)sizeof(const));
    >>> why both these questions are giving o/p as 4 on my machine..???.
    >>> does this mean that it is printing the size of int on my
    >>> machine....????

    >> When you have only specifiers and qualifiers the default is
    >> assumed to be int.
    >>
    >> --
    >> Ben.

    >
    > then what about printf("\n sizeof(NULL) = %d",(int)sizeof(NULL));
    > giving o/p as :-
    > sizeof(NULL) = 4
    >
    > is it because NULL is defined as (void*)0, so it sizeof(NULL)
    > is actually giving the size of memory address at location 0
    >

    No, it's sizeof(void*).

    --
    Ian Collins.
    Ian Collins, Nov 19, 2007
    #5
  6. wrote:
    > 1) why this program gives o/p as variable = 123 value = 0
    > does it mean that if a variable is not known the pre processor treats
    > it as zero


    Check the following from the standard, especially the last clause quoted:
    2 Preprocessing directives of the forms
    # if constantexpression newline group opt
    # elif constantexpression newline group opt
    check whether the controlling constant expression evaluates to nonzero.
    3 Prior to evaluation, macro invocations in the list of preprocessing
    tokens that will become the controlling constant expression are replaced
    (except for those macro names modified by the defined unary operator),
    just as in normal text. If the token defined is generated as a result of
    this replacement process or use of the defined unary operator
    does not match one of the two specified forms prior to macro
    replacement, the behavior is undefined. After all replacements due to
    macro expansion and the defined unary operator have been performed, all
    remaining identifiers are replaced with the pp number 0, and then each
    preprocessing token is converted into a token.

    [...]
    > 2)
    >
    > #include<stdlib.h>
    > #include<stdio.h>
    >
    > int main(void)
    > {
    >
    > struct st
    > {
    > int i;
    > struct st *j;
    > }t1,t2;
    >
    > t1.i = 123;
    > t2.i = 456;
    >
    > t1.j = &t2;
    > t2.j = &t1;
    >
    > printf("\n %d %d",*t2.j,*t1.j);

    ^^^^^ ^^^^
    Neither of these is an int. Any output is at best gibberish, as is the
    call to printf.

    >
    > puts("");
    > return(EXIT_SUCCESS);
    > }


    [...]
    > 3) printf("\n sizeof(volatile) === %d",(int)sizeof(volatile));
    > printf("\n sizeof(const) === %d",(int)sizeof(const));
    >
    > why both these questions are giving o/p as 4 on my machine..???.
    > does this mean that it is printing the size of int on my
    > machine....????


    It means that (sizeof volatile int) == 4 and sizeof(const int) == 4.
    Martin Ambuhl, Nov 19, 2007
    #6
  7. Ian Collins wrote:
    > wrote:

    [...]
    >> then what about printf("\n sizeof(NULL) = %d",(int)sizeof(NULL));
    >> giving o/p as :-
    >> sizeof(NULL) = 4
    >>
    >> is it because NULL is defined as (void*)0, so it sizeof(NULL)
    >> is actually giving the size of memory address at location 0
    >>

    > No, it's sizeof(void*).


    It's just as likely to be sizeof(int).

    The NULL macro expands to an implementation-defined null pointer
    constant. Likely possibilities are ``((void*)0)'', which is of type
    void*, and ``0'', which is of type int.

    sizeof(NULL) is legal, but not particularly useful.

    --
    Keith Thompson (The_Other_Keith) <>
    Looking for software development work in the San Diego area.
    "We must do something. This is something. Therefore, we must do this."
    -- Antony Jay and Jonathan Lynn, "Yes Minister"
    Keith Thompson, Nov 19, 2007
    #7
  8. Ian Collins Guest

    Keith Thompson wrote:
    > Ian Collins wrote:
    >> wrote:

    > [...]
    >>> then what about printf("\n sizeof(NULL) = %d",(int)sizeof(NULL));
    >>> giving o/p as :-
    >>> sizeof(NULL) = 4
    >>>
    >>> is it because NULL is defined as (void*)0, so it sizeof(NULL)
    >>> is actually giving the size of memory address at location 0
    >>>

    >> No, it's sizeof(void*).

    >
    > It's just as likely to be sizeof(int).
    >
    > The NULL macro expands to an implementation-defined null pointer
    > constant. Likely possibilities are ``((void*)0)'', which is of type
    > void*, and ``0'', which is of type int.
    >
    > sizeof(NULL) is legal, but not particularly useful.
    >

    I should have been explicit (I forgot the nature of this group!):

    If NULL *is* defined as (void*)0, sizeof(NULL) will be sizeof(void*).

    --
    Ian Collins.
    Ian Collins, Nov 19, 2007
    #8
  9. pete Guest

    Ian Collins wrote:
    >
    > Keith Thompson wrote:
    > > Ian Collins wrote:
    > >> wrote:

    > > [...]
    > >>> then what about printf("\n sizeof(NULL) = %d",(int)sizeof(NULL));
    > >>> giving o/p as :-
    > >>> sizeof(NULL) = 4
    > >>>
    > >>> is it because NULL is defined as (void*)0, so it sizeof(NULL)
    > >>> is actually giving the size of memory address at location 0
    > >>>
    > >> No, it's sizeof(void*).

    > >
    > > It's just as likely to be sizeof(int).
    > >
    > > The NULL macro expands to an implementation-defined null pointer
    > > constant. Likely possibilities are ``((void*)0)'', which is of type
    > > void*, and ``0'', which is of type int.
    > >
    > > sizeof(NULL) is legal, but not particularly useful.
    > >

    > I should have been explicit (I forgot the nature of this group!):
    >
    > If NULL *is* defined as (void*)0, sizeof(NULL) will be sizeof(void*).


    If NULL *is* defined as 0L, sizeof(NULL) will be sizeof(long).

    I've seen it.

    --
    pete
    pete, Nov 20, 2007
    #9
  10. Ian Collins wrote:
    > Keith Thompson wrote:
    >> Ian Collins wrote:
    >>> wrote:

    >> [...]
    >>>> then what about printf("\n sizeof(NULL) = %d",(int)sizeof(NULL));
    >>>> giving o/p as :-
    >>>> sizeof(NULL) = 4
    >>>>
    >>>> is it because NULL is defined as (void*)0, so it sizeof(NULL)
    >>>> is actually giving the size of memory address at location 0
    >>>>
    >>> No, it's sizeof(void*).

    >> It's just as likely to be sizeof(int).
    >>
    >> The NULL macro expands to an implementation-defined null pointer
    >> constant. Likely possibilities are ``((void*)0)'', which is of type
    >> void*, and ``0'', which is of type int.
    >>
    >> sizeof(NULL) is legal, but not particularly useful.
    >>

    > I should have been explicit (I forgot the nature of this group!):
    >
    > If NULL *is* defined as (void*)0, sizeof(NULL) will be sizeof(void*).


    I see; I should have realized you were assuming that.

    Pedantic quibble having nothing to do with your actual point follows.

    If NULL is defined as (void*)0, then the implementation is
    non-conforming because ``sizeof NULL'' becomes a syntax error (though
    ``sizeof(NULL)'' would still be valid). See C99 7.1.2p5:

    Any definition of an object-like macro described in this clause
    shall expand to code that is fully protected by parentheses where
    necessary, so that it groups in an arbitrary expression as if it
    were a single identifier.

    A common definition for NULL is ((void*)0). A subtle argument could be
    made that this is not a valid definition either, but that's the result
    of an oversight in the wording of the standard; it's clear that the
    intent is for ``#define NULL ((void*)0)'' to be a valid definition.
    (The argument is that the definition of a parenthesized expression in
    6.5.1p5 doesn't say that a parenthesized null pointer constant is a null
    pointer constant.)

    To anyone who wants to complain that this is a pedantic quibble, please
    note that I explicitly acknowledged that above.

    --
    Keith Thompson (The_Other_Keith) <>
    Looking for software development work in the San Diego area.
    "We must do something. This is something. Therefore, we must do this."
    -- Antony Jay and Jonathan Lynn, "Yes Minister"
    Keith Thompson, Nov 20, 2007
    #10
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Ali Syed
    Replies:
    3
    Views:
    552
    Mark McIntyre
    Oct 13, 2004
  2. pkirk25
    Replies:
    50
    Views:
    1,113
    Bill Pursell
    Oct 4, 2006
  3. Replies:
    10
    Views:
    547
  4. Vijay
    Replies:
    2
    Views:
    583
  5. reema
    Replies:
    0
    Views:
    261
    reema
    Aug 26, 2008
Loading...

Share This Page