210 likes | 360 Vues
Week 2 – Introduction to Computer Programming. CONTENTS. Identifiers Variable Constant Data Types and Types Declaration Operators Pre-Processor Directives scanf/printf Program debugging/compiling. IDENTIFIERS (1). There are two types of identifiers Variable Constance
E N D
Week 2 – Introduction to Computer Programming Created By: Dr. Shahrul Nizam Yaakob
CONTENTS • Identifiers • Variable • Constant • Data Types and Types Declaration • Operators • Pre-Processor Directives • scanf/printf • Program debugging/compiling Created By: Dr. Shahrul Nizam Yaakob
IDENTIFIERS (1) • There are two types of identifiers • Variable • Constance • The naming rules: • Can’t used the reserved words • (e.g. void, include, main, return, printf, scanf etc) • No blanks spacing • (e.g. data 1, out 1) Created By: Dr. Shahrul Nizam Yaakob
IDENTIFIERS (2) • Variable • Hold data and can change during program execution • e.g. • int x, double y • Constant • Hold data and can’t change during program execution • e.g. • const int x=1, const double y=0.1 Created By: Dr. Shahrul Nizam Yaakob
DATA TYPES Created By: Dr. Shahrul Nizam Yaakob
TYPES OF DECLARATION • Single declaration: (e.g.) • float fInput; • float fOutput; • Combine declaration: (e.g.) • float fInput, fOutput; • Declare and initialize: (e.g.) • float fInput=0.1,fOutput=0.0076; const int iK=2; Created By: Dr. Shahrul Nizam Yaakob
OPERATORS • Types of operators are: • Arithmetic operators • Unary operators • Compound assignment operators • Relational operators • Logical operators • Bitwise operators Created By: Dr. Shahrul Nizam Yaakob
C Operation Arithmetic Operator Algebraic Expression C Expression Addition + f + 7 f + 7 Subtraction - p – c p - c Multipication * bm b * m Division / x / y x / y Remainder (Modulus) % r mod s r % s ARITHMATIC OPERATORS 1 Created By: Dr. Shahrul Nizam Yaakob
ARITHMATIC OPERATORS 1 • How Remainder operator works: • E.g. given x=10 y=3 z = 10 % 3 = 1 void main () { z = x % y; } Created By: Dr. Shahrul Nizam Yaakob
UNARY OPERATORS • Operating on ONE operand • E.g. ++j, j++, --i, i-- int j, i=2; j = i++; // j=2 and i=3 j = ++i; // j=3 and i=3 j = --i; // j=1 and i=1 j = i--; // j=2 and i=1 Created By: Dr. Shahrul Nizam Yaakob
COMPOUND ASSIGNMENT OPERATORS • Combination between ‘+’ and ‘=‘ • Combination between ‘-’ and ‘=‘ int j=4, i=2; j+=i; //j=j+i j=6 i=2 j+=3; //j=j+3 j=7 j-=i; //j=j-2 j=2 j-=4; //j=j-4 j=0 Created By: Dr. Shahrul Nizam Yaakob
RELATIONAL OPERATORS • X == Y;X is equal to Y • X != Y; X is not equal to Y • X > Y;X is greater then Y • X < Y; X is less then Y • X <= Y; X is less then or equal to Y • X >= Y;X is greater then or equal to Y Reminder: DO NOT confuse == (relational operator) with = (assignment operator) Created By: Dr. Shahrul Nizam Yaakob
LOGICAL OPERATORS • Logical operators are manipulation of logic and there are: • && (AND) • || (OR) e.g. if ((i==1)&&(k!=2)) //must both if ((i==1)||(k!=2)) //either one Created By: Dr. Shahrul Nizam Yaakob
BITWISE OPERATORS • Will not be covered in this course! Created By: Dr. Shahrul Nizam Yaakob
Pre-Processors Directives • The C Pre-processor (cpp) executes before a program is compiled • Any Pre-processor directives begin with # • It can performs (e.g): • Inclusion of other files (e.g: #include < >) • Definition of symbolic definition • Conditional of compilation • Conditional execution of pre-processor directives Created By: Dr. Shahrul Nizam Yaakob
printf (1) • Require stdio.h header file printf(“…%[width][.precision]specifier…”, identifier,…); Created By: Dr. Shahrul Nizam Yaakob
printf (2) int x=1; double y=0.3; printf (“x=%d y=%lf”, x, y); x=1 y=0.3 printf(“x=%3d y=%.3lf”,x,y); x= 1 y=0.300 Printf(“x=%3.1d y=%3.3lf”, x,y); x= 1.0 y=0.3 Created By: Dr. Shahrul Nizam Yaakob
printf (2) Created By: Dr. Shahrul Nizam Yaakob
scanf(1) • Require stdio.h header file • e.g int x; float y; char w; scanf(“%d%f%c”, &x,&y,&w); Created By: Dr. Shahrul Nizam Yaakob
scanf(2) Created By: Dr. Shahrul Nizam Yaakob
Program Debugging/Compiling • Syntax error • Mistakes caused by violating “grammar” of C • C compiler can easily diagnose during compilation • Run-time error • Called semantic error or smart error • Violation of rules during program execution • C compiler cannot recognize during compilation • Logic error • Most difficult error to recognize and correct • Program compiled and executed successfully but answer wrong Created By: Dr. Shahrul Nizam Yaakob