C-Questions

A

aarklon

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....????
 
B

Ben Bacarisse

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.

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);
}
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.
 
A

aarklon

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

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
 


$)CHarald van D)&k

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

Ian Collins

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*).
 
M

Martin Ambuhl

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

Keith Thompson

Ian said:
(e-mail address 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

Ian Collins

Keith said:
Ian said:
(e-mail address 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*).
 
P

pete

Ian said:
Keith said:
Ian said:
(e-mail address 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.
 
K

Keith Thompson

Ian said:
Keith said:
Ian said:
(e-mail address 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.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,011
Latest member
AjaUqq1950

Latest Threads

Top