You are on page 1of 63

CHAPTER 4:

INTRODUCTION TO C

1
Chapter 4 Objectives

• Understand the structure of the program.

• Understand a good programming style.

• Write simple computer programs using C


programming language.

• Use simple input and output statements.

• Use the fundamental data types.


Chapter 4 Contents

4.1 Introduction
4.2 Structure of C Program
4.3 Programming Style
4.4 A Simple C Programs
4.5 Summary
Chapter 4 4.1 Introduction

• The C language facilitates a structured and


disciplined approach to computer-program design.

• In this chapter we introduce the basic C programming


and present several examples that illustrate many
important features of C.

• Each example is analyzed one statement at a time to


the related elements.

_____________________________________________________________________________________________________________________
I
Deitel, H.M. and Deitel, P.J (2013). C How to Program 7/E. United State of America: Pearson Education.
Chapter 4 4.1 Introduction

• In Chapter 1, we have briefly described the common elements in


programming languages including:

o Syntax
o Keywords
o Programmer-Defined Identifiers
o Operators
o Punctuation
o Variables

• This chapter will describe the element within the examples of the
C programming code.
Chapter 4 Contents

4.1 Introduction
4.2 Structure of C Program
4.3 Programming Style
4.4 A Simple C Programs
4.5 Summary
Chapter 4 4.2 Structure of C Program

• Basic structure of the C program consists of a few parts.

• Example: Coding in C++

#include <stdio.h>
#include <conio.h>

using namespace std: (No global variable)


int main (void)
{
int age = 33;

cout<<“My First C Program\n";


cout<<“I'm %d years old”;
return 0;
}

My First C++ 0 signifies that the program exited without any error to
Program the OS while returning any other value shows that
some error have occured in your program.
I'm 33 years old
Chapter 4 4.2 Structure of C Program
Program execution

1. Global declarations set up

2. Function main executed

1. Local declarations set up


2. Each statement in statement
section executed
• executed in order (first to last)
• changes made by one
statement affect later
statements
Chapter 4 Contents

4.1 Introduction
4.2 Structure of C Program
4.3 Programming Style
4.4 A Simple C Programs
4.5 Summary
Chapter 4 4.3 Programming Style
Introduction

• The programming style is is a term used to describe the


effort a programmer should take to make his or her code
easy to read and easy to understand.

• Good organization of the code and meaningful variable


names help readability, and liberal use of comments can
help the reader understand what the program does and why.

• The programming style does not affect the syntax of the


program BUT affects the readability of the source code.
Chapter 4 4.3 Programming Style

• Common elements to improve readability:

• Braces { } aligned vertically.

• Indentation of statements within a set of braces.

• Blank lines between declaration and other statements.

• Long statements wrapped over multiple lines with


aligned operators.
Chapter 4 4.3 Programming Style

• C doesn’t care much about spaces !


• Some examples of coding written in C. 2
1 /* Program_01.c /* Program_01.c A first program
A first program in in C */ #include <stdio.h> /*
C */ function main begins */ int
#include <stdio.h> main( void ){ printf( "Welcome
/* function main to C!\n" );} /* end function
begins program main */
execution */
3
int main
( void ) /* Program_01.c
{ A first program in C */
printf ( #include <stdio.h>
"Welcome to C!\n"
); /* function main begins */
} int main( void ){
/* end function printf( "Welcome to C!\n" );
main */ } /* end function main */
Chapter 4 4.3 Programming Style

• All of these programs are exactly the same as the original


as far as your compiler is concerned.

• Note that words have to be kept together and so do things


in quotes.
Chapter 4 4.3 Programming Style
Indentation

/* Program_01.c 4 • As you can see, the


A first program in C */ statement in the body of
#include <stdio.h>
main() do not line up
/* function main begins */ with the rest of the code.
int main( void )
{ • This is called indentation.
printf( "Welcome to C!\n" );
} /* end function main */

• Orderly indentation is very important; it makes your code


readable.
• The C compiler ignores white space
Chapter 4 Contents

4.1 Introduction
4.2 Structure of C Program
4.3 Programming Style
4.4 A Simple C Programs
(a) Printing a line of text
(b) Adding two integers
(c) Multiplying some values
4.5 Summary
Chapter 4 4.4 A Simple C Program
(a) Printing a line of text
• We begin by writing a simple C program.
• The program and its screen output on screen are shown in
the following figure.

