Re: C Questions
- From: Ben Pfaff <blp@xxxxxxxxxxxxxxx>
- Date: Sat, 03 Nov 2007 13:48:02 -0700
aarklon@xxxxxxxxx writes:
1) why the following program is not showing compilation error in the
lines no: of arguments in the function declaration and function
definition do not match???
void swap();
No prototype is given for this function, so the compiler cannot
check that the arguments are the correct types.
To prototype a function with no parameters, use void:
void swap(void);
*a ^= *b,*b ^= *a, *a ^= *b;
See the C FAQ:
20.15c: How can I swap two values without using a temporary?
A: The standard hoary old assembly language programmer's trick is:
a ^= b;
b ^= a;
a ^= b;
But this sort of code has little place in modern, HLL
programming. Temporary variables are essentially free,
and the idiomatic code using three assignments, namely
int t = a;
a = b;
b = t;
is not only clearer to the human reader, it is more likely to be
recognized by the compiler and turned into the most-efficient
code (e.g. using a swap instruction, if available). The latter
code is obviously also amenable to use with pointers and
floating-point values, unlike the XOR trick. See also questions
3.3b and 10.3.
2) why (~0 == (unsigned int)-1)
can any body explain???
It's not necessarily true. But on 2's complement machines, -1 is
represented by all-bits-one.
3)why does printf("%d %f",14.0,2.50); gives o/p as 0 0.00000
Actually it yields undefined behavior: %d takes an int argument,
but you're providing a double argument.
4)why printf("%d",-1>>3); gives o/p as -1
Shifting a negative number right invokes undefined behavior, but
most machines define it to propagate the sign bit to the right.
Since -1 is all-bits-one on your machine, this means that
shifting -1 to the right doesn't change any bits.
5)
main()
{
int i= -1;
-i;
printf("i = %d -i = %d",i,-i);
}
the o/p is given as i = -1, -i =1
but when this pgm is run
main()
{
int i= -1;
+i;
printf("i = %d +i = %d",i,-i);
}
the o/p is given as i = -1, +i = -1
Compiler bug?
i=i^=j ||i;
Undefined behavior. See the C FAQ, e.g.
3.3: I've experimented with the code
int i = 3;
i = i++;
on several compilers. Some gave i the value 3, and some gave 4.
Which compiler is correct?
A: There is no correct answer; the expression is undefined. See
questions 3.1, 3.8, 3.9, and 11.33. (Also, note that neither
i++ nor ++i is the same as i+1. If you want to increment i,
use i=i+1, i+=1, i++, or ++i, not some combination. See also
question 3.12.)
--
A competent C programmer knows how to write C programs correctly,
a C expert knows enough to argue with Dan Pop, and a C expert
expert knows not to bother.
.
- Follow-Ups:
- Re: C Questions
- From: Old Wolf
- Re: C Questions
- From: Philip Potter
- Re: C Questions
- From: aarklon
- Re: C Questions
- From: Ark Khasin
- Re: C Questions
- References:
- C Questions
- From: aarklon
- C Questions
- Prev by Date: Re: Variable Length
- Next by Date: Re: Variable Length
- Previous by thread: C Questions
- Next by thread: Re: C Questions
- Index(es):
Relevant Pages
|
|