Surigao Del Norte State University
C COLLEGE OF ENGINEERING & INFORMATION TECHNOLOGY
Narciso Street, Surigao City
Output Formatting
DRILL 3
STUDENT NAME :
SECTION :
DATE OF SUBMISSION: SCORE
Score
Engr. Catherine M. Verallo, MSCpE
Instructor
Topic 1. Output Manipulations
I. Learning Objective
At the end of the session, the student must be able to
• Familiarize the various C output formatting techniques
• Identify the format identifiers suitable for specific data types
• Debug sample programs
• Create and implement programs in C with escape and placeholders.
II. Discussion
Basic Escape Sequences
Escape sequences are special character combinations that allow you to represent characters
that are difficult to type or have a special meaning within a string. In C programming, escape
sequences are used to insert characters that cannot be typed directly into a string. This guide
will cover the common escape sequences in C, their usage, and provide examples to illustrate
their functionality.
Common Escape Sequences
\n: Newline - Moves the cursor to the beginning of the next line.
\t: Tab - Inserts a horizontal tab.
\": Double quote - Inserts a double quote character.
\': Single quote - Inserts a single quote character.
\\: Backslash - Inserts a backslash character.
\b: Backspace - Moves the cursor back one position.
\r: Carriage return - Moves the cursor to the beginning of the current line.
\f: Form feed - Moves the cursor to the next logical page.
Placeholders
When displaying output in C programming, it is common to include variable values within the
output statements. Placeholders, also known as format specifiers, allow you to specify where
and how the values of variables should be inserted into the output. In C, the printf function
provides format specifiers for different data types, allowing you to control the formatting of the
displayed values. This guide will cover the usage of placeholders in output statements and the
format specifiers available in printf for different data types.
Placeholders in Output Statements
Placeholders are used within output statements to indicate the position and format of variable
values. They are specified by using format specifiers, which start with the % symbol, followed by
a letter that represents the data type of the variable. The format specifiers are replaced with the
corresponding values when the output is displayed.
2
Format Specifiers in printf for Different Data Types
printf provides a range of format specifiers to accommodate different data types. Here are
some commonly used format specifiers:
%d or %i: Signed integer
%u: Unsigned integer
%f: Floating-point number
%c: Character
%s: String
%p: Pointer address
%x or %X: Hexadecimal number
III. Drill Exercises
1. Execute the following code write the output in the box provided (5 points):
#include <stdio.h>
Write the Output Here
int main() {
printf("This is a new line.\n");
printf("Hello\tworld!\n");
printf("She said, \"Hello!\"\n");
printf("He said, 'I'm happy.'\n");
printf("This is a backslash: \\ \n");
printf("Hello\bWorld\n");
printf("Carriagereturn:\rOverwritten text\n");
printf("Form feed:\fHello\fWorld\n");
return 0;
}
2. Execute the following code write the output in the box provided (5 points):
#include <stdio.h>
int main() {
int age = 25;
float height = 1.75;
printf("My age is %d and my height is %.2f meters.\n", age, height);
return 0;
}
3. Execute the following code write the output in the box provided (5 points):
Write the Output Here
int main() {
int age = 25;
float height = 1.75;
char grade = 'A';
char name[] = "John";
3
printf("Age: %d\n", age);
printf("Height: %.2f meters\n", height);
printf("Grade: %c\n", grade);
printf("Name: %s\n", name);
return 0;
}
4. Execute the following code write the output in the box provided (5 points):
#include <stdio.h>
int main() {
float value = 3.14159265359;
printf("Default precision: %f\n", value);
printf("Precision of 2 decimal places: %.2f\n", value);
printf("Precision of 4 decimal places: %.4f\n", value);
printf("Precision of 8 decimal places: %.8f\n", value);
return 0;
}
III. Laboratory Exercise.
1. Create a C program that will ask the user to input a decimal number and print this
number with2 decimal places on the next line. (5 points)
Sample Output :
Enter an integer : 1
1.20
2. Create a program that will accept 2 integers and output the sum and the products of 2
integers separated by a space. (5 points)
IV. Review Questions (2 points each)
1. What does \n represent?
______________________________________________________________________
2. What doe \\ represent?
______________________________________________________________________
3. What escape sequence can be used for carriage return?
______________________________________________________________________
4. Which escape sequence can be used to print “hello”
_______________________________________________________________________
5. Which format specifier is used for a floating – point number?
______________________________________________________________________
References
1. CodeChum
2. Coursera.org
3. GeeksforGeeks.com
Give a man a fish, and you’ll feed him for a day. Teach a man to fish, and you’ll feed him for a
lifetime
4