%G Format Specifier in C
In this article, we shall learn about the format specifier %g
using the C programming language.
Format Specifiers in C Programming Language
Format Specifiers are nothing more than a special kind of string or operator that may be used in C. They are often used when collecting input from the user and producing information for the console.
Their goal is to indicate to the compiler the data type of any information being input or produced. When we talk about data types, we mean stuff like integers, strings, floats, and so on.
There is not a single data type that does not have its own unique set of format specifiers. The %
sign is always the first character in a format specifier, followed by a string of other characters.
They are used with scanf
for input and printf
for output in the C programming language. Following are some of the format specifiers used in the C programming language.
Format Specifier | Description |
---|---|
%g |
Similar as %e or %E |
%f |
Float Format Specifier |
%c |
Character Format Specifier |
%s |
String Format Specifier |
%e |
Scientific notation of floats |
%d |
Integer Format Specifier |
%x |
Hexadecimal representation |
the %g
Format Specifier in C
It utilizes the fixed precision standard for printing decimal floating-point numbers and is utilized for the purpose. In the C programming language, the %g
format specifier is used when working with a decimal kind of data which may be stored in C’s double
and float
data types.
The %g
takes a number that might be represented as either %f
(a simple float or double) or %e
(scientific notation), and it will return the number in the shorter format of the two.
In the following example, we will take three values of the double datatype, referred to as firstValue, secondValue,
and thirdValue,
and then print these values one at a time using the notation %g
and %G
.
Code Example:
#include <stdio.h>
int main() {
double firstValue = 123.45;
printf("Printing 123.45 using %%g %g\n", firstValue);
printf("Printing 123.45 using %%G %G\n\n", firstValue);
double secondValue = 123.45e8;
printf("Printing 123.45e8 using %%g %g\n", secondValue);
printf("Printing 123.45e8 using %%G %G\n\n", secondValue);
double thirdValue = 123.45e-8;
printf("Printing 123.45e-8 using %%g %g\n", thirdValue);
printf("Printing 123.45e-8 using %%G %G\n", thirdValue);
return 0;
}
Output:
Printing 123.45 using %g 123.45
Printing 123.45 using %G 123.45
Printing 123.45e8 using %g 1.2345e+10
Printing 123.45e8 using %G 1.2345E+10
Printing 123.45e-8 using %g 1.2345e-06
Printing 123.45e-8 using %G 1.2345E-06
I am Waqar having 5+ years of software engineering experience. I have been in the industry as a javascript web and mobile developer for 3 years working with multiple frameworks such as nodejs, react js, react native, Ionic, and angular js. After which I Switched to flutter mobile development. I have 2 years of experience building android and ios apps with flutter. For the backend, I have experience with rest APIs, Aws, and firebase. I have also written articles related to problem-solving and best practices in C, C++, Javascript, C#, and power shell.
LinkedIn