DAY4 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

TypeSpecifierExample Output
Signed integer%d26
Unsigned integer%u888899
Octal%o32
Hexadecimal (lowercase)%x1a
Hexadecimal (uppercase)%X1A
Character%c'a'
String%s"hello"
Float%f3.141500
Double%lf700000.000000
Scientific notation%e7.000000e+05
Pointer (address)%p0x7ffee123abcd
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

FormatDescriptionExample
%5dWidth 5, right-aligned 26
%-5dWidth 5, left-aligned26
%05dPadded with zeros00026
%.2fTwo decimal places3.14
%#oShow prefix 0 for octal032
%#xShow prefix 0x for hex0x1a

4. Formatted Input: scanf()

int scanf(const char *format, ...);

1. Important Notes

  1. Use & before variables to pass their addresses.
  2. Do not use & with strings.
  3. Avoid adding \n or 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

EscapeMeaningExample
\nNew lineprintf("Hello\n");
\tTab (4 spaces)printf("\tHi");
\bBackspaceprintf("Y\b=");
\\Backslashprintf("\\");
\rReturn to line startused for dynamic refresh
%%Percent signprintf("100%%");

7. Summary Table

FunctionPurposeReturn Value
putchar()Output one characterASCII value of the printed char
getchar()Input one characterASCII value of the input char
printf()Formatted outputNumber of characters printed
scanf()Formatted inputNumber of successfully read values
puts()Print string with newlineNon-negative on success
gets()Read string (unsafe)Pointer to string
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值