Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
23 views

Computer Programming in C

This course introduces students to computer programming in C. It is designed for engineering students to learn how to conceive, create, construct and write basic computer programs in the C programming language. By the end of the course, students will understand key concepts of algorithms, functions, control structures, data types and be able to write and debug simple C programs. The course will be taught using the C by Example textbook and make use of the Ubuntu Linux operating system and GCC compiler.

Uploaded by

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

Computer Programming in C

This course introduces students to computer programming in C. It is designed for engineering students to learn how to conceive, create, construct and write basic computer programs in the C programming language. By the end of the course, students will understand key concepts of algorithms, functions, control structures, data types and be able to write and debug simple C programs. The course will be taught using the C by Example textbook and make use of the Ubuntu Linux operating system and GCC compiler.

Uploaded by

King Ebuka
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 39

Computer 

Programming in C

By
Mr. Harry & Damen

   
Overview

This course is designed for Engineering students.

By the end of the course, students should be able to conceive, 
create, construct and write basic computer programs in the C 
programming language.

Book
C By Example by Noel Kalicharan

Operating System / Environment
Ubuntu Linux / Gnu Common Compiler (gcc)

   
Outline
Introduction
Algorithms
Basics of Functions and Operators
Control Flow Constructs
The C Preprocessor
Data types and storage classes
Character handling and strings
Pointers
Basic Structures and Linked Lists
File Input / Output
Binary Trees and Other Structures

   
Introduction

● What is computer programming?
● Why study computer programming as an Engineer?
● What are computer programs?
● How are computers programmed?
● What are programming languages?

   
Introduction

● How to write a program
● How to compile a program
● Running the program
● Debugging the program

   
How to write a program
● Keywords in C (reserved words)
● Basic Data types
● Variables and their values
● Operators
● Expressions and statements
● Functions and header files
● A word on program layout
   
How to write a program

● Keywords in C (reserved words)
auto break case char const continue
default do double else enum extern
float for goto if int long
register return short signed sizeof static
struct switch typedef union unsigned void
volatile while

All reserved words should be written in lower case.

   
How to write a program
● Basic Data types
● char – A single character enclosed by single quotes (apostrophes) e.g 
'a', 'd', '9', '\n', '\''...
● int  –  integer  (whole  number)  e.g  25,  99,  0,  ­34,  235...  Octal  constants 
begin  with  leading  0  e.g  59  =  073.  Hexadecimal  constants  begin  with  a 
leading 0x (0X) e.g 0x3b or 0x3B
● double – double­precision floating point number. Written with a decimal 
point (78.5, ­58.9) with exponential e or E (0.58e89, 1.5E­9)
● float – single­precision floating point. Written by adding suffix f or F to a 
double constant (75.8F, 0.5e2f)
   
How to write a program
● Variables and their values
Variables  are  denoted  by  Identifiers.  An  Identifier  begins  with  a 
letter or underscore(_). In ANSI C, only the first 31 characters of an 
identifier's  name  are  significant.  Both  upper  &  lower  case  letters 
may be used. e.g maxStudentNumber, _score, ...
Variable  names  should  not  be  the  same  as  reserved  words. 
Variables have types and are declared (defined) as such
<data type> <variable name>; e.g int maxLength;

Analogy to variables in mathematics
   
How to write a program
●Qualifiers
Integers(int),  characters(char)  and  doubles  can  be  qualified  in 
declarations  by  one  of  a  combination  of  the  following  reserved 
words (qualifiers): short, long, signed, unsigned. e.g
short int numTeams;
long int gross;
unsigned int count;
short unsigned int numStudents;

Assuming  that  a  char  is  stored  in  8bits,  an  unsigned  char can  assume values  from  0  to 
255 while a signed char can assume values from ­128 to 127.
Long double gives twice  the precision as double.
   
How to write a program
●Operators and Operands
Arithmetic  –  They  include  addition(+),  subtraction(­), 
multiplication(*), division(/), modulus(%).
Assignment  –  used  to  assign  the  value  of  an  expression  to  a 
variable. (=, +=, ­=, *=, /=, %=)  
variable op= expression is equivalent to
variable  = variable op expression.   e.g c += 4 ≡ c = c + 4.
Relational  –  used  for  comparing  quantities.  They  include  equal 
to(==),  not  equal  to(!=),  greater  than(>),  less  than(<),  less  than  or 
equal to(<=), greater than or equal to(>=).
   
How to write a program
● Operators and Operands

Logical  –  used  for  forming  compound  conditions.  They  include   


and(&&), or(||), and not(!).
Increment / Decrement – Increment by 1 (++), decrement by 1(­­).
++n and n++ are both equivalent to n = n+1;
­­n and n­­ are both equivalent to n = n­1;
if  the  value  of  n  is  5,  then  ++n  increases  n  to  6  before  using  its 
value whereas n++ uses the value before increasing it.
   
How to write a program

● BoDMAS (Brackets of Division, Multiplication, Addition and Subtraction)
a = b = c = 12  /*Assignment operator associate from right to left a = (b = (c = 12))*/
a == b == c  /*Compare operator associates as such (a == b) == c 
                             better (a == b)&&(c == c)*/

