Computer Science

C Printf

C printf is a function in the C programming language used to print formatted output. It allows for the insertion of variables and control over the formatting of the output, such as specifying the number of decimal places for floating-point numbers or padding strings with spaces. The printf function is a fundamental tool for displaying information in C programs.

Written by Perlego with AI-assistance

4 Key excerpts on "C Printf"

  • C Programming For Dummies
    • Dan Gookin(Author)
    • 2020(Publication Date)
    • For Dummies
      (Publisher)
    Employing escape sequences .”

    Introducing the printf() function

    The printf() function streams a formatted string of text to the standard output device. The official format is a bit overwhelming:
    #include <stdio.h> int printf(const char *restrict format, …);
    Don’t let your eyeballs pop out of your head. Instead, consider my abbreviated format, which pretty much describes how printf() is used in this chapter:
    printf("text ");
    In this definition, text is a string of text wedged between double quotes.
    The printf() function requires the inclusion of the stdio.h header file.
    The name printf() means print f ormatted, and the function really shows its horsepower in displaying formatted output. You can see this feature demonstrated in Chapter 13 . The print part of the name hails back to the days when C programs sent their output primarily to printers, not to video displays.

    Understanding the newline

    Unlike the puts() function, the printf() function doesn't tack a newline character at the end of its output. A newline is the character that ends a line of text and directs the terminal to display any following text on the next line — the “new” line.
    The following puts() function outputs the text Goodbye, cruel world on a line by itself:
    puts("Goodbye, cruel world");
    The following printf() function displays the text Goodbye, cruel world :
    printf("Goodbye, cruel world");
    After displaying the text, the cursor waits at the space after the d in world . Any additional text that's displayed appears on the same line, which is what you see for the output of Exercise 4-12:
    Hickory, dickory, dock,The mouse ran up the clock.The clockstruck one,The mouse ran down,Hickory, dickory, dock.
    The code does exactly what you programmed it to do, albeit without fully knowing how printf()
  • Assembly Language Step-by-Step
    eBook - ePub

    Assembly Language Step-by-Step

    Programming with Linux

    • Jeff Duntemann(Author)
    • 2011(Publication Date)
    • Wiley
      (Publisher)
    With puts() we can only send a simple text string to a file (by default, stdout), without any sort of formatting. Worse, puts() always includes an EOL character at the end of its display, whether we include one in the string data or not. This prevents us from using multiple calls to puts() to output several text strings all on the same line. About the best you can say for puts() is that it has the virtue of simplicity. For nearly all character output needs, you're way better off using a much more powerful library function called printf(). The printf() function enables us to do a number of truly useful things, all with one function call: Output text either with or without a terminating EOL Convert numeric data to text in numerous formats, by outputting formatting codes along with the data Output text to a file that includes multiple strings stored separately If you've worked with C for more than half an hour, printf() will be perfectly obvious to you, but for people coming from other languages (such as Pascal, which has no direct equivalent), it may take a little explaining. The printf() routine will gladly display a simple string like “Eat at Joe's!”—but we can merge other text strings and converted numeric data with that base string as it travels toward standard output, and show it all seamlessly together. This is done by dropping formatting codes into the base string, and then passing a data item to printf() for each of those formatting codes, along with the base string. A formatting code begins with a percent sign and includes information relating to the type and size of the data item being merged with the base string, as well as how that information should be presented. Let's look at a very simple example to start out
  • Scientific Programming: C-language, Algorithms And Models In Science
    eBook - ePub
    • Luciano Maria Barone, Enzo Marinari, Giovanni Organtini, Federico Ricci Tersenghi(Authors)
    • 2013(Publication Date)
    • WSPC
      (Publisher)
    On line 5, the string is Value in degrees Fahrenheit =, and this string is printed on the output device (for instance the terminal). The aim is to be able to write informative messages that indicate what the program is computing, or what operations it is performing and similar information. On line 8 the syntax of the string included between the parentheses () is slightly different: besides the message to be printed, it also includes a term %f. This term is called a format specifier as it serves to specify the format of the variable which follows the message. In this case the format specifier %f informs the compiler that the variable tc is of the double type, such that it can be correctly printed out onto the output. The printf function admits a variable number of arguments, separated from each other by a comma,. Its generic format is The character string is always enclosed between double quotes " ···", and, in case it is followed by one or more expressions which need to be printed out, it contains a format specifier for each expression which fol-lows: the format specifier has the form %characters and reflects the type of the expression it corresponds to. The expressions exp1, exp2,... are optional and their number may vary. We use the term expressions as exp1 could be either a simple variable or a complex expression since we could have written printf("Value in degrees Celsius = %f",(tf - offset) * conv); i.e., we could have computed the value of tc directly in the arguments of printf. The format specifier %f refers to a quantity which is represented in floating-point format. In Table 3.7 the most frequently used format specifiers are listed for printf. Table 3.7 Output format specifiers. The scanf function, on line 6 of Listing 3.4, serves to read the value of one or more input variables (for instance from the keyboard). Its format is similar to that of the printf function. However, there are some important differences between the two
  • Learning AWK Programming
    required for it to function. For example, if want to put a percentage symbol anywhere with the printf statement, we can put it as follows: $ awk 'BEGIN { printf "Percentage = 21.33 %%\n"}'
    The output of the preceding code is:
    Percentage = 21.33 % The following table summarizes the use of different control characters in format specifiers:
    Character Description
    %c Prints the ASCII character.
    %d Prints the decimal integer.
    %i Prints the decimal integer (Added in POSIX).
    %e Prints a number in exponential floating-point (scientific) notation format.
    %f Prints a number in fixed floating-point format.
    %g Prints a number in either scientific notation or floating-point notation, whichever is shorter, with trailing zeros removed.
    %o Prints an unsigned octal value.
    %u Prints an unsigned decimal integer.
    %s
    Prints a s tring.
    %x
    Prints an unsigned hexadecimal number. Uses a-f for 10 to 15.
    %X
    Prints an unsigned hexadecimal number. Uses A-F for 10 to 15.
    %% Prints a single percentage symbol (%).
    Passage contains an image

    Format specification modifiers

    Each format specification begins with a % and ends with a character that determines the conversion, known as format control letter. In between, it may contain optional modifiers that control how much of the item's value is printed or how much of total space it gets.
    The following are the possible modifiers that may appear in a printf format specifier.
    Passage contains an image

    Printing with fixed column width

    To create a fixed-column-width report, we have to specify a number immediately after the % in the format specifier. This number shows the minimum number of characters to be printed.
Index pages curate the most relevant extracts from our library of academic textbooks. They’ve been created using an in-house natural language model (NLM), each adding context and meaning to key research topics.