C Program To Reverse Singly Linked List

PROGRAM:
#include <stdio.h>
#include <stdlib.h>

typedef struct _list {
int data;
struct _list *next;
}list;

int main(int argc, char **argv)
{
printf("\n.. Start of singly linked list reverse program .. \n\n");

list *head=NULL, *head_start=NULL, *display = NULL;
list *newnode;
int a = 0;

/* Add 5 elements to linked list */
for (; a<10; a++)
{
newnode = malloc(sizeof(list));
newnode->data = a+1;
newnode->next = NULL;
if (a==0) {
head = newnode;
head_start = newnode;
}
else {
head->next = newnode;
head = head->next;
}
}

C program to check file available in a path


PROGRAM:
#include <stdio.h>
#include <unistd.h>
 
int main(void) {
    if( access("/home/pc/test.txt", F_OK) != 0 )
                printf("File not available\n");
        else
                printf("File available\n");
        return 0;
}
OUTPUT:
File not available

6. Pthread condition variable in Linux

Condition variable one type of thread synchronization mechanism.

By using condition variable we can tell one thread that when to execute.

using pthread_cond_signal pass the signal to another thread, this signal can be catch by another thread using pthread_cond_wait.

5. Pthread Mutex in Linux

Mutex is used to synchronize the thread.

What would be the final value of globalCount in below program?

4. Thread joining in Linux

Program which contain in the post this and this main doesn't wait for thread finishes their work. 
We can make the main thread wait for the child thread to finish their work with the help of thread join mechanism.
We need to set thread as joinable by using thread attribute.

3. Passing arguments to thread in Linux

Program contain in the previous thread post tell how to pass single argument to thread handler function. 

If you want to pass two or more variable to thread handler then need to define structure globally then pass the structure variable as argument when creating thread with the help of pthread_create.

2. Thread creation in Linux

Main program implicitly create the starting thread. Other thread can be created by programmer with the help of pthread_create API.
int pthread_create(pthread_t *thread, const pthread_attr_t *attr,  
                                 void *(*start_routine) (void *), void *arg);

1. Introduction to thread in Linux


What is thread?
    Thread is the independent piece of code executed parallel y when main program executed. 

Why Thread ?
    Consider you are creating student report program in C, It should contain number of students written final exam, and their name and total mark. 

What actually you do?
Answer is this . You will write main program which find number of students, list their name and total mark. So step by step you do.

But if you use thread, 
main program list students name and total mark. same time thread will find total number of students. So we can save some time if we use thread. 

Threading is an example of parallel programming.

Working environment :
    Here used pthread (POSIX standard thread) library under Ubuntu(Linux).
    gcc compiler used to compile the program

Compilations : 
   There is need to add pthread linker when compiling thread program with the help of gcc compiler.
gcc -o <output> <input.c> -lpthread 

C program to reverse the sring

PROGRAM : 
#include <stdio.h>
 
int main(int argc, char *argv[])
{
    char str[1024];
        char revStr[1024];
        int charCount = 0, x = 0, i = 0;
        
        printf("Enter the string : ");
        gets(str);
        while(str[charCount] != '\0')
                charCount++;
                
        for(x=charCount-1; x >= 0; x--){
                revStr[i] = str[x];
                i++;
        }
        revStr[i] = '\0';
        printf("\nReversed string : %s \n\n", revStr);
}
OUTPUT : 
Enter the string : TESTSTRING

 Reversed string : GNIRTSTSET 

Create shared library for C program in Linux

Consider you c program has two file, which are functions.c and main.c

Consider your current working directory is /home/sujin/shared/

functions.c
int add(int x, int y)
{
    return x + y;
}
 
int sub(int x, int y)
{
        return x - y;
}
 
int mul(int x, int y)
{
        return x * y;
}
 
int div(int x, int y)
{
        return x / y;
}
main.c
#include <stdio.h>
int add(int x, int y);
int sub(int x, int y);
int mul(int x, int y);
int div(int x, int y);
 
int main(int argc, char *argv[])
{
    printf("%d\n", add(10, 15));
        printf("%d\n", sub(50, 15));
        printf("%d\n", mul(10, 3));
        printf("%d\n", div(100, 4));
        return 0;
}

1. Compile library source file(functions.c) into position independent code
gcc -c -fpic functions.c -Wall
2. Create shared library from the object file generated
gcc -shared -o libfun.so functions.o 
3. Compile the main program with shared library
gcc -o output main.c -lfun
/usr/bin/ld: cannot find -lfun
collect2: ld returned 1 exit status
4. Compile with giving path of shared library
gcc -L/home/sujin/shared/ -Wall -o output main.c -lfun
5. Run the program
./output 
./output: error while loading shared libraries: libfun.so: cannot open shared object file: No such file or directory
6. Check current LD_LIBRARY_PATH. 
echo $LD_LIBRARY_PATH
7. ILD_LIBRARY_PATH is not current working directory or empty then set the LD_LIBRARY_PATHS as current working directory
LD_LIBRARY_PATH=/home/sujin/shared:$LD_LIBRARY_PATH
8. Run the program
./output 
./output: error while loading shared libraries: libfun.so: cannot open shared object file: No such file or directory
9. Export the LD_LIBRARY_PATH. In linux we need to export the changes in the environment variable
export LD_LIBRARY_PATH=/home/sujin/shared/:$LD_LIBRARY_PATH
10. Run the program
./output
25
35
30
25

Implementing switch using function pointer in C

PROGRAM : 
#include <stdio.h>
int add(int, int);
int sub(int, int);
int mul(int, int);
int div(int, int);
 
int add(int var1, int var2)
{
    return var1 + var2;
}
 
int sub(int var1, int var2)
{
        return var1 - var2;
}
 
int mul(int var1, int var2)
{
        return var1 * var2;
}
 
int div(int var1, int var2)
{
        return var1 / var2;
}
 
int main(int argc, char **argv)
{
        int (*fun_table[4]) ();
        int result = 0, var1 = 0, var2 = 0, choice = 0;
        fun_table[0] = add;
        fun_table[1] = sub;
        fun_table[2] = mul;
        fun_table[3] = div;
        printf("\nSelect : 1.add 2.sub 3.mul 4.div\n\n");
        printf("Enter your choice : ");
                scanf("%d", &choice);
                if(choice <= 4){
                        printf("\nEnter the first variable  : ");
                        scanf("%d", &var1);
                        printf("Enter the second variable : ");
                        scanf("%d", &var2);
                
                        result = (*fun_table[choice-1])(var1, var2);
                        printf("\nResult = %d\n\n", result);
                }
                else
                        printf("\nInvalid choice\n\n");
        return 0;
}
OUTPUT :
Select : 1.add 2.sub 3.mul 4.div

Enter your choice : 1

Enter the first variable  : 23
Enter the second variable : 20

Result = 43

Addition program using class and object in C++

PROGRAM : 
#include <iostream>
#include <string.h>
using namespace std;
 
class addition
{
    private:
        int var1, var2, result;
    public:
        void getValue();
        int addOperation();
};
 
void addition :: getValue()
{
        cout << "Enter the first value : " << "\n";
        cin >> var1;
        cout << "Enter the second value : " << "\n";
        cin >> var2;
}
 
int addition :: addOperation()
{
        result = var1 + var2;
        return result;
}
 
int main(int argc, char *argv[])
{
        addition obj1;
        int output = 0;
        obj1.getValue();
        output = obj1.addOperation();
        cout << "Result : " << output << endl;
        return 0;
}
OUTPUT : 
Enter the first value : 
25
Enter the second value : 
30

Result : 55

Create static library for C program in Linux

Consider your C program has three files, which are addSub.c, mulDiv.c and main.c

addSub.c : 
int add(int x, int y)
{
    return x + y;
}
 
int sub(int x, int y)
{
        return x - y;
}
mulDiv.c : 
int mul(int x, int y)
{
    return x * y;
}
 
int div(int x, int y)
{
        return x / y;
}
main.c : 
#include <stdio.h>
 
int main(int argc, char *argv[])
{
    printf("%d\n", add(10, 15));
    printf("%d\n", sub(50, 15));
    printf("%d\n", add(10, 3));
    printf("%d\n", add(100, 4));
    return 0;
}

1 . Compile and create object file for addSub.c and mulDiv.c 
gcc -c addSub.c mulDiv.c -Wall
2.  Create static library( liboperation.a ) for compiled files 
ar -cvq libopeartion.a addSub.o mulDiv.o
3.  We can list the available files from library file using following
ar -t libopeartion.a
4. Compile the main program with linking the library 
gcc -o output main.c libopeartion.a
5. Run the created output executable file
./output 

Passing function as argument in C


#include <stdio.h>
int add(int, int);
int sub(int, int);
int mul(int, int);
int div(int, int);
int myfunPtr(int , int , int(*)(int, int));
 
int add(int var1, int var2)
{
 return var1 + var2;
}
 
int sub(int var1, int var2)
{
 return var1 - var2;
}
 
int mul(int var1, int var2)
{
 return var1 * var2;
}
 
int div(int var1, int var2)
{
 return var1 / var2;
}
int myfunPtr(int a, int b, int(*argFun)(int, int))
{
 int x = 0;
 x = (*argFun)(a, b);
 return x;
}
 
int main(int argc, char **argv)
{
 int x = 0;
 x = myfunPtr(100, 5, add);
 printf("o/p : %d\n", x);
 
 x = myfunPtr(100, 5, sub);
 printf("o/p : %d\n", x);
 
 x = myfunPtr(100, 5, mul);
 printf("o/p : %d\n", x);
 
 x = myfunPtr(100, 5, div);
 printf("o/p : %d\n", x);
 
 return 0;
}



Function table in C



#include <stdio.h>
int add(int, int);
int sub(int, int);
int mul(int, int);
int div(int, int);
 
int add(int var1, int var2)
{
    return var1 + var2;
}
 
int sub(int var1, int var2)
{
        return var1 - var2;
}
 
int mul(int var1, int var2)
{
        return var1 * var2;
}
 
int div(int var1, int var2)
{
        return var1 / var2;
}
 
int main(int argc, char **argv)
{
        int (*fun_table[4]) ();
        int result = 0;
        
        fun_table[0] = add;
        fun_table[1] = sub;
        fun_table[2] = mul;
        fun_table[3] = div;
        
        result = (*fun_table[0])(100, 5);
        printf("add : %d\n", result);
        
        result = (*fun_table[1])(100, 5);
        printf("sub : %d\n", result);
        
        result = (*fun_table[2])(100, 5);
        printf("mul : %d\n", result);
        
        result = (*fun_table[3])(100, 5);
        printf("div : %d\n", result);
        
        return 0;
}