You are on page 1of 44

COE 281

INTRODUCTION TO PROGRAMMING

DR. (MRS) T-S.M.A. ADJAIDOO

COE 281: Programming and Problem-Solving 1


TODAY'S LESSON

The
Elements of Introduction to Keywords and
Computational Hello World in C
Programming C programming Identifiers
Process

COE 281: Programming and Problem-Solving 2


LEARNING OUTCOMES

• Understand computational
1 processes

• Introduce learners to the


2 foundations of C

• The concept of expressions and


3 naming in C

COE 281: Programming and Problem-Solving 3


THE COMPUTATIONAL PROCESS

  Computational processes are abstract beings that inhabit computers.


  They manipulate other abstract things called data.
  The evolution of a process is directed by a pattern of rules called a program.
  People create programs to direct processes.

COE 281: Programming and Problem-Solving 4


THE COMPUTATIONAL PROCESS

  A computational processes are intangible, however, they are very real.


  They can perform intellectual work like answering questions.
  The programs which invoke these processes are carefully composed from
symbolic expressions in programming languages that prescribe the tasks we want
our processes to perform.
  A computational process, in a correctly working computer, executes programs
precisely and accurately

COE 281: Programming and Problem-Solving 5


THE COMPUTATIONAL PROCESS

  Programmers must learn to understand and to anticipate the consequences of


their invoking processes.
  Even small errors (usually called bugs or glitches) in programs can have complex
and unanticipated consequences.
  Real-world programming, requires care, expertise, and wisdom.
  A small bug in a computer-aided design program, for example, can lead to the
catastrophic collapse of an airplane or a dam or the self-destruction of an
industrial robot.

COE 281: Programming and Problem-Solving 6


THE COMPUTATIONAL PROCESS

  Master software engineers have the ability to organize programs so that they can
be reasonably sure that the resulting processes will perform the tasks intended.
  They can visualize the behavior of their systems in advance.
  They know how to structure programs so that unanticipated problems do not lead
to catastrophic consequences, and when problems do arise, they can debug their
programs.
  Well-designed computational systems, like well-designed automobiles or nuclear
reactors, are designed in a modular manner, so that the parts can be constructed,
replaced, and debugged separately.

COE 281: Programming and Problem-Solving 7


THE ELEMENTS OF PROGRAMMING

  A powerful programming language is more than just a means for instructing a


computer to perform tasks.
  The language also serves as a framework within which we organize our ideas
about processes.
  Thus, when we describe a language, we should pay particular attention to the
means that the language provides for combining simple ideas to form more
complex ideas.

COE 281: Programming and Problem-Solving 8


THE ELEMENTS OF PROGRAMMING

  Every powerful language has three mechanisms for combining simple ideas to
form more complex ideas:
  The use of primitive expressions, which represent the simplest entities the language is
concerned with
  A means of combination, by which compound elements are built from simpler ones
  A means of abstraction, by which compound elements can be named and manipulated
as units.

COE 281: Programming and Problem-Solving 9


THE ELEMENTS OF PROGRAMMING

  In programming, we deal with two kinds of elements:


  procedures and data.
  Data is "stuff or things" that we want to manipulate
  Procedures are descriptions of the rules for manipulating the data.
  Any powerful programming language should be able to describe primitive data
and primitive procedures and should have methods for combining and abstracting
procedures and data.

COE 281: Programming and Problem-Solving 10


EXPRESSIONS

  Expressions in C are a combination of operators and operands to usually perform


some computation or operation
  Operands can be in the form of raw values or variables holding values
  For example:
x+3
x is a variable
+ is an operator
3 is an integer value

COE 281: Programming and Problem-Solving 11


EXPRESSIONS

Operands manipulated
Arithmetic by arithmetic operators

Returns 1 if the condition is Types of Computes either a zero


Conditional Expressions Logical
true otherwise 0 or non-zero value

Used to compare two Relational


operands

COE 281: Programming and Problem-Solving 12


EXPRESSIONS

  Simple and Compound Expressions will be discussed further in later chapters of


this course

COE 281: Programming and Problem-Solving 13


PROGRAMMING IN C
Introduction to C Programming
Keywords and Identifiers
Hello World in C

