I think you are very new to c programming. This is a very simple job. This can be done as:-

float a;
// to input a data in the variable a
scanf("%f",&a);
//to display the stored data
printf("%f\n",a);
//or 
printf("%.nf\n",a);//specify value of n
//maximum value of n is 6 also is its default value
//for e.g. printf("%.2f",a); will display decimal number upto two digits after the decimal place
//you can know more about displaying data as
%d: decimal value (int) 
%c: character (char) 
%s: string (const char *) 
%u: unsigned decimal value (unsigned int) 
%ld: long int (long) 
%f: float value (float or double) 
%x: decimal value in Hexadecimal format 
%o: decimal value in octal format

For avialabe list of formatting specifications go here

Answer from nurakantech on Stack Overflow
🌐
Sololearn
sololearn.com › en › Discuss › 2910424 › how-to-add-integer-part-of-the-decimal-numbers-by-c-language
how to add integer part of the decimal numbers by c language | Sololearn: Learn to code for FREE!
You need to use modf() function from <math.h>. The function's purpose is to break a floating point value into integral and fractional parts. http://www.cplusplus.com/reference/cmath/modf/ Once you extracted the integral parts of the two numbers, ...
🌐
Quora
quora.com › How-do-I-write-function-in-C-that-will-add-decimal-numbers-who-have-point-on-different-places
How to write function in C that will add decimal numbers who have point on different places - Quora
Answer (1 of 3): (After reading details in the comment.) Without knowing what [code ]Number_t[/code] looks like it’s impossible to answer this question, but from the sample snippets I can guess how it should behave. [code]// This is just a guess. typedef struct __number_t { int exponent; un...
Discussions

how to keep decimals in c - Stack Overflow
I am writing simple code for homework. I get one number from user, which is 3.4, when I define it with scanf("%d",&a) it takes only 3 and do it that way. I defined a as int a; What shou... More on stackoverflow.com
🌐 stackoverflow.com
scanf - Input decimal numbers in C - Stack Overflow
I am extremely beginner in programming language. so i am facing some problems. please help me out. Is it possible to take input a floating or double number with 2 digits after the decimal point usi... More on stackoverflow.com
🌐 stackoverflow.com
Decimal places
Can you use an integer and add in a decimal point when you display it? A lot of times when software is doing something with money amounts people will just store, say, $15.00 dollars as 1500. You can end up with inaccuracies trying to store these as a float and rounding because floating point numbers are stored as binary and not base 10. More on reddit.com
🌐 r/C_Programming
9
2
September 15, 2018
c - printf, how to insert decimal point for integer - Stack Overflow
A new AI Addendum clarifies how Stack Overflow utilizes AI interactions. Find centralized, trusted content and collaborate around the technologies you use most. Learn more about Collectives ... Connect and share knowledge within a single location that is structured and easy to search. Learn more about Teams ... How to use printf to insert a decimal point before the last two digits so the example numbers ... More on stackoverflow.com
🌐 stackoverflow.com
🌐
Stack Overflow
stackoverflow.com › questions › 40650293 › adding-decimal-numbers-as-strings-in-c
Adding decimal numbers as strings in C - Stack Overflow
Add the integerPart, add the decimalPart and handle the carryForwards in the decimalPart. ... Sign up to request clarification or add additional context in comments.
🌐
Reddit
reddit.com › r/c_programming › decimal places
r/C_Programming on Reddit: Decimal places
September 15, 2018 -

Can I create a variable and set it to a specific decimal place? For example "float z to 5 dp" where z would be recorded to 5 dp for the rest of the program

