Fundamentals of C Language Programming: Input and Output (printf() and scanf())

In C language, input and output are the most basic operations, so it is essential to master some of the most commonly used rules. We mainly understand the standard library functions that implement input and output functionality defined in the <span><span><stdio.h></span></span> header file.1. Standard Output Function:printf()

1. Function Purpose and Functionality

<span><span>printf()</span></span> is the most commonly used formatted output function in C language, primarily used to output data to the standard output device (usually the screen) in a specified format. It can:

  • Output various types of data (integers, floating-point numbers, characters, strings, etc.)

  • Control the output format (width, precision, alignment, etc.)

  • Combine multiple data items for output

2. Basic Syntax

printf("format control string", output item list);

Common Format Specifiers

Format Specifier Description Example Output Example
%d Decimal integer printf(“%d”, 10) 10
%f Floating-point number printf(“%f”, 3.14) 3.140000
%c Single character printf(“%c”, ‘A’) A
%s String printf(“%s”, “Hello”) Hello
%lf Double precision floating-point number printf(“%lf”, 3.1415926) 3.141593
%x Hexadecimal integer printf(“%x”, 255) ff
%o Octal integer printf(“%o”, 8) 10
%% Output % itself printf(“%%”) &
int num = 123;float pi = 3.14159;
// Width control
printf("%5d\n", num);   // "  123" (right aligned, width 5)
printf("%-5d\n", num);  // "123  " (left aligned, width 5)
// Precision control
printf("%.2f\n", pi);   // "3.14" (keep 2 decimal places)
printf("%5.2f\n", pi);  // " 3.14" (width 5, keep 2 decimal)
// Zero padding
printf("%05d\n", num);  // "00123" (width 5, zero padded)

Output result:Fundamentals of C Language Programming: Input and Output (printf() and scanf())3. Common Mistakes

  • Format specifier does not match the actual data type
int a = 10;printf("%f", a);  // Error: should use %d
  • Forget variable parameters
printf("Value: %d");  // Missing variable parameter

2. Standard Input Function: scanf()

1. Function Purpose and Functionality

<span><span>scanf()</span></span> is a function that reads formatted input from the standard input device (usually the keyboard), with the main functionalities:

  • Read input data according to the specified format

  • Store the read data into specified variables

  • Support reading various data types

2. Basic Syntax

scanf("format control string", variable address list);

Common Format Specifiers

Format Specifier Description Example
%d Decimal integer scanf(“%d”, &num)
%f Floating-point number scanf(“%f”, &fval)
%lf Double precision floating-point number scanf(“%lf”, &dval)
%c Single character scanf(“%c”, &ch)
%s String (no spaces) scanf(“%s”, str)
// Multiple inputs
int a, b;float c;char sr;
scanf("%d%d", &a, &b);  // Input "10 20"
scanf("%f",&c);// Input floating-point
// Specified delimiter
scanf("%d,%d", &a, &b); // Input "10,20"
scanf("%c",&sr);// Input character
// Limit input width
char name[20];scanf("%10s", name);    // Read at most 10 characters

3. Common Mistakes

  • Forget to use the address operator: &
int num;scanf("%d", num);  // Error: should be &num
  • Input type mismatch
float f;scanf("%d", &f);  // Error: should use %f

3. Character Input and Output Functions

1. getchar() and putchar()

Function Characteristics

  • <span>getchar()</span>: Reads one character at a time, returning the character read or EOF

  • <span>putchar()</span>: Outputs one character, returning the character output or EOF

    Basic syntax:

#include &lt;stdio.h&gt;
int main() {    printf("Please enter a character:");    int c = getchar();
    printf("The character you entered is:");    putchar(c);    putchar('\n');
    return 0;}

Leave a Comment