#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;
}
A Blog to learn Software Technologies like C, Golang and Java
Passing function as argument in C
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;
}
Function pointer in C
One pointer variable can point to another variable.
Similarly one function can point another function.
This method is helped in callback mechanism. i,e) passing function as argument to anther function.
#include <stdio.h>
int add(int, int);
int add(int var1, int var2)
{
return var1 + var2;
}
int main(int argc, char **argv)
{
int x = 0;
int (*functionPtr)(int, int);
functionPtr = &add;
x = (*functionPtr)(10, 15);
printf("%d\n", x);
return 0;
}
Shell script to check Armstrong number
SCRIPT:
(i)
Enter the nuber : 178
178 not a armstrong number
(ii)
Enter the nuber : 178
178 not a armstrong number
#!/bin/bash echo -n "Enter the nuber : " read read_val number=$read_val buf1=0 buf2=0 sum=0 while [ $number -gt 0 ] do buf1=`expr $number % 10` buf2=`expr $buf1 \* $buf1 \* $buf1` sum=`expr $sum + $buf2` number=`expr $number / 10` done if [ $sum -eq $read_val ] then echo "$read_val is armstrong number" else echo "$read_val not a armstrong number" fiOUTPUT:
(i)
Enter the nuber : 178
178 not a armstrong number
(ii)
Enter the nuber : 178
178 not a armstrong number
Shell script to find factorial of an number
SCRIPT:
#!/bin/bash input=$1 fact_op=1 while [ $input -gt 0 ] do echo "$fact_op $input" fact_op=$(( $fact_op * $input )) input=$(( $input - 1 )) done echo "factorial of value $1 is $fact_op"
OUTPUT:
sujin@sujin:~/work/shell$ ./factorial.sh 5
1 5
5 4
20 3
60 2
120 1
factorial of value 5 is 120
sujin@sujin:~/work/shell$ ./factorial.sh 5
1 5
5 4
20 3
60 2
120 1
factorial of value 5 is 120
Subscribe to:
Posts (Atom)