Chap 1 Computer Programming
Chap 1 Computer Programming
CSC1261
C programming @2017-2018
Chapter 1 Introduction
1.1 General Programming Concepts
1.2 Stages in Program Development
1.3 Debugging the Computer Program
1.4 C Fundamentals
C programming @2017-2018
1.1 GENERAL PROGRAMMING CONCEPTS
C programming @2017-2018
a. Programming Languages Description
• A computer program can be defined as a set of step-by-
step computer instructions that are designed to tell a
computer what and how to accomplish a task.
C programming @2017-2018
Programming Languages categories
1. Low level language: is a programming language that is
machine dependent, it runs on only on particular type of
computer.
– e.g:
✓ A- for Addition
✓ C- for Compare
✓ L- for Load
✓ M- for Multiply
C programming @2017-2018
Programming language categories
2. High-level language
• A programming language such as C, FORTRAN, or Pascal that
enables a programmer to write programs that are more or
less independent of a particular type of computer.
C programming @2017-2018
Advantages of high level programming
1. The main advantage of high-level languages over low-level
languages is that they are easier to read, write, and maintain.
C programming @2017-2018
Compilation Terminologies
• A program that is written in high-level language, must be translated
into machine language before it can be executed.
• Compilers
– The compiler translates the entire program (written in high-level
language) into machine language before executing any set of the
instructions.
C programming @2017-2018
Compilation Terminology
• Interpreter
– An interpreter reads only one line of a source code at a time and
converts it to object codes.
– In case of any errors, the same will be indicated instantly.
– The program written with an interpreter can easily be read and
understood by the other users as well.
C programming @2017-2018
Comparison between Interpreter and Compiler
Interpreter Compiler
C programming @2017-2018
1.2 STAGES IN PROGRAM DEVELOPMENT
C programming @2017-2018
CONT’D…
a. Compile (syntax) error: you get it when you’ve broken the
rules of computer programming language.
E.g: Spelling printf as prinntf or printif
➢ You also receive a compiler error, if you accidentally use the
wrong punctuation or place a punctuation in the wrong place.
b. Run-time error: it can be caused by attempting to do
impossible arithmetic operations, such as calculating non-
numeric data, dividing a number by zero, or find the square
root of a negative number.
c. Logic error: with this, your application runs but produces
incorrect result.
➢ Perhaps the results of calculation are incorrect or the wrong text
appears, or the text is ok but appears in the wrong location.
C programming @2017-2018
FLOWCHART
• A flowchart: A flow chart is a graphical or symbolic
representation of a process.
C programming @2017-2018
Different flow chart symbols
C programming @2017-2018
Different flow chart symbols
C programming @2017-2018
Example
• A flowchart to compare two numbers and display the greatest
Start
A,B
A,B
No Yes
A>B
B A
End
C programming @2017-2018
1.4 C FUNDAMENTALS
• C is a computer programming language.
• C is the most popular programming languages developed in
1972 by Dennis Ritchie.
• C is one of thousands of programming languages currently in
use.
• To write and run a C program, you must have access to a C
compiler.
C programming @2017-2018
How C is applied in our daily basis
1. It is used in making computer games.
2. Used for developing operating systems.
3. Used in commercial company for robot control mechanism
using micro controller system.
4. Used to control traffic light system. Using loop structures.
5. Used in motor system control for increase the speed and
velocity of a motor.
C programming @2017-2018
Advantages of C
➢ C is portable
➢ C is Powerful and Flexible
➢ C has few keywords
➢ Wide acceptability (Popularity)
C programming @2017-2018
C is portable
• C program written for one computer system (an IBM PC, for
example) can be compiled and run on another system with a
little or no modification.
C programming @2017-2018
C is Powerful and Flexible
• What you can accomplish is limited only by your imagination.
The language itself places no constraints on you.
C programming @2017-2018
C has few keywords
• The following names are reserved by the C language.
C programming @2017-2018
Wide acceptability (Popularity)
C programming @2017-2018
Program Development Cycle
1. Editing(or writing) the program
2. Compiling it
3. Linking it
Executing Editing
4. Executing it
Linking Compiling
C programming @2017-2018
Step 1
• Use an editor to write your source code(the program).
• By tradition C source code files have the extension .c
• That means that you have to save the source code with .c
extension. ex: Hello.c
C programming @2017-2018
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 Hello.c to
Hello.obj).
C programming @2017-2018
Step 3
• Link the program with a Linker.
• If no errors occur, the Linker produces an executable program
with an .exe extension and the same name as the object file.
hello.c hello.exe
Compiler Linker
hello.obj
C programming @2017-2018
Steps 4
• Execute the program to see the results.
C programming @2017-2018
STRUCTURE OF A C PROGRAM
• Include header file section
• Global Declaration Section
• /* comments */
main() Function name
{
/* comments*/
Declaration part
Executable part
}
User-defined functions{
}
C programming @2017-2018
STRUCTURE OF A C PROGRAM
• Consist of the following key parts:
1. Include header file section.
2. Global declaration.
3. Function main.
4. Declaration part.
5. Executable part.
6. User- defined function.
7. Comments.
C programming @2017-2018
STRUCTURE OF A C PROGRAM
1. Include header file section: (Link Section)
• C program depends upon some header files for function
definition that are used in program. Each header file by
default is extended with .h. The file should be included using
# include directive as given below.
• For Example
# include<stdio.h>
# include<math.h>
C programming @2017-2018
STRUCTURE OF A C PROGRAM
2. Global declaration: This section declares some variables that
are used in more than one function. These variables are
known as global variables. This section must be declared
outside of all the functions.
3. Function main: Every program written in C language must
contain main () function. Empty parentheses after main are
necessary. The function main () is a starting point of every
‘C’ program. The execution of a program always begins with
the function main (). (A function is a self-contained block of
statements that perform coherent task).
4. The program execution starts from the opening brace ({ )
and ends with the closing brace ( }). Between these two
braces the program should declare the declaration and the
executable part.
C programming @2017-2018
5. Declaration part: The declaration part declares the entire
variables that are used in executable part.
• The initialization of variables are also done in this section.
• The initialization means providing initial values to the
variables.
6. Executable part: This part contains the statements following
the declaration of the variables.
• It contains a set of statements or a single statement. These
statements are enclosed between the braces. Eg. printf(“…..”),
scanf(“….”)
C programming @2017-2018
7. User-defined function: The functions defined by the user are
called user-defined functions. These functions are generally
defined after the main () function. They can also be defined
before main () function.
C programming @2017-2018
The Simplest C Program
#include <stdio.h>
main()
{
printf("Hello!\n");
}
Program Output:
Hello!
C programming @2017-2018
Second Program
#include <stdio.h>
main()
{
printf("Hello, software\n");
}
• Program Output:
Hello, software
C programming @2017-2018
Third program
#include <stdio.h>
main()
{
printf("Hello,\n software\n development\n");
}
• Output
Hello,
software
development
C programming @2017-2018
Other special notation
C programming @2017-2018
Comment example
#include<stdio.h>
main()
/* main function heading */
{
printf("\n Hello, World! \n");
/* Display message on */
/* the screen * /
}
C programming @2017-2018
Exercises
• Write a c program which will print the
following out put :
• Question 1:
• Define the following word :
”programming”
• Question 2:
– a=b+c a b c d
– b=a/5
C programming @2017-2018
• Question 3:
– Write a C program to display your personal identification
such as Names, Registration number, Year of study,
Department, Email, District of college location and
telephone number.
C programming @2017-2018
End
C programming @2017-2018