knowt logo

Intro to Programming: HANDLING STANDARD INPUT AND OUTPUT

HANDLING STANDARD INPUT AND OUTPUT

Objectives:

  • Learn about standard input and output functions in C, including:

    • printf()

    • getc()

    • putc()

    • getchar()

    • putchar()

UNDERSTANDING STANDARD I/O

  • In C, files are treated as a series of bytes or a stream.

  • All file streams are treated equally, whether they come from a disk, terminal, or other sources.

  • Three pre-opened file streams are available:

    • stdin for standard input

    • stdout for standard output

    • stderr for standard error

Using the getc() Function

  • getc() reads the next character from a file stream and returns it as an integer.

  • Syntax: int getc(FILE *stream);

  • Example:

    int ch;
    ch = getc(stdin);

Using the getchar() Function

  • getchar() is equivalent to getc(stdin).

  • Syntax: int getchar(void);

  • Example:

    int ch;
    ch = getchar();

Using the putc() Function

  • putc() writes a character to a file stream, such as stdout.

  • Syntax: int putc(int c, FILE *stream);

Example: Outputting a character with putc():

int ch;
ch = 65; // Numeric value of 'A'
putc(ch, stdout);

Using the putchar() Function

  • putchar() is similar to putc() but uses stdout as the default file stream.

  • Syntax: int putchar(int c);

  • Example: Outputting characters with putchar():

    putchar(65);
    putchar(10); // Newline

The printf() Function

  • printf() is used for formatted output in C.

  • Syntax: int printf(const char *format, ...);

  • Format specifiers include %c, %d, %i, %f, %e, %E, %g, %G, %o, %s, %u, %x, %X, %p, %n, %%, and more.

  • You can use format specifiers to control the output format.

    Example: Converting to hex numbers:

    printf("%X %x %d\n", 10, 10, 10);

Specifying the Minimum Field Width

  • You can specify the minimum field width in printf() using %Xd where X is the width.

  • Example:

    printf("%5d\n", 10);
    printf("%05d\n", 10); // Padded with zeros

LEFT- OR RIGHT-JUSTIFIED OUTPUT

  • You can align output using format specifiers:

    • %Xd for right-justified

    • %-Xd for left-justified

  • Example:

    printf("%8d %-8d\n", num1, num1);

USING PRECISION SPECIFIERS

  • Precision specifiers can be used to control the format of output.

  • Example:

    printf("%2.8d\n", 123);
    printf("%-10.2f\n", 123.45);

SUMMARY

  • C treats files as streams of bytes.

  • stdin, stdout, and stderr are pre-opened file streams.

  • Functions like getc(), getchar(), putc(), putchar(), and printf() are used for I/O.

  • Format specifiers control output format, including width and alignment.

  • %x or %X can be used to convert decimal numbers to hex numbers.

K

Intro to Programming: HANDLING STANDARD INPUT AND OUTPUT

HANDLING STANDARD INPUT AND OUTPUT

Objectives:

  • Learn about standard input and output functions in C, including:

    • printf()

    • getc()

    • putc()

    • getchar()

    • putchar()

UNDERSTANDING STANDARD I/O

  • In C, files are treated as a series of bytes or a stream.

  • All file streams are treated equally, whether they come from a disk, terminal, or other sources.

  • Three pre-opened file streams are available:

    • stdin for standard input

    • stdout for standard output

    • stderr for standard error

Using the getc() Function

  • getc() reads the next character from a file stream and returns it as an integer.

  • Syntax: int getc(FILE *stream);

  • Example:

    int ch;
    ch = getc(stdin);

Using the getchar() Function

  • getchar() is equivalent to getc(stdin).

  • Syntax: int getchar(void);

  • Example:

    int ch;
    ch = getchar();

Using the putc() Function

  • putc() writes a character to a file stream, such as stdout.

  • Syntax: int putc(int c, FILE *stream);

Example: Outputting a character with putc():

int ch;
ch = 65; // Numeric value of 'A'
putc(ch, stdout);

Using the putchar() Function

  • putchar() is similar to putc() but uses stdout as the default file stream.

  • Syntax: int putchar(int c);

  • Example: Outputting characters with putchar():

    putchar(65);
    putchar(10); // Newline

The printf() Function

  • printf() is used for formatted output in C.

  • Syntax: int printf(const char *format, ...);

  • Format specifiers include %c, %d, %i, %f, %e, %E, %g, %G, %o, %s, %u, %x, %X, %p, %n, %%, and more.

  • You can use format specifiers to control the output format.

    Example: Converting to hex numbers:

    printf("%X %x %d\n", 10, 10, 10);

Specifying the Minimum Field Width

  • You can specify the minimum field width in printf() using %Xd where X is the width.

  • Example:

    printf("%5d\n", 10);
    printf("%05d\n", 10); // Padded with zeros

LEFT- OR RIGHT-JUSTIFIED OUTPUT

  • You can align output using format specifiers:

    • %Xd for right-justified

    • %-Xd for left-justified

  • Example:

    printf("%8d %-8d\n", num1, num1);

USING PRECISION SPECIFIERS

  • Precision specifiers can be used to control the format of output.

  • Example:

    printf("%2.8d\n", 123);
    printf("%-10.2f\n", 123.45);

SUMMARY

  • C treats files as streams of bytes.

  • stdin, stdout, and stderr are pre-opened file streams.

  • Functions like getc(), getchar(), putc(), putchar(), and printf() are used for I/O.

  • Format specifiers control output format, including width and alignment.

  • %x or %X can be used to convert decimal numbers to hex numbers.