“volatile” keyword in C (and Java)

Problem What is the significance of the keyword “volatile” in C? Solution Lets under stand volatile in C Volatile tells the compiler not to optimize anything that has to do with the volatile variable.There is only one reason to use it: When you interface with hardware. Volatile informs the compiler that the value of the variable can change from the outside, without any update done by the code. [Read More]

Selection Sort

The sorting problem Input: Array of numbers , unsorted. Eg. Output : Same numbers sorted in some order, say increasing order. Eg. Selection Sort is a sorting algorithm that sorts data items into ascending or descending order, which comes under the category of in-place comparison sort. Pseudocode of selection sort Get the smallest element and put it in the first position Get the next smallest element and put it in the 2nd position …. [Read More]

Using == operator in better way in cpp

In cpp, it is possible that instead of
i==5

we can do

i=5

So we assign i = 5 and if it is like
if(cond)
cond gets true.

So better is
5==i
beause == is symmetric.
If someone writes by mistake is
5=i
As we get error = ‘can’t assign value to literal’.

Self reproducing program in C

main(){char\*p="main(){char\*p=%c%s%c;  
 (void)printf(p,34,p,34,10);}%c"  
 (void)printf(p,34,p,34,10);}

zero sized allocation using malloc

int main() { int \*p \= 0; printf("before addr: %pn", p); p \= (int \*) malloc(0); printf("after addr: %pn", p); printf("sizeof: %un", sizeof(\*p)); \*p \= 1; printf("\--- %d -- this is the last statment.n", \*p); free(p); } Output before addr: (nil) after addr: 0x80496c8 sizeof: 4 -– 1 – this is the last statment. Note : Linux allows a ‘read’ of the zero sized allocated memory allows a ‘write’ on the zero sized allocated memory sizeof shows an allocation of 4 bytes. [Read More]

return and exit from main: difference

Basically the difference between following programs ! //ret.c int main() { return 43; } //exit.c int main() { exit(43); } well, there are three ways for the processes to exit: - 1. Voluntary exit (implicit) 2. Voluntary exit (explicit) 3. Involunatary exit Voluntary exit can be implicit e.g. in case of return and explicit e.g. in case of a call to exit(). Involuntary exit is like being killed by a [Read More]

How to create function polymorphism in C

Why should we try for polymorphism in c? There are already many high-level, easy-to-use languages out there with full object-oriented capabilities, including polymorphism. Why insist on doing this in C? I am working on a communications systems simulator that is used, among other things, to estimate the complexity of different communication algorithms. It is able to do so at quite a low-level, keeping count of every operation required. At one point, I found myself needing to execute the same algorithm, but with different data types. [Read More]

C Strings

Strings are arrays of chars. String literals are words surrounded by double quotation marks. "This is a static string" ```To declare a string of 49 letters, you would want to say:``` char string5050; ```This would declare a string with a length of 50 characters. Do not forget that arrays begin at zero, not 1 for the index number. In addition, a string ends with a null character, literally a '\\0' character. [Read More]

Finding time of execution of code in c

#include <time.h\>  

clock_t start;
clock_t diff;
clock_t end;

start= clock();
various algorithm;
end = clock();

diff = end - start;
print diff;

Bubble sort

The sorting problem Input: Array of numbers , unsorted. Eg. Output : Same numbers sorted in some order, say increasing order. Eg. What is Bubble Sort? The bubble sort works by comparing each item in the list with the item next to it, and swapping them if required. The algorithm repeats this process until it makes a pass all the way through the list without swapping any items (in other words, all items are in the correct order). [Read More]