473,387 Members | 3,821 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

Confused at type conversion

Hi,
I have the following 2 C files:

f.c:
#include <stdio.h>

void banana_peel(char c, short s, float f){
printf("char c = %c short s = %d float f = %f \n", c, s, f);
}

1.c:
int main(){
short ss = 7;
char cc = 65;
float ff = 10.0;
banana_peel(cc, ss, ff);

return 0;
}

I use gcc to compile them on Linux, and I see the result is :

char c = A short s = 7 float f = 0.000000

I am wondering why the value of f is changed?

When banana_peel is called in main(), ff is converted to double, but
it is still 10.0 right, then why argument f in function banana_peel
gets chopped off?

Aug 14 '07 #1
3 1698
12*******@gmail.com wrote:
I have the following 2 C files:
f.c:
#include <stdio.h>
void banana_peel(char c, short s, float f){
printf("char c = %c short s = %d float f = %f \n", c, s, f);
}
1.c:
int main(){
short ss = 7;
char cc = 65;
float ff = 10.0;
banana_peel(cc, ss, ff);
return 0;
}
I use gcc to compile them on Linux, and I see the result is :
char c = A short s = 7 float f = 0.000000
I am wondering why the value of f is changed?
When banana_peel is called in main(), ff is converted to double,
Yes, but only since there is no prototype for banana_peel() in
scope in 1.c. If there were one than this conversion to double
wouldn't happen. You're just lucky that the integral promotion
for the first two arguments did not also lead to unexpected
output - on a different architecture that also could happen.
but it is still 10.0 right, then why argument f in function banana_peel
gets chopped off?
Your function banana_peel() expects a float as the third argument,
but gets a double, which it then treats as if it would be a float
anyway. I guess you wouldn't surprised if something strange would
be printed out if you would call banana_peel() from main() with an
int as the third argument - integers have a completely different
bit representation than floats. Why do you expect then that it
should be any different when you pass it a double instead of the
expected float? Also floats and doubles have different bit repre-
sentations. The double 10.0 doesn't get chopped off, 0.0 seems to
be what you get when the computer tries to interpret parts of this
double as a float value.
Regards, Jens
--
\ Jens Thoms Toerring ___ jt@toerring.de
\__________________________ http://toerring.de
Aug 14 '07 #2
12*******@gmail.com writes:
I have the following 2 C files:

f.c:
#include <stdio.h>

void banana_peel(char c, short s, float f){
printf("char c = %c short s = %d float f = %f \n", c, s, f);
}

1.c:
int main(){
short ss = 7;
char cc = 65;
float ff = 10.0;
banana_peel(cc, ss, ff);

return 0;
}

I use gcc to compile them on Linux, and I see the result is :

char c = A short s = 7 float f = 0.000000
[...]

As Jens pointed out, the problem is that when the compiler sees the
call to banana_peel(), it has no visible delaration for that function,
so it doesn't know how to call it properly. It falls back to certain
default assumptions which happen to be incorrect in this case.

The solution is to add a declaration.

The simplest way is to combine your f.c and 1.c into a single source
file, but that doesn't scale well to very large programs, and I
suspect the whole point here is to learn how to use multiple source
files.

In general, you want to *define* functions in ".c" files and *declare*
them in ".h" files. Here's my version of your program, with an added
"f.h" file:
========================================
==f.h <==
void banana_peel(char c, short s, float f);

==f.c <==
#include <stdio.h>
#include "f.h"

void banana_peel(char c, short s, float f){
printf("char c = %c short s = %d float f = %f \n", c, s, f);
}

==1.c <==
#include "f.h"

int main(){
short ss = 7;
char cc = 65;
float ff = 10.0;
banana_peel(cc, ss, ff);

return 0;
}
========================================

#including "f.h" in "f.c" isn't strictly necessary, but it allows the
compiler to check that your declaration is consistent with your
definition.

For more complex cases, where a header might be #included multiple
times (due to headers #including other headers), you want "include
guards", so the header is only processed once for each translation
unit:

#ifndef H_F
#define H_F
void banana_peel(char c, short s, float f);
#endif

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <* <http://users.sdsc.edu/~kst>
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
Aug 14 '07 #3
Jens Thoms Toerring wrote:
>
12*******@gmail.com wrote:
I have the following 2 C files:
f.c:
#include <stdio.h>
void banana_peel(char c, short s, float f){
printf("char c = %c short s = %d float f = %f \n", c, s, f);
}
1.c:
int main(){
short ss = 7;
char cc = 65;
float ff = 10.0;
banana_peel(cc, ss, ff);
return 0;
}
I use gcc to compile them on Linux, and I see the result is :
char c = A short s = 7 float f = 0.000000
I am wondering why the value of f is changed?
When banana_peel is called in main(), ff is converted to double,

Yes, but only since there is no prototype for banana_peel() in
scope in 1.c. If there were one than this conversion to double
wouldn't happen. You're just lucky that the integral promotion
for the first two arguments did not also lead to unexpected
output - on a different architecture that also could happen.
but it is still 10.0 right, then why argument f in function banana_peel
gets chopped off?

Your function banana_peel() expects a float as the third argument,
but gets a double, which it then treats as if it would be a float
anyway. I guess you wouldn't surprised if something strange would
be printed out if you would call banana_peel() from main() with an
int as the third argument - integers have a completely different
bit representation than floats. Why do you expect then that it
should be any different when you pass it a double instead of the
expected float? Also floats and doubles have different bit repre-
sentations. The double 10.0 doesn't get chopped off, 0.0 seems to
be what you get when the computer tries to interpret parts of this
double as a float value.
I'll provide a reference:

N869
6.5.2.2 Function calls
[#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.

--
pete
Aug 15 '07 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

34
by: Richard Hunt | last post by:
I'm sorry for asking such a silly question, but I can't quite get my head around malloc. Using gcc I have always programmed in a lax C/C++ hybrid (which I suppose is actually c++). But I have...
9
by: vijay | last post by:
Hello, I am new to C Programming and just started reading K&R. I was about to finish the pointers chapter but got very confused with: 1. int arr; >From what I have read, arr is a pointer to...
7
by: sunglo | last post by:
My doubt comes from trying to understand how thread return values work (I know, it's off topic here), and I'm wondering about the meaning of the "void **" parameter that pthread_join expects (I...
3
by: Ken Varn | last post by:
I have a managed C++ function that accepts an IntPtr argument. I am passing in a variable of type HANDLE for the IntPtr argument. The compiler does not issue any warnings for this, so I am...
33
by: Lalatendu Das | last post by:
Dear friends, I am getting a problem in the code while interacting with a nested Do-while loop It is skipping a scanf () function which it should not. I have written the whole code below. Please...
11
by: herpers | last post by:
Hello, I probably don't see the obvious, but maybe you can help me out of this mess. The following is my problem: I created two classes NormDistribution and DiscDistribution. Both classes...
11
by: timmu | last post by:
Someone asked me a question about integer division and printf yesterday, I tell him he should do a casting to float/double before you do any interger division. But he doesn't think so, so I try...
4
by: bryant058 | last post by:
cout<<"-Please enter the spacing:"; cin>>space; cout<<"-Please enter a string of length<70:"<<endl; getline(cin,s); ----------------------------------------------------------------- when...
1
by: rlwebbnafex | last post by:
Hello, I found this example of a class and I'm just trying to understand what's going on with the operator function, could someone help? I understand the template part. But i dont get the statment...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.