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

3 Key excerpts on "C Printf"

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.
  • Fast and Effective Embedded Systems Design
    eBook - ePub
    • Tim Wilmshurst, Rob Toulson(Authors)
    • 2012(Publication Date)
    • Newnes
      (Publisher)

    ...Text, data, formatting and control formatting can be specified. Only summary information is provided here; a full statement can be found in Reference B.1. Examples below are taken from book chapters. In each case the function appears in the form pc.printf( ), indicating that printf( ) is being used as a member function of a C++ class pc created in the example program. This does not affect the format applied. Simple Text Messages pc.printf("ADC Data Values…\n\r");  \\send an opening text message This prints the text string ‘ADC Data Values…’ to screen, and uses control characters \n and \r to force a new line and carriage return, respectively. Data Messages pc.printf("%1.3f",ADCdata); This prints the value of the float variable ADCdata. A conversion specifier, initiated by the % character, defines the format. Within this, the ‘f’ specifies floating point, and the.3 causes output to three decimal places. pc.printf("%1.3f \n\r",ADCdata);  \\send the data to the terminal As above, but includes \n and \r to force a new line and carriage return. Combination of Text and Data pc.printf("random number is %i\n\r", r_delay); This prints a text message, followed by the value of int variable (indicated by the ‘i’ specifier) r_delay. pc.printf("Time taken was %f seconds\n", t.read()); //print timed value to pc This prints a text message, followed by the return value of function t.read( ), which is of type float. B.10 File Access Operations B.10.1 Overview In C we can open files, read and write data and also scan through files to specific locations, even searching for particular types of data. The commands for input and output operations are all defined by the C stdio library, already mentioned in connection with printf( ). The stdio library uses the concept of streams to manage the data flow. All streams have similar properties, even though the actual application of the data flow may be very varied...

  • The SAGE Encyclopedia of Educational Research, Measurement, and Evaluation

    ...A variant of this common code is presented in Figure 1. The first line begins with code comments placed between /* and */ to denote the beginning and end of a comment, respectively. Text between these two placeholders can span multiple lines. The importance of commenting code cannot be emphasized enough, especially if code is shared with others or is to be revisited at a later date. Figure 1 Example of C programming code The code example continues with an # include preprocessor directive to tell the C compiler that the functions in the standard input/output header file (<stdio.h>) should be made available for the subsequent code to use. The third line begins the declaration of the main function, which returns an integer data type as the output. The void given to the function is not required technically but is shown here to illustrate an assumption that the compiler would otherwise make. The code within the function is indented for readability purposes. This is a very useful practice in programming because C compilers ignore such spacing. Within the curly braces on Line 5 of Figure 1, the printf function is used to send formatted output to the terminal window. The content inside the double quotes gets printed on the screen. Inside of the quotes, any character that immediately follows a backslash (the combination of which is known as an escape sequence) is ignored, so that the compiler can receive additional instructions. The \ n tells the compiler to create a line break. The end of the statement ends with a semicolon, as all expression or declaration statements should. However, preprocessor directives (i.e., #include) and control structures (i.e., loops and conditional logic) do not require a semicolon, as evidenced in the example code. Finally, Line 6 returns the integer zero to the terminal to indicate that the program executed correctly...

  • Programming 8-bit PIC Microcontrollers in C
    eBook - ePub

    Programming 8-bit PIC Microcontrollers in C

    with Interactive Hardware Simulation

    • Martin P. Bates(Author)
    • 2008(Publication Date)
    • Newnes
      (Publisher)

    ...The code %d means display the variable value as an integer decimal number, %c means display the ASCII character corresponding to the number. Multiple values can be inserted in order, as seen in program LCD.C. A summary of formatting codes is shown in Table 2.9. Table 2.8: Essential Control Codes for Serial 2x16 LCD Code Effect 254 Switch to control mode followed by 00 Home to start of row 1 01 Clear screen 192 Go to start of row 2 Listing 2.16 shows the program FLOAT.C, which illustrates how different variable types are displayed, as well as showing the range of each type. Each variable type is output in turn to the display. The general form of the format code is %nt, where n is the number of significant figures to be displayed and t is the output variable type. The number of decimal places printed can also be specified for floating point numbers; for example, %5.3d displays a decimal number with five significant digits and three decimal places. Table 2.9: Output Format Codes Code Displays %d Signed integer %u Unsigned integer %Lu Long unsigned integer (16 or 32 bits) %Ls Long signed integer (16 or 32 bits) %g Rounded decimal float (use decimal formatting) %f Truncated decimal float (use decimal formatting) %e Exponential form of float %w Unsigned integer with decimal point inserted (use decimal formatting) %X Hexadecimal %LX Long hex %c ASCII character corresponding to numerical value %s Character or string Keypad and Calculator A simple calculator application demonstrates the use of the LCD and a keypad, as well as some numerical processing. A matrix keypad provides a simple data entry device for microcontroller systems. The keys are connected in rows and columns, such that pressing a button connects a row to a column. The required connections are shown in Figure 2.12. The rows, labeled A, B, C, and D, are connected as outputs at Port B, avoiding the programming pins...