🌐
Linux Hint
linuxhint.com › setting_decimal_precision_c_-language
Setting Decimal Precision in C – Linux Hint
One method of adjusting the decimal precision of a variable is to multiply it by a factor of 10, 100, or some other multiple of 10 depending on what precision you want to achieve, and round the result with the ceilf() function. Finally, divide the result by the same factor that it was multiplied by.
Find elsewhere
🌐
GeeksforGeeks
geeksforgeeks.org › c language › g-fact-41-setting-decimal-precision-in-c
Setting decimal precision in C - GeeksforGeeks
January 10, 2025 - // C program to set precision in floating point numbers // using format specifier #include<stdio.h> int main() { float num = 5.48958123; // 4 digits after the decimal point printf("%0.4f", num); return 0; }
🌐
W3Schools
w3schools.com › c › c_data_types_dec.php
C Data Types Decimal Precision
C Examples C Real-Life Examples C Exercises C Quiz C Code Challenges C Practice Problems C Compiler C Syllabus C Study Plan C Interview Q&A C Certificate ... You have probably already noticed that if you print a floating point number, the output will show many digits after the decimal point: float myFloatNum = 3.5; double myDoubleNum = 19.99; printf("%f\n", myFloatNum); // Outputs 3.500000 printf("%lf", myDoubleNum); // Outputs 19.990000 Try it Yourself » · If you want to remove the extra zeros (set decimal precision), you can use a dot (.) followed by a number that specifies how many digits that should be shown after the decimal point:
🌐
IBM
ibm.com › docs › en › zos › 2.4.0
Using decimal data types in C
We cannot provide a description for this page right now
🌐
Reddit
reddit.com › r/learnprogramming › [c] how can i store a decimal number within an integer variable?
r/learnprogramming on Reddit: [C] How can I store a decimal number within an integer variable?
April 16, 2020 -

Hi! I'm learning to use operators and my handoobk used this program to explain them:

#include <stdio.h>

int main(void) {

/* Variables */

int a =100; b =2; c =25; d =4; result;

result = a - b; //substraction

printf ("a - b = %i\n", result);

}

But what if I want to operate result= c/b and then display it? How can I store the result of 25 / 2 in a variable that has been defined as an int type? Changing the variable type to floator doublemid program resulted in an error, and changing its type throughout the program displayed a 0 on all the operations.

🌐
W3Schools
w3schools.com › c › c_data_types_numbers.php
C Numbers
C Examples C Real-Life Examples C Exercises C Quiz C Code Challenges C Compiler C Syllabus C Study Plan C Interview Q&A C Certificate ... Use int when you need to store a whole number without decimals, like 35 or 1000, and float or double when you ...
🌐
Cprogramming
cboard.cprogramming.com › cplusplus-programming › 84700-allow-decimals.html
allow decimals
October 28, 2006 - What allows decimals?. Also lets say I make an integer called x, a and b, and I want x to be a*b what should I do? ... In C++ the default type for decimal numbers is double, so use double instead of int. To assign the value of a*b to x, you do just that, assign a*b to x.
🌐
Reddit
reddit.com › r/computerscience › efficiently use decimal numbers in c
r/computerscience on Reddit: Efficiently use decimal numbers in C
March 2, 2022 -

TL;DR: Can someone point me to research on very efficient decimal number manipulation and interoperability with floats and ints, written in the C language?


Sorry for the wall of text, but I want to give the full background here. As most of you know, floating point numbers are a pain when you need a certain level of exact mathematical calculations.

For example, .1 + .2 - .3 equals zero, but not for most programming languages (static languages, for varying definitions of static tend to have this issue, too):

$ perl -E 'say .1+.2-.3'
5.55111512312578e-17
$ ruby -e 'puts 0.1 + 0.2 - 0.3'
5.551115123125783e-17
$ python -c 'print .1 + .2 - .3'
5.55111512313e-17
$ echo "puts [expr .1+.2-.3]"|tclsh
5.551115123125783e-17

Those are very small numbers, but they're not zero. If you divide the mass of the sun by those values of "zero", you get something which is roughly the mass of Mount Everest. You want an exception instead.

For any of you working on financial systems, you know about converting floats to ints, doing math on them, and converting them back, but it's a pain:

$ python -c 'print (10*.1 + 10*.2 - 10*.3)/10'
0.0

Then there's Raku's approach:

$ rakudo -e 'say .1+.2-.3'
0

The last example from the Raku programming language looks interesting, but it's not using decimal numbers. It's using rationals:

$ rakudo -e 'say (.1+.2-.3).WHAT'
(Rat)

That looks pretty cool, but as soon as you get something that can't be represented as a rational (numerator over denominator), such as the square root of 2, it falls back to Num, a floating point:

$ rakudo -e 'say (.5+1.5).WHAT'
(Rat)
$ rakudo -e 'say (.5+1.5).sqrt.WHAT'
(Num)

With decimal numbers, I could specify an arbitrary precision and I'll get a decimal number back. Some languages offer built-in classes to support this, such as java.math.BigDecimal in Java, or decimal.Decimal in Python. However, there's a catch: they're classes that wrap native numbers, not direct native support for decimals.

