0% found this document useful (0 votes)
0 views

1 Topic One - Introduction

The document provides an introduction to computer programming concepts, focusing on the C programming language, its history, and its development life cycle. It outlines the components of a C program, including functions, variables, and key statements like printf() and scanf(). Additionally, it covers programming keywords, escape sequences, and provides examples and exercises to reinforce learning.

Uploaded by

tikoj35
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
0 views

1 Topic One - Introduction

The document provides an introduction to computer programming concepts, focusing on the C programming language, its history, and its development life cycle. It outlines the components of a C program, including functions, variables, and key statements like printf() and scanf(). Additionally, it covers programming keywords, escape sequences, and provides examples and exercises to reinforce learning.

Uploaded by

tikoj35
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 47

COMPUTER PROGRAMMING CONCEPTS

Facilitator: Enlai Watson (0752626662)

1
References
• C Programming For The Absolute Beginner,
Second Edition. Michael Vine
• C For Dummies 2nd Edition. Dan Gookin

2
Topic One: Introduction

3
What is a Computer?
• An electronic device for storing and processing
data, typically in binary form, according to
instructions given to it in a variable program.
• From the above definition we see that
computer can process data in binary form only
(series of 0s and 1s).

4
Computer Programming
• Programming languages bridge the gap between human
thought processes and computer binary circuitry.
• Computer programming (often shortened to programming) is a
process that leads from an original formulation of a computing
problem to executable computer programs.
• Programming language: A series of specifically defined
commands designed by human programmers to give directions
to digital computers.
• Commands are written as sets of instructions, called programs.
• All programming language instructions must be expressed in
binary code before the computer can perform them.

5
C Programming Language
• C programming language is a general-purpose, high-level
language that was originally developed by Dennis M.
Ritchie to develop the UNIX operating system at Bell Labs.
• It was originally first implemented on the DEC PDP-11
computer in 1972.
• In 1978, Brian Kernighan and Dennis Ritchie produced the
first publicly available description of C, now known as the
K&R standard.
• The UNIX operating system, the C compiler, and essentially
all UNIX applications programs have been written in C.

6
• C was invented to write an operating system
called UNIX.
• C is a successor of B language, which was
introduced around 1970.
• The UNIX OS was totally written in C by 1973.
• Today, C is the most widely used and popular
System Programming Language.
• Most of the state-of-the-art software have been
implemented using C.
• Today's most popular Linux OS and RDBMS
MySQL have been written in C.
7
• The C has now become a widely used
professional language for various reasons
which are;
o Easy to learn
o Structured language
o It produces efficient programs.
o It can handle low-level activities.
o It can be compiled on a variety of computer
platforms.
8
C development life cycle
Step 1: Use an editor to write your source code. By
tradition, C source code files have the extension .C
(for example, MYPROG.C, DATABASE.C, and so on).
The source code file is a text file on disk. The file
contains instructions for the computer that are
written in the C programming language.

9
• Step 2: Compile the program using a compiler. If the
compiler doesn't find any errors in the program, it
produces an object file. The compiler produces object
files with an .OBJ extension and the same name as
the source code file (for example, MYPROG.C
compiles to MYPROG.OBJ). If the compiler finds
errors, it reports them. You must return to step 1 to
make corrections in your source code.
• Compiler is a special program that reads the
instructions stored in the source code file, examines
each instruction, and then translates the information
into the machine code understood only by the
computer’s microprocessor.
10
Step 3: Link the program using a linker. If no errors
occur, the linker produces an executable program
located in a disk file with an .EXE extension and the
same name as the object file (for example,
MYPROG.OBJ is linked to create MYPROG.EXE).

11
• Step 4: Execute the program. You should test
to determine whether it functions properly. If
not, start again with step 1 and make
modifications and additions to your source
code.

12
Example 1
#include <stdio.h>
main( )
{
printf(“This is my first C - program”);
return 0;
}

13
Example 2
#include <stdio.h>
main( )
{
printf(“Hello World”);
printf(“This is my first C - program”);
}

14
Exercise
Write a program to display First and Second
names, Age, Gender and address. Save the file
as MyParticulars

15
The Components of a C Program

• A Short C Program
• The Program's Components
– The main() Function
– The #include Directive
– The Variable Definition
– Program Statements
– The Function Definition
– Program Comments
– Braces
– Running the Program
16
Every C program consists of several components
combined in a certain way. To get the overall
picture, however, you should begin by seeing a
complete (though small) C program with all its
components identified.

17
A Short C Program

• The program below presents the source code


for MULTIPLY.C. This is a very simple program.
All it does is input two numbers from the
keyboard and calculate their product. At this
stage, don't worry about understanding the
details of the program's workings. The point is
to gain some familiarity with the parts of a C
program.

