Simple question not for test but for discussion

2 views
Skip to first unread message

Ravina

unread,
Jul 30, 2010, 10:44:35 AM7/30/10
to Knowledge Test
1.Is C allow function like

void call(int /*no name here*/)
{
printf("Hello")
}

2. what is the o/p of following code

main()
{
int i=-3,j=2,k=0,m;
m=++i&&++j||++k;
printf("%d%d%d%d",i,j,k,m);

}

3. Is the size of int is always 2 byte?

4. Consider the following declarations:
int a[10];
int *p=a;
Which among the following is portable way to print value of
pointer p?
a) printf("%u\n",p);
b) printf("%p\n",(void*)p);
c) printf("%d\n",p);
d) printf("%lu",p);

5. conio.h is an ANSI standard header file. True or false

TheSuyog

unread,
Jul 31, 2010, 12:13:23 PM7/31/10
to Knowledge Test
1. The C function is perfectly okay & valid, just if u add a ';' at d
end of "printf" statement. And while the function you need to pass
some integer parameter to the function.

2. The o/p of this code should have been: -2311 but god knows how is
it coming: -2301.... if u have any explanation please reply...

3. Size of a datatype depends on the operating system it is
implemented upon, but is usually 2 bytes. (2 under Windows & 4 under
Unix and similar OSs'.).

4. Can u plz elaborate "portability" in this context? All 3 ways are
obviously valid just that:
1: gives garbage value for the first element of the array,
2: gives its address in segment:offset format,
3: gives same value as in 1.,
4: gives unsigned long garbage value...

5. conio.h isn't a standard ANSI header file. For a list of ANSI C
Standard Library refer: http://en.wikipedia.org/wiki/C_standard_library

Hope I'm satisfactorily correct. :)

Dhruvesh harsola

unread,
Jul 31, 2010, 12:52:06 PM7/31/10
to knowledge-...@googlegroups.com
@Himanshu..
    Keep it up boy..

2010/7/31 TheSuyog <himanshu...@gmail.com>

Ravina

unread,
Aug 1, 2010, 5:21:37 AM8/1/10
to Knowledge Test

1. yes it vaild after putting ; (i forgot to write).....this type
fuction needed 4 specific purpose search it

2.o/p is -2301 bcoz in 'm=++i && ++j || ++k' it evaluates as
++i= -2
++j = 3
++i and ++j are non zero so
++i && ++j = 1
so value of whole expression '1 || ++k' will be 1, no matter what ++k
evaluates to.... compiler wont execute ++k and value of k will remain
0....
so final output will be -2 3 0 1....



3 perfect answer

4. It is often believed that pointers can be printed with %d / %u /
%lu. Though this might work on most implementation, it is not
guaranteed to work always. Because a pointer need not be an int or
unsigned int. The only format specifier suitable for printing pointer
value is %p.

5. yes conio.h is nt an ASCI standard header it is specific to Borland/
Turbo C compilers


@ Himanshu gud response

Himanshu Malpande

unread,
Aug 1, 2010, 11:55:59 AM8/1/10
to knowledge-...@googlegroups.com
%p might be the most portable format specifier but it does not print the value held at the memory location but the memory address itself...

Thx for the compliment guys :)

Ravina

unread,
Aug 2, 2010, 11:54:09 AM8/2/10
to Knowledge Test


It's nt clear wht u wanted to explain I think %p simply print the
address of the variable to which the pointer point,like
int a=2;
int*p=&a;
printf("%d",p) ; // print -12 or any value like it
printf("%p",p); //print FF2 or any address like it

pointer hold address nt any int or signed unsigned value that's way %p
should be used

TheSuyog

unread,
Aug 23, 2010, 6:39:25 AM8/23/10
to Knowledge Test
For 2nd.: agreed that no matter what ++k evaluates to, '1 || ++k' will
always be 1 but why won't the compiler execute ++k at all? what the
compiler's got problem with that??

Pallavi Jajoo

unread,
Aug 26, 2010, 4:26:02 AM8/26/10
to knowledge-...@googlegroups.com


gud questn...i 2 need its rite explanatin.....
bt in ma view....... "1||++k" compiler jus check 4 either f d conditn 2 be true ....wen it simply gets d 1st conditn true it doesn't bother abt 2nd exprssin.....

SONALI-

unread,
Aug 26, 2010, 12:57:22 PM8/26/10
to knowledge-...@googlegroups.com
yup pallavi i think its d rite answer because..or condition define itself either one of dem must be true.if the compiler gets true at beginning he wont compile the second one..am i rite???
Reply all
Reply to author
Forward
0 new messages