90 likes | 250 Vues
Chapter 4 focuses on the essential aspects of data input and output in C programming, specifically single-character handling. It covers how to read a single character using `getchar()` and display it with `putchar()`. The chapter also explains the `scanf` function for inputting different data types, including integers and floating-point numbers, with specific format control strings. Additionally, it introduces functions like `printf` for formatted output and demonstrates reading full lines using `gets()` and writing them using `puts()`, culminating in a practical example involving calculating exam scores.
E N D
Chapter 4: Data Input Output Dr. Ameer Ali
Single Character Input/Output • Single character can be entered • char c; c=getchar(); • Single character output • putchar(c);
Entering Input Data • scanf(control string, arg1, arg2,…,argn); • scanf(“%d”, &variable);
More About scanf • scanf(“%3d %3d %d”, &a, &b, &c); • scan(“%3hd %7lx %15lf”, &ix, &lx, &dx);
Writing Output Data • printf(control string, arg1, arg2, …, argn); • printf(“%s %d %f”, item, partno, cost); • printf(“%12s %.3d %.5f”, item, partno, cost);
gets and puts • char line[80]; • gets(line); • puts(line);
Example • Calculating exam scores • #include <stdio.h> main() { char name[20]; float score1, score2, score3, score4; printf(“enter your name”); scanf(“%s”, name); printf(“enter your first score”); scanf(“%f”, &score1); printf(“enter your second score”); scanf(“%f”, &score2); getch(); }