What many people don't know is that COBOL not only offers native decimal number support, but COBOL to Java conversions sometimes fails because Java simulates this at the language level, not the compiler level. As a result, I've read about COBOL to Java conversion projects that failed because Java implementations were often slower than their COBOL counterparts. This is a massive problem when dealing with millions of dollars of financial transactions in a batch process. Those errors add up, so using decimals minimizes errors, but at the cost of performance.

It's also worth noting that many mainframes have native hardware support for decimal numbers, something COBOL takes advantage of, but most languages do not.

I was recently investigating introducing a native decimal type to the Perl programming language, but while some interest was expressed, I couldn't provide decent background on extremely efficient implementations. Having a native decimal type would be a huge boon for producing programs that need very accurate math. Can someone offer suggestions on where to go for more information?

🌐
CodingTechRoom
codingtechroom.com › question › add-leading-zeros-decimal-figures-printf-c
How to Add Leading Zeros and Specify Decimal Figures Using printf in C? - CodingTechRoom
To format numbers with leading zeros and specify the number of decimal places, you can use format specifiers within the `printf` function. This allows for clear control over the output format, which is particularly useful in financial applications, data logging, and wherever consistent number representation is crucial. ... To add leading zeros, you need to specify a width for the number and prepend a zero in the format specifier.
🌐
Quora
quora.com › How-do-you-write-a-C-program-that-will-take-a-decimal-number-and-if-it-has-any-value-except-zero-in-the-decimal-place-then-it-will-print-it-otherwise-it-will-only-print-the-integer-part
How to write a C program that will take a decimal number, and if it has any value except zero in the decimal place then it will print it, otherwise it will only print the integer part - Quora
Answer (1 of 6): Re “How do you write a C program”: By doing your own programming instead of grubbing for cheats on Quora. Re “that will take a decimal number”: There is no such thing as “a decimal number”. Did you mean “a decimal representation of a real number”? But computers ...
🌐
Reddit
reddit.com › r/c_programming › maximum decimal places
r/C_Programming on Reddit: Maximum decimal places
June 10, 2020 -

What is the maximum decimal places that can be stored using c? I tried to store - 1.86264514923107200935514487085e-09 but only got 0.00000000186264514923107200000000.....

Top answer
1 of 5
3
https://en.wikipedia.org/wiki/Double-precision_floating-point_format Significant precision is 53 bits = 9.00719925e15, so that's 15 (almost 16) decimal digits of significance
2 of 5
3
If you care about preserving the value across a complete decimal → floating-point → decimal round-trip: #include #include int main(void) { printf("FLT_DIG = %d\n", FLT_DIG); printf("DBL_DIG = %d\n", DBL_DIG); printf("LDBL_DIG = %d\n", LDBL_DIG); } On my system, I get: FLT_DIG = 6 DBL_DIG = 15 LDBL_DIG = 18 Note that this does not mean that all values with at most this many decimal digits can be represented "exactly". It just means that the round-trip will yield the same decimal digits at the end. Note also that "decimal digits" here is not just the digits after the decimal point. 1234.56 has 6 significant decimal digits. If stored in a float and then reformatted as a decimal value, it will yield the same decimal value on my system. But 1234.567 has 7 significant decimal digits, and this is not guaranteed to yield the same decimal value if round-tripped through a float on my system. If you care about a floating-point → decimal → floating-point round-trip, you need more decimal digits: #include #include int main(void) { printf("FLT_DECIMAL_DIG = %d\n", FLT_DECIMAL_DIG); printf("DBL_DECIMAL_DIG = %d\n", DBL_DECIMAL_DIG); printf("LDBL_DECIMAL_DIG = %d\n", LDBL_DECIMAL_DIG); } I get: FLT_DECIMAL_DIG = 9 DBL_DECIMAL_DIG = 17 LDBL_DECIMAL_DIG = 21 These values are greater than the ones before because round-tripping in this direction needs to preserve the precision in the original floating-point value.
🌐
Programiz
programiz.com › c-programming › examples › product-numbers
C Program to Multiply Two Floating-Point Numbers
#include <stdio.h> int main() { double a, b, product; printf("Enter two numbers: "); scanf("%lf %lf", &a, &b); // Calculating product product = a * b; // %.2lf displays number up to 2 decimal point printf("Product = %.2lf", product); return 0; } Output · Enter two numbers: 2.4 1.12 Product = 2.69 ·