Handling SIGALRM in C program.


Program for demonstrating use of REAL VIRTUAL PROF timers in Linux


Copying contents of one file to another using open() and read() system calls


Printing 1 to 1000 without loop or conditionals- C Programming

Task: Print numbers from 1 to 1000 without using any loop or conditional statements. Don’t just write the printf()  statement 1000 times.
Program Code 1:

Program Code 2:


Printing all Permutations of a String – C Programming Tutorial

In this c programming tutorial we are going to see a c program to print all permutations of a given string. Now let’s see what is a permutation.

A permutation, also called an arrangement number or order, is a rearrangement of the elements of an ordered list S into a one-to-one correspondence with S itself. A string of length n has n! permutation. Source: Mathword

Below are the permutations of string CBA.
Algorithm Paradigm: Backtracking
Time Complexity: O(n*n!)
Please write comments if you find the above codes/algorithms incorrect, or find other ways to solve the same problem.

 Output :
all permutation of string

Determining if Two Trees are Identical – C Programming Tutorial

In this c programming tutorial we are going to see how we can compare two trees to check whether they are identical or not. Two trees are identical when they have same data and arrangement of data is also same. To identify if two trees are identical, we need to traverse both trees simultaneously, and while traversing we need to compare data and children of the trees.
Below is the simple Algorithm to compare two trees:

Time Complexity :

Complexity of the identicalTree() will be according to the tree with lesser number of nodes. Let number of nodes in two trees be m and n then complexity of sameTree() is O(m) where m < n.

Program Code :

Output :

comparing 2 trees

Frequency of characters in a String – C Programming Tutorial

In this c programming tutorial we are going to see program which computes frequency of characters in a string i.e. which character is present how many times in a string. For example in the string “krishna” each of the character ‘k’, ‘r’, ‘i’,’s’,’h’,’n’, and ‘a’ has occurred one time. Note that only lower case alphabets are considered, other characters (uppercase and special characters) are ignored. For counting number of characters in a string we need to traverse the whole string from its start to end and store the count for each character that appears in string. For storing character count we can use simple integer array with name say “count” and having size 26 as there are only 26 alphabets (we are ignoring special symbols and uppercase characters).
Now to make program efficient we are making use of ASCII values of characters. See the “count[string[c]-‘a’]++” statement in code, consider input string begins with ‘a’ so c is 0 initially and string[0] = ‘a’ and string[0]-‘a’ = 0 (see ASCII codes for respective characters if you are still confused) and we increment count[0] i.e. a has occurred one time and repeat this till complete string is scanned and that’s it, afterward we simply printed the values in count[] array if its not equals to zero. You can easily modify this program to handle uppercase and special symbols.
Following is the complete C programming code for computing frequency of characters in a string.


freq of char in string

Print matrix in spiral form – C programming Tutorial

In this c programming tutorial we are going to see how to print elements of given matrix in spiral form. Consider we have a 2D array as input

        1  2  3  4

        5  6  7  8

        9 10 11 12

       13 14 15 16

Then our output should be as follows:
1 2 3 4 8 12 16 15 14 13 9 5 6 7 11 10

To get such output we need to traverse the matrix in spiral manner as show in diagram. To do this we will be using five variables k, l, m, n and i.

  • m for ending row index
  • k for starting row index
  • l for starting column index
  • n for ending column index 
  • i as iterator


Now note that for traversing matrix elements in spiral form we need to ignore row or column which is already printed, that’s what we did in the first three loops inside while statement. Following is the complete c programming code for printing matrix elements in spiral form.

C Program Code :


c programming tutorial

Implementing Vector – C Programming Tutorial

In this c programming tutorial we are going to see code for implementing vector in c language or creating array of size N (Any size array) in language. So let’s proceed, in this c programming tutorial we’re just going to create a dynamically sized array of integers. Here’s what the definition of a vector interface might look like:

Note that we are writing this code in header file and we will be calling it vector.h

Implementing Vector

Below is the code for implementation of the interface we defined (vector.h) above. We will be naming this code as vector.c

What we did here is simply allocated memory using malloc() function. The size of the memory allocated will be the simply the size of the vector we want, multiplied by the size of type of the vector we are going to create. Here we are creating Vector for storing integer values, so the size of memory would be “sizeof(int) * vector->capacity”. Note that if you want create vector for storing values with some other data type, then simply change the data type of the “* data” in structure we have defined to required one and that’s it.

Using our Vector

In this usage example, we keep things simple and just pass around the pointer to a variable called vector allocated on the stack, we will be naming this file as vector-usage.c


C Programming Interview Question 3

Problem Statement : The Utopian Tree goes through 2 cycles of growth every year. The first growth cycle occurs during the spring, when it doubles in height. The second growth cycle occurs during the summer, when its height increases by 1 meter. Now, a new Utopian Tree sapling is planted at the onset of spring. Its height is 1 meter. Can you find the height of the tree after N growth cycles?

Input Format : The first line contains an integer, T, the number of test cases. T lines follow; each line contains an integer, N, that denotes the number of cycles for that test case.
Output Format : For each test case, print the height of the Utopian Tree after N cycles. Each line thus has to contain a single integer, only.
Sample Input
Sample Output
Continue reading “C Programming Interview Question 3”