C Language Input and Output Functions
1. What Are Input and Output?
In C language, input and output are defined from the computer’s perspective:
- Input → Data sent to the computer.
- Output → Data displayed from the computer to the screen or a file.
To use input and output functions, include the standard header file:
#include <stdio.h>
2. Character Input and Output Functions
1. putchar()
Used to print a single character to the terminal.
int putchar(int c);
- Parameter: The character to be printed.
- Return value: The ASCII value of the printed character.
Example:
#include <stdio.h>
int main()
{
char c = 'c';
putchar('a');
putchar('a' + 1); // prints 'b'
putchar(c); // prints 'c'
return 0;
}
2. getchar()
Reads one character from the keyboard (blocking input).
int getchar(void);
- Return value: The ASCII value of the character entered.
Example:
#include <stdio.h>
int main()
{
char c = 0;
c = getchar(); // waits for user input
putchar(c); // prints the same character
}
Blocking input means the program waits for you to type and press Enter before continuing.
3. Formatted Output: printf()
1. Basic Syntax
int printf(const char *format, ...);
format: Format control string....: One or more variables to display.
2. Common Format Specifiers
| Type | Specifier | Example Output |
|---|---|---|
| Signed integer | %d | 26 |
| Unsigned integer | %u | 888899 |
| Octal | %o | 32 |
| Hexadecimal (lowercase) | %x | 1a |
| Hexadecimal (uppercase) | %X | 1A |
| Character | %c | 'a' |
| String | %s | "hello" |
| Float | %f | 3.141500 |
| Double | %lf | 700000.000000 |
| Scientific notation | %e | 7.000000e+05 |
| Pointer (address) | %p | 0x7ffee123abcd |
| Percent sign | %% | % |
3. Example: 04printf.c
#include <stdio.h>
#include <unistd.h>
int main()
{
int num = 26;
printf("Signed int: %d\n", num);
printf("Octal: %o\n", num);
printf("Hexadecimal: %x\n", num);
char c = 'a';
printf("Character: %c, ASCII: %d\n", c, c);
unsigned int b = 888899;
printf("Unsigned int: %u\n", b);
float f = 3.1415;
printf("Float: %f\n", f);
double d = 7e5;
printf("Double: %lf (scientific: %e)\n", d, d);
long l = 8899712;
printf("Long: %ld\n", l);
printf("String example: %s\n", "Zhang San");
printf("Memory address: %p\n", &num);
printf("100%% Complete!\n");
}
4. Extended Format Options
| Format | Description | Example |
|---|---|---|
%5d | Width 5, right-aligned | 26 |
%-5d | Width 5, left-aligned | 26 |
%05d | Padded with zeros | 00026 |
%.2f | Two decimal places | 3.14 |
%#o | Show prefix 0 for octal | 032 |
%#x | Show prefix 0x for hex | 0x1a |
4. Formatted Input: scanf()
int scanf(const char *format, ...);
1. Important Notes
- Use
&before variables to pass their addresses. - Do not use
&with strings. - Avoid adding
\nor extra characters in the format string.
2. Example
#include <stdio.h>
int main()
{
int num = 0;
printf("Please enter an integer: ");
scanf("%d", &num);
printf("You entered: %d\n", num);
return 0;
}
3. Practice Example (from 06scan_lianxi.c)
int num1 = 0, num2 = 0;
printf("Enter two integers: ");
scanf("%d %d", &num1, &num2);
printf("Sum: %d\n", num1 + num2);
5. String Input and Output
1. gets() ( Not recommended)
Reads one line of text from input — unsafe because it can overflow memory.
char str[50];
gets(str);
printf("You entered: %s\n", str);
Use fgets() instead:
fgets(str, 50, stdin);
2. puts()
Prints a string and automatically adds a newline.
#include <stdio.h>
int main()
{
char str[50] = "Hello, Zhangsan!";
puts("Hi!");
puts(str);
}
Output:
Hi!
Hello, Zhangsan!
6. Common Escape Sequences in printf
| Escape | Meaning | Example |
|---|---|---|
\n | New line | printf("Hello\n"); |
\t | Tab (4 spaces) | printf("\tHi"); |
\b | Backspace | printf("Y\b="); |
\\ | Backslash | printf("\\"); |
\r | Return to line start | used for dynamic refresh |
%% | Percent sign | printf("100%%"); |
7. Summary Table
| Function | Purpose | Return Value |
|---|---|---|
putchar() | Output one character | ASCII value of the printed char |
getchar() | Input one character | ASCII value of the input char |
printf() | Formatted output | Number of characters printed |
scanf() | Formatted input | Number of successfully read values |
puts() | Print string with newline | Non-negative on success |
gets() | Read string (unsafe) | Pointer to string |
8591

被折叠的 条评论
为什么被折叠?



