122
Here’s a breakdown of the key differences between the functions you mentioned:
1. printf():
- Purpose: Used to format and print output to the console.
- Syntax:
printf("format string", arguments);
- Examples:
printf("Hello, world!\n");
printf("Age: %d, Name: %s\n", age, name);
2. scanf():
- Purpose: Used to read formatted input from the console.
- Syntax:
scanf("format string", &variables);
- Examples:
scanf("%d", &age);
scanf("%s", name);
3. getchar():
- Purpose: Reads a single character from the console.
- Syntax:
char ch = getchar();
- Example:
ch = getchar();
4. putchar():
- Purpose: Prints a single character to the console.
- Syntax:
putchar(character);
- Example:
putchar('A');
5. char:
- Purpose: A data type used to store a single character.
- Example:
char letter = 'A';
6. string:
- Purpose: Not a built-in type in C, but often refers to a character array (null-terminated sequence of characters).
- Example:
char name[] = "Bard";
Key Differences Summary:
Function | Purpose | Input/Output |
---|---|---|
printf() | Formatted output to console | Output |
scanf() | Formatted input from console | Input |
getchar() | Read a single character | Input |
putchar() | Print a single character | Output |
char | Data type for single character | N/A |
string | Character array (informal term) | N/A |