例195:写一个函数计算不定个整型参数的和
#include <stdio.h>
#include <stdarg.h>
/**
* 整型加法函数,参数不定
*/
int add_values(int value, ...)
{
va_list argument_ptr;
int result = 0;
if (value != 0)
{
result += value;
// 初始化刚定义的VA_LIST变量
va_start(argument_ptr, value);
// va_arg返回可变的参数, 等于0时停止加
while ((value = va_arg(argument_ptr, int)) != 0)
result += value;
// 结束可变参数的获取
va_end(argument_ptr);
}
return(result);
}
int main(int argc, char const *argv[])
{
printf("Sum of 3 is %d\n", add_values(3, 0)); // Sum of 3 is 3
printf("Sum of 3 + 5 is %d\n", add_values(3, 5, 0)); // Sum of 3 + 5 is 8
printf("Sum of 3 + 5 + 8 is %d\n", add_values(3, 5, 8, 0)); // Sum of 3 + 5 + 8 is 16
printf("Sum of 3 + 5 + 8 + 9 is %d\n", add_values(3, 5, 8 , 9, 0)); //Sum of 3 + 5 + 8 + 9 is 25
return 0;
}
例196:写一个函数计算int或double的和
#include <stdio.h>
#include <stdarg.h>
/**
* 根据格式字符串做加法
*/
double add_values(char *str, ...)
{
va_list marker;
double result = 0.0;
va_start(marker, str);
// 根据第一个字符串参数来返回的参数的类型
while (*str)
{
if (*str == '%')
{
// %的下一个字符
switch (*(++str)) {
case 'd':
result += va_arg(marker, int);
break;
case 'f':
result += va_arg(marker, double);
break;
}
}
str++;
}
va_end(marker);
return(result);
}
int main(int argc, char const *argv[])
{
double result;
printf("Result %f\n", add_values("%f", 3.3)); // Result 3.300000
printf("Result %f\n", add_values("%f %f", 1.1, 2.2)); // Result 3.300000
printf("Result %f\n", add_values("%f %d %f", 1.1, 1, 2.2)); // Result 4.300000
printf("Result %f\n", add_values("%f %d %f %d", 1.1, 1, 2.2, 3)); // Result 7.300000
return 0;
}
例197:自定义函数打印一个字符串
#include <stdio.h>
/**
* 打印字符串函数
*/
void display_backward(char *string)
{
if (*string)
{
display_backward(string+1);
// 打印指针指向的字符
putchar(*string);
}
}
int main(int argc, char const *argv[])
{
display_backward("ABCDE");
return 0;
}
例198:连续调用两次函数
#include <stdio.h>
void hello_world(void)
{
printf("Hello, world!\n");
}
void three_hellos(void)
{
int counter;
for (counter = 1; counter <= 3; counter++)
hello_world();
}
int main(int argc, char const *argv[])
{
three_hellos();
return 0;
}
例199:写一个函数将字符转为大写
#include <stdio.h>
#include <ctype.h>
void no_change(const char *string)
{
char *alias = string;
// 用库函数将字符转为大写
while (*alias)
// 注意这里的++的执行时间, ‘=’并不会产生序列点《c primer plus》中有说明
*alias++ = toupper(*alias);
}
int main(int argc, char const *argv[])
{
char title[] = "Jamsa's 1001 C/C++ Tips";
no_change(title);
printf(title);
return 0;
}
例200:传递指针参数1
#include <stdio.h>
void change_first(int *first, int second)
{
// 将第二个值通过指针直接赋给第一个
*first = second;
}
int main(int argc, char const *argv[])
{
int a = 0, b = 5;
change_first(&a, b);
printf("Value of a %d value of b %d\n", a, b);
return 0;
}
例201:传递指针参数2
#include <stdio.h>
void display_and_change(int *first, int *second, int *third)
{
// 函数执行前变量的值
printf("Original function values %d %d %d\n", *first, *second, *third);
*first += 100;
*second += 100;
*third += 100;
// 函数执行后变量的值
printf("Ending function values %d %d %d\n", *first, *second, *third);
}
int main(int argc, char const *argv[])
{
int a = 1, b = 2, c = 3;
display_and_change(&a, &b, &c);
// 函数执行后main中变量的值
printf("Ending values in main %d %d %d\n", a, b ,c);
return 0;
}
例202:编译报错
#include <stdio.h>
void no_change(const char *string)
{
while (*string)
// gcc编译报错 - assignment of read-only location '*string++'
*string++ = toupper(*string);
}
int main(int argc, char const *argv[])
{
char title[] = "Jamsa's 1001 C/C++ Tips";
no_change(title);
printf(title);
return 0;
}
例203:变量的作用域
#include <stdio.h>
int a = 1, b = 2, c = 3;
void conflict_a(void)
{
int a = 100;
printf("a contains %d b contains %d c contains %d\n", a, b, c); // a contains 100 b contains 2 c contains 3
}
int main(int argc, char const *argv[])
{
conflict_a();
printf("a contains %d b contains %d c contains %d\n", a, b, c); // a contains 1 b contains 2 c contains 3
return 0;
}
例204:全局变量
#include <stdio.h>
int tip_count = 1001;
void show_title(void)
{
printf("Jamsa's 1001 C/C++ Tips");
}
例205:斐波那契数列
#include <stdio.h>
int factorial(int value)
{
if (value == 1)
return(1);
else
return(value * factorial(value-1));
}
int main(int argc, char const *argv[])
{
int i;
for (i = 1; i <= 5; i++)
printf("The factorial of %d is %d\n", i, factorial(i));
return 0;
}
例206:体会函数调用的时间消耗
#include <stdio.h>
#include <time.h>
float add_em(long int a, float b)
{
float result;
result = a + b;
return(result);
}
int main(int argc, char const *argv[])
{
long int i;
float result = 0;
time_t start_time, stop_time;
printf("Working...\n");
time(&start_time);
// +10万次
for (i = 1; i <= 100000L; i++)
result += i;
time(&stop_time);
printf("Using loop %d seconds\n", stop_time - start_time);
printf("Working...\n");
time(&start_time);
// 函数调用+10万次
for (i = 1; i <= 100000L; i++)
result = add_em(i, result);
time(&stop_time);
printf("Using function %d seconds\n", stop_time - start_time);
return 0;
}