a && b || c  /* ! has a higher precedence than && which is higher than || */

   
How to write a program

● Expressions and statements
An expression consists of operands and operators. 
e.g y = m*x + c, length  > width, aSalary = mSalary*numMonth + tBonus, ...

A simple statement is an expression followed by a semicolon. 
e.g y = m*x;

A  compound  statement  begins  with  an  open  brace  ({)  followed  by 
any number of simple statements and ends with a close brace (})
e.g {count = 0; max = 5; return 0;}

   
How to write a program
●Functions and header files
Functions  are  building  blocks  from  which  larger  programs  are 
constructed.  (dividing  large  jobs  into  smaller  once)  Analogous  to 
functions in mathematics.
An integral part of any C programming system is the provision of a 
library of functions (at least I/O functions)
Each  class  of  functions  has  a  Header  file  which  contains  the 
function  declaration,  together  with  other  variables  and  constants. 
e.g ctype.h – declarations for character handling functions
      stdio.h ­  declarations for input / output handling functions.
   
How to write a program

● A word on program layout
In general, C does not require the program to be laid out in a particular way. 
In the layout of your program text, the following should be considered:

Comments – /* Every thing between these symbols is considered as comments */
● Documentation  –  The  habit  of  including  author's  name,  program  name,  date, 
modification history etc  at the beginning of the program text as comments.
● Indentations – Use to group statement blocks.
● Flow and readability – Ensure that your program reads logically from left to 
right, top to bottom.

   
The program text

/******************************************************/
/* Program title: Welcome */
/* Authors:  Harry and Damen */
/* Date:  26 October 2010 */
/* */
/******************************************************/

#include <stdio.h> /*  includes the standard I/O header file  */
main() /*  Calls the main function  */
{
printf(“Welcome to CEF207\n”);  /*   \n – newline character */
}

   
How to compile a program
● The program text
● The Text editor
● The compiler
● The Object file
● The linker
● The loader
● The executable program
   
How to compile a program

harry@ubuntu:~$ cd Desktop

harry@ubuntu:~/Desktop$ cd CEF207

harry@ubuntu:~/Desktop/CEF207$ gcc ­c welcome.c

harry@ubuntu:~/Desktop/CEF207$ gcc ­o welcome welcome.o

harry@ubuntu:~/Desktop/CEF207$ ./welcome

   
Running the program

● Ways of running a program
● Requirements for running a program
● Environmental and hardware requirements

● The input of a program
● The output of a program
● Program termination

   
Debugging the program
A debugger program

The executable program

Patience to handle the frustration

Stepping through the program code while 

observing its progress.
 Identify errors and make corrections in the 

program text.
Recompile the program text

   
Algorithms

● What are algorithms?
● Why are algorithms relevant to computer programming?

   
Definition of an Algorithm

●Informal:  An  ordered  sequence  of  instructions 


(operations) that is guaranteed to solve a specific 
problem

●Formal:  A  well­ordered  collection  of 


unambiguous  and  effectively  computable 
operations that, when executed, produces a result 
and halts in a finite amount of time.

   
Properties of an Algorithm
1. Precision. The steps are precisely stated.
2. Uniqueness. The result of executing each step is uniquely 
   determined by the inputs and the result of preceding steps.
3. Finiteness. The algorithm stops after finitely many instructions
   have been executed.
4. Input. The algorithm receives input.
5. Output. The algorithm produces output.
6. Generality. The algorithm applies to a set of inputs.

   
Representation of Algorithms
● An  algorithm  can  be  represented  in  a  natural 
language such as English, French, Pidgin, etc. 
● It  can  also  be  represented  as  a  computer 
program or even as a hardware design.
● There  are  two  popular  ways  of  show  the 
representation  of  algorithms:  flow  diagrams  (flow 
charts) and pseudo­codes.
   
Flow Diagram Symbols

   
Flow Diagram ­ Example

   
Pseudo­code

   
Pseudo­code ­ Example

   
Basics of Functions and Operators

● What are functions?
● What are operators?
● How are they used in computer programming?

   
Control Flow Constructs

● What are control flow constructs?
● What  are  some  examples  of  control  flow  constructs  in  computer 
programming?

   
Control Flow Constructs

● The if...else statement
● The while statement
● The for statement
● The do..while statement
● The switch statement
● The continue statement
   
The C Preprocessor

● What is a processor?
● What is a preprocessor?
● What is the use of #include<stdio.h> in a C program?

   
Data types and storage classes

● What are data types?
● What are storage classes?
● Give examples of data types and storage classes.

   
Character handling and strings

● What are strings?
● How  are  characters  and  strings  handled  (declared,  stored,   
manipulated and represented) in a computer program?

   
Pointers

● What are pointers?
● Why are pointers necessary in computer programming?

   
Basic Data Structures and Linked Lists

● What are data structures?
● What are linked lists?

   
File Input / Output

● What is a File?
● How is data stored / read from a file?

   
Binary Trees and Other Data Structures

● What are Binary trees?
● What other Data structures exist?

   

You might also like