Sunteți pe pagina 1din 24

Input and Output in C

By Shiela Marie B. Pelayo

You have seen the programming environment of C in our last meeting. I am very sure your questions are: How can information be displayed on the monitor? How can information or values be manipulated in Turbo C++? Lets first see if you get the correct vibes about the program that you have seen in our last meeting by answering the anticipation guide in the next slide.

Directions: The class shall have 3 members per group. The group shall answer the anticipation guide. On the space in front of each of the numbers, place an X" to indicates where you stand in regard to the statement that follows: Agree _____ Disagree ______ 1. printf() and scanf() use format conversion specifiers _____ ______ 2.scanf() is used to scan text or picture ______ ______ 3. printf() displays text or information on the monitor ______ ______4. C language has print and input statements ______ ______5. C language has printf and scanf functions

Be ready to share your answers to the class.

Note: Do not panic when you see error messages. Follow the directions and answer the questions after the program. I designed the activities that way to emphasize important points.
How to debug errors: Error#1: Unable to create file noname00.cpp

-Save the program properly in the labserve folder. Dont forget to change directory.
Error#2: Statement missing ; -Check all the C statements. Put a semi-colon at the end of the line/s. Error#3: Declaration syntax error -Put a semi-colon at the end of the variable declaration statement

You have different answers in the anticipation guide. Lets now find out which answers are correct by doing the hands-on activities.

The PrintF Activity


1. Make the subfolder named PRINTF in the 3rd term folder. 2. Click Start button and select Turbo C++ IDE 3.After viewing TurboC++ window, click File menu and select New. 4.Type the code below in the coding area #include<stdio.h> #include<conio.h> void main() { print("Welcome to C programming."); getch(); } 5. Click Compile Menu and select Compile or simply press Alt+F9.

Questions: Did you see errors on the monitor? __________________

How are you going to debug the code?________________ (Tip: Check the line in blue color. Edit it.)
What is the output? _____________________________

5. View the output by pressing Ctrl+F9 or select the Run command in the Run menu
6. Save the file as print.cpp in the PrintF subfolder. 7. Make an exe file. Press F9.

The ScanF Activity


1. Make the subfolder named SCANF in the 3rd term folder. 2. Type the code below in the coding area #include<stdio.h> #include<conio.h> void main() { char *fname; printf("Please enter your first name:"); scan("%s",fname); printf("Welcome to C++ %s", fname); getch(); } 3. Click Compile Menu and select Compile or simply press Alt+F9.

Questions: Did you see errors on the monitor? __________________ How are you going to debug the code?________________ (Tip: Check the line in blue color. Edit it.) What is the output? _____________________________ 4. View the output by pressing Ctrl+F9 or select the Run command in Run menu.

5. Save the file as scan.cpp in the SCANF subfolder


6. Make an exe file. Press F9. Are things getting clearer now? Do you have an idea now on what statement you are going to use to get and display information on the monitor? To give you more ideas on how these things are done, please go to the next slide.

You have seen simple examples of C programs and their output. You might be wondering now how strings are displayed on the monitor and how computer get information from the user in C. To satisfy your curiosity, it is a must to study first the basic functions that are used in displaying and getting information from the user. 1. Please open the link http://www.geocities.com/learnprogramming123/Clesson4Beginner.htm 2. Check/read the function of printf(), scanf(), and be familiar with the conversion specifiers.

3. Answer the questions in the next slide. Type your answers on the space provided for each number.

3.1 What is the function of printf()?

3.2 What is the function of scanf()?


3.3 What are the different conversion specifiers? Be ready to share your answers to the class. Aside from printf(), scanf(), and conversion specifiers, you have to know also the different relational and logical operators, and the types of identifiers in order to make a better C program. Lets check them in the next slide.

Cs data types
Variable Type
Character Integer

Keyword char int

Range
-128 to 127 -32768 to 32767

Long Integer

long

-2,147,483,648 to 2,147,483,647
1.2E-38 to 3.4E381 2.2E-308 to 1.8E3082

Single-precision floating pt Double-precision floating pt

float double

Arithmetic Operators
Operator + Action Addition

* / %

Subtraction
Multiplication Division Modulus Operation

^
++ --

Exponentiation
Increment Decrement

Examples
1. 10 % 3=1 how? 10/3=3 remainder 1 2. S++ is the same as S=S+1 3. S-- is the same as S=S-1

Identifiers
Identifiers are names that are used to referenced variables, functions, labels and user- defined objects. They consist of a letter or an underscore followed by any combinations of letters, digits or underscores. They can be as long as 127 characters. C has standard identifiers and user-defined identifiers. User-Defined Identifiers start with a letter and may contain any combination of letters and digits. It can be of any length but only the first eight characters will be recognized.