COE 281: Programming and Problem-Solving 14


WHAT IS C?

  C is closely associated with Unix Operating system


  The developers of Unix Operating system (including Dennis Ritchie and
Stephen C. Johnson) decided to rewrite the system in B language.
  However, B couldn’t suffice some of the features of that PDP-11 version of
Unix, which led to the development of C.

COE 281: Programming and Problem-Solving 15


HISTORY OF C PROGRAMMING

  In 1972, the development of C started on the PDP-11 Unix system.


  A large part of Unix was then rewritten in C.
  By 1973, C was powerful enough to be used in Unix Kernel.
  Dennis Ritchie and Stephen C. Johnson made further changes to the
language for several years to make it portable in Unix Operating system.

COE 281: Programming and Problem-Solving 16


WHY LEARN C?

  Learning C makes the inner workings of a computer more understandable


thus providing a clear mental model of how the computer works.
  C is the lingua franca of programming. Almost all high-level programming
languages like Java, Python, JavaScript etc. can interface with C
programming.

COE 281: Programming and Problem-Solving 17


WHY LEARN C?

  Learning C provides one with the opportunity and capability to work on


open source projects that impact millions of people.
  This is so because most high-level programming languages (e.g. Python)
were birthed from C. Thus, C is the main backbone on which they ride.
  Therefore to contribute to how these other programming languages
work, one must learn C.

COE 281: Programming and Problem-Solving 18


WHY LEARN C?

  Most programming languages use a similar syntax to C.


  Therefore by knowing how to code in C you would get a head start in
learning these other programming languages (e.g. C++, C#, Javascript
and PHP)

COE 281: Programming and Problem-Solving 19


WHY LEARN C?

  It is also important to know that C can be used in programming most


microprocessor based systems.
  The world of robotics, artificial intelligence and embedded systems thrive
heavily on efficient programs written in C.
  Further Reading: Read on real-world applications of C programming
language

COE 281: Programming and Problem-Solving 20


THE CHARACTER SET OF C

  Character set is a set of alphabets, letters and some special characters that
are valid in C language.
  C uses the uppercase letters A to Z, lowercase letters a to z, blank space,
the digits 0 to 9 and certain special characters as building blocks to form
basic program elements.

COE 281: Programming and Problem-Solving 21


SPECIAL CHARACTERS IN C

, < > . -

( ) ; $ :

% [ ] # ?

’ & { } “

^ ! * / |

_ \ ~ + =

COE 281: Programming and Problem-Solving 22


C KEYWORDS

  Keywords are predefined, reserved words used in programming that have


special meanings to the compiler.
  As C is a case sensitive language, all keywords must be written in
lowercase.
  The next slide presents a list of all keywords allowed in ANSI (standard) C.

COE 281: Programming and Problem-Solving 23


C KEYWORDS

auto double int struct break else long switch

case enum register typedef char extern return union

continue for signed void do if static while

default goto sizeof volatile const float short unsigned

COE 281: Programming and Problem-Solving 24


IDENTIFIERS

  Identifier refers to name given to entities such as variables, functions,


structures etc.
  Identifier must be unique. They are created to give unique name to an
entity to identify it during the execution of the program.
  For example:
  money = 500; age = 24;
  Here money and age are being used as identifiers.

COE 281: Programming and Problem-Solving 25


RULES FOR WRITING IDENTIFIERS

  A valid identifier can have letters (both uppercase and lowercase letters),
digits and underscores.
  It is discouraged to start an identifier name with an underscore.
  An identifier cannot be a Keyword.
  Giving a meaningful name to an identifier, makes it easy for you and other
programmers to understand your code.
  For example storing the age of friend using an identifier called age instead afa

COE 281: Programming and Problem-Solving 26


COMMENTS

  A comment is a note to yourself (or others) that you put into your source
code.
  All comments are ignored by the compiler. They exist solely for your
benefit.
  Comments are used primarily to document the meaning and purpose of
your source code, so that you can remember later how it functions and
how to use it.

COE 281: Programming and Problem-Solving 27


COMMENTS

  Comments are however not mandatory. A developer may decide not to