1 // Program_01.c
2 // A first program in C
3 #include <stdio.h>
4
5 // function main begins program execution
6 int main( void )
7 {
8 printf( "Welcome to C!\n" );
9 } // end function main

Welcome to C!
_____________________________________________________________________________________________________________________
I
Deitel, H.M. and Deitel, P.J (2013). C How to Program 7/E. United State of America: Pearson Education.
Chapter 4 4.4 A Simple C Program

Comments
Comments

Preprocessor
Preprocessor
directive
directive

1 // Program_01.c The main


2 The main
// A first program in Cfunction
function An
Anoutput
output
3 #include <stdio.h> statement
statement
4
5 // function main begins program execution
6 int main( void )
7 {
8 printf( "Welcome to C!\n" );
9 } // end function main

Blank
Blanklines
linesand
and Escape
Escape
white space
white space sequences
sequences
_____________________________________________________________________________________________________________________
I
Deitel, H.M. and Deitel, P.J (2013). C How to Program 7/E. United State of America: Pearson Education.
Chapter 4 4.4 A Simple C Program

Special Characters
Character Name Meaning
/* Slash star Beginning of a comment
/* */ Star slash End of a comment
# Pound sign Beginning of preprocessor
directive
< > Open/close brackets Enclose filename in #include
( ) Open/close Used when naming a function
parentheses
{ } Open/close brace Encloses a group of statements

" " Open/close quotation Encloses string of characters


marks
; Semicolon End of a programming statement
Chapter 4 4.4 A Simple C Program

Comment

• Line 1, 2 and 5 begin with //


• Used to “document programs” the code for the human
reader
• Ignored by compiler (not part of program)

• The preceding comment simply describes the file name and


purpose of the program
// Program_4.1.c
// A first program in C
// function main begins program execution

• Comments also help other people read and understand


your program easily.
_____________________________________________________________________________________________________________________
I
Deitel, H.M. and Deitel, P.J (2013). C How to Program 7/E. United State of America: Pearson Education.
Chapter 4 4.4 A Simple C Program

• You may also use /*...*/ for multi-line comments.


• Everything from /* on the first line to */ at the end of the
last line is a comment.

• Example:
/* Program_4.1.c
A first program in C */

/* function main begins program execution */

• We prefer // comments because they’re shorter and


eliminate common programming errors.

_____________________________________________________________________________________________________________________
I
Deitel, H.M. and Deitel, P.J (2013). C How to Program 7/E. United State of America: Pearson Education.
Chapter 4 4.4 A Simple C Program

Preprocessor Directive

• Line 3 is a directive to the C preprocessor.


• It begins with #
• Instruct compiler to perform some transformation to file
before compiling

• Example:
#include <stdio.h>

add the header file stdio.h to this file


.h for header file
stdio.h defines useful input/output functions

_____________________________________________________________________________________________________________________
I
Deitel, H.M. and Deitel, P.J (2013). C How to Program 7/E. United State of America: Pearson Education.
Chapter 4 4.4 A Simple C Program

Function

int main( void ) Header


{
printf( "Welcome to C!\n" ); Body { }
}

• Consists of header and body

•Functions can return information.


int indicates that the main “return” an integer value.

• Functions also can receive information when are called


(invoked) - more later
• The void means that main does not received any information

_____________________________________________________________________________________________________________________
I
Deitel, H.M. and Deitel, P.J (2013). C How to Program 7/E. United State of America: Pearson Education.
Chapter 4 4.4 A Simple C Program

An Output Statement
• Line 8 instructs the computer to print on the screen the string
of characters marked by the double quotes “m” marks.
• A string is sometimes called a character string, a message or
a literal.

printf( "Welcome to C!\n" );

• The entire line is called statement.

• When the printf statement executed, it prints the message


Welcome to C! on the screen.
• The characters normally print exactly as they appear
between the double quotes in the printf statement.

_____________________________________________________________________________________________________________________
I
Deitel, H.M. and Deitel, P.J (2013). C How to Program 7/E. United State of America: Pearson Education.
Chapter 4 4.4 A Simple C Program

Escape Sequences

• Notice that the character \n were not printed on the screen.

• The backslash (\) is called an escape character.

• It indicates that printf is supposed to do something out of


the ordinary.

• The \n means newline that causes the cursor to position to


the beginning of the next line on the screen.

printf( "Welcome to C!\n" );

