int times = 5; int[ ] primes = new int [times]; int primeIndex = 0; int primesFound = 0; boolean isPrime = true; for (int i = 2; ; i++) { for (int j = 2; j < i; j++) { if (i % j == 0) { isPrime = false; break; } } if (isPrime) { primes [primeIndex++] = i; primesFound++; } else { isPrime = true; } if (primesFound == times) { break; } }
Top answer
1 of 5
4

The basic types in C are just there to standardize a single unit of memory (a block of bits) that can be used to store a value. We all know that a computer uses bits as its basic unit of information, however a bit is too small to be useful in and of itself, by defining different types we define different blocks of bits and how to interpret them. When we say "int a", the compiler knows that we are dealing with a basic, signed binary number, and knows the amount of memory it needs to set aside to store that number.

The biggest issue is that the exact size definitions are system dependent and not defined in the specifications.

  1. A float and double are both implementations of floating point values in C. floating point numbers are a way to implement decimal and fractional values, of any magnitude in binary and also streamline their arithmetic. It is essentially a binary version of scientific notation. Imagine writing the number 3.14159 In decimal, The mantissa here would be 314159 and the exponent would be -5. You could define a simple function that takes the two numbers (stored in binary) and prints out their combined decimal value, your program could therefore store all decimal numbers as two different numbers in binary, and use its own functions to add them together, print them etc. However, C streamlines this process by giving you a simple container that does all that work for you behind the scene. A floating point standard describes how you would encode the mantissa and exponent into a single binary number. In C, the only difference between a float and a double is the amount of memory set aside to store your number, and thus the greater range and precision of the numbers that you can store. The benefit of this is that you can also describe large integer numbers, for instance, you would need 32 bits to write the number "4 billion" explicitly in binary, but you only need 8 to do the same in floating point (4 x 109). However, you would not be able to write the number 4,123,456,789 as an 8 bit floating point or even a 32 bit floating point number (as you still need to encode the exponent), but it will fit perfectly well in a 32 bit int.

  2. A char and int are just basic binary numbers, their only difference being in length (potentially). A character is defined as the smallest unit of data necessary to hold a single text character for that architecture, this is a bit abstractly defined, but for most computer today running x86 platforms, a char is defined as 8 bits (although, on unicode systems it could be 16 bits). An integer is another basic binary number, but it is required to be at least 16 bits long (typically it is either 32 or 64 on modern systems). So a 8 bit char is capable of encoding 256 unique characters, or a number between 0-255 (or a number between -127 and 128) the difference is only in how you interpret the collection of bits. An 32 bit int can store a number between 0 and slightly over 4 billion, or you could half that range and use it to represent a number between (roughly) -2 billion and 2 billion

On some systems, the size of an int may be the same as the size of float, from the memory point of view they are just collections of bits, however the processor will interpret the values differently, and will actually use different circuits to add two numbers together if they are floats vs if they ints.

2 of 5
2

I don't know about examples, but they are simply different primitive types in C. Both double and float are for floating point numbers (e.g., numbers with fractional parts) and int and char are for whole numbers.

The reason there is more than one type for each class of number is because they take up a different amount of memory, and can therefore me bigger / more precise. There are actually quite a few more besides the four you've listed.

๐ŸŒ
W3Schools
w3schools.com โ€บ c โ€บ c_data_types_numbers.php
W3Schools.com
Use int when you need to store a whole number without decimals, like 35 or 1000, and float or double when you need a floating point number (with decimals), like 9.99 or 3.14515.
๐ŸŒ
MIP Australia
mip.com.au โ€บ blogs โ€บ data-basics-data-types
Data Basics: Data Types - MIP Australia | AI, Data & Analytics Consulting in Sydney
July 22, 2023 - If youโ€™re recording the ages ... even then, you could go up to 32,767 years old with int16.) Float: Float is the data term used for decimal numbers. Double: A double, is simply a float that can hold double the amount of ...
๐ŸŒ
Mount Allison University
mta.ca โ€บ ~rrosebru โ€บ oldcourse โ€บ comp1711 โ€บ 171102 โ€บ comp1711A โ€บ numbers.html
Primitive Data Types
A primitive data type is only a value. It has no methods. This is because primitive data types are provided as part of the java language - not through a java class definition ยท The java language provides many different primitive data types to represent and store numeric values.
๐ŸŒ
Reddit
reddit.com โ€บ r/c_programming โ€บ what is the difference between int and double and how do you know when to use them?
r/C_Programming on Reddit: What is the difference between int and double and how do you know when to use them?
February 6, 2015 - Well they can't really be interchanged. An int is used to represent integer numbers (+- whole numbers). While a double is used to represent a double precision floating point number.
๐ŸŒ
Medium
medium.com โ€บ @edu.hoyos โ€บ understanding-int-float-and-double-in-swift-91c1229083b2
Understanding Int, Float, and Double in Swift | by Eduardo Hoyos | Medium
March 28, 2025 - Due to its limited precision, Float is not ideal for calculations requiring high accuracy. Can introduce rounding errors in calculations. ... Double is a 64-bit floating-point number and provides higher precision than Float.
๐ŸŒ
Reddit
reddit.com โ€บ r/learnprogramming โ€บ [c] how to know when to use a double vs float or a long vs int?
r/learnprogramming on Reddit: [C] How to know when to use a double vs float or a long vs int?
November 28, 2017 -

