Computer Science

Formatted Output in C

Formatted output in C refers to the ability to control the way data is displayed on the screen or in a file. This is achieved through the use of format specifiers, which are special characters that tell the program how to format the data. The printf() function is commonly used for formatted output in C.

Written by Perlego with AI-assistance

4 Key excerpts on "Formatted Output in C"

  • Book cover image for: Basic Data Structures and Program Statements
    • Xingni Zhou, Qiguang Miao, Lei Feng(Authors)
    • 2020(Publication Date)
    • De Gruyter
      (Publisher)
    4.2.3 Formatted output function 4.2.3.1 Syntax and signature of formatted output function Let us look at a formatted output function that can output efficiently: the printf function. There are several parameters inside parentheses. Its function signature and functionality are given in Figure 4.11. We shall cover the usage of printf through examples and introduce its rules after that. Signature printf (format control sequence, parameter 1,…,parameter n) Functionality Output values of parameter 1 to parameter n to standard output devices in format specified by “format control sequence”. Parameters are expressions. Formatted output function Figure 4.11: Formatted output function. 146 4 Input/output Example 4.4 Formatted output example: format coordination Variable definitions and output cases are shown in Figure 4.12. Suppose int a=12, b=56; float x=10.8; Output case 2 printf(“%d %d”, a, b ); Format control sequence Argument Output %d□%d a,b 12□56 Output case 1 printf(“%d ”, a); Format control sequence Argument Output %d a 12 We use squares to represent spaces for clearer demonstration Output case 4 printf(%d+%d =%dn, a, b, a+b ); Format control sequence Argument Output %d+%d =%d n a, b, a+b 12+56=68 Output case 3 printf(“%d %f”, a, x); Format control sequence Argument Output %d□□%f a,x 12□□10.8 Rule n is an escape character that represents newline. It is output as defined in grammar. (replaced with arguments) and escape characters (output as defined in grammar) specifiers and arguments Figure 4.12: Formatted output example: format coordination. [Analysis] In output case 1, we print the value of an integer variable a . The format control sequence is the content wrapped by double quotation marks, where %d means that the data will be output as integers. The argument of this function is a , so 12 is output by printf . In case 2, we print values of a and b , which are both integer variables.
  • Book cover image for: Introduction to Embedded Systems
    eBook - PDF

    Introduction to Embedded Systems

    Using ANSI C and the Arduino Development Environment

    • David Russell(Author)
    • 2022(Publication Date)
    • Springer
      (Publisher)
    2.2, main() has only one statement, a function call to printf() with a single argument, the address of a string of bytes. The function printf() was not written by us; it is a library function that is defined in the header file stdio.h, and ultimately linked in by the linker. Hence, there must be an object library or object file somewhere that contains the object code for printf(). 26 2. ANSI C 2.3 FORMATTED OUTPUT Often in embedded environments, we have to rely completely on printf() in order to look at variable values, which is a fundamental component in the debugging process. The fprintf() function provides formatted output conversion. Example 2.4 int int int fprintf (FILE *stream , const const const char char char *format , ...) This function converts and writes output to stream under the control of format. The return value is the number of characters written, or negative if an error occurred. The format string contains two types of objects. 1. Ordinary characters, which are copied to the output stream, and 2. conversion specifications, each of which causes conversion and printing of the next successive argument to fprintf(). Each conversion specification begins with the character % and ends with a conversion character. Between the % and the conversion character there may be, in the following order : • Flags (in any order), which modify the specification, are listed in Table 2.1. Table 2.1: Formatted Printing Conversion Specification Flags Flag Modification - specifies left adjustment of the converted argument in its field + specifies that the number will always be printed with a sign space if the first character is not a sign, a space will be prefixed 0 for numeric conversion, specifies padding to the field width with leading zeros # specifies an alternative output form (e.g., for x, 0x will be prefixed to a non-zero result) • A number specifying a minimum field width. The converted argument will be printed in a field at least this wide, and wider if necessary.
  • Book cover image for: Learn C Programming
    eBook - ePub

    Learn C Programming

    A beginner's guide to learning C programming the easy and disciplined way

    Exploring Formatted Input
    Using console command-line arguments to get string input into our running programs is often handy, but not very useful if we want to read lots of values of any data type while our program is running. To do that, we need to explore how to use formatted input. Formatted input is the opposite end of the pipe, so to speak, to formatted output. Just like we can use formatted output with print(), various value formats can be easily read to the console by using scanf(). Both of these functions do the heavy lifting of converting values into desired output strings or, conversely, converting input character strings into desired values.
    To understand input formatting, we will need to first understand the concept of streams. We will then use the technique of experimentation, or more specifically, trial and observation , to discover and verify our understanding of how C input streams operate. We will later expand the concept of streams to include filesand file processing in Chapter 22 , Working with Files , and Chapter 23 , Using File Input and File Output
  • Book cover image for: C Programming
    No longer available |Learn more

    C Programming

    A Self-Teaching Introduction

    It is a formatted output function. Its syntax is as follows: Syntax printf(“format string”, arguments); For example, printf(“My name is Dr. Rajiv”); /* double quotes are used */ For another example, printf (“%d”, a); Also note that here the value of ‘a’ is inserted at the ‘%d’ position and thus the value of ‘a’ gets printed. 2.4 FUNCTION CALLS FROM A LIBRARY Functions like getchar( ), putchar( ), printf( ), and scanf( ) are defined in the stdio.h file. Functions like strlen( ), strcpy( ), strcat( ), strcmp( ), and strrev( ) are defined in a string.h header file. A function like exit(0) is defined in a process.h file. Similarly, functions like sin( ), cos( ), tan( ), and so on are included in a math.h header file. Please note that all these functions that are included in one or the other header files are built into the C library. This means that the C compiler will automatically execute their code and give the desired result. Also note that we cannot modify these inbuilt functions. If we want to modify any of these functions, we have to write our own functions, known as user-defined functions. With every C compiler a large set of useful string handling library functions is provided. These functions are predefined in the compiler of the language and stored in a special library file. 2.5 DATA TYPES IN C C has a concept of data types that are used to define a variable before its use. The definition of a variable will assign storage for the variable and define the type of data that will be held in the location. The value of a variable can be changed any time. In C, the following data types are given: PROGRAMMING USING C • 45 (a) int (b) float (c) double (d) char Please note that there is not a Boolean data type. C does not have the traditional view of logical comparison.
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.