You are on page 1of 32

May 6, 2021

BFF/BHM 2003: LECTURE 2


BFF/ BHM 2003
COMPUTER
PROGRAMMING
1
LECTURE 2
INTRODUCTION TO THE C
LANGUAGE

May 6, 2021
 Introduce the basics of the C language.

BFF2003: LECTURE 2
 Write your first C program. ‘Hello World’

BY END OF THIS CLASS, YOU SHOULD BE ABLE


TO:
CONSTRUCT AND ANALYZE A SIMPLE C PROGRAM
THAT CAN DISPLAY FORMATTED DATA USING
PRINTF( ) FUNCTIONS AND RELATED ESCAPE
2
SEQUENCES.
THE C PROGRAMS

May 6, 2021
BFF2003: LECTURE 2
It is time to write your first C program!

3
STRUCTURE OF A C
PROGRAMS
 EveryC program is made of:

May 6, 2021
One or more preprocessor commands,

BFF2003: LECTURE 2
A global declaration
One or more functions

 The global declaration section comes at the beginning of


the program and it visible to all parts of the program.

4
STRUCTURE OF A C
PROGRAMS
 The work of the program is carried out by its functions.

May 6, 2021
BFF2003: LECTURE 2
 Functions= blocks of code that accomplish a task within
a program.

 One, and only one, of the functions must be named main.

5
STRUCTURE OF A C
PROGRAMS
 ‘main’ function = starting point for the program.

May 6, 2021
BFF2003: LECTURE 2
 Dividedinto two:
The declaration section
The statement section

6
STRUCTURE OF A C
PROGRAMS
 The declaration section

May 6, 2021
1. Beginning of the function.

BFF2003: LECTURE 2
2. Describes the data that you will be using in the
function.
3. Known as local declarations (opposed to global
declarations)
4. Visible only to the function that contain them.

7
STRUCTURE OF A C
PROGRAMS
 The statement section

May 6, 2021
1. Follows the declaration section.

BFF2003: LECTURE 2
2. Contains the instruction to the computer that cause it
to do something (e.g. add two numbers).
3. Write in the form of statements.

8
May 6, 2021 BFF2003: LECTURE 2
9
STRUCTURE OF A C
PROGRAMS
STRUCTURE OF A C
PROGRAMS
 Preprocessor commands:

May 6, 2021
1. Special instructions to the preprocessor that tell it

BFF2003: LECTURE 2
how to prepare the program for compilation.
2. The most important preprocessor commands:
include

10
STRUCTURE OF A C
PROGRAMS
 include

May 6, 2021
 Tell the preprocessor that we need information from
selected libraries known as header files.

BFF2003: LECTURE 2
 It is almost impossible to write even the smallest of
programs without at least one library function.

 In your first program, you will use one include command to


tell C that you need the input/output library to write data to
the monitor.

11
May 6, 2021 BFF2003: LECTURE 2
12
YOUR FIRST C PROGRAM
YOUR FIRST C PROGRAM
 Your program will be very simple.

May 6, 2021
 Only have: 1 preprocessor

BFF2003: LECTURE 2
0 global declarations
0 local declarations

13
YOUR FIRST C PROGRAM
 It purpose will be simply to print a greeting to the user.

May 6, 2021
 The statement section only have 2 statements:

BFF2003: LECTURE 2
 Prints a greeting
 Stops the program

14
PREPROCESSOR COMMANDS
 Come at the beginning of the program.