18
Before looking at the sample program, you need to know
what a function is, because functions are central to C
programming.
A function is an independent section of program code
that performs a certain task and has been assigned a
name. By referencing a function's name, your program
can execute the code in the function. The program also
can send information, called arguments, to the function,
and the function can return information to the main part
of the program.
The two types of C functions are library functions, which
are a part of the C compiler package, and user-defined
functions, which you, the programmer, create. You will
learn about both types of functions in the next sections.19
/*The program to calculate the product of two numbers. */
#include <stdio.h>
main()
{
int a, b, c;
/* Input the first number */
printf("Enter a first number: ");
scanf("%d", &a);
/* Input the second number */
printf("Enter another number: ");
scanf("%d", &b);
/* Calculate and display the product */
c = a*b;
printf (“The product of two numbers is %d”, c);
return 0;
20
}
The Program's Components

The following are the various components of the


preceding sample program.

21
The main() Function

The only component that is required in every C


program is the main() function. In its simplest form,
the main() function consists of the name main
followed by a pair of empty parentheses (()) and a
pair of braces ({}). Within the braces are statements
that make up the main body of the program. Under
normal circumstances, program execution starts at
the first statement in main() and terminates at the
last statement in main().

22
The #include Directive

• The #include directive instructs the C compiler to


add the contents of an include file into your
program during compilation. An include file is a
separate disk file that contains information
needed by your program or the compiler. Several
of these files (sometimes called header files) are
supplied with your compiler. You never need to
modify the information in these files; that's why
they're kept separate from your source code.
Include files should all have an .H extension (for
example, STDIO.H). 23
• You use the #include directive to instruct the
compiler to add a specific include file to your
program during compilation. The #include
directive in this sample program means "Add
the contents of the file STDIO.H." Most C
programs require one or more include files.
• The library name stdio.h is short for standard
input output and contains links to various
standard C library functions, such as printf().
24
The Variable Definition

• A variable is a name assigned to a data


storage location. Your program uses variables
to store various kinds of data during program
execution. In C, a variable must be defined
before it can be used. A variable definition
informs the compiler of the variable's name
and the type of data it is to hold.
• In the sample program, the definition on line,
int a,b,c; , defines three variables--named a, b,
and c--that will each hold an integer value. 25
Program Statements

• The real work of a C program is done by its


statements. C statements display information on-
screen, read keyboard input, perform
mathematical operations, call functions, read disk
files, and carry out all the other operations that a
program needs to perform.
• Remember that in your source code, C statements
are generally written one per line and always end
with a semicolon. The statements in MULTIPLY.C
are explained briefly in the following sections. 26
• Most program statements control program
execution and functionality and may require a
program statement terminator (;).
• Program statements that do not require a
terminator include preprocessor directives,
comment blocks, and function headers.

27
printf()
• The printf() statement is a library function that
displays information on-screen. The printf()
statement can display a simple text message
or a message and the value of one or more
program variables.
• The function is called printf() for a reason. The
f stands for formatted. The advantage of the
printf function over other, similar display-this-
or-that functions in C is that the output can be
formatted.
printf(“format_string”[,var[,...]]);
28
• What appears in the double quotes is really a formatting
string. It’s still text that appears in printf()’s output, but
secretly inserted into the text are various conversion
characters, or special “placeholders,” that tell the printf()
function how to format its output and do other powerful
stuff.
• After the format string comes a comma (still inside the
parentheses) and then one or more items called arguments.
The argument shown in the preceding example is var, which
is short for variable. We can use printf() to display the
content or value of one or more variables. We do this by
using special conversion characters in format string.
29
scanf()

• The scanf() statement is another library


function. It reads data from the keyboard and
assigns that data to one or more program
variables.

30
The return statement

• All functions in C can return values. For instance, when you


make a function to add two numbers, you can make such a
function that returns to you the value of the addition.
• The main() function itself returns a value. By default, main()
returns an integer. In C, integers are decimal numbers without
fraction portions.
• Therefore, in the program, there is a statement, return 0;, that
indicates that 0 is returned from the main() function and the
program is terminated normally.
• A nonzero value returned by the return statement tells the
operating system that an error has occurred. The bigger the
return value, the more severe the error.
31
The Function Definition
• A function is an independent, self-contained section of code that is
written to perform a certain task. Every function has a name, and
the code in each function is executed by including that function's
name in a program statement. This is known as calling the function.
• There are two types of functions: User-defined & Library.
• The user-defined function: As the name implies, user-defined
functions are written by the programmer during program
development.
• The library functions that are a part of the C compiler package.
Library functions perform most of the common tasks (such as
screen, keyboard, and disk input/output) your program needs. In the
sample program, printf() and scanf() are library functions.