Sorry if this seems like a basic question, but I've been learning C over the past several weeks. In some of the tutorials I've been using, I've noticed some of the instructors just use int/float while others tend to default to double when declaring floating point variables.

Is there any sort of best practice in terms of deciding what size variable to use?

Find elsewhere
๐ŸŒ
Quora
quora.com โ€บ What-is-the-difference-between-a-Float-Double-and-an-Int-in-C
What is the difference between a 'Float', 'Double', and an 'Int' in C? - Quora
Answer (1 of 4): Float and double are representations of real numbers, int is representation of integers. In a nutshell: separate concept from implementation and representation. Concepts are fixed, implementations and representations are changeable, even if in practice they are common. Languages...
๐ŸŒ
W3Schools
w3schools.com โ€บ cpp โ€บ cpp_data_types_numeric.asp
C++ Numeric Data Type
Use int when you need to store a whole number without decimals, like 35 or 1000, and float or double when you need a floating point number (with decimals), like 9.99 or 3.14515.
๐ŸŒ
Wikipedia
en.wikipedia.org โ€บ wiki โ€บ C_data_types
C data types - Wikipedia
October 2, 2026 - The C99 standard includes new real floating-point types float_t and double_t, defined in <math.h>. They correspond to the types used for the intermediate results of floating-point expressions when FLT_EVAL_METHOD is 0, 1, or 2.
๐ŸŒ
YouTube
youtube.com โ€บ caleb curry
C Programming Tutorial 21 - Int, Float, and Double Data Types - YouTube
Now, we've discussed data types some, but this video is going to be devoted to discussing numeric data types. Numeric data types are used to describe numbers...
Published: April 9, 2017
Views: 80K
๐ŸŒ
Quora
quora.com โ€บ What-is-the-difference-between-int-char-float-and-double-data-types
What is the difference between int, char, float and double data types? - Quora
Answer (1 of 4): Its all about semantics. * Use an int when you don't need fractional numbers and you've no reason to use anything else; on most processors/OS configurations, this is the size of number that the machine can deal with most efficiently; * Use a double when you need fractional num...
๐ŸŒ
Cplusplus
cplusplus.com โ€บ forum โ€บ beginner โ€บ 146152
Differences between int vs long, float v - C++ Forum
October 28, 2014 - Actually double is usually native floating point type for modern CPU and should be used unless you have very compelling reasons to use floats instead. http://en.wikipedia.org/wiki/IEEE_floating_point There are several integral types: char, short, int, long and long long.
๐ŸŒ
Arm Learning
learn.arm.com โ€บ learning-paths โ€บ cross-platform โ€บ integer-vs-floats โ€บ introduction-integer-float-types
Learn about integer and floating-point conversions: An introduction to integer and floating-point data types
Types may change in the future as big integer arithmetic is particularly useful in cryptography. Wikipedia provides an excellent overview of the 32-bit floating point number representation. A short summary is provided below. Every real number is represented in approximation by the closest 32-bit floating point number: ... A similar expression exists for the 64-bit floating-point number (double data type).
๐ŸŒ
Quora
quora.com โ€บ What-is-the-difference-between-double-long-float-and-int-variables-in-Java-Which-one-should-be-used-for-storing-large-numbers-more-than-two-digits-Why
What is the difference between double, long, float, and int variables in Java? Which one should be used for storing large numbers (more than two digits)? Why? - Quora
Answer: In Java, int and long are primitive integer datatypes. They store integers as twoโ€˜s complement numbers. Ints use 32 bits and have a range from -2^{31}=-2147483648 to 2^{31}-1=2147483647, longs use 64 bits and have a range from -2^{63}=-9223372036854775808 to 2^{63}-1=9223372036854775807.
๐ŸŒ
Kotlin
kotlinlang.org โ€บ docs โ€บ numbers.html
Numbers | Kotlin Documentation
September 29, 2026 - Use Long for whole numbers outside the Int range. Use Double for decimal numbers. Use Float when lower precision is acceptable or required.
Top answer
1 of 8
219

The default choice for a floating-point type should be double. This is also the type that you get with floating-point literals without a suffix or (in C) standard functions that operate on floating point numbers (e.g. exp, sin, etc.).

float should only be used if you need to operate on a lot of floating-point numbers (think in the order of thousands or more) and analysis of the algorithm has shown that the reduced range and accuracy don't pose a problem.

long double can be used if you need more range or accuracy than double, and if it provides this on your target platform.

In summary, float and long double should be reserved for use by the specialists, with double for "every-day" use.

2 of 8
48

There is rarely cause to use float instead of double in code targeting modern computers. The extra precision reduces (but does not eliminate) the chance of rounding errors or other imprecision causing problems.

The main reasons I can think of to use float are:

  1. You are storing large arrays of numbers and need to reduce your program's memory consumption.
  2. You are targeting a system that doesn't natively support double-precision floating point. Until recently, many graphics cards only supported single precision floating points. I'm sure there are plenty of low-power and embedded processors that have limited floating point support too.
  3. You are targeting hardware where single-precision is faster than double-precision, and your application makes heavy use of floating point arithmetic. On modern Intel CPUs I believe all floating point calculations are done in double precision, so you don't gain anything here.
  4. You are doing low-level optimization, for example using special CPU instructions that operate on multiple numbers at a time.

So, basically, double is the way to go unless you have hardware limitations or unless analysis has shown that storing double precision numbers is contributing significantly to memory usage.