May 6, 2021
 Start with a pound sign (#) rule of C or syntax.

BFF2003: LECTURE 2
 Can start in any column (usually in column 1).

15
PREPROCESSOR COMMANDS

May 6, 2021
 Tell the compiler to include the standard input/output library file in the program.

 This library file is for print a message to the terminal.

BFF2003: LECTURE 2
 No space between the # and include.

16
MAIN

May 6, 2021
 Begin the executable part of the program.

 int says that the function (main) will return an integer value to the operating system.

BFF2003: LECTURE 2
 The function has no parameters (the parameter list is void).

 No punctuation after the function header.

17
STATEMENTS

May 6, 2021
 Within the main there are two statements:
 Print your message

BFF2003: LECTURE 2
 Terminate the program

18
STATEMENTS

May 6, 2021
 Print statement uses a library function to the actual writing to the monitor.

 To execute this print function, you call it.

BFF2003: LECTURE 2
 All
function call statements consist of the name of the function (printf) followed by a
parameter list enclosed in parentheses.

19
STATEMENTS

May 6, 2021
 Theparameter list:
 Contains what you want to displayed.
 Enclosed in two double quote marks (“…”)

BFF2003: LECTURE 2
 The \n at the end of the message tells the computer to advance to the next line in
the output. Escapes character.

20
STATEMENTS

May 6, 2021
 return 0;
 Terminates the program
Returns control to the operating system.

BFF2003: LECTURE 2

 Thefunction main start with open brace ( { ) and terminates with a close
brace ( } ).

21
ESCAPE CHARACTER
Escape Character Meaning Description

May 6, 2021
\n New line Position the cursor at the beginning of the next line.

\t Horizontal tab Move the cursor to the next tab stop.

BFF2003: LECTURE 2
\r Carriage return Move the cursor to the initial point of the current line.

\a Alert/bell Generates an audible or visible alert.

\\ Backslash Print a backslash character in a printf statement.

\” Double quote Print a double quote character in a printf statement.

\’ Single quote Print a single quote character in a printf statement.

\% Percent Print a percent character in a printf statement.


22
\? Question mark Print a question mark character in a printf statement.
COMMENTS
 Sometimes, meaning of a section of code is not entirely clear.

May 6, 2021
 Itis helpful if the person who writes the code places some comments in
the code to help the reader.

BFF2003: LECTURE 2
 Comments are merely internal program documentation. The compiler
ignores the comments when it translates the program into executable
code.

23
COMMENTS
 To
identify a comment, C uses two different formats;
Block comments

May 6, 2021
Line comments

BFF2003: LECTURE 2
24
May 6, 2021 BFF2003: LECTURE 2
25
BLOCK COMMENTS
BLOCK COMMENTS
 Used when the comment will span several lines.

May 6, 2021
 It uses opening and closing comment tokens.

 Token = symbol(s) understood by the compiler that help it interpret code.

BFF2003: LECTURE 2
 The opening token is ( /* ) and the closing token is ( */ ). no space between them.

 Everything between the opening and closing comment tokens is ignored by the
compiler.

26
BLOCK COMMENTS
 The token can start in any column, and they do not have
to be on the same line.

May 6, 2021
 The only requirement is that the opening token must

BFF2003: LECTURE 2
precede the closing token.

27
BLOCK COMMENTS
 Comments cannot be nested (cannot have comments
inside comments)

May 6, 2021
BFF2003: LECTURE 2
28
LINE COMMENTS
 Use 2 slashes ( // ) to identify a comment.

May 6, 2021
 Not require an end-of-comment token. the end of the line
automatically ends the comment.

BFF2003: LECTURE 2
 Use for short comments.

 Can start anywhere on the line.

29
THE GREETING PROGRAM
 Includedsome comments at the beginning that explain what the
program is going to do.

May 6, 2021
 Each program we write MUST begins with documentation

BFF2003: LECTURE 2
explaining the purpose of the program.

 We have also shown comment to identify the declaration and


statement sections of our program.

30
May 6, 2021 BFF2003: LECTURE 2
31
THE GREETING PROGRAM
END OF TODAY’S LECTURE
 Thank you for your attention.

May 6, 2021
BFF2003: LECTURE 2
 Now, we were move to the practical work
(the important part of this class).

 Please sit in your own group.

 Please do the assignment NOW. Submit the assignment according


32

to the date on the assignment sheet.

You might also like