C language programming---type conversion and error handling

type conversion

  • Small to large, automatic conversion;
  • From large to small, forced type conversion will cause loss of precision (decimals are discarded);
// 小到大,自动转换
int a = 10;
double b = 21.5;
double c = a + b; // a自动转为double

//大到小,强制转换
int d = a + (int)b; // 31

// 强制转换
int a = 5;
int b = 10;
double c = (double) a / b; // 整数除以整数 得整数;强制类型转换  优于 除运算符

Automatic conversion of types (data promotion)
Insert image description here
 

Error handling

  • When the program runs incorrectly, you can obtain the error code through the <errno.h> header file and the errno variable;
  • The errno error code is int, 0 indicates success;
  • perror("Prompt:") directly outputs the error message (string);
  • strerror(errno) converts the error code into an error string; then fprintf(stderr)
#include <stdio.h>
#include <errno.h>
#include <string.h>   //三个头文件必须包含
 
 
int main ()
{
    
    
   FILE * ptr;
   ptr = fopen ("lauf.txt", "rb");
   if (ptr == NULL)
   {
    
     // !pf
	  // 直接输出错误信息
	  perror("打开文件失败");

	  // 输出错误码
      fprintf(stderr, "错误号: %d\n", errno);
      // strerror 输出错误信息
      fprintf(stderr, "打开文件错误: %s\n", strerror( errnum ));
   }
   else
   {
    
    
      fclose (pf);
   }
   return 0;
}

For division-by-zero errors, just judge whether the divisor is 0;
use exit(0) to successfully exit the program;
use exit(-1) to exit on failure;

 

recursion

  • The function calls itself internally;
  • There must be clear stopping conditions;
// 斐波那契数列
#include <stdio.h>
 
int fibonaci(int i)
{
    
    
   if(i == 0)
   {
    
    
      return 0;
   }
   if(i == 1)
   {
    
    
      return 1;
   }
   return fibonaci(i-1) + fibonaci(i-2);
}
 
int  main()
{
    
    
    int i;
    for (i = 0; i < 10; i++)
    {
    
    
       printf("%d\t\n", fibonaci(i));
    }
    return 0;
}

 

variable parameter

  • When the function passes parameters, the number is uncertain;
  • Use the <stdarg.h> header file;
  • Variables of type va_list are responsible for receiving parameters and forming a parameter list;
  • va_start(va_list argList, int num); Initializes a list of num parameters for parameters of type va_list;
  • va_arg(va_list, type); Get each parameter in the list, type is the type of the next parameter;
  • va_end(va_list argList); Release the memory of argList;

Integer parameter list

#include <stdio.h>
#include <stdarg.h> // 必须使用该头文件
 
// 第一个参数为 可变参数的个数
// ... 表示可变参数列表
int lauf(int num, ...)
{
    
    
 	// 声明可变参数列表
    va_list argList;
    // 为 num 个参数初始化 argList 列表
    va_start(argList, num);
 	int arg;
    // 逐一打印参数
    for (i = 0; i < num; i++)
    {
    
    
       // 获取每一个参数
       arg = va_arg(argList, int); //下一个参数类型 根据实际情况决定
       printf("per arg: %d\n", arg);
    }
    // 清理为 argList 内存
    va_end(argList);
 
    return 0;
}
 
int main()
{
    
    
   printf("args of 2, 3, 4, 5 = %f\n", lauf(4, 2,3,4,5)); // 4个参数
   printf("args of 5, 10, 15 = %f\n", lauf(3, 5,10,15)); //3个参数
}

String parameter list

#include <stdarg.h>

int lauf1(int num, ...) {
    
    
	// 声明可变参数列表
	va_list argList;
	// 初始化
	va_start(argList, num);

	char* arg;

	for (int i = 0; i < num; i++) {
    
    
	
		arg = va_arg(argList, char*);
		printf("per arg: %s\n", arg);
	}
	// 释放内存
	va_end(argList);

	return 0;
}

int main() {
    
    
	
	lauf1(3, "tom", "jack", "lucy");
	
	system("pause");
	return 0;
}

 
 
[Previous article]: C language programming - strings and structures
[Next article]: C language programming - memory management and command line parameters

Guess you like

Origin blog.csdn.net/weixin_45228198/article/details/131864654