Ex: sum, num1, ave


Examples of Standard Identifiers are printf(), scanf(),getch().

The many things that you have read are the basic concepts in Turbo C. They will make your programming activities easier. Let us see if you understand the lesson on identifiers by answering the exercises below.

Syntax Rules and Valid Identifiers


1. An identifier must consist of letters, digits and underscores only. 2. An identifier must not begin with a digit. 3. A C reserved word cannot be used as an identifier. 4. An identifier defined in a C library must not be redefined. Directions: Classify the identifiers. Type V on the blank if the identifier is valid otherwise, type I. ___ 1. quiz ___ 2. quiz2 ___ 3. quiz 3 ___ 4. sum_1 ___ 5. *quiz1* ___ 6. main ___ 7. 2answer ___ 8. username

I am sure you also want to see mathematical computation in Turbo C. All you need to do is to remember the syntax of the statements and apply math concepts. Lets do them now. The program is intended to ask the user 2 numbers. After entering 2 numbers, the program shall compute and display the sum.

The Sum Activity


1. Make the subfolder named SUM in the 3rd term folder. 2. Click File menu and select New. 3. Type the code below in the coding window

/* This program shall compute the sum of two numbers. (The symbols, asterisk and slash, are used for comments.) */ #include<stdio.h> #include<conio.h> void main() {

int num1,num2,sum; printf("Input your first number:"); scanf("%d", &num1); printf("\nInput your second number:"); scanf("%d", &num2); sum=num1+num2; /*The sum of the values of num1 and num2 shall be saved in the variable sum.*/ printf("\nThe sum of %d and %d is %d., num1,num2,sum); getch(); } 4. Click Compile Menu and select Compile or simply press Alt+F9. 5. View the output by pressing Ctrl+F9 or select the Run command in Run menu. 6. Save the program as sum.cpp in the Sum subfolder. 7.Press F9 to make an EXE file. 8. Update your index.

\n is an escape sequence. It is called the newline character, and it


means, move to the start of the next line.

Comprehension Questions
Diretions: Please answer the questions below as a group. Type the answers on the space provided after each question. 1. What are the variables in the program? 2. What is the data type of the variables? 3. What is the format modifier/conversion specifier for int data type?

4. What mathematical operator is used in the program?


5. Why does the statement, printf("\nThe sum of %d and %d is %d., num1,num2,sum); contain 3 %d?

What you have seen is just a simple example of solving mathematical problem in Turbo C. I am sure making your own Turbo C program sounds challenging to you. Its time to show your programming skills by doing the average program. You always want to know the average that you get from examinations. Instead of using your calculator in getting the average, all you need to do is to make a simple program in Turbo C. Lets read the details in the next slide.

1. Make the subfolder named AVERAGE in the 3rd term folder. 2. Read the programming problem below: Make a C program that shall get 3 quiz grades from the user. The program should display the quiz grades as well as the average.

Sample output:
Please enter quiz1 grade: 85 Please enter quiz1 grade: 90 Please enter quiz1 grade: 88 You have entered 85, 90 and 88. The average is 87.67. 3. Save the program as average.cpp in the AVERAGE subfolder. Make an EXE file by pressing F9.

Before you make the program, take note of the following: 1. The variable for average must use the %f conversion specifier. ex: scanf(%f, &ave);

ave is the variable used for average here.


2. The number of decimal places can be formatted in printf().

ex: printf(The answer is %.3f ., answer);


output: The answer is 123.456. The value of the variable answer was formatted in three decimal places using %.3f .

Once the program is finished, get 1/8 sheet cross wise. Write your name, section, date , and title of the activity which is Average. Evaluate the output using the rating scale below: 10/10 The output is correct. All requirements are present.

6/10 The program has a minor error.


4/10 The program has many errors. No output was seen in the run mode.

Assignment
Write your answers in cross wise of paper. 1. Do the flowchart of The Average Activity. 2.Write a C program that shall ask for a radius in centimeter(cm) from the user. The program must compute the area of the circle. Output: Please enter a radius in cm: 25 (enter) The area of the circle is 1963.5 sq cm 3. Please study the sample C programs and the Introduction to C lesson. Seatwork#2 shall be given next meeting.

Which of the following are needed by printf() and scanf()? text comma(,) ampersand(&) double quotes()

variable

conversion specifier(%f)

Type your answers in the correct boxes.

Printf()

Scanf()

Be ready to share your answers to the class.

1. Please answer the question below in sheet of pad paper. Use 2 to 3 statements only.
Inputs can be your preparations to achieve the desired outputs. Outputs can be your goals or dreams in life.

As a student, what inputs must be done to achieve the desired outputs in life?
2. Submit the paper to the teacher. 3. Be ready to share your answers to the class.

S-ar putea să vă placă și