Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > C-Questions

Reply
Thread Tools

C-Questions

 
 
aarklon@gmail.com
Guest
Posts: n/a
 
      11-19-2007
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....????
 
Reply With Quote
 
 
 
 
Ben Bacarisse
Guest
Posts: n/a
 
      11-19-2007
http://www.velocityreviews.com/forums/(E-Mail Removed) 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.
 
Reply With Quote
 
 
 
 
aarklon@gmail.com
Guest
Posts: n/a
 
      11-19-2007

>
> > 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

 
Reply With Quote
 
Harald van Dijk
Guest
Posts: n/a
 
      11-19-2007
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.
 
Reply With Quote
 
Ian Collins
Guest
Posts: n/a
 
      11-19-2007
(E-Mail Removed) 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.
 
Reply With Quote
 
Martin Ambuhl
Guest
Posts: n/a
 
      11-19-2007
(E-Mail Removed) 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 constant*expression new*line group opt
# elif constant*expression new*line 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.


 
Reply With Quote
 
Keith Thompson
Guest
Posts: n/a
 
      11-19-2007
Ian Collins wrote:
> (E-Mail Removed) 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) <(E-Mail Removed)>
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"
 
Reply With Quote
 
Ian Collins
Guest
Posts: n/a
 
      11-19-2007
Keith Thompson wrote:
> Ian Collins wrote:
>> (E-Mail Removed) 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.
 
Reply With Quote
 
pete
Guest
Posts: n/a
 
      11-20-2007
Ian Collins wrote:
>
> Keith Thompson wrote:
> > Ian Collins wrote:
> >> (E-Mail Removed) 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
 
Reply With Quote
 
Keith Thompson
Guest
Posts: n/a
 
      11-20-2007
Ian Collins wrote:
> Keith Thompson wrote:
>> Ian Collins wrote:
>>> (E-Mail Removed) 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) <(E-Mail Removed)>
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"
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off




Advertisments