Академический Документы
Профессиональный Документы
Культура Документы
Student Name:
Student ID:
Section:
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:-
{ Statement
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.
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:
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;
}
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.
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
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:-
Page 6 of 6