include them in code.
  However, it is good programming practice to include comments.
  In C, the start of a comment is signaled by the /* character pair. A
comment is ended by */
  For example, this is a syntactically correct C comment: /* This is a
comment. */

COE 281: Programming and Problem-Solving 28


COMMENTS

§ Comments that span over multiple lines can be encapsulated just as


shown earlier.
§ However, single line comments can be started with // such as:
§ //This is a single line comment

COE 281: Programming and Problem-Solving 29


MY FIRST PROGRAM – HELLO WORLD

§ The Code on the next slide is an example of a simple program to get you
started in C.

COE 281: Programming and Problem-Solving 30


MY FIRST PROGRAM – HELLO WORLD
/*
Finally My Very
first C program
*/
#include <stdio.h>
int main()
{
printf("Hello, World!\n"); // displays Hello, World! on the screen
return 0;
}
COE 281: Programming and Problem-Solving 31
MY FIRST PROGRAM – HELLO WORLD

  In the code above it is very obvious that some of the text in there are just
comments.
  Can you identify any multi-line and/or single line comments?

COE 281: Programming and Problem-Solving 32


#INCLUDE <STDIO.H>

  The program seen above displays to the screen the text “Hello World!”
  To do this successfully the program needs to speak to the computer’s
hardware responsible for display.
  This requires an elaborate sequence of actions. However, this heavy lifting
has been done for us and bundled in a Header File called stdio (Standard
Input/Output) header file.

COE 281: Programming and Problem-Solving 33


#INCLUDE <STDIO.H>

  The .h simply connotes that this is a header file.


  This header file contains a library of functions responsible for input and output
operations, such as displaying to the computer screen using the printf (print function).
  So to use any of these functions one must first include it into the code using the #include
preprocessor command.
  Read on preprocessor commands later.

COE 281: Programming and Problem-Solving 34


THE MAIN() FUNCTION

  In C programming, the code execution begins from the start of main()


function (doesn’t matter if main() isn’t located at the beginning of the
code).
  The code inside the curly braces { } is the body of main() function.
  The main() function is mandatory in every C program.

COE 281: Programming and Problem-Solving 35


THE MAIN() FUNCTION

  The data type int that comes before the main() function is simply to cater
for the returning of the integer 0 from the statement return 0.
  A lot more would be shared on the returning value when studying the topic
FUNCTIONS.

COE 281: Programming and Problem-Solving 36


THE PRINTF() FUNCTION

  The printf( ) is a library function that sends formatted output to the screen
(displays the string inside the quotation marks).
  Notice the semicolon(;) at the end of the statement.
  In our program, it displays Hello, World! on the screen.
  Remember, you need to include stdio.h file in your program for this to
work.

COE 281: Programming and Problem-Solving 37


THE RETURN STATEMENT

  The return statement return 0; inside the main() function ends the
program.
  This statement isn’t mandatory. However, it’s considered good
programming practice to use it.

COE 281: Programming and Problem-Solving 38


ESCAPE SEQUENCES

  You must have noticed a \n pair of characters inside the printf() function.
This pair of characters is an example of an Escape Sequence.
  The combination of these characters provide a special effect. For example
the \n produces a newline (like the enter key) and the \t produces a
horizontal space (like the tab key)

COE 281: Programming and Problem-Solving 39


ESCAPE SEQUENCES

Escape Sequence Character Escape Sequence Character

\b Backspace \\ Backslash

\f Form feed \’ Single quotation mark

\n Newline \” Double quotation mark

\r Return \? Question mark

\t Horizontal tab \0 Null character

\v Vertical tab

COE 281: Programming and Problem-Solving 40


RUNNING THE CODE

  The code presented above would be typed into a favourable Integrated


Development Environment (IDE) that has support for C, for example
CodeBlocks.
  After saving the code with a file name having an extension .c the code
would be built and ran/executed.
  After running the code the result would be displayed on the screen.

COE 281: Programming and Problem-Solving 41


RUNNING THE CODE

COE 281: Programming and Problem-Solving 42


RUNNING THE CODE

COE 281: Programming and Problem-Solving 43


THE END
ANY QUESTIONS?

Contact: tsadjaidoo@knust.edu.gh
Office: Caesar Building, Room 413

COE 281: Programming and Problem-Solving 44

You might also like