You are here:

C/function

Advertisement


Question
Dear Sir,
Function is my weakest area in C. I did this question. Convert inches to cm using function. (1 inch =2.54cm.) then write a prog that enable user to key in the measure in inches, then calls the conversion functin n print out the measurement in cm.

Here are my coding. Please help me.You've been a great help in my other exercises. Can you see what is wrong with my coding and explain the mistakes and correct it?

#include<stdio.h>         /*Prototype declarations*/

float getNum(void);
float conversion(float x);
void printnumber(float x);

int main(void)
{
  float a;
  float b;                /*Local definitions*/

  a=getNum();

  b=(2.540000*a);

  printnumber(b);

  return 0;

}

float getNum(void)
{
  float numIn;

  printf("Enter the number to be converted:");
  scanf("%f",&numIn);
  return numIn;
}

float conversion(float x)
{
  return (x*2.540000);
}

void printnumber(float x)
{   
  printf("The number in centimeter is:%f\n",x);
  return;
}
  


and here are the warnings:
C:\Documents and Settings\Dzurihan Yusoff\Desktop\Assignment C\question3.c(18) : warning C4244: '=' : conversion from 'double ' to 'float ', possible loss of data

C:\Documents and Settings\Dzurihan Yusoff\Desktop\Assignment C\question3.c(37) : warning C4244: 'return' : conversion from 'double ' to 'float ', possible loss of data

Answer
I compiled your code on gcc with all the warning flags enabled. There is no warnings or errors!
Also, I don't see any problem in the code. It is working fine.
May be the compiler you are using is not ANSI compliant. If possible try to compile on another compiler (on windows) and see if you get the same warnings again.

Get back if the problem persists....

-ssnkumar

C

All Answers


Answers by Expert:


Ask Experts

Volunteer


Narendra

Expertise

I can answer questions in C related to programming, data structures, pointers and file manipulation. I use Solaris for doing C code and if you have questions related to C programming on Solaris, I will be able to help better.

Experience

6.5

Organizations belong to
Sun Microsystems

Awards and Honors
Brain Bench Certified Expert C programmer.
Advanced System Software Certified

©2012 About.com, a part of The New York Times Company. All rights reserved.