32
• Functions allow you to group a logical series of
activities, or program statements, under one
name
• Each function implementation requires that
you use a beginning brace ({) and a closing
brace (})

33
Program Comments
Any part of your program that starts with /* and ends with */ is
called a comment. The compiler ignores all comments, so they
have absolutely no effect on how a program works. You can put
anything you want into a comment, and it won't modify the way
your program operates. A comment can span part of a line, an
entire line, or multiple lines. Here are three examples:
/* A single-line comment */
int a,b,c; /* A partial-line comment */
/* a comment
spanning
multiple lines */

Comments help to identify program purpose and explain complex


routines
34
Braces

• You use braces ({}) to enclose the program


lines that make up every C function--including
the main() function. A group of one or more
statements enclosed within braces is called a
block. As you will see in later chapters, C has
many uses for blocks.

35
Keywords
• There are 32 words defined as keywords in the
C programming language. These keywords
have predefined uses and cannot be used for
any other purpose in a C program. These
keywords are used by the compiler as an aid
to building the program.
• Note that these keywords must always be
written in lowercase

36
Keyword Description
auto Defines a local variable as having a local lifetime
break Passes control out of the programming construct
case Branch control
char Basic data type
const Unmodifiable value
continue Passes control to loop’s beginning default Branch control
do Do While loop
double Floating-point data type
else Conditional statement
enum Defines a group of constants of type int
extern Indicates an identifier as defined elsewhere
float Floating-point data type
for For loop
goto Transfers program control unconditionally
if Conditional statement
int Basic data type
37
long Type modifier
register Stores the declared variable in a CPU register
return Exits the function
short Type modifier
signed Type modifier
sizeof Returns expression or type size
static Preserves variable value after its scope ends
struct Groups variables into a single record
switch Branch control
typedef Creates a new type
union Groups variables that occupy the same storage space
unsigned Type modifier
void Empty data type
volatile Allows a variable to be changed by a background
routine
while Repeats program execution while the condition is true
38
Escape Sequences
• Escape sequences are specially sequenced characters
used to format output.
• When combined with the backslash (\), special
characters such as n, t, r, ‘ and “ make up an escape
sequence.
Escape Sequence Purpose
\n Creates a new line
\t Moves the cursor to the next tab
\r Moves the cursor to the beginning of
the current line
\\ Inserts a backslash
\" Inserts a double quote
\' Inserts a single quote 39
Escape Sequence \n
• This particular escape sequence (\n) tells the
program to add a new line.
• Take a look at the following program
statement. How many new lines are added to
standard output with these printf()
statements?
printf("\nC you later\n");
printf("line 1\nline2\nline3\n");

40
Escape Sequence \t
• Escape sequence \t moves the cursor to the next tab space.
This escape sequence is useful for formatting output in
many ways. For example, a common formatting desire is to
create columns in your output, as the following program
statements demonstrate.
printf("\nSun\tMon\tTue\tWed\tThu\tFri\tSat\n");
printf("\t\t\t\t1\t2\t3\n");
printf("4\t5\t6\t7\t8\t9\t10\n");
printf("11\t12\t13\t14\t15\t16\t17\n");
printf("18\t19\t20\t21\t22\t23\t24\n");
printf("25\t26\t27\t28\t29\t30\t31\n");
41
Escape Sequence \\

• Escape sequence \\ inserts a backslash into


your text. This may seem unnecessary at first,
but remember that whenever the program
reads a backslash in a printf() function, it
expects to see a valid escape character right
after it. In other words, the backslash
character (\) is a special character in the
printf() function; if you need to display a
backslash in your text, you must use this
escape sequence.

42
• The following program statement
demonstrates escape sequence \\. printf("c:\\
cygwin\\bin must be in your system path");
The output is
c:\cygwin\bin must be in your system path

43
Escape Sequence \”
• Another reserved character in the printf()
function is the double quote (") character. To
insert a quote into your outputted text, use
the escape sequence \" as demonstrated in
the following program statement.
printf("\"This is quoted text\"");
• The output is
“This is quoted text”

44
Escape Sequence \’
• Similar to the double quote escape sequence
(\") is the single quote (also called an
apostrophe) escape sequence (\'). To insert a
single quote into your outputted text, use the
escape sequence \' as demonstrated in the
following program statement
printf("\nA single quote looks like \'\n");
• The output is
A single quote looks like ‘
45
Common Errors
• The following are some of the common errors
that many beginner programmers can
encounter during compiling the source code.
– Missing Program Block Identifiers
– Missing Statement Terminators
– Invalid Preprocessor Directives
– Invalid Escape Sequences
– Invalid Comment Blocks

46
• A single error at the top of your program can
cause a lot of errors to be seen during compile
time.
• The best place to start debugging compile
errors is with the first error

47

You might also like