Вы находитесь на странице: 1из 6

EEEB114 Programming For Engineers

LAB 5: Fundamental of C and Basic Input / Output Function

Student Name:

Student ID:

Section:

5.1) LEARNING OBJECTIVES


By the end of this experiment, you should be able to:

Execute C program structure, variables, character sets and tokens.


Execute standard input-output functions in C, and basic C operators.

5.2) PRE LAB ASSIGNMENT


a. Describe the function of the following table. Note: Use the internet

Function in C Function Description

getchar

putchar

gets

puts

printf

scanf

Page 1 of 6
EEEB114 Programming For Engineers
b. Students are required to printscreen their output from the Code:: Block and link the box together
as shown below:-

#include <stdio.h> End of segment

main () /* find the sum of the two given number


*/

{ Statement

int first, second;

printf ("Enter first number: "); Start of segment

scanf (%d, &first);

printf ("Enter second number: "); Preprocessor Directives

scanf ("%d", &second);

printf ("Their sum is %d\n", first +


second); Function main

For example, as the program runs, the numbers 10 and 20 are typed by the user; everything else
is typed by the computer as shown below:-
Enter first number: 10
Enter second number: 20
Their sum is 30

5.3) BACKGROUND
C source code is preprocessed before it is compiled into object code. A preprocessor directive, which
begins with a # sign (such as #include, #define). It tells the preprocessor to perform a certain action (such
as including a header file, or performing the text replacement), before compiling the source code into
object code. Preprocessor directives are not programming statements, and therefore should NOT be
terminated with a semi-colon. Figure 1 shows the C source code is preprocessed before it is compiled into
object code.

The header file STDIO.H and its associated object code file define the standard input and output
operations. This file forms part of the larger C standard library defined in the ANSI standard for the
language. The standard input and output library implements a simple model of text input and output

Page 2 of 6
EEEB114 Programming For Engineers
Standard input and output are special cases of text file input and output. The name STDIO.H is a cryptic
form of the name standard input/output header, because that is exactly what it does. It defines the
standard input and output functions in the form of #defines and macros.

I/O routines in C can be roughly divided into two categories:-

Those which read/write the standard input/output stream and


Those which read/write specified streams. The specified streams are usually disk files, but could
also be stdin, stdout or stderr.

Figure 1: C source code is preprocessed before it is compiled into object code

Variables
A variable can store a value of that particular type. It is important to take note that a variable in most
programming languages is associated with a type, and can only store value of the particular type. For
example, an int variable can store an integer value such as 123, but NOT real number such as 12.34, nor
texts such as "Hello". Figure 2 shows a variable has a name, stores a value of the declared type.

Page 3 of 6
EEEB114 Programming For Engineers
Figure 2: Variable (Identifier)

Printf()
The "f" in printf() stands for "formatted" printing. To do formatted printing, you need to use the following
syntax:

printf(formattingString, variable1, variable2, ...)

/*Test formatted printing for numbers*/


#include <stdio.h>

int main()
{
int number1 = 12345, number2 = 678;
float number3 = 12.015125;
printf("Hello, number1 is %d.\n", number1);// 1 format specifier
printf("number1=%d, number2=%d.\n", number1, number2);//2 format specifiers
printf("number1=%8d, number2=%5d.\n", number1, number2);// Set field-widths
printf("number1=%08d, number2=%05d.\n", number1, number2);// Pad with zero
printf("number1=%-8d, number2=%-5d.\n", number1, number2);// Left-align
printf("number3=%f.\n", number3);// float numbers
printf("number3=%e.\n", number3);// float with 'e' specifier
printf("number3=%.3f.\n", number3);// float with 3 precision
return 0;
}

Page 4 of 6
EEEB114 Programming For Engineers
5.4) IN LAB ACTIVITIES
Activity 1
1) Copy and paste the code below into the IDE. Then compile and run the program.

#include <stdio.h>
int main ()
{
/* find the sum of the two given serial resistor */
int resistor_1, resistor_2;
printf ("Enter first resistance: ");
scanf ("%d", &resistor_1);
printf ("Enter second resistance: ")
scanf ("%d", &resistor_2);
printf ("Their sum is %d\n", resistor_1+resistor_2);
return 0;
}

2) Printscreen the output from the Code:: Block

Activity 2
Edit the codes in Activity 1 so that the the system will calculate and display the total resistance of two
parallel resistors, WITHOUT deleting any line. Hint: use comments.

1) Copy and paste the new code.

Activity 3
Write a program which requests a user to enter the amount of energy in joule. The program converts the
joule to watt-hour and prints the results.

Hints:

- In order to allow joule with fractional parts to be entered, variables which can store floating-point
values will be used. The specification used for the variable should be a width of 6 and with 2
places after the decimal points
- 1 joule = 0.000277777778 watt hours

1) Write the code with Code::Block IDE

2) Printscreen the output from the Code:: Block

Page 5 of 6
EEEB114 Programming For Engineers
Activity 4
Write a program which generates the following output by using the combination of operators using initial
values only:-

5.5) State your learning curve (What have you learned?).


Students are required to list their learning outcomes; this feedback is essential for the course lecturer to
measure all learning outcome achieved during the lab session.

Page 6 of 6

Вам также может понравиться