经典编程900例(c语言)(第十八篇)

it2025-10-28  6

例207:使用全局变量

#include <stdio.h> int a = 1, b = 2, c = 3; void global_values(void) { printf("a contains %d b contains %d c contains %d\n", a, b, c); } int main(int argc, char const *argv[]) { global_values(); printf("a contains %d b contains %d c contains %d\n", a, b, c); return 0; }

例208:变量的作用域

#include <stdio.h> void unknown_title(void) { // gcc编译报错 - 'title' was not declared in this scope printf("The book's title is %s\n", title); } char title[] = "Jamsa's 1001 C/C++ Tips"; int main(int argc, char const *argv[]) { printf("Title: %s\n", title); return 0; }

例209:使用库函数strupr()

#include <stdio.h> #include <string.h> int main(int argc, char const *argv[]) { char alphabet[] = "abcdefghijklmnopqrstuvwxyz"; // 将从第13个下标位置开始转换为大写形式 strupr(&alphabet[13]); printf(alphabet); return 0; }

例210:写一个计算立方的函数

#include <stdio.h> /** * 计算value的立方并返回 */ int i_cube(int value) { return(value * value * value); } int main(int argc, char const *argv[]) { printf("The cube of 3 is %d\n", i_cube(3)); printf("The cube of 5 is %d\n", i_cube(5)); printf("The cube of 7 is %d\n", i_cube(7)); return 0; }

例211:变量的作用域

#include <stdio.h> void local_values(void) { int a = 1, b = 2, c = 3; printf("a contains %d b contains %d c contains %d\n", a, b, c); } int main(int argc, char const *argv[]) { // gcc编译报错 - 'a' was not declared in this scope // gcc编译报错 - 'b' was not declared in this scope // gcc编译报错 - 'c' was not declared in this scope printf("a contains %d b contains %d c contains %d\n", a, b, c); return 0; }

例212:写一个函数计算一个数的阶乘

#include <stdio.h> /** * 计算阶乘value! */ int factorial(int value) { int result = 1; int counter; for (counter = 2; counter <= value; counter++) result *= counter; return(result); } int main(int argc, char const *argv[]) { int i; for (i = 1; i <= 5; i++) printf("Factorial of %d is %d\n", i, factorial(i)); return 0; }

例213:写一个函数用来复制字符串

#include <stdio.h> char *strcpy(char *destination, const char *source) { // 计算字符串的指针(首地址) char *start = destination; // 一个个赋值并指针++ while (*destination++ = *source++) ; return(start); } int main(int argc, char const *argv[]) { char title[64]; strcpy(title, "Jamsa's 1001 C/C++ Tips"); printf(title); return 0; }

例214:使用递归计算字符串长度,并计算运行时间

#include <stdio.h> #include <time.h> int string_length(const char *str) { if (*str) // 字符指针没到'\0'就递归 return(1 + string_length(str+1)); else return(0); } int main(int argc, char const *argv[]) { long int counter; time_t start_time, end_time; time(&start_time); for (counter = 0; counter < 100000L; counter++) string_length("Jamsa's 1001 C/C++ Tips"); time(&end_time); printf("Processing time %d\n", end_time - start_time); return 0; }

例215:不使用递归计算字符串长度,并计算运行时间

#include <stdio.h> #include <time.h> int string_length(const char *str) { int length = 0; // 字符指针没到'\0'计数自增 while (*str++) length++; return(length); } int main(int argc, char const *argv[]) { long int counter; time_t start_time, end_time; time(&start_time); for (counter = 0; counter < 100000L; counter++) string_length("Jamsa's 1001 C/C++ Tips"); time(&end_time); printf("Processing time %d\n", end_time - start_time); return 0; }

例216:参数的值传递

#include <stdio.h> void display_and_change(int first, int second, int third) { printf("Original function values %d %d %d\n", first, second, third);// ginal function values 1 2 3 first += 100; second += 100; third += 100; printf("Ending function values %d %d %d\n", first, second, third); // Ending function values 101 102 103 } int main(int argc, char const *argv[]) { int a = 1, b = 2, c = 3; display_and_change(a, b, c); printf("Ending values in main %d %d %d\n", a, b ,c); // Ending values in main 1 2 3 return 0; }

例217:变量的作用域

#include <stdio.h> int main(int argc, char const *argv[]) { int value = 1001; if (value > 1000) { int value = 1; printf("Inner value is %d\n", value); // Inner value is 1 } printf("Outer value is %d\n", value); // Outer value is 1001 return 0; }
最新回复(0)