C和指针 第十四章 习题
14.1 打印函数
#include <stdio.h> void print_ledger_long(){ printf("function print_ledger_long\n"); } void print_ledger_detailed(){ printf("function print_ledger_detailed\n"); } void print_ledger_default(){ printf("function print_ledger_default\n"); } void print_ledger(){ #ifdef OPTION_LONG print_ledger_long(); #else #ifdef OPTION_DETAILED print_ledger_detailed(); #else print_ledger_default(); #endif #endif } int main() { print_ledger(); return 0; }
运行:
14.2 编写函数,cpu_type提示运行这个函数的计算机类型
cpu_type.h
#ifndef C_CPU_TYPE_H #define C_CPU_TYPE_H #define CPU_VAX 1 #define CPU_68000 2 #define CPU_68020 3 #define CPU_80386 4 #define CPU_6809 5 #define CPU_6502 6 #define CPU_3B2 7 #define CPU_UNKNOWN 8 #endif //C_CPU_TYPE_H
main.c
#include <stdio.h> #include "cpu_type.h" int cpu_type() { #ifdef VAX return CPU_VAX; #elif M68000 return CPU_68000; #elif M68020 return CPU_68020; #elif I80386 return CPU_80386; #elif X6809 return CPU_6809; #elif X6502 return CPU_6502; #elif U3B2 return CPU_3B2; #else return CPU_UNKNOWN; #endif } int main() { printf("%d", cpu_type()); return 0; }
运行:
cpu_known。#_#。上古cpu。