_____________________________________________________________________________________________________________________
I
Deitel, H.M. and Deitel, P.J (2013). C How to Program 7/E. United State of America: Pearson Education.
Chapter 4 4.4 A Simple C Program

Example: Using multiple printf


• The following program will produces the same output as
before.
• This works because each printf resumes printing where
the previous printf stopped printing.

1 // Program_02.c • The first printf (line 8)


2 // A first program in C prints Welcome followed
3 #include <stdio.h> by a space
4 • The second printf
5 // function main
(line 9) begins printing
6 int main( void )
7 {
on the same line
8 printf( "Welcome " ); immediately after the
9 printf( “to C!\n" ); space.
10 } // end function main
_____________________________________________________________________________________________________________________
I
Deitel, H.M. and Deitel, P.J (2013). C How to Program 7/E. United State of America: Pearson Education.
Chapter 4 4.4 A Simple C Program

Activity 4.1: Write a C program to display your name as the


example below.

Tips: .
Put comment in
your program and
save your file as
Activity_4.1.c
Chapter 4 4.4 A Simple C Program
(b) Adding two integers

• Our next program (next page) uses the Standard Library


function scanf to obtain two integers typed by a user at
the keyboard, computes the sum of the values and prints
the result using printf.

• As stated earlier, each program begins execution with main.

• The left brace { (line 7) marks the beginning of the body of


main, and the corresponding right brace } (line 21) marks
the end of main function.

_____________________________________________________________________________________________________________________
I
Deitel, H.M. and Deitel, P.J (2013). C How to Program 7/E. United State of America: Pearson Education.
Chapter 4 4.4 A Simple C Program
1 // Program_03.c
2 // Addition program
3 #include <stdio.h>
4
5 // function main begins program execution
6 int main( void )
7 {
8 int integer1; // first number to be entered by user
9 int integer2; // second number to be entered by user
10 int sum; // variable in which sum will be stored
11
12 printf( "Enter first integer\n" ); // prompt
13 scanf( "%d", &integer1 ); // read an integer
14
15 printf( "Enter second integer\n" ); // prompt
16 scanf( "%d", &integer2 ); // read an integer
17
18 sum = integer1 + integer2; // assign total to sum
19
20 printf( "Sum is %d\n", sum ); // print sum
21 } // end function main
_____________________________________________________________________________________________________________________
I
Deitel, H.M. and Deitel, P.J (2013). C How to Program 7/E. United State of America: Pearson Education.
Chapter 4 4.4 A Simple C Program
1 // Program_03.c
2 // Addition program
3 #include <stdio.h> Variables
4 Variables
with
with
5 valid identifier
6 int main( void ) valid identifier
7 {
8 int integer1; Prompt
Prompt
9 int integer2; messages
messages
10 int sum;
11
12 printf( "Enter first integer\n" ); The
Theinput
input
13 scanf( "%d", &integer1 ); statements
statements
14
15 printf( "Enter second integer\n" );
16 scanf( "%d", &integer2 );
17
sum = integer1 + integer2; Assignment
Assignment
18
statements
statements
19 Printing
20 printf( "Sum is %d\n", sum ); Printing
statements
statements
21 } _____________________________________________________________________________________________________________________
I
Deitel, H.M. and Deitel, P.J (2013). C How to Program 7/E. United State of America: Pearson Education.
Chapter 4 4.4 A Simple C Program

Variables:

• Line 8-10 are definitions.

int integer1;
int integer2;
int sum;

• The names integer1, integer2 and sum are the names


of variables.

• These variables are of type int, which means that they’ll


hold integer values that stored in memory.

_____________________________________________________________________________________________________________________
I
Deitel, H.M. and Deitel, P.J (2013). C How to Program 7/E. United State of America: Pearson Education.
Chapter 4 4.4 A Simple C Program

• All variables must be defined with a name and a data type


before they can be used in a program.

• Definitions could have been combined into a single


definition statement as follows:

int integer1, integer2, sum;

_____________________________________________________________________________________________________________________
I
Deitel, H.M. and Deitel, P.J (2013). C How to Program 7/E. United State of America: Pearson Education.
Chapter 4 4.4 A Simple C Program

Identifiers and Case Sensitivity:

• A variable name in C is any valid identifier.

• An identifier is a series of characters consisting of letters


( A-Z,a-z ) , digits ( 0-9 ) and underscores ( _ ) that does
not begin with a digit.

• C is case sensitive — uppercase and lowercase letters are


different in C.

• Example: a1 and A1 are different identifiers.

_____________________________________________________________________________________________________________________
I
Deitel, H.M. and Deitel, P.J (2013). C How to Program 7/E. United State of America: Pearson Education.
Chapter 4 4.4 A Simple C Program

Prompting Messages:

printf( "Enter first integer\n" );

• Line 12 displays the literal "Enter first integer" and


positions the cursor to the beginning of the next line.

• This message is called a prompt because it tells the user to


take a specific action.

• The user responds by typing an integer, then pressing the


Enter key to send the number to the computer.
enter

_____________________________________________________________________________________________________________________
I
Deitel, H.M. and Deitel, P.J (2013). C How to Program 7/E. United State of America: Pearson Education.
Chapter 4 4.4 A Simple C Program

The scanf function (Input):

• The statement scanf (line 13) obtain the value from the user.
• The computer then assigns this number, or value, to the
variable integer1.

• scanf has two arguments

scanf("%d", &integer1);

Format
Formatspecifier
specifier Address
Addressoperator
operator

Begin with % and followed by Begin with & and followed by


d Indicates the type of data is the variable name, integer1
integer
_____________________________________________________________________________________________________________________
I
Deitel, H.M. and Deitel, P.J (2013). C How to Program 7/E. United State of America: Pearson Education.
Chapter 4 4.4 A Simple C Program

• Some of other different format of format specifiers:

Format Specifier Use for


%c Single character
%s String
%d Signed decimal integer
%f Floating point (decimal notation)
%e Exponential notation
%u Unsigned decimal integer

_____________________________________________________________________________________________________________________
I
Deitel, H.M. and Deitel, P.J (2013). C How to Program 7/E. United State of America: Pearson Education.
Chapter 4 4.4 A Simple C Program

Assignment Statement (Operation):

sum = integer1 + integer2;

• The assignment statement in line 18 calculate the total of


variables integer1 and integer2

• Assigns the result to variable sum using the assignment


operator =

_____________________________________________________________________________________________________________________
I
Deitel, H.M. and Deitel, P.J (2013). C How to Program 7/E. United State of America: Pearson Education.
Chapter 4 4.4 A Simple C Program

Printing:

• Line 20 calls function printf to print the literal Sum is


followed by the numerical value of variable sum on the screen.

• The printf has two arguments

printf("Sum is %d\n", sum);

Format
Formatspecifier
specifier Address
Addressoperator
operator

It contains some literal


characters “Sum is ”and Specifies the value in
format specifier %d to be variable sum to be printed.
displayed.
_____________________________________________________________________________________________________________________
I
Deitel, H.M. and Deitel, P.J (2013). C How to Program 7/E. United State of America: Pearson Education.
Chapter 4 4.4 A Simple C Program

Notice
Noticethat
thatthe
theformat
formatspecifier
specifierfor
foran
aninteger
integerisisthe
thesame
sameinin
both printfand
bothprintf andscanf
scanf

• Calculations can also be performed inside printf statements


by combining the previous two statements into the statement

printf( "Sum is %d\n", integer1 + integer2);

_____________________________________________________________________________________________________________________
I
Deitel, H.M. and Deitel, P.J (2013). C How to Program 7/E. United State of America: Pearson Education.
Chapter 4 4.4 A Simple C Program

Activity 4.2: Modify the program Activity_4.1.c to enter


TWO integer and display the output as below.

Tips: .
Put comment in
your program and
save your file as
Activity_4.2.c
Chapter 4 4.4 A Simple C Program
(c) Multiplying some values

• Our next program (next page) is based π = 3.14


on the Activity 2.1 in Chapter 2.

• The program will obtain an integer


typed for the radius by a user at the
keyboard using scanf, the high value
is consistent, computes the volume and
prints the result using printf.

_____________________________________________________________________________________________________________________
I
Deitel, H.M. and Deitel, P.J (2013). C How to Program 7/E. United State of America: Pearson Education.
Chapter 4 4.4 A Simple C Program
1 // Program_04.c
2 // Multiplication program: Calculate a cylinder’s volume
3 #include <stdio.h>
4
5 // function main begins program execution
6 int main( void )
7 {
8 int high, radius; // the values to be entered by user
9 double volume; // variable for result to be stored
10 const double pi = 3.14; // pi value always 3.14
11 char letter = 'B'; // label of the cylinder
12
13 printf( "\nEnter the high: " ); // prompt
14 scanf( "%d", &high ); // read an integer
15
16 printf( "Enter the radius: " ); // prompt
17 scanf( "%d", &radius ); // read an integer
18
19 // assign result to volume
20 volume = pi * high * radius * radius;
21
22 printf("\nVolume for cylinder %c is %f\n\n”, letter ,volume);
23 } // end function main
Chapter 4 4.4 A Simple C Program
1 // Program_04.c
2 // Multiplication program: Calculate a cylinder’s volume
3 #include <stdio.h>
4 Integer
Integer Floating
Floatingpoint
point
5 data types
data types data types
data types
6 int main( void )
7 {
8 int high, radius;
9 double volume;
10 const double pi = 3.14;
11 char letter = 'B';
12 Character
13 printf( "\nEnter the high: " ); Character
14 scanf( "%d", &high );
data
datatypes
types
Constant
15Constant
16 printf( "Enter the radius: " );
17 scanf( "%d", &radius );
18 Output:
19
20 volume = pi * high * radius * radius;
21
22 printf("\nVolume for cylinder %c is %f\n\n”, letter ,volume);
23 }
Chapter 4 4.4 A Simple C Program
Integer Data Types:

• Line 8 defined the integer variables with valid identifier


(high and radius) in the same line.
• Variables of different types must be in different definition line.
• Each integer has a valid size in bytes.

Data type Size Range

Short 2 bytes -32,768 to 32,767

Unsigned short 2 bytes 0 to 65,535

Int 2 bytes -32,768 to 32,767

Unsigned int 2 bytes 0 to 65,535

Long 4 bytes -2,147,483,648 to 2,147,483,647

Unsigned long 4 bytes 0 to 4,294,967,295

Float 4 bytes 1.2E-38 to 3.4E+38

Double 8 bytes 2.3E-308 to 1.7E+308


Chapter 4 4.4 A Simple C Program

• Integer literal is an integer value that is typed into a


program’s code.

• It stored in memory as int by default.

• Example: In the code, 15 is the integer literal.

high = 15;
radius = 15;
Chapter 4 4.4 A Simple C Program

Floating Point Data Types: (Line 9-10)


float
double
• The floating-point data types are: long double

• They can hold real numbers such as: 12.45


-3.8

• Stored in a form similar to scientific notation


• All floating-point numbers are signed
Chapter 4 4.4 A Simple C Program

• Floating points can be represented in:

• Fixed point (decimal) notation:


31.4159 0.0000625

• E notation:
3.14159e-1 6.25e-5

• Floating points are double by default.

• It can be forced to be:


• float (3.14159f) or
• long double (0.0000625L)
Chapter 4 4.4 A Simple C Program

Constants:

• Line 10 initialized a qualifier constant pi = 3.14 within


the main function.

• Constants stored in memory location that holds a non-


changeable value.
• Must be initialized with a value
• Can not be modified after initialization

• 2 ways to declare constant:


• Using type qualifier const
• Using #define preprocessor directive

_____________________________________________________________________________________________________________________
I
Deitel, H.M. and Deitel, P.J (2013). C How to Program 7/E. United State of America: Pearson Education.
Chapter 4 4.4 A Simple C Program

• preprocessor directive constant must be initialized before


the main function.

• Example: • Syntax
:
#include <stdio.h>
#define NAME value
#define PI 3.14

int main( void ) the


thename
nameisisusually
usually
{ capitalized
capitalized
int high, radius;
double volume;
• • Preprocessor
Preprocessordirectives
directives
.
are not statements.
are not statements.
.
• • There
Thereshould
shouldbebeno
no
.
semicolon
semicolonatatthe
theend.
end.
}
Chapter 4 4.4 A Simple C Program

Character Data Type

• Line 11 defined as char that used to hold characters or


very small integer values.

• Usually the size is 1 byte (8 bits) of memory.

• Numeric value of character from the character set is stored


in memory.

CODE: MEMORY
:
char letter;
letter = 'C'; letter
67
Chapter 4 4.4 A Simple C Program

Important:
Important:Note
Notethat
thatthe
thevalue
valueofofaacharacter
characterisisenclosed
enclosed
ininsingle
singlequotes
quotesmarks.
marks.Example:‘B’
Example:‘B’

• A computer normally stores characters using the ASCII code


(American Standard Code for Information Exchange)
• ASCII is used to represent:
❖ the characters A to Z (both upper and lower case)
❖ the digits 0 to 9
❖ special characters (e.g. @, <, etc)
❖ special control codes

• The decimal code will be converted into binary (stored as a


integer byte)
Chapter 4 4.4 A Simple C Program

Table: Character constants Table: Decimal value for symbols

Char Special symbols Decimal


Symbol
(code)
‘\n’ New line
65 A
‘\t’ horizontal tab
66 B
‘\v’ vertical tab 67 C
‘\r’ carriage return 68 D
‘\b’ backspace 69 E
97 a
‘\f’ formfeed
98 b
‘\\’ Backslash (\) 99 c
‘\x41’ Hexa 0x41 100 d
‘\101’ Octal 101 101 e
‘\0’ null
Chapter 4 4.4 A Simple C Program

Printing:

• For printing the output (Line 22), we need to use the correct
format specifier for:
• char data type, letter
• double data type, volume.

printf(“Volume for cylinder %c is %f”, letter ,volume);

Format
Formatspecifier
specifier

%c will displays the %f will displays the


char data type value. double data type value.

_____________________________________________________________________________________________________________________
I
Deitel, H.M. and Deitel, P.J (2013). C How to Program 7/E. United State of America: Pearson Education.
Chapter 4 4.4 A Simple C Program

Figure: Data type with the correct format specifier


_____________________________________________________________________________________________________________________

http://3.bp.blogspot.com/-5b2Smx0fx5E/TrGT3SrXa1I/AAAAAAAAAH4/SsnplVh7agk/s1600/Format+Specifications+in+C+programming+language.bmp
Chapter 4 4.4 A Simple C Program

Activity 4.3: Modify the program Activity_4.2.c to calculate


the circle area and the volume of a cylinder.
Assume that you need to enter TWO integer values
Tips: .
for it high and radius.
Put comment in
your program and Display the output as below.
save it file as
Activity_4.3.c
Chapter 4 4.4 A Simple C Program

Solution 4.3:

Activity_4.3.c

Hint:
Hint:Need
Needtoto
write
write%.2f
%.2f
Chapter 4 4.4 A Simple C Program

Activity 4.4: Modify the program Activity_4.3.c to calculate


the circle area and the volume of a cylinder.
Now, assume that you need to enter TWO floating
Tips: .
point values for it high and radius.
Put comment in
your program and Display the output as below.
save it file as
Activity_4.4.c
Chapter 4 4.4 A Simple C Program

Summary of the Simple C Programs

• ALL statements end with a semicolon! (;)

• C is case sensitive.
• printf() is NOT the same as Printf().
• All C commands (functions) are lowercase.

To make your program more


readable:
• Always write comments.
• Indent your code.

_____________________________________________________________________________________________________________________
I
Deitel, H.M. and Deitel, P.J (2013). C How to Program 7/E. United State of America: Pearson Education.
Chapter 4 Contents

4.1 Introduction
4.2 Structure of C Program
4.3 Programming Style
4.4 A Simple C Programs
4.5 Summary
Chapter 4 4.5 Summary

• The C language facilitates a structured and disciplined


approach to write a computer program.

• Always write comments to document the programs and


practice coding with a good programming style will
improve program readability.

• A syntax error is caused when compiler cannot recognize


a statement. Normally the compiler will issues an error
message to help us to locate and fix the incorrect
statement.

_____________________________________________________________________________________________________________________
I
Deitel, H.M. and Deitel, P.J (2013). C How to Program 7/E. United State of America: Pearson Education.
Chapter 4

Self-Reviews
Chapter 4 Self-Review

Exercise 4.1: Fill in the blanks in each of the following questions.

a) Every C program begins execution at the


function __________.

b) Every statement ends with a(n) __________.

c) The __________ standard library function


displays information on the screen.

d) The __________ standard library function is


used to obtain data from the keyboard.
Chapter 4 Self-Review

Solution 4.1: Fill in the blanks in each of the following questions.

a) Every C program begins execution at the


function main.

b) Every statement ends with a(n) semicolon.

c) The print standard library function displays


information on the screen.

d) The scanf standard library function is used to


obtain data from the keyboard.
Chapter 4 Self-Review

Exercise 4.2: Calculate and display the power (watts) and the
voltage (volts) when enter TWO set of floating point
values contain the current (amps) and resistance
Tips: .
(ohms) from the keyboard.
Put comment in
your program and The result values should be displayed with a
save it file as precision of 3. You can only declare 4 variables.
Exercise_4.2.c

You might also like