format specifiers in C programming

Humble Devassy Chirammal Forums C-programming.. format specifiers in C programming

Viewing 1 post (of 1 total)
  • Author
    Posts
  • #2183 Reply
    Humble
    Keymaster

    We should know .. Representation ( ex:printf()) or input formatting (ex:scanf()) can make use of format specifiers or they should. Using correct format specifier for a data type is a must thing to do. So, better to know about it:

    From wikipedia:
    http://en.wikipedia.org/wiki/Scanf_format_string#Format_string_specifications

    The formatting placeholders in scanf are more or less the same as that in printf, its reverse function.

    There are rarely constants (i.e. characters that are not formatting placeholders) in a format string, mainly because a program is usually not designed to read known data. The exception is one or more whitespace characters, which discards all whitespace characters in the input.

    Some of the most commonly used placeholders follow:

    %d : Scan an integer as a signed decimal number.
    %i : Scan an integer as a signed number. Similar to %d, but interprets the number as hexadecimal when preceded by 0x and octal when preceded by 0. For example, the string 031 would be read as 31 using %d, and 25 using %i. The flag h in %hi indicates conversion to a short and hh conversion to a char.
    %u : Scan for decimal unsigned int (Note that in the C99 standard the input value minus sign is optional, so if a negative[clarification needed] number is read, no errors will arise and the result will be the two’s complement, effectively ignoring the negative sign in most cases. See strtoul().[not in citation given]) Correspondingly, %hu scans for an unsigned short and %hhu for an unsigned char.
    %f : Scan a floating-point number in normal (fixed-point) notation.
    %g, %G : Scan a floating-point number in either normal or exponential notation. %g uses lower-case letters and %G uses upper-case.
    %x, %X : Scan an integer as an unsigned hexadecimal number.
    %o : Scan an integer as an octal number.
    %s : Scan a character string. The scan terminates at whitespace. A null character is stored at the end of the string, which means that the buffer supplied must be at least one character longer than the specified input length.
    %c : Scan a character (char). No null character is added.
    whitespace: Any whitespace characters trigger a scan for zero or more whitespace characters. The number and type of whitespace characters do not need to match in either direction.
    %lf : Scan as a double floating-point number.
    %Lf : Scan as a long double floating-point number.
    *****

Viewing 1 post (of 1 total)
Reply To: format specifiers in C programming
Your information: