Re: C Questions



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



Relevant Pages

  • Re: fields for methods?
    ... but only by a compiler that is allowed to ... struct A {void foo();}; ... int static_instance i = 0; ... Is not possible because foo is the only member of A... ...
    (comp.programming)
  • Re: fields for methods?
    ... void A::foo{ ... but only by a compiler that is allowed to ... int static_instance i = 0; ... it totally breaks the idea of encapsulation, which is the reason a lot ...
    (comp.programming)
  • Re: Virtual Machine implementation problem, Please help me to spot the bug
    ... This is non-standard (Conceivably your compiler allows it ... tmp1 might not be correctly aligned for u32. ... void change_endian{ ... typedef unsigned int u32; ...
    (comp.lang.c)
  • Re: The_Sage & void main()
    ... shall have a return type of type int but otherwise in all other respects ... > main may have the return type void then main may indeed have the ... If the compiler accepts void main, then you may use void main ... about whether void mainis conforming or not. ...
    (comp.lang.cpp)
  • Re: confusion: casting function pointers
    ... pointer from the 'actual/other modules' that takes arguments of type ... list to types of void *). ... int main{ ... without a prototype, a number of special "promotion" rules take ...
    (comp.lang.c)