Professional Documents
Culture Documents
2009 Hazizulden Abdul Aziz, Faculty of Electrical & Electronics Engineering, UMP
Course Synopsis
This course introduces computer programming techniques to electrical & electronic engineering students using a structured programming language called C
2009 Hazizulden Abdul Aziz, Faculty of Electrical & Electronics Engineering, UMP
Outcomes
Explain hardware/software interfacing Demonstrate structured programming techniques using high-level language Use computer programming techniques in solving electrical & electronics engineering problem Work in team and communicate effectively
2009 Hazizulden Abdul Aziz, Faculty of Electrical & Electronics Engineering, UMP
Syllabus
Computer programming fundamentals Structured program development 5 elements of computer programming Aggregate data types Files Input and Output Hardware interfacing
2009 Hazizulden Abdul Aziz, Faculty of Electrical & Electronics Engineering, UMP
Assessment
2009 Hazizulden Abdul Aziz, Faculty of Electrical & Electronics Engineering, UMP
References
B.W. Kernighan and D.M. Ritchie, C Programming Language, 2nd Ed, USA: Prentice Hall, 1988
2009 Hazizulden Abdul Aziz, Faculty of Electrical & Electronics Engineering, UMP
Additional References
S.G. Kochan, Programming in C, 3rd Ed., USA: Sams Publishing, 2005 G. Perry, Absolute Beginners Guide to Programming, 3rd Ed., IN: Que, 2003 H.M. Deitel and P.J. Deitel, C: How to Program, 3rd Ed., NJ:Prentice Hall, 2001 J.R. Hanly and E.B. Koffman, C Program Design for Engineers, 2nd Ed., MA: Addison Wesley, 2001
2010 Hazizulden Abdul Aziz, Faculty of Electrical & Electronics Engineering, UMP
Internet Resources
www.cprogramming.com
2010 Hazizulden Abdul Aziz, Faculty of Electrical & Electronics Engineering, UMP
Computer Programming
2009 Hazizulden Abdul Aziz, Faculty of Electrical & Electronics Engineering, UMP
Computer in engineering environment CNC machines PC based integrated system Embedded microprocessor & controller Scientific computing, complex tasking, data analysis, simulation & visualization
2009 Hazizulden Abdul Aziz, Faculty of Electrical & Electronics Engineering, UMP
Inside a Computer
I/O interfaces
Control bus
CPU
Memory (RAM/ROM)
Address Bus
2009 Hazizulden Abdul Aziz, Faculty of Electrical & Electronics Engineering, UMP
The CPU
The brain of a computing devices Registers instruction & memory address ALU (Arithmetic & Logic Units) Clock processor speed Floating points coprocessor
2009 Hazizulden Abdul Aziz, Faculty of Electrical & Electronics Engineering, UMP
Operating software Master controller between hardware and other system application software Application software Written to solve specific problem and/or to simplified work processes
2009 Hazizulden Abdul Aziz, Faculty of Electrical & Electronics Engineering, UMP
Computer Languages
Means of communicating with computers Sequence of instructions for CPU Three types Machine Assembly High Level
2009 Hazizulden Abdul Aziz, Faculty of Electrical & Electronics Engineering, UMP
Machine Language
Built-in to the CPU Binary code Alternative codes: Octal Hexadecimal bits & bytes
Example:
00000000 00010101 00010110 00110101 01110111
2009 Hazizulden Abdul Aziz, Faculty of Electrical & Electronics Engineering, UMP
Assembly Language
Example:
CLR MOV A,#2 ADD A,#2 MOV R0,A END
2009 Hazizulden Abdul Aziz, Faculty of Electrical & Electronics Engineering, UMP
High Level Language Fortran (1950s) Basic (1960s) Pascal (1960s) C (1970s)
2009 Hazizulden Abdul Aziz, Faculty of Electrical & Electronics Engineering, UMP
C Programming Language
History 1972 by Dennis Ritchie Based on B/BCPL OS language ANSI/ISO C (1989) Evolution
C advantages Powerful language Lean & efficient Less rigid structure Support bitmanipulation
2009 Hazizulden Abdul Aziz, Faculty of Electrical & Electronics Engineering, UMP
2009 Hazizulden Abdul Aziz, Faculty of Electrical & Electronics Engineering, UMP
Compiler Translate code to machine language Linker Built executable program file
2009 Hazizulden Abdul Aziz, Faculty of Electrical & Electronics Engineering, UMP
Errors in Programming
Compilation error
Logic error
Programmer error Not easy to identify and correct May cause run-time error
2009 Hazizulden Abdul Aziz, Faculty of Electrical & Electronics Engineering, UMP
Development Environment
What you needs? A source code editor A compiler and linker An optional debugger Integrated Development Environment IDE All-in-One application (programming) software
2009 Hazizulden Abdul Aziz, Faculty of Electrical & Electronics Engineering, UMP
DEVCpp Compiler
OSS License compiler IDE for ANSI C/C++ developer Source editor Compiler & Linker Debugger
2009 Hazizulden Abdul Aziz, Faculty of Electrical & Electronics Engineering, UMP
2009 Hazizulden Abdul Aziz, Faculty of Electrical & Electronics Engineering, UMP
Outcomes
Understand the needs to learn computer programming List the three levels of computer programming languages Explain how high level languages are translated to machine language Use DEVCpp and/or Borland C++ compilers IDE features
2009 Hazizulden Abdul Aziz, Faculty of Electrical & Electronics Engineering, UMP
Structured Programming
2009 Hazizulden Abdul Aziz, Faculty of Electrical & Electronics Engineering, UMP
What is a Program?
A set of instruction for computer to execute A collection of instructions necessary to solve a specific problem SOFTWARE or FIRMWARE?
2009 Hazizulden Abdul Aziz, Faculty of Electrical & Electronics Engineering, UMP
Programming Paradigm
2009 Hazizulden Abdul Aziz, Faculty of Electrical & Electronics Engineering, UMP
Structured Programming
Disciplined approach to writing program that are clear, correct & easy to maintain Sequential, procedural or functional Top-Down approach to programming
2009 Hazizulden Abdul Aziz, Faculty of Electrical & Electronics Engineering, UMP
Program Execution
Computers execute instruction line-by-line basis Instructions and data are stored in allocated memory addresses Information stored in memory address is accessed by fetch-execute cycle Program Counter (PC) store current memory address information Registers store current instruction and data executed
2009 Hazizulden Abdul Aziz, Faculty of Electrical & Electronics Engineering, UMP
2009 Hazizulden Abdul Aziz, Faculty of Electrical & Electronics Engineering, UMP
Program Construction
Start with problem statement Define requirements and test plan Develop Algorithm Create source file Compile and make correction Execute, test and debug
2009 Hazizulden Abdul Aziz, Faculty of Electrical & Electronics Engineering, UMP
Understand what is the problem Define scope and generality Define inputs & outputs Define constants & formula Define computational needs
2009 Hazizulden Abdul Aziz, Faculty of Electrical & Electronics Engineering, UMP
Contains list of inputs & correct outputs Test normal & abnormal conditions Test extreme & out-of-bound data
2009 Hazizulden Abdul Aziz, Faculty of Electrical & Electronics Engineering, UMP
Design a Solution
Algorithm to consider Interaction user & program Fool proofing against illegal inputs
2009 Hazizulden Abdul Aziz, Faculty of Electrical & Electronics Engineering, UMP
Algorithm
Method for solving well structured problem Sequence of steps to accomplish specific task
2009 Hazizulden Abdul Aziz, Faculty of Electrical & Electronics Engineering, UMP
Representing Algorithm
Pseudo Code Algorithmic language Code without syntax May use borrowed syntax Easily translate
2009 Hazizulden Abdul Aziz, Faculty of Electrical & Electronics Engineering, UMP
input/output
Off-page connector
decision
2009 Hazizulden Abdul Aziz, Faculty of Electrical & Electronics Engineering, UMP
Pseudo-code statement
Simple English Gets first number Gets second number Add the two numbers Show result
2009 Hazizulden Abdul Aziz, Faculty of Electrical & Electronics Engineering, UMP
Steps
Start A Problem Statement
Compile
Conceptual design Test Algorithm design Solution Source Code End
2009 Hazizulden Abdul Aziz, Faculty of Electrical & Electronics Engineering, UMP
Outcomes
Explain the structured programming approach Write appropriate problem definition and test plan Develop simple algorithm to solve basic arithmetic operation
2009 Hazizulden Abdul Aziz, Faculty of Electrical & Electronics Engineering, UMP
C Programming Language
2009 Hazizulden Abdul Aziz, Faculty of Electrical & Electronics Engineering, UMP
C Program Elements
2009 Hazizulden Abdul Aziz, Faculty of Electrical & Electronics Engineering, UMP
Pre-Processor
Analyze source file before compiling Process pre-processor directive Remove comments Divide program into tokens Result in more efficient & clearer code
2009 Hazizulden Abdul Aziz, Faculty of Electrical & Electronics Engineering, UMP
Pre-processor
Compiler
2009 Hazizulden Abdul Aziz, Faculty of Electrical & Electronics Engineering, UMP
Pre-Processor Directive
Begin with # character Call up essential definitions & libraries Pre-processor directives: #include includes header files #define define constant & macros
2009 Hazizulden Abdul Aziz, Faculty of Electrical & Electronics Engineering, UMP
Declaration
Define attribute (data type) of identifiers used Must precede executable statements Global vs. Local declaration
2009 Hazizulden Abdul Aziz, Faculty of Electrical & Electronics Engineering, UMP
Global variable - declared outside any function Local variable - declared inside a function Local variable take precedence over global variable AVOID using global variables
2009 Hazizulden Abdul Aziz, Faculty of Electrical & Electronics Engineering, UMP
Keywords
Reserved words Predefined by compiler libraries Cannot be used as identifiers Must be written in lowercase Instruction set
2009 Hazizulden Abdul Aziz, Faculty of Electrical & Electronics Engineering, UMP
Statements
Sequence of instructions Branching or looping or Function calls Start with a command word and ends with ; Compounded by curly brackets { }
2009 Hazizulden Abdul Aziz, Faculty of Electrical & Electronics Engineering, UMP
Comments
Use to clarify program statements Make program code more readable Start with /* and end with */ Can be included anywhere in the program
2009 Hazizulden Abdul Aziz, Faculty of Electrical & Electronics Engineering, UMP
C Program Structure
Pre-processor directives
Global declaration
main () {
Local declaration
Executable statements }
2009 Hazizulden Abdul Aziz, Faculty of Electrical & Electronics Engineering, UMP
2009 Hazizulden Abdul Aziz, Faculty of Electrical & Electronics Engineering, UMP
Outcomes
Explain each element of C Language structure Describe how pre-processor works in C Outline basic structure of C Language
2009 Hazizulden Abdul Aziz, Faculty of Electrical & Electronics Engineering, UMP
Programming Fundamentals
2009 Hazizulden Abdul Aziz, Faculty of Electrical & Electronics Engineering, UMP
Declaration statements Action statements Selection statements Repetition statements Invocation statements
2009 Hazizulden Abdul Aziz, Faculty of Electrical & Electronics Engineering, UMP
Declaration Statement
Associates memory location to identifier Associates identifier to its attribute and data type Variable Constant Function
2009 Hazizulden Abdul Aziz, Faculty of Electrical & Electronics Engineering, UMP
ANSI C Identifiers
Alphabets, digits and underscore Cannot begin with digit Case sensitive Unspecified length
Example:
2009 Hazizulden Abdul Aziz, Faculty of Electrical & Electronics Engineering, UMP
Data types
2009 Hazizulden Abdul Aziz, Faculty of Electrical & Electronics Engineering, UMP
Variables Declaration
Syntax
2009 Hazizulden Abdul Aziz, Faculty of Electrical & Electronics Engineering, UMP
Initialization
Give initial value to variables data type Initialization statement In declaration statement Just before using the variable
2009 Hazizulden Abdul Aziz, Faculty of Electrical & Electronics Engineering, UMP
Assignment
= symbols (GETS) Assign a value to an identifier Syntax: identifier = value; identifier = expression;
2009 Hazizulden Abdul Aziz, Faculty of Electrical & Electronics Engineering, UMP
Constant Declaration
#define
const
2009 Hazizulden Abdul Aziz, Faculty of Electrical & Electronics Engineering, UMP
Action Statements
Input /Output statements Expression statements Arithmetic operations Algebraic expression Trigonometry functions
2009 Hazizulden Abdul Aziz, Faculty of Electrical & Electronics Engineering, UMP
Input/Output Statements
2009 Hazizulden Abdul Aziz, Faculty of Electrical & Electronics Engineering, UMP
C Input/Output Statements
Pre-processor functions library #include <stdio.h> Input and output instruction in C... puts printf scanf
2009 Hazizulden Abdul Aziz, Faculty of Electrical & Electronics Engineering, UMP
Syntax: puts
puts(string);
Outputs a text string constant to the standard output device Example: puts(Computer Programming);
2009 Hazizulden Abdul Aziz, Faculty of Electrical & Electronics Engineering, UMP
Syntax: printf
printf(format,argument);
Outputs a formatted text string to the output in a form defined by format using arguments argument Example:
2009 Hazizulden Abdul Aziz, Faculty of Electrical & Electronics Engineering, UMP
Formatting Operators
Operator %c %d %e %f %u Format Single character Signed decimal integer Scientific floating point Floating point Unsigned decimal integer Operator %s %o %% %x %g Format String of character Unsigned octal integer Print % character Unsigned hexadecimal integer Scientific notation
2009 Hazizulden Abdul Aziz, Faculty of Electrical & Electronics Engineering, UMP
2009 Hazizulden Abdul Aziz, Faculty of Electrical & Electronics Engineering, UMP
Syntax: scanf
scanf(format,&argument);
Reads formatted values from keyboard in a format defined by format and loads them into the argument argument
Example: scanf(%f %d %c,&fVal1,&iVal2, &cCh);
2009 Hazizulden Abdul Aziz, Faculty of Electrical & Electronics Engineering, UMP
Arithmetic Expression
2009 Hazizulden Abdul Aziz, Faculty of Electrical & Electronics Engineering, UMP
Arithmetic Operators
Operator + * / Operation Subtraction Addition Multiplication Division Operator -= += *= /= Operation minus equals Add equals Multiplied equals Divide equals
Modulus
2009 Hazizulden Abdul Aziz, Faculty of Electrical & Electronics Engineering, UMP
j--
++j
--j
2009 Hazizulden Abdul Aziz, Faculty of Electrical & Electronics Engineering, UMP
Try this!
2009 Hazizulden Abdul Aziz, Faculty of Electrical & Electronics Engineering, UMP
Selection Statements
Decision & branching Choose between several possible sequences of action based on the value of expressions Two commonly used selection algorithm IF/ELSE SELECT/CASE
2009 Hazizulden Abdul Aziz, Faculty of Electrical & Electronics Engineering, UMP
Relational Operators
Operator Operation Operator Operation
<
<= ==
Less than
Less than or equal equal
>
>= !=
Greater than
Greater than or equals NOT equals
2009 Hazizulden Abdul Aziz, Faculty of Electrical & Electronics Engineering, UMP
Logical Operators
Operator Operation
&& ||
!
AND OR
NOT
2009 Hazizulden Abdul Aziz, Faculty of Electrical & Electronics Engineering, UMP
Used when one of the two statements must be chosen depending on a condition
2009 Hazizulden Abdul Aziz, Faculty of Electrical & Electronics Engineering, UMP
Syntax: if..else
if (expression) { statement block1; } else { statement block2; }
2009 Hazizulden Abdul Aziz, Faculty of Electrical & Electronics Engineering, UMP
if (expression1) { statement block1; } else if (expression2) { statement block2; } else { statement block3; }
2009 Hazizulden Abdul Aziz, Faculty of Electrical & Electronics Engineering, UMP
Used when one statement from many must be chosen as determined by a value
2009 Hazizulden Abdul Aziz, Faculty of Electrical & Electronics Engineering, UMP
Syntax: switch..case
switch (expression) { case const1: statement(s); break; case const2: statement(s); break; default: statement(s); break; }
2009 Hazizulden Abdul Aziz, Faculty of Electrical & Electronics Engineering, UMP
Syntax: break
Use to exit from a statement block Use with: switch..case for while () do..while()
2009 Hazizulden Abdul Aziz, Faculty of Electrical & Electronics Engineering, UMP
Repetition Statement
Looping and counting Control the repetitive re-execution or iteration of sequences of actions Two commonly used looping algorithm DO...WHILE WHILE...DO
2009 Hazizulden Abdul Aziz, Faculty of Electrical & Electronics Engineering, UMP
2009 Hazizulden Abdul Aziz, Faculty of Electrical & Electronics Engineering, UMP
Syntax: do..while
do { statement block; } while (condition);
2009 Hazizulden Abdul Aziz, Faculty of Electrical & Electronics Engineering, UMP
2009 Hazizulden Abdul Aziz, Faculty of Electrical & Electronics Engineering, UMP
Syntax: while
while (condition) { statement block; }
2009 Hazizulden Abdul Aziz, Faculty of Electrical & Electronics Engineering, UMP
Syntax: for
for (start_value;test_condition;operation) { statement(s); }
where start_value test_condition operation: Starting value of the loop counter relational condition to continue looping. if test condition is TRUE, looping continue loop counting operation
2009 Hazizulden Abdul Aziz, Faculty of Electrical & Electronics Engineering, UMP
Syntax: continue
Use only in repetitive statement Transfer instruction execution sequence to test condition for repetitive statement
2009 Hazizulden Abdul Aziz, Faculty of Electrical & Electronics Engineering, UMP
Invocation
2009 Hazizulden Abdul Aziz, Faculty of Electrical & Electronics Engineering, UMP
Function
A sub-program or sub-routine A module of code that define specific job An identifiable pieces of code with defined interface Reusable can be used repeatedly
2009 Hazizulden Abdul Aziz, Faculty of Electrical & Electronics Engineering, UMP
Avoid redundancy in coding Provide logical clarity to program structure Customized library of routine instruction
2009 Hazizulden Abdul Aziz, Faculty of Electrical & Electronics Engineering, UMP
stdio.h
math.h ctype.h string.h stddef.h stdlib.h
time.h
Time functions
2009 Hazizulden Abdul Aziz, Faculty of Electrical & Electronics Engineering, UMP
Examples:
#include <math.h>
abs(int val) fabs(double val) exp(double val) log(double val) sqrt(double val) pow(double val1, double val2) M_PI
2009 Hazizulden Abdul Aziz, Faculty of Electrical & Electronics Engineering, UMP
Prototype Define function interface (declaration) Definition Block of statements associated to function Specify function operation Call Cause function to be executed
2009 Hazizulden Abdul Aziz, Faculty of Electrical & Electronics Engineering, UMP
Function Prototype
Syntax:
2009 Hazizulden Abdul Aziz, Faculty of Electrical & Electronics Engineering, UMP
Function Definition
Function block
Header()
Header correspond to prototype definition Body collection of statements contained within {} bracket
{
declaration statements;
2009 Hazizulden Abdul Aziz, Faculty of Electrical & Electronics Engineering, UMP
Function Call
Syntax:
Function call can be made from Main() function block or from another function block
2009 Hazizulden Abdul Aziz, Faculty of Electrical & Electronics Engineering, UMP
void:void Function
Function definition
void fname(void) {
Function call
2009 Hazizulden Abdul Aziz, Faculty of Electrical & Electronics Engineering, UMP
void:one Function
Function definition
Function call
2009 Hazizulden Abdul Aziz, Faculty of Electrical & Electronics Engineering, UMP
one:void Function
Return a value and does not accept any argument Prototype declaration
Function definition
Function call
2009 Hazizulden Abdul Aziz, Faculty of Electrical & Electronics Engineering, UMP
one:one Function
Function definition
Function call
identifier = fname(arguments);
2009 Hazizulden Abdul Aziz, Faculty of Electrical & Electronics Engineering, UMP
Outcomes
Explain five elements of structured programming Write an executable code in C demonstrating all five elements of structured programming approach Develop a basic algorithm and write a basic program to solve engineering related numerical analysis
2009 Hazizulden Abdul Aziz, Faculty of Electrical & Electronics Engineering, UMP
Numeric Arrays
2009 Hazizulden Abdul Aziz, Faculty of Electrical & Electronics Engineering, UMP
Arrays
A series of variables with same data type that share one variable name Related terms Array slots/cells Array elements Array size/length (always an integer number)
2009 Hazizulden Abdul Aziz, Faculty of Electrical & Electronics Engineering, UMP
Using Arrays
Array data type can be used in all elements of programming Declaration Action statements Repetitive statement Selective statement Function statement
2009 Hazizulden Abdul Aziz, Faculty of Electrical & Electronics Engineering, UMP
data_type array_name[array_length];
Initialization
array_name[array_length] = {val1,};
2009 Hazizulden Abdul Aziz, Faculty of Electrical & Electronics Engineering, UMP
Functions use same memory area for arrays Function can not return an array value Declare array as function argument if function modify array element value
2009 Hazizulden Abdul Aziz, Faculty of Electrical & Electronics Engineering, UMP
void fname(ary_type ary_name[], int n, arg_type arg_name); return_type fname(ary_type ary_name[], int n, arg_type arg_name)
2009 Hazizulden Abdul Aziz, Faculty of Electrical & Electronics Engineering, UMP
Declaration
data_type ary_name[row][col];
2009 Hazizulden Abdul Aziz, Faculty of Electrical & Electronics Engineering, UMP
void fname(ary_type ary_name[][col], int n, arg_type arg_name); return_type fname(ary_type ary_name[][col], int n, arg_type arg_name)
2009 Hazizulden Abdul Aziz, Faculty of Electrical & Electronics Engineering, UMP
Outcomes
Explain the structure of numeric array data type (one- and two-dimensional array) Write a code in C demonstrating the use of numeric array data type
2009 Hazizulden Abdul Aziz, Faculty of Electrical & Electronics Engineering, UMP
Strings
2009 Hazizulden Abdul Aziz, Faculty of Electrical & Electronics Engineering, UMP
2009 Hazizulden Abdul Aziz, Faculty of Electrical & Electronics Engineering, UMP
Note:
2009 Hazizulden Abdul Aziz, Faculty of Electrical & Electronics Engineering, UMP
Logical operators
Example
Increment/decrement
2009 Hazizulden Abdul Aziz, Faculty of Electrical & Electronics Engineering, UMP
String
Character array (array of char data type) Declaration & initialization of string variable char str[str_len]; char str[] = strings_value;
Note: string will add null character \0 to mark end of string causing number of character stored always one less than str_len
2009 Hazizulden Abdul Aziz, Faculty of Electrical & Electronics Engineering, UMP
Note:
Note:
Ampersand (&) not included as prefix to string variable identifier Input Text longer than string length will cause overflow of string variable
Adding minus (-) sign to type specifier (s) cause left justification of string String variable without null character (\0) may cause run-time error
2009 Hazizulden Abdul Aziz, Faculty of Electrical & Electronics Engineering, UMP
2009 Hazizulden Abdul Aziz, Faculty of Electrical & Electronics Engineering, UMP
Sting is array of character, therefore using string data type with function is similar to using numeric array with function discussed in earlier topic.
2009 Hazizulden Abdul Aziz, Faculty of Electrical & Electronics Engineering, UMP
Array of String
Two-dimensional array of character type Declaration char str_ary[row][col]; char str_ary[row][col] = {str1, str2, , str_row};
2009 Hazizulden Abdul Aziz, Faculty of Electrical & Electronics Engineering, UMP
Pointers
2009 Hazizulden Abdul Aziz, Faculty of Electrical & Electronics Engineering, UMP
What is Pointers?
Address of a memory location of a data Points to a memory location where a data is stored Pointer variable contains address of a memory location
2009 Hazizulden Abdul Aziz, Faculty of Electrical & Electronics Engineering, UMP
Usage
Pointer variable is used To return more than one value from a function To create and/or process string variable To manipulate contents of arrays To construct dynamics array
2009 Hazizulden Abdul Aziz, Faculty of Electrical & Electronics Engineering, UMP
2009 Hazizulden Abdul Aziz, Faculty of Electrical & Electronics Engineering, UMP
Pointer Operators
& operator
* operator
2009 Hazizulden Abdul Aziz, Faculty of Electrical & Electronics Engineering, UMP
Return multiple values from a function Pointer variable is declared as a parameter in function prototype Address of a variable is pass to a function in function call Address of a variable is stored in pointer variable (parameter) of the function
2009 Hazizulden Abdul Aziz, Faculty of Electrical & Electronics Engineering, UMP
Function call
fname(&var_name);
2009 Hazizulden Abdul Aziz, Faculty of Electrical & Electronics Engineering, UMP
File INPUT/OUTPUT
2009 Hazizulden Abdul Aziz, Faculty of Electrical & Electronics Engineering, UMP
Use files to store data in permanent form Useful when collecting large quantity of data
Use text file (.txt or .dat file format) Data stored in ASCII characters format
2009 Hazizulden Abdul Aziz, Faculty of Electrical & Electronics Engineering, UMP
2009 Hazizulden Abdul Aziz, Faculty of Electrical & Electronics Engineering, UMP
Files Attributes
Attribute
r w a
Description
Open for reading only Create for writing Append/create for writing
r+ w+
a+
2009 Hazizulden Abdul Aziz, Faculty of Electrical & Electronics Engineering, UMP
Syntax
fprintf(file_ptr,format,argument);
2009 Hazizulden Abdul Aziz, Faculty of Electrical & Electronics Engineering, UMP
Syntax
fscanf(file_ptr,format,arguments);
2009 Hazizulden Abdul Aziz, Faculty of Electrical & Electronics Engineering, UMP
feof(file_ptr);
2009 Hazizulden Abdul Aziz, Faculty of Electrical & Electronics Engineering, UMP
Syntax fclose(file_ptr);
2009 Hazizulden Abdul Aziz, Faculty of Electrical & Electronics Engineering, UMP
Outcomes
Explain how program read from and store data to a file Write a code in C to write data to a data file Write a code in C to read data from a data file
2009 Hazizulden Abdul Aziz, Faculty of Electrical & Electronics Engineering, UMP
Hardware Interface
2009 Hazizulden Abdul Aziz, Faculty of Electrical & Electronics Engineering, UMP
Software/Hardware Interface
Common I/O ports Parallel port Serial port FIREWIRE port USB port
2009 Hazizulden Abdul Aziz, Faculty of Electrical & Electronics Engineering, UMP
Access to I/O ports is restricted by OS Need I/O port device driver to allow access Communicate via IOCTL calls
User program
IOCTL Call
I/O Port
Peripheral device
2009 Hazizulden Abdul Aziz, Faculty of Electrical & Electronics Engineering, UMP
Commonly used port for (Engineering) projects Allow data transfer in parallel 1 byte at a time Identifiable by D-Type 25 pins connector PC uses Male type connector Almost OBSOLETE replaced by USB port
2009 Hazizulden Abdul Aziz, Faculty of Electrical & Electronics Engineering, UMP
Hardware Properties
IEEE 1284 Type A: D-Type 25pin connector Data line: Pins (2-9) Port Addresses LPT1: 378h 37Fh LPT2: 278h 27Fh
2009 Hazizulden Abdul Aziz, Faculty of Electrical & Electronics Engineering, UMP
PORTTALK
Parallel Port device driver for Windows XP Originally written for Borland turbo C++ compiler Pre-Installed on the programming device Invoke through preprocessor directive #include pt_ioctl.c
2009 Hazizulden Abdul Aziz, Faculty of Electrical & Electronics Engineering, UMP
PortTalk Calls
2009 Hazizulden Abdul Aziz, Faculty of Electrical & Electronics Engineering, UMP
Receive data from peripheral device var_name = inportb(PortID,dataHx); Sent data to peripheral device outportb(PortID,dataHx);
2009 Hazizulden Abdul Aziz, Faculty of Electrical & Electronics Engineering, UMP
Outcomes
Explain how program communicates with peripheral devices Write a code in C to write data to parallel port Write a code in C to read data from parallel port
2009 Hazizulden Abdul Aziz, Faculty of Electrical & Electronics Engineering, UMP
CLOSING NOTES
2009 Hazizulden Abdul Aziz, Faculty of Electrical & Electronics Engineering, UMP
Demonstrate structured programming techniques using high-level language Use computer programming techniques in solving electrical & electronics engineering problem
2009 Hazizulden Abdul Aziz, Faculty of Electrical & Electronics Engineering, UMP
Further Readings
2009 Hazizulden Abdul Aziz, Faculty of Electrical & Electronics Engineering, UMP
Migration to RAD
RAD Rapid Application Development programming language (e.g. C++ Builder, Visual C++) Object Oriented Programming paradigm
2009 Hazizulden Abdul Aziz, Faculty of Electrical & Electronics Engineering, UMP
Application Languages
Scripting programming language (e.g. VBA Macros) Webpage programming language (e.g. HTML, Java) Database programming language (e.g. SQL)
2009 Hazizulden Abdul Aziz, Faculty of Electrical & Electronics Engineering, UMP
Ultimately, its not about the language you use or its syntax, but about how you think about your problems. And more about the tools and structure in your mind than in your language. If you have the former, you can usually work around the deficiencies in the latter
2009 Hazizulden Abdul Aziz, Faculty of Electrical & Electronics Engineering, UMP
The END
Hazizulden Abdul Aziz Faculty of Electrical & Electronics Engineering Universiti Malaysia PAHANG
2009 Hazizulden Abdul Aziz, Faculty of Electrical & Electronics Engineering, UMP