1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

Format Strings in C

Discussion in 'C' started by lionaneesh, Jun 15, 2011.

  1. lionaneesh

    lionaneesh Active Member

    Mar 21, 2010
    Likes Received:
    Trophy Points:

    What are format strings

    Format strings are strings that are used to render and arbitrary number of varied datatype parameters into a string when given as a input to string formatting functions like printf, sprint etc.

    Understanding 'printf'

    'printf' is a standard format string function in C , Its used to print formatted strings on the screen


    int printf(const char *format, ...)
    The string constant ‘format’ provides a description of the output with placeholders , marked with ‘%’ escape characters , that specifies the type of output the function will produce!

    This functions simply inputs n number of terms , Pushes them onto the stack and them pops them off one by one.
    This function can be very Dangerous if not used in a proper way Check This

    Format Placeholders

    Format placeholders are basically the special characters that can be used to format the output or to display integers, strings, floats etc.

    Some of the ‘type format’ Placeholders:-

    %d for Outputting Integers
    %s for Outputting strings
    %f for Outputting floats
    %x for Outputting hexadecimals
    %ld for Outputting long integers

    Some of the special Escaped Characters:-

    \n for newline
    \t for tab
    \r for carriage return

    Actually there are many of format placeholders supported by format string functions in C , But for the scope of this tutorial we’ll only be concentrating on few of them.


    int main()
        int integer = 22;
        float number = 12.224455;
        printf("Hello\nI am in a new line now\n");
    Explanation :-
    1. At the starting few lines we declare few variables
    2. Then we simply print out a string including some newlines(\n).
    3. In the second printf statement we used %d format placeholders to print an integer.
    4. In the third printf statement we used %f format placeholders to print a float.
    5. In the second printf statement we used %x format placeholders to print the hexadecimal form of ‘22’ i.e ‘16’

Share This Page