What does the constant E do in the c language

The documentation for floating-point constants is here.

The form 1e2 or 1E2 means 1 times ten to the power of 2, and you're perfectly correct that 2E-1 means 2 times ten to the power of -1, or 0.2.

It's based on the scientific E notation linked by Eugene.

When I run the code it prints a weird number

That's just because you used the wrong format specifier. These are documented here, and you can see you should use one of eEfFgG for doubles.

Answer from Useless on Stack Overflow
🌐
CopyProgramming
copyprogramming.com › howto › e-notation-in-c
E notation in C - Scientific notation
August 26, 2023 - The result of 12 should be right justified by default and occupy a minimum of 12 characters in width. The result from .5 should be precise up to 5 decimal places. The result of e will be in exponential notation.
🌐
Reddit
reddit.com › r/c_programming › quick question on %e "scientific notation" conversion specifier
r/C_Programming on Reddit: Quick question on %e "scientific notation" conversion specifier
September 23, 2022 -

Quick question about the %e conversion specifier in c.

I understand how the conversion works, but i'm a little confused when i encountered the solutions to these two problems as to why a 0 is added in front of the exponent in our scientific notation?!

In all the math tutorials/explanations found online, the exponents are simply written as 8.392e+2, 3.02530e+1

Now, in %m.pX, i am aware that m (minimal width) is what we check last and we fill in the extra spaces.... but why did we add the 0 in front of our exponents? I know the result fufills our "m" minimal width for each problem, but if we dont add the 0 there, couldn't we have just added two spaces to the left and that would have satisfied the (minimal width), the right amount of characters we're seeking?

x = 839.21f;

printf("|%10.3e|\n");

Answer: | 8.392e+02|

Question: why isn't it | 8.392e+2|

2.

x = 30.253f;

printf("|%12.5e|\n");

Answer: | 3.02530e+01|

Question: why isn't it | 3.02530e+1| <--- with 2 spaces in front (reddit post is not showing the 2 spaces clearly)

These two problems are from the book:

C Programming: A Modern Approach

I'm currently going through the chapters, but I don't remember it having mentioned or explained the reasoning to why we're filling in 0s for exponents.

🌐
Learn C++
learncpp.com › cpp-tutorial › introduction-to-scientific-notation
4.7 — Introduction to scientific notation – Learn C++
April 23, 2019 - Scientific notation is a useful shorthand for writing lengthy numbers in a concise manner. And although scientific notation may seem foreign at first, understanding scientific notation will help you understand how floating point numbers work, and more importantly, what their limitations are. Numbers in scientific notation take the following form: significand x 10exponent. For example, in the scientific notation 1.2 x 10⁴, 1.2 is the significand and 4 is the exponent.
🌐
Reddit
reddit.com › r/cprogramming › quick question on %e "scientific notation" conversion specifier
r/cprogramming on Reddit: Quick question on %e "scientific notation" conversion specifier
September 23, 2022 -

Quick question about the %e conversion specifier in c.

I understand how the conversion works, but i'm a little confused when i encountered the solutions to these two problems as to why a 0 is added in front of the exponent in our scientific notation?!

In all the math tutorials/explanations found online, the exponents are simply written as 8.392e+2, 3.02530e+1

Now, in %m.pX, i am aware that m (minimal width) is what we check last and we fill in the extra spaces.... but why did we add the 0 in front of our exponents? I know the result fufills our "m" minimal width for each problem, but if we dont add the 0 there, couldn't we have just added two spaces to the left and that would have satisfied the (minimal width), the right amount of characters we're seeking?

x = 839.21f;

printf("|%10.3e|\n");

Answer: | 8.392e+02|

Question: why isn't it | 8.392e+2|

2.

x = 30.253f;

printf("|%12.5e|\n");

Answer: | 3.02530e+01|

Question: why isn't it | 3.02530e+1| <--- with 2 spaces in front (reddit post is not showing the 2 spaces clearly)

These two problems are from the book:

C Programming: A Modern Approach

I'm currently going through the chapters, but I don't remember it having mentioned or explained the reasoning to why we're filling in 0s for exponents.

🌐
W3Schools
w3schools.com › c › c_data_types_numbers.php
C Numbers
The float could not store the number exactly, so the last digits are wrong. The double stores it correctly. In C, you can write very large or very small floating-point numbers using scientific notation.
🌐
Wikipedia
en.wikipedia.org › wiki › Scientific_notation
Scientific notation - Wikipedia
1 month ago - Scientific notation is a way of expressing numbers that are too large or too small to be conveniently written in decimal form, since to do so would require writing out an inconveniently long string of digits. It may be referred to as scientific form or standard index form, or standard form ...
🌐
Quora
quora.com › What-is-‘-e’-in-C-programming
What is ‘%e’ in C programming? - Quora
Answer (1 of 5): Basically in C Programming the concept of % is that of a format specifier. Typically the format specifiers you can use in ANSI C are as below: %c char single character %d (%i) int signed integer %e (%E) float or double exponential format %f float or double signed decimal %g ...
Find elsewhere
Top answer
1 of 2
26

