C programming - Output result in decimals - c

At the momemt, i'm learning the C and the basics of the language, but I have a problem with my code. When I multiply two numbers, I cant get the decimals, even I float the numbers I enter.
My code:
int main()
{
double result_met, km;
result_met = km = 0.0f;
/*Display text*/
printf("Enter values of the distance between the two cities in km's\n");
scanf_s("%d", &km);
/*Formular for a simple km conversion*/
result_met = km * 1000.0f;
/*Result print*/
printf("Meter:%d", result_met);
printf("\nWaiting for a character to be pressed from the keyboard to exit.\n");
getch();
return 0;
}

The format specifiers are incorrect - it should be %lf - %d is for int.
scanf_s("%lf", &km);
/*Formular for a simple km conversion*/
result_met = km * 1000.0f;
/*Result print*/
printf("Meter:%lf", result_met);
Format string specifications

The format specifier for double is %lf, not %d
You may as well use a float instead of a double, it saves memory (which is important when you begin to write big programs), then you must use the format specifier %f (the "l" in %lf is for "long", because a double is a long float)
When treating decimals, you want to print only a few decimals on the screen (to avoid the printing of a "2.50000001"), then you can use the format specifier %.3f if you want 3 and only 3 digits printed (3 can be any integer).
for example, the following code :
printf("%.2f\n",3.1415);
has the following output :
3.14
the printf function has many different format specifier, that can be very useful.
Go see the cpluplus.com reference if you want to learn more about it

Related

calculate the average number C [duplicate]

