This post has been edited by scorpionviolinist: 28 May 2008  07:02 PM
%lg vs %g
Page 1 of 13 Replies  3667 Views  Last Post: 29 May 2008  05:20 AM
#1
%lg vs %g
Posted 28 May 2008  06:42 PM
In C, I was wondering what the difference was between %lg and %g, and when to use each.
Replies To: %lg vs %g
#2
Re: %lg vs %g
Posted 28 May 2008  07:03 PM
Basically, g is the specifier for a floating point type (float or double)
l is to specify the long keyword.
In other words, %g would is a specifier for a float/double, and %lg is a long double.
l is to specify the long keyword.
In other words, %g would is a specifier for a float/double, and %lg is a long double.
Quote
modifiers
Specifies a size different from int (in the case of d, i and n), unsigned int (in the case of o, u and x) or float (in the case of e, f and g) for the data pointed by the corresponding additional argument:
h : short int (for d, i and n), or unsigned short int (for o, u and x)
l : long int (for d, i and n), or unsigned long int (for o, u and x), or double (for e, f and g)
L : long double (for e, f and g)
e,E,f,g,G
Floating point: Decimal number containing a decimal point, optionally preceeded by a + or  sign and optionally folowed by the e or E character and a decimal number. Two examples of valid entries are 732.103 and 7.12e4
Specifies a size different from int (in the case of d, i and n), unsigned int (in the case of o, u and x) or float (in the case of e, f and g) for the data pointed by the corresponding additional argument:
h : short int (for d, i and n), or unsigned short int (for o, u and x)
l : long int (for d, i and n), or unsigned long int (for o, u and x), or double (for e, f and g)
L : long double (for e, f and g)
e,E,f,g,G
Floating point: Decimal number containing a decimal point, optionally preceeded by a + or  sign and optionally folowed by the e or E character and a decimal number. Two examples of valid entries are 732.103 and 7.12e4
This post has been edited by gabehabe: 28 May 2008  07:03 PM
#3
Re: %lg vs %g
Posted 29 May 2008  03:46 AM
More specifically, declaring a long variable gives you certain limits on values within them.
A long int for example goes from ~2,000,000,000 to ~2,000,000,000
Where as an unsigned one goes from 0 to ~4,000,000,000
A long int for example goes from ~2,000,000,000 to ~2,000,000,000
Where as an unsigned one goes from 0 to ~4,000,000,000
This post has been edited by KYA: 29 May 2008  04:06 AM
#4
Re: %lg vs %g
Posted 29 May 2008  05:20 AM
man 3 printf gives a lot of details
The length modifier
Here, ‘integer conversion’ stands for d, i, o, u, x, or X conversion.
hh A following integer conversion corresponds to a signed char or
unsigned char argument, or a following n conversion corresponds
to a pointer to a signed char argument.
h A following integer conversion corresponds to a short int or
unsigned short int argument, or a following n conversion corre
sponds to a pointer to a short int argument.
l (ell) A following integer conversion corresponds to a long int
or unsigned long int argument, or a following n conversion cor
responds to a pointer to a long int argument, or a following c
conversion corresponds to a wint_t argument, or a following s
conversion corresponds to a pointer to wchar_t argument.
ll (ellell). A following integer conversion corresponds to a long
long int or unsigned long long int argument, or a following n
conversion corresponds to a pointer to a long long int argument.
L A following a, A, e, E, f, F, g, or G conversion corresponds to
a long double argument. (C99 allows %LF, but SUSv2 does not.)
q (‘quad’. 4.4BSD and Linux libc5 only. Don’t use.) This is a
synonym for ll.
j A following integer conversion corresponds to an intmax_t or
uintmax_t argument.
z A following integer conversion corresponds to a size_t or
ssize_t argument. (Linux libc5 has Z with this meaning. Don’t
use it.)
t A following integer conversion corresponds to a ptrdiff_t argu
ment.
The SUSv2 only knows about the length modifiers h (in hd, hi, ho, hx,
hX, hn) and l (in ld, li, lo, lx, lX, ln, lc, ls) and L (in Le, LE, Lf,
Lg, LG).
The conversion specifier
A character that specifies the type of conversion to be applied. The
conversion specifiers and their meanings are:
d,i The int argument is converted to signed decimal notation. The
precision, if any, gives the minimum number of digits that must
appear; if the converted value requires fewer digits, it is
padded on the left with zeros. The default precision is 1.
When 0 is printed with an explicit precision 0, the output is
empty.
o,u,x,X
The unsigned int argument is converted to unsigned octal (o),
unsigned decimal (u), or unsigned hexadecimal (x and X) nota
tion. The letters abcdef are used for x conversions; the let
ters ABCDEF are used for X conversions. The precision, if any,
gives the minimum number of digits that must appear; if the con
verted value requires fewer digits, it is padded on the left
with zeros. The default precision is 1. When 0 is printed with
an explicit precision 0, the output is empty.
e,E The double argument is rounded and converted in the style
[]d.ddde±dd where there is one digit before the decimalpoint
character and the number of digits after it is equal to the pre
cision; if the precision is missing, it is taken as 6; if the
precision is zero, no decimalpoint character appears. An E
conversion uses the letter E (rather than e) to introduce the
exponent. The exponent always contains at least two digits; if
the value is zero, the exponent is 00.
f,F The double argument is rounded and converted to decimal notation
in the style []ddd.ddd, where the number of digits after the
decimalpoint character is equal to the precision specification.
If the precision is missing, it is taken as 6; if the precision
is explicitly zero, no decimalpoint character appears. If a
decimal point appears, at least one digit appears before it.
(The SUSv2 does not know about F and says that character string
representations for infinity and NaN may be made available. The
C99 standard specifies ‘[]inf’ or ‘[]infinity’ for infinity,
and a string starting with ‘nan’ for NaN, in the case of f con
version, and ‘[]INF’ or ‘[]INFINITY’ or ‘NAN*’ in the case of
F conversion.)
g,G The double argument is converted in style f or e (or F or E for
G conversions). The precision specifies the number of signifi
cant digits. If the precision is missing, 6 digits are given;
if the precision is zero, it is treated as 1. Style e is used
if the exponent from its conversion is less than 4 or greater
than or equal to the precision. Trailing zeros are removed from
the fractional part of the result; a decimal point appears only
if it is followed by at least one digit.
a,A (C99; not in SUSv2) For a conversion, the double argument is
converted to hexadecimal notation (using the letters abcdef) in
the style []0xh.hhhhp±d; for A conversion the prefix 0X, the
letters ABCDEF, and the exponent separator P is used. There is
one hexadecimal digit before the decimal point, and the number
of digits after it is equal to the precision. The default pre
cision suffices for an exact representation of the value if an
exact representation in base 2 exists and otherwise is suffi
ciently large to distinguish values of type double. The digit
before the decimal point is unspecified for nonnormalized num
bers, and nonzero but otherwise unspecified for normalized num
bers.
c If no l modifier is present, the int argument is converted to an
unsigned char, and the resulting character is written. If an l
modifier is present, the wint_t (wide character) argument is
converted to a multibyte sequence by a call to the wcrtomb(3)
function, with a conversion state starting in the initial state,
and the resulting multibyte string is written.
s If no l modifier is present: The const char * argument is
expected to be a pointer to an array of character type (pointer
to a string). Characters from the array are written up to (but
not including) a terminating null byte (’\0’); if a precision is
specified, no more than the number specified are written. If a
precision is given, no null byte need be present; if the preci
sion is not specified, or is greater than the size of the array,
the array must contain a terminating null byte.
If an l modifier is present: The const wchar_t * argument is
expected to be a pointer to an array of wide characters. Wide
characters from the array are converted to multibyte characters
(each by a call to the wcrtomb(3) function, with a conversion
state starting in the initial state before the first wide char
acter), up to and including a terminating null wide character.
The resulting multibyte characters are written up to (but not
including) the terminating null byte. If a precision is speci
fied, no more bytes than the number specified are written, but
no partial multibyte characters are written. Note that the pre
cision determines the number of bytes written, not the number of
wide characters or screen positions. The array must contain a
terminating null wide character, unless a precision is given and
it is so small that the number of bytes written exceeds it
before the end of the array is reached.
C (Not in C99, but in SUSv2.) Synonym for lc. Don’t use.
S (Not in C99, but in SUSv2.) Synonym for ls. Don’t use.
p The void * pointer argument is printed in hexadecimal (as if by
%#x or %#lx).
n The number of characters written so far is stored into the inte
ger indicated by the int * (or variant) pointer argument. No
argument is converted.
m (Glibc extension.) Print output of strerror(errno). No argu
ment is required.
% A ‘%’ is written. No argument is converted. The complete con
version specification is ‘%%’.
The length modifier
Here, ‘integer conversion’ stands for d, i, o, u, x, or X conversion.
hh A following integer conversion corresponds to a signed char or
unsigned char argument, or a following n conversion corresponds
to a pointer to a signed char argument.
h A following integer conversion corresponds to a short int or
unsigned short int argument, or a following n conversion corre
sponds to a pointer to a short int argument.
l (ell) A following integer conversion corresponds to a long int
or unsigned long int argument, or a following n conversion cor
responds to a pointer to a long int argument, or a following c
conversion corresponds to a wint_t argument, or a following s
conversion corresponds to a pointer to wchar_t argument.
ll (ellell). A following integer conversion corresponds to a long
long int or unsigned long long int argument, or a following n
conversion corresponds to a pointer to a long long int argument.
L A following a, A, e, E, f, F, g, or G conversion corresponds to
a long double argument. (C99 allows %LF, but SUSv2 does not.)
q (‘quad’. 4.4BSD and Linux libc5 only. Don’t use.) This is a
synonym for ll.
j A following integer conversion corresponds to an intmax_t or
uintmax_t argument.
z A following integer conversion corresponds to a size_t or
ssize_t argument. (Linux libc5 has Z with this meaning. Don’t
use it.)
t A following integer conversion corresponds to a ptrdiff_t argu
ment.
The SUSv2 only knows about the length modifiers h (in hd, hi, ho, hx,
hX, hn) and l (in ld, li, lo, lx, lX, ln, lc, ls) and L (in Le, LE, Lf,
Lg, LG).
The conversion specifier
A character that specifies the type of conversion to be applied. The
conversion specifiers and their meanings are:
d,i The int argument is converted to signed decimal notation. The
precision, if any, gives the minimum number of digits that must
appear; if the converted value requires fewer digits, it is
padded on the left with zeros. The default precision is 1.
When 0 is printed with an explicit precision 0, the output is
empty.
o,u,x,X
The unsigned int argument is converted to unsigned octal (o),
unsigned decimal (u), or unsigned hexadecimal (x and X) nota
tion. The letters abcdef are used for x conversions; the let
ters ABCDEF are used for X conversions. The precision, if any,
gives the minimum number of digits that must appear; if the con
verted value requires fewer digits, it is padded on the left
with zeros. The default precision is 1. When 0 is printed with
an explicit precision 0, the output is empty.
e,E The double argument is rounded and converted in the style
[]d.ddde±dd where there is one digit before the decimalpoint
character and the number of digits after it is equal to the pre
cision; if the precision is missing, it is taken as 6; if the
precision is zero, no decimalpoint character appears. An E
conversion uses the letter E (rather than e) to introduce the
exponent. The exponent always contains at least two digits; if
the value is zero, the exponent is 00.
f,F The double argument is rounded and converted to decimal notation
in the style []ddd.ddd, where the number of digits after the
decimalpoint character is equal to the precision specification.
If the precision is missing, it is taken as 6; if the precision
is explicitly zero, no decimalpoint character appears. If a
decimal point appears, at least one digit appears before it.
(The SUSv2 does not know about F and says that character string
representations for infinity and NaN may be made available. The
C99 standard specifies ‘[]inf’ or ‘[]infinity’ for infinity,
and a string starting with ‘nan’ for NaN, in the case of f con
version, and ‘[]INF’ or ‘[]INFINITY’ or ‘NAN*’ in the case of
F conversion.)
g,G The double argument is converted in style f or e (or F or E for
G conversions). The precision specifies the number of signifi
cant digits. If the precision is missing, 6 digits are given;
if the precision is zero, it is treated as 1. Style e is used
if the exponent from its conversion is less than 4 or greater
than or equal to the precision. Trailing zeros are removed from
the fractional part of the result; a decimal point appears only
if it is followed by at least one digit.
a,A (C99; not in SUSv2) For a conversion, the double argument is
converted to hexadecimal notation (using the letters abcdef) in
the style []0xh.hhhhp±d; for A conversion the prefix 0X, the
letters ABCDEF, and the exponent separator P is used. There is
one hexadecimal digit before the decimal point, and the number
of digits after it is equal to the precision. The default pre
cision suffices for an exact representation of the value if an
exact representation in base 2 exists and otherwise is suffi
ciently large to distinguish values of type double. The digit
before the decimal point is unspecified for nonnormalized num
bers, and nonzero but otherwise unspecified for normalized num
bers.
c If no l modifier is present, the int argument is converted to an
unsigned char, and the resulting character is written. If an l
modifier is present, the wint_t (wide character) argument is
converted to a multibyte sequence by a call to the wcrtomb(3)
function, with a conversion state starting in the initial state,
and the resulting multibyte string is written.
s If no l modifier is present: The const char * argument is
expected to be a pointer to an array of character type (pointer
to a string). Characters from the array are written up to (but
not including) a terminating null byte (’\0’); if a precision is
specified, no more than the number specified are written. If a
precision is given, no null byte need be present; if the preci
sion is not specified, or is greater than the size of the array,
the array must contain a terminating null byte.
If an l modifier is present: The const wchar_t * argument is
expected to be a pointer to an array of wide characters. Wide
characters from the array are converted to multibyte characters
(each by a call to the wcrtomb(3) function, with a conversion
state starting in the initial state before the first wide char
acter), up to and including a terminating null wide character.
The resulting multibyte characters are written up to (but not
including) the terminating null byte. If a precision is speci
fied, no more bytes than the number specified are written, but
no partial multibyte characters are written. Note that the pre
cision determines the number of bytes written, not the number of
wide characters or screen positions. The array must contain a
terminating null wide character, unless a precision is given and
it is so small that the number of bytes written exceeds it
before the end of the array is reached.
C (Not in C99, but in SUSv2.) Synonym for lc. Don’t use.
S (Not in C99, but in SUSv2.) Synonym for ls. Don’t use.
p The void * pointer argument is printed in hexadecimal (as if by
%#x or %#lx).
n The number of characters written so far is stored into the inte
ger indicated by the int * (or variant) pointer argument. No
argument is converted.
m (Glibc extension.) Print output of strerror(errno). No argu
ment is required.
% A ‘%’ is written. No argument is converted. The complete con
version specification is ‘%%’.
Page 1 of 1
