How to swap two variables in one line in C? [duplicate] - c

This question already has answers here:
Interchanging values of two variables by xor operator using references or pointers
(2 answers)
Swapping two variable value without using third variable
(31 answers)
Closed 9 years ago.
I want to know that is there any other way of swapping 2 numbers in one line and of course without 3rd variable.
I know one way of doing this:
b=a+b-(a=b)
or
a=a+b-(b=a)
both are same(approximately). If you know then please help me out.

The frequently cited classic answer that you are probably looking for is:
a^=b^=a^=b;
But, it is technically wrong, because it changes the same variable more than once before a sequence point.

Use bit twiddling in C. Following swap two variables:
if (a != b) {
a ^= b ^= a ^= b;
}

Related

Any append (Python) or push_back (C++) equivalent in C? [duplicate]

This question already has an answer here:
Python-like array filling - C equivalent
(1 answer)
Closed 2 years ago.
I'm looking for an "append" (Python) or "push_back" (C++) equivalent in C to fill an array of strings (char). Does it exists?
The question doesn't make sense because you cannot add elements to an array in C. In C, you set the size of the array when you create it, and the size cannot change.
If you want to "append", you have to create a big array (e.g. 1000 items), and use a separate variable to remember how many items you're actually using (the rest are spare).

What is a bit vector? [duplicate]

This question already has answers here:
C/C++ Bit Array or Bit Vector
(5 answers)
Closed 9 years ago.
I looked online for a good while now, and can't seem to find a good example of what a bit vector actually is.
I have an assignment to do for college which is to add, remove, union of 2 vectors and the intersection too. But I am struggling to comprehend what an actual bit vector is. I am using C to write this.
Could someone please help me on this, it would be a massive help.
Bit vector is a structure which purpose is an accessability of individual bits. Implementation-wise it can be an integer array with some function provided for addressing and manipulation of individual bits of the array. To the final user the array has to look as a "string" of bits, and the functions have to be able to access arbitrary nth bit of the "string".
There is a bitset class in the standard library of C++ which represent this concept, but I am not aware of some option in C language.

code won't compile generates infinite 2222s [duplicate]

This question already has answers here:
C code won't finish running
(4 answers)
Closed 9 years ago.
The purpose of this code is to construct a char Hadamard matrix of the size of my choosing.
This question is related to a previous question I asked. The answer given there was an integer not char matrix, but the code here is pretty much the same format.
The code compiles but when executed it doesn't finish and I don't know why. When executed infinite 2's are printed.
I get the same result when swap the dynamic Hadamard matrix section for one of a fixed size.
Note: I've no idea what your program does, but obviously this is wrong. You failed to actually change the control variable in your for-loop (which can be done in the final expression or the loop body itself).
Change this:
for (ind=1;ind<=sizeH;ind*2)
to this:
for (ind=1;ind<=sizeH;ind*=2) // << note *=

What is the history of using i in for loops? [duplicate]

This question already has answers here:
Why are variables "i" and "j" used for counters?
(23 answers)
Closed 10 years ago.
In all the programming languages I have come across there seems to be the best practice to use variable i in for loop iterations. Usually i is followed by l in the nested loop. This seem to apply both for statically compiled and scripting languages.
What is the history of this practice? Does i mark for integer, index, or something else? Why, for example, we don't use x which would be more common, considering math background.
I've got two theories: i can stand for 1) index 2) integer (value of integer type)
It makes most sense that i stands for index because the loop is over each element of an array and each element is indexed.

Concept of function pointers in C? [duplicate]

This question already has answers here:
Closed 12 years ago.
Possible Duplicate:
What is the point of function pointers?
hi all,
I want to get the basic and concrete idea of function pointers in C language.
ie 1) its usage in C
2) main applications it is currently using
3) unique features
4) its scope in embedded applciations etc
Hoping your co operation in this too.
__Kanu
Function Pointers are pointers, that is variables, which point to the address of a function.
Nice example here. Also this answer is a must read.

Resources