Lab 2: Formatted and Unformatted Input/output in C
Lab Report Information
Lab No.: 2
Title: Formatted and Unformatted Input/output in C
Course: Computer Programming (CT 101), I Year I Part
Credit: Important Notes
Objective: To become familiar with data types, operators, expressions, and formatted input/output functions.
C Programming Lab Report: Theory
This lab report focuses on variables, operators, expressions, and the core concepts of Formatted and Unformatted Input/output in C.
Datatypes and Operators
C uses various data types (like `int`, `float`, `char`) to define variables. Operators are symbols used to perform operations. These include arithmetic (`+`, `-`), relational (`==`, `>`), logical (`&&`, `||`), and assignment (`=`, `+=`) operators.
Type Casting and Promotion
Type casting is the manual conversion of one data type to another, e.g., `(float)my_integer;`. Type promotion is the automatic conversion done by the compiler in expressions to prevent data loss.
Format Specifiers
Format specifiers (like `%d`, `%f`, `%s`) are used in `printf()` and `scanf()` to define the type and format of data. They can be modified with flags (`-`, `+`, `0`), width specifiers, and precision specifiers (`.2f`).
Lab 2 Programs: Formatted and Unformatted Input/output in C
1. Program to Illustrate the Increment Operator
Question:
WAP to illustrate the increment operator.
Program:
#include <stdio.h>
#include <conio.h>
void main() {
clrscr();
int a = 10, b = 10, x, y;
x = ++a * ++a - ++a;
y = ++b * b++ - b++;
printf("Value of a = %d and value of x = %d\n", a, x);
printf("Value of b = %d and value of y = %d", b, y);
getch();
}
Output:
Value of b = 13 and value of y = 119
2. Program for Seconds to Minutes Conversion
Question:
WAP to input seconds and convert them into minutes.
Program:
#include <stdio.h>
#include <conio.h>
void main() {
clrscr();
int s;
float m;
printf("Enter seconds: ");
scanf("%d", &s);
m = s / 60.0;
printf("\n%d sec is = %.2f minutes", s, m);
getch();
}
Output:
150 sec is = 2.50 minutes
Algorithm:
- Start
- Declare integer `s` and float `m`.
- Read the value for seconds `s`.
- Calculate minutes `m = s / 60.0`.
- Display the result `m`.
- Stop.
Flowchart:
3. Program for Equivalent Resistance
Question:
WAP to compute the equivalent resistance of two resistors R1 and R2 when they are connected in series & parallel connection.
Program:
#include <stdio.h>
#include <conio.h>
void main() {
clrscr();
float r1, r2, series, parallel;
printf("\nEnter resistance of R1: ");
scanf("%f", &r1);
printf("\nEnter resistance of R2: ");
scanf("%f", &r2);
series = r1 + r2;
parallel = (r1 * r2) / (r1 + r2);
printf("\nThe equivalent resistance of %.2f and %.2f\n", r1, r2);
printf("While connecting in series = %.2f\n", series);
printf("While connecting in parallel = %.2f", parallel);
getch();
}
Output:
Enter resistance of R2: 20
The equivalent resistance of 10.00 and 20.00
While connecting in series = 30.00
While connecting in parallel = 6.67
Algorithm:
- Start
- Declare float variables r1, r2, series, parallel.
- Read values for r1 and r2.
- Calculate series = r1 + r2.
- Calculate parallel = (r1 * r2) / (r1 + r2).
- Display series and parallel resistances.
- Stop.
Flowchart:
parallel = (r1*r2)/(r1+r2)
4. Program for Expression Evaluation
Question:
WAP to evaluate the expression v = ((x+y)/(3a+b)) * (p+q) + (x/y)^3.5
Program:
#include <stdio.h>
#include <conio.h>
#include <math.h>
void main() {
clrscr();
float v, x, y, a, b, p, q;
printf("\nEnter x, y, a, b, p, q respectively: ");
scanf("%f %f %f %f %f %f", &x, &y, &a, &b, &p, &q);
v = ((x + y) / (3 * a + b) * (p + q)) + pow((x / y), 3.5);
printf("\nThe result is %f", v);
getch();
}
Output:
The result is 26.313709
Algorithm:
- Start
- Declare float variables v, x, y, a, b, p, q.
- Read values for x, y, a, b, p, q.
- Calculate v using the given formula.
- Display the value of v.
- Stop.
Flowchart:
5. Program for Swapping Values
Question:
WAP to swap the values of two variables x and y.
Program:
#include <stdio.h>
#include <conio.h>
void main() {
clrscr();
int x, y, temp;
printf("Enter values for x and y: ");
scanf("%d %d", &x, &y);
printf("\nBefore Swapping:\n");
printf("x=%d, y=%d\n", x, y);
temp = x;
x = y;
y = temp;
printf("\nAfter Swapping:\n");
printf("x=%d, y=%d", x, y);
getch();
}
Output:
Before Swapping:
x=10, y=20
After Swapping:
x=20, y=10
Algorithm:
- Start
- Declare integer variables x, y, temp.
- Read values for x and y.
- Store value of x in temp.
- Assign value of y to x.
- Assign value of temp to y.
- Display the new values of x and y.
- Stop.
Flowchart:
x = y
y = temp
6. Program for Format Specification Observation
Question:
Run the following programs and observe the output regarding format specification.
i. Integer Formatting
#include <stdio.h>
#include <conio.h>
void main() {
int x = 1234;
printf("Case 1: %d\n", x);
printf("Case 2: %6d\n", x);
printf("Case 3: %2d\n", x);
printf("Case 4: %-6d\n", x);
printf("Case 5: %06d\n", x);
printf("Case 6: %-+6d\n", x);
printf("Case 7: %-2d", x);
getch();
}Output:
Case 2: 1234
Case 3: 1234
Case 4: 1234
Case 5: 001234
Case 6: +1234
Case 7: 1234
ii. Float Formatting
#include <stdio.h>
#include <conio.h>
void main() {
float x = 123.8765;
printf("Case 1: %f\n", x);
printf("Case 2: %e\n", x);
printf("Case 3: %.2f\n", x);
printf("Case 4: %12.4f\n", x);
printf("Case 5: %-12.2f\n", x);
printf("Case 6: %012.2f\n", x);
printf("Case 7: %g\n", x);
printf("Case 8: %.0f", x);
getch();
}Output:
Case 2: 1.238765e+02
Case 3: 123.88
Case 4: 123.8765
Case 5: 123.88
Case 6: 000000123.88
Case 7: 123.877
Case 8: 124
iii. Character Formatting
#include <stdio.h>
#include <conio.h>
void main() {
char ch = 'b';
printf("Case 1: %c\n", ch);
printf("Case 2: %8c\n", ch);
printf("Case 3: %-8c", ch);
getch();
}Output:
Case 2: b
Case 3: b
iv. String Formatting
#include <stdio.h>
#include <conio.h>
void main() {
char str[20] = "My Name is Diwakar.";
printf("Case 1: %s\n", str);
printf("Case 2: %-19s\n", str);
printf("Case 3: %-19.13s\n", str);
printf("Case 4: %19.5s\n", str);
printf("Case 5: %-19.7s\n", str);
printf("Case 6: %-1.7s\n", str);
printf("Case 7: %.9s", str);
getch();
}Output:
Case 2: My Name is Diwakar.
Case 3: My Name is Diw
Case 4: My Nam
Case 5: My Name
Case 6: My Name
Case 7: My Name i
v. Unformatted Input with %[^\n]
#include <stdio.h>
#include <conio.h>
void main() {
char str[50];
printf("Enter a string: ");
scanf("%[^\n]", str);
printf("Read string is: %s", str);
getch();
}Output:
Read string is: my name is diwakar bhandari
vi. Unformatted Input with Space Delimiter
#include <stdio.h>
#include <conio.h>
void main() {
char str[20];
printf("Enter string: ");
scanf("%s", str);
printf("Read String is: %s", str);
getch();
}Output:
Read String is: my
vii. Character Set Input
#include <stdio.h>
#include <conio.h>
void main() {
char str[20];
printf("Enter String: ");
scanf("%[a-z]", str);
printf("Read String is: %s", str);
getch();
}Output:
Read String is: diwakar
Discussion and Conclusion
Through this C Programming Lab Report on Formatted and Unformatted Input/output in C, I learned a lot. In this lab, I explored different ways to use operators, especially the pre-increment and post-increment functions which can yield different results depending on compiler behavior. After researching it, I conclude that the output result of complex increment/decrement expressions is often compiler-dependent and should be used with caution. The exercises on formatted output (`printf`) demonstrated the power of format specifiers, flags, width, and precision for controlling the appearance of data, which is essential for creating clean and readable user interfaces.
