写一个函数求数组平均值,float mean(唐人intd tv2.0.7[],int n)在main()中 完成数组元素的输入,调用上述函数

c - How is conversion of float/double to int handled in printf? - Stack Overflow
to customize your list.
Stack Overflow is a community of 4.7 million programmers, just like you, helping each other.
J it only takes a minute:
Join the Stack Overflow community to:
Ask programming questions
Answer and help your peers
Get recognized for your expertise
Consider this program
int main()
float f = 11.22;
double d = 44.55;
//cast float to int
//cast double to int
printf("i = %d, j = %d, f = %d, d = %d", i,j,f,d);
//This prints the following:
// i = 11, j = 44, f = -, d =
Can someone explain why the casting from double/float to int works correctly in the first case, and does not work when done in printf?
This program was compiled on gcc-4.1.2 on 32-bit linux machine.
seems logical, i.e. use of format specifiers to figure out what to pop off the stack. However then consider this follow up question:
int main()
char c = 'd';
// sizeof c is 1, however sizeof character literal
// 'd' is equal to sizeof(int) in ANSI C
printf("lit = %c, lit = %d , c = %c, c = %d", 'd', 'd', c, c);
//this prints: lit = d, lit = 100 , c = d, c = 100
//how does printf here pop off the right number of bytes even when
//the size represented by format specifiers doesn't actually match
//the size of the passed arguments(char(1 byte) & char_literal(4 bytes))
How does this work?
The printf function uses the format specifiers to figure out what to pop off the stack.
So when it sees %d, it pops off 4 bytes and interprets them as an int, which is wrong (the binary representation of (float)3.0 is not the same as (int)3).
You'll need to either use the %f format specifiers or cast the arguments to int.
If you're using a new enough version of gcc, then turning on stronger warnings catches this sort of error:
$ gcc -Wall -Werror test.c
cc1: warnings being treated as errors
test.c: In function ‘main’:
test.c:10: error: implicit declaration of function ‘printf’
test.c:10: error: incompatible implicit declaration of built-in function ‘printf’
test.c:10: error: format ‘%d’ expects type ‘int’, but argument 4 has type ‘double’
test.c:10: error: format ‘%d’ expects type ‘int’, but argument 5 has type ‘double’
Response to the edited part of the question:
C's integer promotion rules say that all types smaller than int get promoted to int when passed as a vararg.
So in your case, the 'd' is getting promoted to an int, then printf is popping off an int and casting to a char.
The best reference I could find for this behavior was .
7,16551927
There's no such thing as "casting to int in printf". printf does not do and cannot do any casting. Inconsistent format specifier leads to undefined behavior.
In practice printf simply receives the raw data and reinterprets it as the type implied by the format specifier. If you pass it a double value and specify an int format specifier (like %d), printf will take that double value and blindly reinterpret it an an int. The results will be completely unpredictable (which is why doing this formally causes undefined behavior in C).
193k23278510
explains how to fix your problem. I'm going to explain why you're getting your unexpected results. Your code is equivalent to:
float f = 11.22;
double d = 44.55;
int i,j,k,l;
k = *(int *) &f;
//cast float to int
l = *(int *) &d;
//cast double to int
printf("i = %d, j = %d, f = %d, d = %d", i,j,k,l);
The reason is that f and d are passed to printf as values, and then these values are interpreted as ints. This doesn't change the binary value, so the number displayed is the binary representation of a float or a double. The actual cast from float to int is much more complex in the generated assembly.
42.1k1396158
Because you are not using the float format specifier, try with:
printf("i = %d, j = %d, f = %f, d = %f", i,j,f,d);
Otherwise, if you want 4 ints you have to cast them before passing the argument to printf:
printf("i = %d, j = %d, f = %d, d = %d", i,j,(int)f,(int)d);
86.3k19128239
printf uses variable length argument lists, which means you need to provide the type information.
You're providing the wrong information, so it gets confused.
Jack provides the practical solution.
168k27351443
The reason your follow-up code works is because the character constant is promoted to an int before it is pushed onto the stack. So printf pops off 4 bytes for %c and for %d. In fact, character constants are of type int, not type char. C is strange that way.
It's worth noting that printf, being a function with a variable-length argument list, ne float arguments are "old school" promoted to doubles.
A recent standard draft introduces the "old school" default promotions first (n.2.2/6):
If the expression that denotes the called function has a type that
does not include a prototype, the integer promotions are performed on
each argument, and arguments that have type float are promoted to
double. These are called the default argument promotions.
Then it discusses variable argument lists (6.5.2.2/7):
ellipsis notation in a function prototype declarator causes argument
type conversion to stop after the last declared parameter. The default
argument promotions are performed on trailing arguments.
The consequence for printf is that it is impossible to "print" a genuine float. A float expression is always promoted to double, which is an 8 byte value for IEEE 754 implementations. This promotion occurs printf will already have an 8 byte argument on the stack when its execution starts.
If we assign 11.22to a double and inspect its contents, with my x86_64-pc-cygwin gcc I see the byte sequence a3702640.
That explains the int value printed by printf: Ints on this target still have 4 bytes, so that only the first four bytes
are evaluated, and again in little endian, i.e. as 0xe0000000. This is - in decimal.
If we reverse all of the 8 bytes, because the Intel processor stores doubles in little endian, too, we get e0000000. We can check the value this byte sequence represents in IEEE it's close to 1.122E1, i.e. 11.22, the expected result.
Your Answer
Sign up or
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Post as a guest
By posting your answer, you agree to the
Not the answer you're looking for?
Browse other questions tagged
Stack Overflow works best with JavaScript enabledmatlab中的mean函数是什么意思啊,如何使用_百度知道
matlab中的mean函数是什么意思啊,如何使用
matlab中的mean函数函数功能是求数组的平均数或者均值。使用方法如下:M = mean(A)返回沿数组中不同维的元素的平均值。如果A是一个向量,mean(A)返回A中元素的平均值。如果A是一个矩阵,mean(A)将其中的各列视为向量,把矩阵中的每列看成一个向量,返M = mean(A,dim)返回A中沿着标量dim指定的维数上的元素的平均值。对于矩阵,mean(A,2)就是包含每一行的平均值的列向量。比如:A = [1 2 3; 3 3 6; 4 6 8; 4 7 7];mean(A)ans =
3.0 6.0000mean(A,2)ans =
6.0000mean(A)
其他类似问题
为您推荐:
提问者采纳
果你有这样一个矩阵;用mean(A)(默认dim=1)就会求每一列的均值ans =
3; 4 7 7].5000
6; 3 3 6:A = [1 2 3.0000
4,2)就会求每一行的均值 ans =
2.0000用mean(A.0000
4; 4 6 8.0000
其他4条回答
4 6 8:A = [1 2 3.0000
6;用mean(A)(默认dim=1)就会求每一列的均值ans =
4; 3 3 6.0000用mean(A; 4 7 7],2)就会求每一行的均值ans =
6matlab中mean函数的用法 如果你有这样一个矩阵
求平均值。直接调用,mean(a),其中a为向量或一维数组;
或mean(x1,x2,x3,...)
mean(X)代表了矩阵X中每一列的均值
dim是dimension的缩写,维数。
matlab的相关知识
等待您来回答
下载知道APP
随时随地咨询
出门在外也不愁c语言设数组a[10]中的元素均为整数,用scanf函数给数组a进行初始化,并求数组中偶数的个数以_百度知道
c语言设数组a[10]中的元素均为整数,用scanf函数给数组a进行初始化,并求数组中偶数的个数以
c语言设数组a[10]中的元素均为整数,用scanf函数给数组a进行初始化,并求数组中偶数的个数以及偶数的平均值。
提问者采纳
scanf(&quot,sum=0,
for(i=0; printf(&quot,i;n&quot,我来搞定#include&i++) {
printf(&please input number %d &#92,&a[i]);i&
if(a[i]%2==0)
evencount++,i);;void main(){ int a[10];10;
sum+=a[i]; } mean=sum&#47,mean=0;stdio:%d &The even number is ,mean),evencount=0.h&gt: %%d&,j等几分钟;10;
#include&stdio.h&void main(){int a[10],i,j,evencount=0,mean=0,sum=0;for(i=0;i&10;i++){printf(&please input number %d \n&,i);scanf(&%d&,&a[i]);if(a[i]%2==0)
evencount++;
sum+=a[i];
}}mean=sum/printf(&The even number is : %d the average is :%d &,evencount,mean);}
什么意思啊!
evencount,是这个,打错了,这个代表的是偶数的数组个数
看懂了吧,加油学吧,给个满意答案
可不可以再帮我编写个程序啊!
提问者评价
太给力了,你的回答完美地解决了我的问题,非常感谢!
来自团队:
其他类似问题
为您推荐:
scanf的相关知识
等待您来回答
下载知道APP
随时随地咨询
出门在外也不愁#include&stdio.h& int average(float *score,int n,float *mean) { float ave,sum=0.0; int count=0,k,j;_百度知道
#include&stdio.h& int average(float *score,int n,float *mean) { float ave,sum=0.0; int count=0,k,j;
哪位好心人帮我逐个翻译一下
提问者采纳
h&gt,第一个为float型的指针score,sum = 0, k,第二个为整型变量n;局部变量
/包含头文件int average(float *score,第三个为float型的指针mean{&#47,j;&#47,一共有三个参数;
int count = 0,/局部变量ave , float *mean)
&#47.0;average函数,返回类型为int型;/stdio#include &lt
提问者评价
谢谢,虽然不是很懂
来自团队:
其他类似问题
为您推荐:
等待您来回答
下载知道APP
随时随地咨询
出门在外也不愁

我要回帖

更多关于 intd tv 安卓 的文章

 

随机推荐