Dr. Cheng-Chung Li
May 23, 2017
#include <stdio.h>
#include "header.h"
A macro in computer science is a rule or pattern that specifies how a certain input sequence (often a sequence of characters) should be mapped to a replacement output sequence (also often a sequence of characters) according to a defined procedure.
#include <stdio.h>
#define ARRAYSIZE 10
int main(void)
{
int a[ARRAYSIZE];
int i;
for (i=0; i< ARRAYSIZE; i++)
scanf("%d", &(a[i]));
for (i=0; i< ARRAYSIZE; i++)
printf("%d", a[i]);
return 0;
}
stack-define.h
#define STACKSIZE 100
struct stack{
int top;
char elements[STACKSIZE];
};
typedef struct stack Stack;
void init_stack(Stack *s);
int stack_full(Stack *s);
int stack_empty(Stack *s);
void push_stack(Stack *s, char data);
int pop_stack(Stack *s);
#include "stack-define.h"
int stack_full(Stack *s){
return (s->top >= STACKSIZE);
}
#define STACKSIZE 100
#define Data int
struct stack{
int top;
Data elements[STACKSIZE];
};
typedef struct stack Stack;
void init_stack(Stack *s);
int stack_full(Stack *s);
int stack_empty(Stack *s);
void push_stack(Stack *s, Data data);
Data pop_stack(Stack *s);
Undefines a preprocessor macro.
#undef FILE_SIZE
#define FILE_SIZE 42
#if condition
source code
#endif
#define DEBUG 1
#if DEBUG == 1
printf ("i = %d\n", i);
#endif
#define DEBUG 0
#if DEBUG == 1
printf ("i = %d\n", i);
#endif
#define DEBUG_LEVEL 4
...
#if DEBUG_LEVEL >= 3
printf ("i = %d\n", i);
#endif
...
#if DEBUG_LEVEL >= 5
printf ("j = %d\n", j);
#endif
we can use “gcc -DDEBUG_LEVEL=8 program.c”“ in command line to set DEBUG_LEVEL as 8 in program.c
However, if we set the value in command line and in program, it may cause “redefine problem” since this value has been set twice.
#ifndef DEBGU_LEVEL
#define DEBGU_LEVEL 4
#endif
#ifdef DEBUG
source code
#endif
#include<stdio.h>
int main(void){
printf("Date :%s\n", __DATE__ );
printf("Time :%s\n", __TIME__ );
}
#include<stdio.h>
#include <stdlib.h>
#ifdef NDEBUG
#define assert(cond) 0
#else
#define assert(cond) \
if (!(cond)) { \
printf("Assertion failed: %s, "\
"file %s, line %d\n", \
#cond, __FILE__, __LINE__); \
exit(0); \
}
#endif
int main(void){
int i = 0;
assert(i==1);
return 0;
}
We can use
void srand(unsigned int seed);
int rand(void);
#include<stdio.h>
#include<stdlib.h>
int main(void){
int i, j;
srand(523);
i = rand();
printf("%d\n", i);
srand(523);
j = rand();
printf("%d\n", j);
return 0;
}
#include<stdio.h>
#include<stdlib.h>
#include<time.h>
int main(void){
srand(time(NULL));
int i,j;
i = rand();
printf("%d\n", i);
j = rand();
printf("%d\n", j);
return 0;
}
#include<stdio.h>
#include <stdlib.h>
#include <time.h>
int main(void){
int i, count =0, n;
double x, y ;
scanf("%d", &n);
srand(time(NULL));
for (i=0; i<n; i++){
x = (double) rand()/RAND_MAX;
y = (double) rand()/RAND_MAX;
if (x*x + y*y<=1.0){
count ++;
}
}
printf("pi is about %f\n", (double) count/n * 4.0);
return 0;
}
#include <stdio.h>
#include <stdlib.h>
void bar (double d){
printf("running bar\n");
if (d >0)
return ;
else
exit(-1);
}
void foo(double d){
bar (d);
printf("running foo\n");
return;
}
int main(void){
double d = 1.0;
foo(d);
scanf("%lf", &d);
foo(d);
}
Instead of inputing parameters from the keyboard or file, we can input them from the command line.
int main(int argc, char *[argv]);
#include <stdio.h>
#include <stdlib.h>
int main (int argc, char *argv[]){
int i;
int sum = 0;
for (i= 1; i<argc; i++){
sum += atoi(argv[i]);
}
printf ("%d\n", sum);
return 0;
}