1.63k likes | 1.84k Vues
Data Structures. C/C++ Review Prof A Alkhorabi أ. د / عـبدالله علي الخــوربي. -. -. Data Structures. C Review Prof A Alkhorabi أ. د / عـبدالله علي الخــوربي. -. -. C Characteristics. Size Type conversion Modern control constructs Bitwise control Pointer implementation
E N D
Data Structures C/C++ Review Prof A Alkhorabi أ.د/ عـبدالله علي الخــوربي - -
Data Structures C Review Prof A Alkhorabi أ.د/ عـبدالله علي الخــوربي - -
C Characteristics • Size • Type conversion • Modern control constructs • Bitwise control • Pointer implementation • Structure
C Program Parts C Program constitutes from 2 parts: • Definition, Inclusion & Declaration Part • Functions Part
1. Definition, Inclusion & Declaration Part This part have two main sections: • C Preprocessor • Declaration part
A. C Preprocessor Before compilation the C preprocessor (part of the C compiler) looks for directives (preceded by #), and execute the required task according to the directive type. There are 3 types of directives: • include Directive • define Directive • conditional compilation Directive
include Directive Includes files to the including file. include <filename>#
define Directive #define SymbolicName constant or #define SymbolicName simple calculation operation
Conditional Compilation Directive ِAllows writing programs to more than one operating system #ifdef
B.Declaration part • Allow declarations of Global Variables and Functions used later in the program.
2. Functions Part • Consists of one or more functions, one of which must be the main() function. • The C program starts execution from the main() function, and stops execution by the main() function end. • The main() function calls other functions, each can call other functions.
C Programs Execution • C program passes through a number of phases starting from the written program by the programmer (Source File) till it became executable (Executable File). • This is normally established by Software Systems that are known as Integrated Development Environment (IDE) provided by specialized Software Companies.
IDE • Program Editor • Preprocessor • Compiler • Assembler • Linker • IDE’s may include other facilities like Debugging.
Typing طبع Editor المحرر Source (ASCII) code Preprocessor المُعد Assembler المُجمع Assembly code Compiler المُترجم Object code Linker المُدمج Libraries المكتبات executable code مراحل تطوير برنامجسي C Program Developing Phases
C Program Structure /* (1) Inclusion, Definition & Global declarations part */ Inclusion part Definition part global declarations part /* (2) Functions part */ return_value main (arguments list) } local declaration part function executable statements { return_value function_2 (arguments list) } local declaration part function executable statements returned value { : :
1 /* Sum3.c - Program to sum 3 integers */ 2 /* 1- inclusion, definition & glbal vars and functions declaration part */ 3 # include “stdio.h” /* a- inclusion part */ 4 # define TEN 10 /* b- definition part */ 5 6 int sum; /* c-Global Variable & Functions Part */ 7 8 /* 2- Functions Part */ 9 void main(void) /* main Function */ 10 { /* Function opening brace */ 11 int num_1, num_2, num_3; /* Local Variables Declaration Part*/ 12 13 num_1 = 5; num_2 = 2; num_3 = num_2*TEN; 14 sum=num_1 + num_2 + num_3; /*Function’s Executable Stateent */ 15 printf(“The sum of the 3 numbers is %d \n”, sum); /* I/O Lib Func*/ 16 } /* Closing Brace */
C Reserved Words • Used within a C program for a specific purposes and according to the language rules, and can not be used in other purposes ( as identifiers for example) • Consists of two main parts and one sub-part.
Reserved Words: 1st main part • 28 words accompanied the C language from the beginning: autobreak case char continuedefault do double elseextern floatfor gotoif int long register return short sizeof static struct switch typedef union unsigned while entry
Reserved Words: 2nd main part • 5 words added by ANSI (American National Standardization Institute): const enum signed void volatile
ٍ Reserved Words: Sub-part • Added by some C Compilers manufacturers: Asm far _cs fortran huge near pascal
Reserved Words: Note • All reserved words written using lower-case letters.
Identifies Used to name and identify: • constants • variables • functions . • An identifier should not be a reserved word. • Small letters identifiers differ than that written as large letters. Ex: #define ONE 1 int sum; float real(void);
Data Types • Basic Data Types أنواع البيانات الأساسية • Derived Data Types أنواع البيانات المشتقة • Data Structures تراكيب البيانات
C Language Have 4 basic data types: Basic Data Types • Integers int. • Single-precision floating numbers float. • Double-precision floating numbersdouble. • Characters char.. • Void is used to define no type function argument or returned value. • Ex: void fn(void);
Integer Operations Assignment operation Arithmetic operations Relational operations Logical operations Bitwise operations Conditional operator
أسبقيات تنفيذ عوامل العمليات للأعداد الصحيحةInteger Operators Precedence
أسبقيات تنفيذ عوامل العمليات للأعداد الصحيحةInteger Operators Precedence
Floating-point Variables • The floating-point number 246.66 can be written in one of the following forms:
Floating point Variables • Single precession real number “float”, 4 bytes. • Double precession real number “double”, 8 bytes. • Long single precession real number“long float”, 8 byes. • Long double precession real number “long double”, 10 bytes. • longfloatand double have the same properties.
Floating point operations • Addition uses the operator + • Subtraction uses the operator- • Multiplication uses the operator* • Division uses the operator/
Characters • Characters in C Language includes: • Alphabet letters • Lower case (a,b,c,..) • Upper case (A,B,C,…) • Digits (0,1,2,...) • Symbols (?,, > , # (,) • Formatting characters,(space, horizontal tabulation, vertical tabulation).
Characters • Characters in C are according to ASCII(American National Standard Code for Information Interchange). • ASCII code also includes transmission characters used for computer network communications (ETX,SYN,...(
/* AsciiCharacters.c ASCII Characters Program Example */ #include <stdio.h> #include <conio.h> void main(void) {int x; for(x = 0; x < 128; x++) { if (x % 20 == 0) getch(); printf("%d\t%c\n",x,x);} getch(); }
Characters Character variables char ch; ch = ‘H’;
Character Functions in the Standard Library • These functions return non-zero integers if it accomplishes their tasks positively, otherwise it returns zeros.
Character strings • “This is string 1”
Control In programming languages control is used to determine next statement to be executed. Normally control can be one of the following types: Sequential control . Conditional control. Iterative control. Jumping control.
Conditional Control • Conditional control is accomplished by one of the following constructs: • if-else-if • switch
Conditional Control • The if-else-ifis used as one of the following forms: • if • if-else • if-else-if
شرط if F T تعبير او مجموعة تعابير if التعبير التالي if if (expression) /* تعبير جبري*/ executable_statement(s); /* تعبير أو مجموعة تعابير*/ next_statement; if ( x == 5 || y < 8) printf (“The if conditions that x= 5 OR y < 8 is TRUE” );
F شرط if T تعبير else تعبير if التعبير التالي if- else if (expression) executable_statements_1; else executable_statements_2; next_statement;
if الشرط الأول F T F else-if الشرط الثاني التعبير الأول التعبير الثاني F else else-if الشرط الثالث تعبير الاختيار النهائي النهائي التعبير الثالث التعبير التالي if-if- else if ( expression_1) statements_1 ; else if (expression_2) statements_2 ; else if (expression_3) statements_3; : else statements_n ; next_statement;
/* ifelseif.c if-else-if Example Program*/ #include <stdio.h> #include <conio.h> void main(void) { int answer, i = 1; printf("What is the Capital of Yemen (y), UK (u), or Saudia Arabia (s) \n"); answer = getche(); if (answer == 'y' || answer == 'Y' ) printf ( "\n The Capital of Yemen is Sana'a\n\n"); else if (answer == 'u' || answer == 'U' ) printf ( "\n The Capital of UK is London\n\n"); else if (answer == ‘s' || answer == ‘S' ) printf ( "\n The Capital of Saudia Arabia is Riyadh\n\n"); else printf ( "\n No information is available about this country\n\n"); }
switch switch (expression) { case constant_1 : statements_1; break; case constant_2: statements_2; break; . : default: statements_n; } next_statement;
switch (تعبير جبري) تعبير الاختبار النهائي default y هل تطابق الحالة الأولى تعبابير break N y هل تطابق الحالة الثانية break تعابير N التعبير التالي switch
/* switch.c switch Example Program */ #include <stdio.h> #include <conio.h> void main(void) { int answer, i = 1; printf("What is the Capital of Yemen (y), UK (u), or Saudia Arabia (s). \n"); answer = getche(); switch ( answer ) { case 'y' : printf ( "\nThe Capital of Yemen is Sana'a\n\n"); break; case 'Y' : printf ( "\nThe Capital of Yemen is Sana'a\n\n"); break; case 'u' : printf ( "\nThe Capital of UK is London\n\n"); break; case 'U' : printf ( "\nThe Capital of UK is London\n\n"); break; case ‘s' : printf ( "\The Capital of Saudia Arabia is Riyadh\n\n"); break; case ‘S' : printf ( "\The Capital of Saudia Arabia is Riyadh\n\n"); break; default : printf ( "\nNo information is available about this country\n\n"); } }