I try to read-in 2 values using scanf() in C, but the values the system writes into memory are not equal to my entered values. Here is the code:
double a,b;
printf("--------\n"); //seperate lines
scanf("%ld",&a);
printf("--------\n");
scanf("%ld",&b);
printf("%d %d",a,b);
If I enter 1 and 2, CMD returns a correct, but b = -858993460 Here is what I already tried:
using float or int instead of double, using scanf_s, using scanf("%d or %f for %i or %li or %lf or %e or %g ), using fflush(stdin) to clear keyboard buffer, reading in b first, trying like all possible combinations. I found out that there is a problem with the length of double on 32 bit OS, so that you are forced to use scanf("%lf", &f) to read in a double. No matter what I do, second value is always wrong.
I use MS VS express 2012 for Desktop on Windows 7 32 bit OS.
Use the %lf format specifier to read a double:
double a;
scanf("%lf",&a);
Wikipedia has a decent reference for available format specifiers.
You'll need to use the %lf format specifier to print out the results as well:
printf("%lf %lf",a,b);
As far as i know %d means decadic which is number without decimal point. if you want to load double value, use %lf conversion (long float). for printf your values are wrong for same reason, %d is used only for integer (and possibly chars if you know what you are doing) numbers.
Example:
double a,b;
printf("--------\n"); //seperate lines
scanf("%lf",&a);
printf("--------\n");
scanf("%lf",&b);
printf("%lf %lf",a,b);
You are using wrong formatting sequence for double, you should use %lf instead of %ld:
double a;
scanf("%lf",&a);
Format specifier in printf should be %f for doubl datatypes since float datatyles eventually convert to double datatypes inside printf.
There is no provision to print float data. Please find the discussion here : Correct format specifier for double in printf
Use this line of code when scanning the second value:
scanf(" %lf", &b);
also replace all %ld with %lf.
It's a problem related with input stream buffer. You can also use fflush(stdin); after the first scanning to clear the input buffer and then the second scanf will work as expected. An alternate way is place a getch(); or getchar(); function after the first scanf line.
Correct ways is:
double a,b;
printf("--------\n"); //seperate lines
scanf("%lf",&a); //%lf is long float numbers
printf("--------\n");
scanf("%lf",&b);
printf("%f %f",a,b); //%f float number
Using %lf will help you in solving this problem.
Use :
scanf("%lf",&doub)

How to read floating points from terminal? scanf always gets 0.0 [duplicate]

I try to read-in 2 values using scanf() in C, but the values the system writes into memory are not equal to my entered values. Here is the code:
double a,b;
printf("--------\n"); //seperate lines
scanf("%ld",&a);
printf("--------\n");
scanf("%ld",&b);
printf("%d %d",a,b);
If I enter 1 and 2, CMD returns a correct, but b = -858993460 Here is what I already tried:
using float or int instead of double, using scanf_s, using scanf("%d or %f for %i or %li or %lf or %e or %g ), using fflush(stdin) to clear keyboard buffer, reading in b first, trying like all possible combinations. I found out that there is a problem with the length of double on 32 bit OS, so that you are forced to use scanf("%lf", &f) to read in a double. No matter what I do, second value is always wrong.
I use MS VS express 2012 for Desktop on Windows 7 32 bit OS.
Use the %lf format specifier to read a double:
double a;
scanf("%lf",&a);
Wikipedia has a decent reference for available format specifiers.
You'll need to use the %lf format specifier to print out the results as well:
printf("%lf %lf",a,b);
As far as i know %d means decadic which is number without decimal point. if you want to load double value, use %lf conversion (long float). for printf your values are wrong for same reason, %d is used only for integer (and possibly chars if you know what you are doing) numbers.
Example:
double a,b;
printf("--------\n"); //seperate lines
scanf("%lf",&a);
printf("--------\n");
scanf("%lf",&b);
printf("%lf %lf",a,b);
You are using wrong formatting sequence for double, you should use %lf instead of %ld:
double a;
scanf("%lf",&a);
Format specifier in printf should be %f for doubl datatypes since float datatyles eventually convert to double datatypes inside printf.
There is no provision to print float data. Please find the discussion here : Correct format specifier for double in printf
Use this line of code when scanning the second value:
scanf(" %lf", &b);
also replace all %ld with %lf.
It's a problem related with input stream buffer. You can also use fflush(stdin); after the first scanning to clear the input buffer and then the second scanf will work as expected. An alternate way is place a getch(); or getchar(); function after the first scanf line.
Correct ways is:
double a,b;
printf("--------\n"); //seperate lines
scanf("%lf",&a); //%lf is long float numbers
printf("--------\n");
scanf("%lf",&b);
printf("%f %f",a,b); //%f float number
Using %lf will help you in solving this problem.
Use :
scanf("%lf",&doub)

How to input a number with 0 and decimal in C and do its sine?

I'm currently doing a C course and I have to make a program that prints the sine function for an input x between (0, 1). The thing is, I don't know what I'm doing wrong.
double x;
double result = 0;
printf("Input a number betwen 0 and 1: ");
scanf("%.f", x);
result = sin(x);
printf("\aThe sine of %.8f is %.8f\n", x, result);
system("pause>null");
return 0;
This is the code I wrote; when I try to input 0.5 the machine returns me a "The sine of 0.00000 is 0.0000", but if I declare the variable as 0.5, the program works fine and gives me the correct answer. The error appears when I try to use scanf to input the number, and the activity says I have to input it.
I looked in the internet and I can't find anything, I'm a newbie in this world, I started about 2 weeks ago.
There are two fundamental problems with your scanf("%.f", x); line. First, the %.f format specifier is wrong: the . is not valid for input of this sort, and a double value requires the l (lowercase L) prefix - so use %lf.
Second, the argument corresponding to that format specifier must be the address of a suitable (double) variable.
So, use scanf("%lf", &x); and your program will work.
Enable warnings
double x; scanf("%.f", x); should have warned as "%f" expects a matching float *, not a double.
Use "%lf" to read a double.
Even better, check the scanf() return value to insure valid input occurred.
if (scanf("%lf", &x) == 1) {
result = sin(x);
printf("\aThe sine of %.8f is %.8f\n", x, result);
}

Issues with printing a floating point number from user input in C [duplicate]

I try to read-in 2 values using scanf() in C, but the values the system writes into memory are not equal to my entered values. Here is the code:
double a,b;
printf("--------\n"); //seperate lines
scanf("%ld",&a);
printf("--------\n");
scanf("%ld",&b);
printf("%d %d",a,b);
If I enter 1 and 2, CMD returns a correct, but b = -858993460 Here is what I already tried:
using float or int instead of double, using scanf_s, using scanf("%d or %f for %i or %li or %lf or %e or %g ), using fflush(stdin) to clear keyboard buffer, reading in b first, trying like all possible combinations. I found out that there is a problem with the length of double on 32 bit OS, so that you are forced to use scanf("%lf", &f) to read in a double. No matter what I do, second value is always wrong.
I use MS VS express 2012 for Desktop on Windows 7 32 bit OS.
Use the %lf format specifier to read a double:
double a;
scanf("%lf",&a);
Wikipedia has a decent reference for available format specifiers.
You'll need to use the %lf format specifier to print out the results as well:
printf("%lf %lf",a,b);
As far as i know %d means decadic which is number without decimal point. if you want to load double value, use %lf conversion (long float). for printf your values are wrong for same reason, %d is used only for integer (and possibly chars if you know what you are doing) numbers.
Example:
double a,b;
printf("--------\n"); //seperate lines
scanf("%lf",&a);
printf("--------\n");
scanf("%lf",&b);
printf("%lf %lf",a,b);
You are using wrong formatting sequence for double, you should use %lf instead of %ld:
double a;
scanf("%lf",&a);
Format specifier in printf should be %f for doubl datatypes since float datatyles eventually convert to double datatypes inside printf.
There is no provision to print float data. Please find the discussion here : Correct format specifier for double in printf
Use this line of code when scanning the second value:
scanf(" %lf", &b);
also replace all %ld with %lf.
It's a problem related with input stream buffer. You can also use fflush(stdin); after the first scanning to clear the input buffer and then the second scanf will work as expected. An alternate way is place a getch(); or getchar(); function after the first scanf line.
Correct ways is:
double a,b;
printf("--------\n"); //seperate lines
scanf("%lf",&a); //%lf is long float numbers
printf("--------\n");
scanf("%lf",&b);
printf("%f %f",a,b); //%f float number
Using %lf will help you in solving this problem.
Use :
scanf("%lf",&doub)

Double and Float format displaying different results

I thought that the difference between double and float were the precision of the decimals. However, I am getting strange results with using double and float and they are no where close to one another.
The first segment of code is used with the float format producing the correct results:
#include <stdio.h>
#define ABSOLUTE_VALUE(number) ( ((number) < 0) ? -(number) : (number) )
int main (void)
{
float number, absNumber;
printf ("What number do you want to check the absolute value for? : ");
scanf ("%f", &number);
absNumber = ABSOLUTE_VALUE(number);
printf ("The absolute value of %.2f is %.2f\n", number, absNumber);
return 0;
}
Output:
What number do you want to check the absolute value for? : -3
The absolute value of -3.00 is 3.00
The second segment of code is used with the double format producing incorrect results:
#include <stdio.h>
#define ABSOLUTE_VALUE(number) ( ((number) < 0) ? -(number) : (number) )
int main (void)
{
double number, absNumber;
printf ("What number do you want to check the absolute value for? : ");
scanf ("%d", &number);
absNumber = ABSOLUTE_VALUE(number);
printf ("The absolute value of %.2d is %.2d\n", number, absNumber);
return 0;
}
Output:
What number do you want to check the absolute value for? : -3
The absolute value of -03 is 2147344384
In the second example, %d is used for integers. (d isn't short for double, but for decimal)
scanf ("%d", &number);
should be
scanf ("%lf", &number);
The printf is incorrect too, as %fis used for double
printf ("The absolute value of %.2d is %.2d\n", number, absNumber);
Instead, this should be:
printf ("The absolute value of %.2f is %.2f\n", number, absNumber);
Notice that the format specifier for double is different for scanf and printf, this C FAQ article has some good explanations.
Your last printf format string is wrong. Should be.
printf ("The absolute value of %.2f is %.2f\n", number, absNumber);
and if you enable all compiler warnings (e.g. compile with gcc -Wall -g) you'll have a warning about that. Always enable all warnings in the compiler.
Your scanf are also wrong, should be scanf("%lf", &number); and with all warnings the compiler would have warned you.
BTW, scanf returns the "number of successfully matched items" and you should test its result.
You really need to carefully read the documentation of printf(3) and of scanf(3). (In a linux terminal, you could type man 3 printf to get it).
Notice that arguments are converted to double and the conversion specifier for double-s is %f in printf and %lf in scanf
And you should learn how to use a debugger (like gdb).
In your second program, you have used %d format specifier for double which is wrong. And that's why you are getting error. You should use %lf (long float or better called 'double') for printing it.
For other format specifier,
I think this might help you
a link
You can also search different format specifiers on google by typing 'format specifiers'.
Make 2 changes for using double data-type in c :
use %lf in scanf
use %f in printf
//remember : %d is for int
%f is for float
%c is for char
If you read the manual, "%d" is for integers. You need "%lf" instead.
Ditto with the printf

Resources