#define just do the copy and not doing any evaluations:
- at first look it gives output like 1 but see carefully
example:
int
main()
{
int
x = 36/square(6);
printf
(
"%d"
, x);
return
0;
}
2)
#include <stdio.h>
#define MULTIPLY(a, b) a*b
int main()
{
printf ( "%d" , MULTIPLY(2+3, 3+5));
return 0;
}
|
3)
root@selvakumar-Lenovo-B460e:/C_pgms/double_pointer# cat dptr.c
#include<stdio.h>
int func(int **pointer_to_pointer);
int main()
{
int a=10;
int *ptr,**pptr;
ptr = (int *) &a;
pptr = &ptr;
printf("\nvalue in *ptr =%d\n",*ptr);
func(&ptr);
printf("\nvalue in *ptr after func call =%d\n",*ptr);
return 0;
}
int func(int **pointer_to_pointer)
// to hold the address of another pointer
{
printf("\nvalue in pointer_to_pointer =%d\n",**pointer_to_pointer);
**pointer_to_pointer = 20;
}
root@selvakumar-Lenovo-B460e:/C_pgms/double_pointer# gcc dptr.c
root@selvakumar-Lenovo-B460e:/C_pgms/double_pointer# ./a.out
value in *ptr =10
value in pointer_to_pointer =10
value in *ptr after func call =20
root@selvakumar-Lenovo-B460e:/C_pgms/double_pointer#
4)
root@selvakumar-Lenovo-B460e:/C_pgms/function_pointer# cat func1.c
#include<stdio.h>
int add(int a,int b)
{
return (a+b);
}
int main()
{
//function pointer declaration
int (*addptr)(int,int);
//assinging address to function pointer
addptr=&add;
//calling via function pointer
printf("\n%d\n",addptr(10,20));
return 0;
}
root@selvakumar-Lenovo-B460e:/C_pgms/function_pointer# gcc func1.c
root@selvakumar-Lenovo-B460e:/C_pgms/function_pointer# ./a.out
30
root@selvakumar-Lenovo-B460e:/C_pgms/function_pointer#
root@selvakumar-Lenovo-B460e:/C_pgms/function_pointer# cat func2.c
#include<stdio.h>
void function_a(void);
void function_b(void);
void main() {
void (*fp)();
fp = &function_a;
(*fp)(); //prints "a"
fp = &function_b;
(*fp)(); //prints "b"
}
void function_a(void) {
printf("a \n");
}
void function_b(void) {
printf("b\n");
}
root@selvakumar-Lenovo-B460e:/C_pgms/function_pointer# gcc func2.c
root@selvakumar-Lenovo-B460e:/C_pgms/function_pointer# ./a.out
a
b
root@selvakumar-Lenovo-B460e:/C_pgms/function_pointer#
root@selvakumar-Lenovo-B460e:/C_pgms/function_pointer# cat calculator_using_fptr.c
#include<stdio.h>
float Plus (float a, float b) { return a+b; }
float Minus (float a, float b) { return a-b; }
float Multiply(float a, float b) { return a*b; }
float Divide (float a, float b) { return a/b; }
// Solution with a switch-statement - specifies which operation to execute
void Switch(float a, float b, char opCode)
{
float result;
// execute operation
switch(opCode)
{
case '+' : result = Plus (a, b); break;
case '-' : result = Minus (a, b); break;
case '*' : result = Multiply (a, b); break;
case '/' : result = Divide (a, b); break;
}
printf("\n result=%f",result);
}
// Solution with a function pointer
// a function which takes two floats and returns a float. The function pointer
// "specifies" which operation shall be executed.
void Switch_With_Function_Pointer(float a, float b, float (*pt2Func)(float, float))
{
float result = pt2Func(a, b); // call using function pointer
printf("\n result=%f",result);
}
// Execute example code
int main()
{
Switch(2, 5, /* '+' specifies function 'Plus' to be executed */ '+');
Switch_With_Function_Pointer(2, 5, /* pointer to function 'Minus' */ &Minus);
return 0;
}
root@selvakumar-Lenovo-B460e:/C_pgms/function_pointer#
root@selvakumar-Lenovo-B460e:/C_pgms/function_pointer# gcc calculator_using_fptr.c
root@selvakumar-Lenovo-B460e:/C_pgms/function_pointer# ./a.out
result=7.000000
result=-3.000000root@selvakumar-Lenovo-B460e:/C_pgms/function_pointer#
root@selvakumar-Lenovo-B460e:/C_pgms/memory# cat last_byte.c
#include<stdio.h>
/* program to mask last one byte of the given number*/
int main()
{
short a=0x0101; // two byte integer in hex format
short b;
printf("\n%d\n",a);
b = a & 0xff; // this mask last 1 byte
printf("\n%d\n",b);
return 0;
}
root@selvakumar-Lenovo-B460e:/C_pgms/memory# gcc last_byte.c
root@selvakumar-Lenovo-B460e:/C_pgms/memory# ./a.out
257
1
root@selvakumar-Lenovo-B460e:/C_pgms/memory#
root@selvakumar-Lenovo-B460e:/C_pgms/voidptr# cat void2.c
#include<stdio.h>
struct Something
{
char *name;
int a;
};
int main()
{
int nValue =10;
float fValue =20.2;
char *name1 = "god";
char name2[] = "help";
struct Something sValue;
sValue.name="selva";
sValue.a=20;
void *pVoid;
//to showcase no error thrown
pVoid = &nValue; // valid
printf("nValue=%d\n",*((int *)pVoid));
pVoid = &fValue; // valid
printf("nValue=%f\n",*((float *)pVoid));
pVoid = name1; // note than not giving the address since its already pointer *name1
printf("name1=%s\n",((char *)(pVoid)));
pVoid = name2; // note than not giving the address since its already array name2[]
printf("name2=%s\n",((char *)(pVoid)));
pVoid = (sValue.name); // valid
printf("name=%s\n",((char *)(pVoid)));
pVoid = &(sValue);
printf("struct something sValue->a =%d\n",(((struct Something *)(pVoid))->a));
return 0;
}
root@selvakumar-Lenovo-B460e:/C_pgms/voidptr# gcc void2.c
root@selvakumar-Lenovo-B460e:/C_pgms/voidptr# ./a.out
nValue=10
nValue=20.200001
name1=god
name2=help
name=selva
struct something sValue->a =20
root@selvakumar-Lenovo-B460e:/C_pgms/voidptr#
root@selvakumar-Lenovo-B460e:/C_pgms/voidptr# cat voidptr.c
#include<stdio.h>
enum Type
{
INT,
FLOAT,
STRING,
};
void Print(void *pValue, enum Type eType)
{
int i;float f;char *c;
switch (eType)
{
case INT:
i = * ((int *)pValue);
printf("\n%d\n",i);
break;
case FLOAT:
f = *((float *)pValue);
printf("\n%f\n",f);
break;
case STRING:
c = (char *)pValue;
printf("\n%s\n",c);
break;
}
}
int main()
{
int nValue = 5;
float fValue = 7.5;
char *szValue = "Mollie";
Print(&nValue, INT);
Print(&fValue, FLOAT);
Print(szValue, STRING); // note here no & symbol useed for char pointer
return 0;
}
root@selvakumar-Lenovo-B460e:/C_pgms/voidptr# gcc voidptr.c
root@selvakumar-Lenovo-B460e:/C_pgms/voidptr# ./a.out
5
7.500000
Mollie
root@selvakumar-Lenovo-B460e:/C_pgms/voidptr#
root@selvakumar-Lenovo-B460e:/C_pgms/sizeof# cat size.c
#include<stdio.h>
/* */
int main()
{
printf("size of char * = %d\n",sizeof(char *));
printf("size of void * = %d\n",sizeof(void *));
return 0;
}
root@selvakumar-Lenovo-B460e:/C_pgms/sizeof# gcc size.c
root@selvakumar-Lenovo-B460e:/C_pgms/sizeof# ./a.out
size of char * = 4
size of void * = 4
root@selvakumar-Lenovo-B460e:/C_pgms/sizeof#
#include <stdio.h>
#include <stdarg.h>
double average(int num,...)
{
va_list valist;
double sum = 0.0;
int i;
/* initialize valist for num number of arguments */
va_start(valist, num);
/* access all the arguments assigned to valist */
for (i = 0; i < num; i++)
{
sum += va_arg(valist, int);
}
/* clean memory reserved for valist */
va_end(valist);
return sum/num;
}
int main()
{
printf("Average of 2, 3, 4, 5 = %f\n", average(4, 2,3,4,5));
printf("Average of 5, 10, 15 = %f\n", average(3, 5,10,15));
}
No comments:
Post a Comment