E notation

Most calculators and many computer programs present very large and very small results in scientific notation. Because superscripted exponents like 107 cannot always be conveniently displayed, the letter E or e is often used to represent times ten raised to the power of (which would be written as "x 10b") and is followed by the value of the exponent. Note that in this usage the character e is not related to the mathematical constant e or the exponential function ex (a confusion that is less likely with capital E); and though it stands for exponent, the notation is usually referred to as (scientific) E notation or (scientific) e notation, rather than (scientific) exponential notation (though the latter also occurs). The use of this notation is not encouraged by publications.


As for your second question:

can we use this approach in other data type or we can only use it in floating points?

See the C# spec:

Real literals [the type of numeric literals that are allowed an E in them] are used to write values of types float, double, and decimal.

However, you have to cast or suffix the literal appropriately when assigning to anyhting other than a Double, because any literal with an e or E in it is recognized as a Double in Visual Studio. I cannot find where this behavior is specified.

float f1 = 7E1;     // Compile error. Needs F suffix (7E1F)
decimal d1 = 8E2;   // Compile error. Needs M suffix (8E2M)
double d2 = 9E3;    // Works.

int overninethousand = (int)9E3 + 1; // Works
2 of 2
19

In general, that's exponential/scientific notation...

1.7E+3 = 1.7 x 103 = 1700

🌐
GeeksforGeeks
geeksforgeeks.org › dsa › exponential-notation-decimal-number
Exponential notation of a decimal number - GeeksforGeeks
July 28, 2022 - Input :19 Output :1.9E1 Explanation: The exponential notation of 16 is 1.6E1. Approach: The simplest way is to find the position of the first non zero digit and the position of the dot. The difference between that positions is the value of b (if the value is positive you should also decrease it by one). Below is the implementation of the above approach: ... // C++ code to find the exponential notation #include <bits/stdc++.h> using namespace std; // function to calculate the exponential // notation void FindExponent(char s[], int n) { int i, j, b, c; for (i = 0; s[i] == '0' || s[i] == '.'; i++
🌐
LinuxQuestions.org
linuxquestions.org › questions › programming-9 › scientific-notation-for-c-14613
Scientific notation for C
I am reading a C book and it is talking about scientific notation... it refers to the number: 5e7 for example and mentions this format [mantissa]e[expo
🌐
Delft Stack
delftstack.com › "delft stack" › "howto" › "c howtos" › "how to print numbers in scientific notation in c"
How to Print Numbers in Scientific Notation in C | Delft Stack
February 12, 2024 - Flexibility in handling positive and negative numbers is provided through optional signs for both decimal value and exponent, ensuring a concise and standardized format. The printf function in C allows developers to control the precision of scientific notation, catering to the specific needs of mathematical calculations.
🌐
Cprogramming
cboard.cprogramming.com › c-programming › 93590-exponential-notation.html
Exponential Notation
September 14, 2007 - #include <stdio.h> int main() { double d; scanf("&#37;lf",&d); printf("d=%f\n", d); return 0; } $ gcc -W -Wall foo.c $ ./a.exe 3.0e-5 d=0.000030 But if you were expecting to see 3.0e-5 printed, then you need to look up what the "%e" and "%g" formats also do. printf() has many ways of controlling how floating point numbers are printed.
Top answer
1 of 4
17

That is 1e6, not le6, and it means 1 * 10^6 or 1000000.0

It is scientific notation.

2 of 4
9

I've seen some code online and I'm trying to work out what it is doing. In particular, I've never seen "1e" convention before.

As others have mentioned, practically speaking, 1e6 is scientific notation for 10^6 which is 1000000 or better known as 1 million. But as has already been mentioned, by David, this is actually treated as a double in C and the value is actually 1000000.0.

But I feel like these answers only focus on that specific piece of the code you provided and not the whole so I wanted to provide some additional context for you since you are trying to work out what the code is doing.

For these lines:

time_t currentTime;
time(&currentTime);

time takes a pointer time_t and operates on it, presumably writing some semblance of time to it.

The next line is actually illegal because currentTime was already declared so let's make a small modification:

uint64_t convertedTime = (uint64_t)currentTime * 1e6;

This line converts time_t currentTime to an unsigned 64-bit integer then multiplies it by 1e6 or 1000000.0. This is likely being done for unit conversion. For instance, let's assume time wrote the time in microseconds (1e-6, 10^-6, or .000001) to currentTime so multiplying it by 1e6 will give you seconds. And I say that just because of what appears to be a unit conversion here not because I actually know what time did (i.e. I'm taking the code at face value here).