C Tutorial
Also called preprocessor directive which is used to replace the token with or without parameters with custom name.
define directive is used to define macro, macro is a fragment of code that is given a name.
We can use that fragment of code in program by using the macro name
Replaces fragment of code for defined macro before programs compilation.
define function syntax,
#define
This c program is used to define new custom name for printf function to print the string.
#include<stdio.h> #define cpf printf void main() { cpf("defines macro"); }
Output:
$cc defines.c $ ./a.out defines macro
This c program is used to deine new custom name for char keyword and used custom name to define the character array to read and print the string.
#include<stdio.h> #define ch char #define cpf printf void main() { ch name[50] = "codingpointer.com"; cpf("welcome to %s",name); }
Output:
$ cc define.c $ ./a.out welcome to codingpointer.com
This c program is used to deine new custom name for constant value 3.14 and used custom name to compute area of circle and prints the area.
#include<stdio.h> #define PI 3.14 void main() { int area, radius=10; area = PI*radius*radius; printf("\nArea of circle: %d", area); }
Output:
$ cc define.c $ ./a.out Area of circle: 314
This c program is used to deine new custom name for expression 3.14*radius*radius and used custom name to compute area of circle and prints the area.
#include<stdio.h> #define AREA 3.14*radius*radius void main() { int area, radius=10; area = AREA; printf("\nArea of circle: %d", area); }
Output:
$ cc define.c $ ./a.out Area of circle: 314
This c program is used to deine new custom name for function compute_area which computes area of circle using formula (3.14*radius*radius) and used custom name to compute area of circle and prints the area.
#include<stdio.h> #define AREA compute_area(radius) int compute_area(int radius) { return 3.14*radius*radius; } void main() { int area, radius=10; area = AREA; printf("\nArea of circle: %d", area); }
Output:
$ cc define.c $ ./a.out Area of circle: 314
C Tutorial
Privacy Policy | Copyright2020 - All Rights Reserved. | Contact us
| Report website issues in Github
| Facebook page
| Google+ page