通过C当通过“字符”被提升到'廉政' [英] 'Char' is promoted to 'int' when passed through in C

查看:206
本文介绍了通过C当通过“字符”被提升到'廉政'的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个问题。我有我的程序行:

I have a question. I have line in my program:

的sprintf(BUFF,Nieznany znak数:%d,(字符)的va_arg(X,为const char));打破;

为什么编译后,我有一个错误:

Why after compile I have an error:

error.c: In function 'error':
error.c:30:46: warning: 'char' is promoted to 'int' when passed through '...' [e
nabled by default]
  case 0xfe: sprintf(buff,"Nieznany znak: %d",(char)va_arg(x, const char)); brea
k;
                                              ^
error.c:30:46: note: (so you should pass 'int' not 'char' to 'va_arg')
error.c:30:46: note: if this code is reached, the program will abort

在我看来一切正常,为什么我不能做到这一点喜欢吗?

In my opinion everything is okay, why I can't do this like that?

推荐答案

编译器的只是告诉你原因:

'char' is promoted to 'int' when passed through '...'

据C语言的规范,通常的算术转换应用到的可变参数函数的命名参数。因此,任何整数类型比 INT 短(如布尔字符)的隐式转换 INT ; 浮动提升为双击

According to the C language specification, usual arithmetic conversions are applied to the unnamed arguments of variadic functions. Hence, any integer type shorter than int (e. g. bool, char and short) are implicitly converted int; float is promoted to double, etc.

做什么,编译器会要求您提供(使用在va_arg() INT 作为它的第二个参数),否则您code将调用未定义的行为。

Do what the compiler asks you for (use va_arg() with int as its second parameter), else your code will invoke undefined behavior.

这篇关于通过C当通过“字符”被提升到'廉政'的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