Answer :

Answer:

To read the given data using `scanf()` functions in C, you need to specify the appropriate format specifiers that match the data types of the input. Here's how you can write the `scanf()` functions for each case:

1. **34 C 123**

- Integer

- Character

- Integer

```c

------------------------------------------------

int num1, num2;

char ch;

scanf("%d %c %d", &num1, &ch, &num2);

------------------------------------------------

```

2. **1.234 D 455**

- Floating-point number

- Character

- Integer

```c

------------------------------------------------

float num1;

int num2;

char ch;

scanf("%f %c %d", &num1, &ch, &num2);

------------------------------------------------

```

3. **A C 23**

- Character

- Character

- Integer

```c

------------------------------------------------

char ch1, ch2;

int num;

scanf(" %c %c %d", &ch1, &ch2, &num);

------------------------------------------------

```

4. **. 03 8.345**

- Character (for the dot)

- Integer

- Floating-point number

```c

------------------------------------------------

char dot;

int num;

float num2;

scanf(" %c %d %f", &dot, &num, &num2);

------------------------------------------------

```

** Explanation**:

- `%d`: Format specifier for integers.

- `%c`: Format specifier for characters. Note the space before `%c` in some cases to skip any whitespace characters.

- `%f`: Format specifier for floating-point numbers.

- `" %c"`: The space before `%c` ensures that any leading whitespace is skipped, which can be useful to correctly read characters following other data types.

**Example Usage**:

Here's a complete example that uses all of the `scanf()` functions:

```c

------------------------------------------------

#include <stdio.h>

int main() {

int num1, num2;

float fnum;

char ch, ch1, ch2, dot;

// For input "34 C 123"

printf("Enter an integer, a character, and an integer (e.g., 34 C 123): ");

scanf("%d %c %d", &num1, &ch, &num2);

printf("You entered: %d %c %d\n", num1, ch, num2)

// For input "1.234 D 455"

printf("Enter a float, a character, and an integer (e.g., 1.234 D 455): ");

scanf("%f %c %d", &fnum, &ch, &num2);

printf("You entered: %.3f %c %d\n", fnum, ch, num2);

// For input "A C 23"

printf("Enter two characters and an integer (e.g., A C 23): ");

scanf(" %c %c %d", &ch1, &ch2, &num1);

printf("You entered: %c %c %d\n", ch1, ch2, num1);

// For input ". 03 8.345"

printf("Enter a character, an integer, and a float (e.g., . 03 8.345): ");

scanf(" %c %d %f", &dot, &num1, &fnum);

printf("You entered: %c %d %.3f\n", dot, num1, fnum);

return 0;

}

------------------------------------------------

```

This example demonstrates how to read various types of input and ensures that the correct data types and format specifiers are used.

Other Questions