SlideShare a Scribd company logo
With/Mohamed Fawzy
C-Programming Language-2015
1 Day #2
2
Lecture Notes:
 Set your phone to vibration mode.
 Ask any time.
 During labs feel free to check any materials or internet.
Contents
3
Arrays.
Control statements.
Pointers.
Functions.
Dynamically memory allocation
4
Arrays.
 which can store a fixed-size sequential collection of elements of the
same type.
 All arrays consist of contiguous memory locations (Single Block).
 The size of array, once declared, is fixed and cannot be modified.
 Single Dimension Array.
 Multi Dimension Array (Array of Arrays).
EX:
char a[5]; //Array of 5 characters.
EX:
char a[3][4];
/*Array of three arrays
and each array has 4 characters.*/
5
Examples:
double d[100]={1.5,2.7};
//first two elements initialized and remaining ones set to zero.
short num[]={1,2,3,4,5,6};
//compiler fixes size at 7 elements.
short num[]={1,2,3,4,5,6};
//compiler fixes size at 7 elements.
6
Take Care !!!!
#define size 10
int a[size];
char size=10;
int a[size];
const char size=10;
int a[size];
int a[5.3];
//size must be an integer
int a[5];
a[5]=50;
//in this case it will overwrite some data.
a[-1]=5;
//the index must be an integer.
7
Control Statements.
 For Statements.
for (initial value;condition;update)
{
//statements
}
OR
initial value;
for(;condition;)
{
//statements
update;
}
8
Take Care !!!!
char x;
for(x=0;x<200;x++)
{
printf(“c programming”);
}
for(;;)
{
printf(“c programming”);
}
9
 while loop.
while (condition)
{
//statements
}
 do while loop.
do
{
//statements
}
while (condition)
Control Statements.
10
Break & Continue.
• Break and continue are used to modify the execution of loops.
 break.
When the break statement is encountered inside a loop, the loop is immediately
terminated and program control resumes at the next statement following the loop.
 Continue.
continue forces the next iteration of the loop to take place, skipping
any code in between.
11
Examples:
//program to print even numbers between(0:50)
int main()
{
char x=0;
while (x<=50)
{
if(x%2){
x++;
continue;
}
printf("%dn",x++);
}
return 0;
}
12
Pointers.
Why pointers?
• Achieve call by reference with functions.
• Arrays and structures are difficult without pointers.
• Create linked list, trees and graph.
Note:
We must take care in using pointers since, there are no safety features.
13
Declaring pointers.
• Pointers are declared using “*”.
Int x; //declaring an integer
Int* x; //declaring pointer to an integer
char* m; //declare pointer to character
Notes:
• Pointers may only point to variables of the same type as the pointer
has been declared.
• A pointer to an int may only point to int.
• A pointer to a double may only point to double not float or long double.
• (&) stands for “address of….”
• (*) stands for “content of…”
14
Example:
15
Take Care !!!!
16
Pointers and arrays.
• The name of array is a pointer to the 0th place of array.
• You cannot apply increment or decrement on array name.
• Pointer is useful for passing array to function and safe stack memory.
EX#1:
double *ptr;
double arr[10];
Ptr=arr; //ptr=&arr[0]
arr++; //not allowed
ptr++; //allowed
EX#2:
void print_arr(char *ptr){
printf(“%dn”,*(ptr++));
}
. . . . . . . . . . .
char arr[10];
Print_arr(arr);
Take Care !!!!
char *ptr[5]; //array of five pointers to char
char (*ptr)[5]; //pointer to array of 5 elements
17
EX#3:
char (*ptr)[5];
char arr[5] {5,6,7,8,9};
Ptr=arr;
ptr++;
5
0
1
2
3
4
5
6
7
8
9
Garbage value
18
Functions.
• Functions are blocks of code that perform a number of pre-defined commands to
accomplish something productive. You can either use the built-in library functions
or you can create your own functions.
• Functions that a programmer writes will generally require a prototype.
• It tells the compiler what the function will return, what the function will be called,
as well as what arguments the function can be passed.
return-type <function-name> (arg_type arg1, arg_type arg2,…);
Exs.
void fun (void); //function which take nothing and return nothing
void fun (int x,int y,…); //function take arguments and return nothing
int fun (void); //function take nothing and return int
int fun (int x,int y,…); //function take arguments and return int
19
Ex#1.
#include <stdio.h>
int mult ( int x, int y ); //prototype of function
int main()
{
int x,int y,int result;
printf( "Please input two numbers to be multiplied: " );
scanf( "%d", &x );
scanf( "%d", &y );
result= mult(x,y); //calling the function
printf( "The product of two numbers is %dn",result);
return 0;
}
//implementation of function
int mult (int x, int y)
{
return x * y;
}
Functions. cont‟d
Pointer to function.
20
What happened in calling function?
Save some data in memory segment called stack.
• the value of PC (Program Counter).
• A copy of parameters passed to function.
• The value which returned from function.
Note:
If size of data stored on stack is larger than whole stack size it will cause
Common error called “Stack Overflow”.
Problem:
What if we need to pass a huge data to function to be processed.
Solution:
We can pass by reference because any pointer only occupy 4 bytes.
Functions. cont‟d
Calling function.
21
Write a c program to calculate the largest number in passed array.
#include <studio.h>
char calc_largest (char *ptr,char siz)
{
char largest=*ptr;
char i=0;
for (i=0;i<siz;i++)
{
if (*(ptr+i) >largest)
largest=*(ptr+i);
else
continue;
}
return largest;
}
int main(){
char arr[]={45,12,5,44,6,8,60}
printf(“the largest value is %d”,calc_largest(arr,sizeof(arr));
}
Functions. cont‟d
22
Functions. cont‟d
Pointer to function.
• Pointer to function allow programmers to pass a function as a
parameter to another function.
• Function pointer syntax.
<return data_type> (* pointer_name)(arguments passed to function);
EX#1:
void (*ptr)(int arg1,int arg2);
/*
Pointer to function which return nothing and take two
integers parameters.
*/
Note:
Don't be confused between pointer to function and pointer to array.
23
Functions. cont‟d
Initializing Pointer to function.
EX#2:
void my_int_func(int x)
{
printf( "%dn", x );
}
int main()
{
void (*ptr)(int);
/* the ampersand is actually optional */
ptr = &my_int_func;
(*ptr)( 2 ); //or ptr(2);
return 0;
}
24
Hands ON
Email: mo7amed.fawzy33@gmail.com
Phone: 01006032792
Facebook: mo7amed_fawzy33@yahoo.com
Contact me:
25
26

More Related Content

PDF
See through C
PPT
detailed information about Pointers in c language
PDF
Pointers_c
PPTX
Pointers in C
PDF
Types of pointer in C
PPTX
Dynamic Memory Allocation in C
PPT
Pointer in C
PDF
Lecturer23 pointersin c.ppt
See through C
detailed information about Pointers in c language
Pointers_c
Pointers in C
Types of pointer in C
Dynamic Memory Allocation in C
Pointer in C
Lecturer23 pointersin c.ppt

What's hot (20)

PPT
Pointers in C
PPT
Hooking signals and dumping the callstack
PPTX
Pointers in C
PPT
C pointers
PPT
Pointers - DataStructures
PPSX
Pointers
PPTX
Pointers in C/C++ Programming
PPT
Pointers+(2)
PDF
Python-02| Input, Output & Import
PPT
Basics of pointer, pointer expressions, pointer to pointer and pointer in fun...
PPTX
C programming(part 3)
PPTX
#OOP_D_ITS - 2nd - C++ Getting Started
PPTX
PPT
Void pointer in c
PPT
PPT
Lecture 17 - Strings
PPS
C programming session 05
PPT
Pointers in C
PPTX
Pointer in C
Pointers in C
Hooking signals and dumping the callstack
Pointers in C
C pointers
Pointers - DataStructures
Pointers
Pointers in C/C++ Programming
Pointers+(2)
Python-02| Input, Output & Import
Basics of pointer, pointer expressions, pointer to pointer and pointer in fun...
C programming(part 3)
#OOP_D_ITS - 2nd - C++ Getting Started
Void pointer in c
Lecture 17 - Strings
C programming session 05
Pointers in C
Pointer in C
Ad

Viewers also liked (20)

PDF
C programming day#3.
PDF
02 Interfacing High Power Devices.2016
PDF
PDF
Ximea - the pc camera, 90 gflps smart camera
PDF
Embedded Systems: Lecture 6: Linux & GNU
PDF
Course 101: Lecture 6: Installing Ubuntu
PDF
Embedded Systems: Lecture 8: The Raspberry Pi as a Linux Box
PDF
Lecture 15 ryuzo okada - vision processors for embedded computer vision
PPTX
Intelligent Video Surveillance - Synesis integrated hardware and software sol...
PDF
Embedded Systems: Lecture 7: Unwrapping the Raspberry Pi
PDF
Embedded Systems: Lecture 7: Lab 1: Preparing the Raspberry Pi
PDF
Embedded Systems: Lecture 5: A Tour in RTOS Land
PDF
Embedded Systems: Lecture 8: Lab 1: Building a Raspberry Pi Based WiFi AP
PDF
Course 102: Lecture 16: Process Management (Part 2)
PDF
Embedded Systems: Lecture 2: Introduction to Embedded Systems
PDF
Embedded Systems: Lecture 4: Selecting the Proper RTOS
PDF
Course 102: Lecture 11: Environment Variables
PDF
Embedded Systems: Lecture 10: Introduction to Git & GitHub (Part 1)
PDF
Embedded Systems: Lecture 1: Course Overview
PDF
Course 102: Lecture 28: Virtual FileSystems
C programming day#3.
02 Interfacing High Power Devices.2016
Ximea - the pc camera, 90 gflps smart camera
Embedded Systems: Lecture 6: Linux & GNU
Course 101: Lecture 6: Installing Ubuntu
Embedded Systems: Lecture 8: The Raspberry Pi as a Linux Box
Lecture 15 ryuzo okada - vision processors for embedded computer vision
Intelligent Video Surveillance - Synesis integrated hardware and software sol...
Embedded Systems: Lecture 7: Unwrapping the Raspberry Pi
Embedded Systems: Lecture 7: Lab 1: Preparing the Raspberry Pi
Embedded Systems: Lecture 5: A Tour in RTOS Land
Embedded Systems: Lecture 8: Lab 1: Building a Raspberry Pi Based WiFi AP
Course 102: Lecture 16: Process Management (Part 2)
Embedded Systems: Lecture 2: Introduction to Embedded Systems
Embedded Systems: Lecture 4: Selecting the Proper RTOS
Course 102: Lecture 11: Environment Variables
Embedded Systems: Lecture 10: Introduction to Git & GitHub (Part 1)
Embedded Systems: Lecture 1: Course Overview
Course 102: Lecture 28: Virtual FileSystems
Ad

Similar to C programming day#2. (20)

PPT
C programming
PDF
C_Program_Yr1[1].pdf for computer science
PPTX
C language
PPTX
Introduction to c
PPTX
C programming language tutorial
PPT
C++ Language
PPTX
Programming in C sesion 2
PDF
The best every notes on c language is here check it out
PDF
C programming day#1
PPTX
Technical Interview
PPSX
Esoft Metro Campus - Certificate in c / c++ programming
PPTX
Advance topics of C language
PPTX
Programming Fundamentals
PDF
Oh Crap, I Forgot (Or Never Learned) C! [CodeMash 2010]
PPTX
PPTX
C programming language
PPTX
Fundamental programming Nota Topic 2.pptx
PPT
An imperative study of c
PDF
Functions and Pointers in C: Achieving Modularity and Memory-Level Access Thr...
PDF
Functions
C programming
C_Program_Yr1[1].pdf for computer science
C language
Introduction to c
C programming language tutorial
C++ Language
Programming in C sesion 2
The best every notes on c language is here check it out
C programming day#1
Technical Interview
Esoft Metro Campus - Certificate in c / c++ programming
Advance topics of C language
Programming Fundamentals
Oh Crap, I Forgot (Or Never Learned) C! [CodeMash 2010]
C programming language
Fundamental programming Nota Topic 2.pptx
An imperative study of c
Functions and Pointers in C: Achieving Modularity and Memory-Level Access Thr...
Functions

More from Mohamed Fawzy (9)

PDF
07 Analogue to Digital Converter(ADC).2016
PDF
06 Interfacing Keypad 4x4.2016
PDF
05 EEPROM memory.2016
PDF
04 Interfacing LCD Displays.2016
PDF
01 GPIO||General Purpose Input Output.2016
PDF
00 let us get started.2016
PPTX
أزاى تروح فى داهيه !!!
PPTX
Ce from a to z
PDF
07 Analogue to Digital Converter(ADC).2016
06 Interfacing Keypad 4x4.2016
05 EEPROM memory.2016
04 Interfacing LCD Displays.2016
01 GPIO||General Purpose Input Output.2016
00 let us get started.2016
أزاى تروح فى داهيه !!!
Ce from a to z

Recently uploaded (20)

PPTX
M Tech Sem 1 Civil Engineering Environmental Sciences.pptx
PDF
PRIZ Academy - 9 Windows Thinking Where to Invest Today to Win Tomorrow.pdf
PPTX
Infosys Presentation by1.Riyan Bagwan 2.Samadhan Naiknavare 3.Gaurav Shinde 4...
PPTX
Strings in CPP - Strings in C++ are sequences of characters used to store and...
PPTX
FINAL REVIEW FOR COPD DIANOSIS FOR PULMONARY DISEASE.pptx
PDF
Mohammad Mahdi Farshadian CV - Prospective PhD Student 2026
PDF
Embodied AI: Ushering in the Next Era of Intelligent Systems
PPTX
UNIT 4 Total Quality Management .pptx
PPTX
bas. eng. economics group 4 presentation 1.pptx
PPTX
CYBER-CRIMES AND SECURITY A guide to understanding
PPTX
MCN 401 KTU-2019-PPE KITS-MODULE 2.pptx
PPTX
Construction Project Organization Group 2.pptx
PPTX
Engineering Ethics, Safety and Environment [Autosaved] (1).pptx
PDF
Evaluating the Democratization of the Turkish Armed Forces from a Normative P...
PPTX
Foundation to blockchain - A guide to Blockchain Tech
PPTX
OOP with Java - Java Introduction (Basics)
PPTX
UNIT-1 - COAL BASED THERMAL POWER PLANTS
PPTX
Sustainable Sites - Green Building Construction
PPTX
Welding lecture in detail for understanding
PPTX
CH1 Production IntroductoryConcepts.pptx
M Tech Sem 1 Civil Engineering Environmental Sciences.pptx
PRIZ Academy - 9 Windows Thinking Where to Invest Today to Win Tomorrow.pdf
Infosys Presentation by1.Riyan Bagwan 2.Samadhan Naiknavare 3.Gaurav Shinde 4...
Strings in CPP - Strings in C++ are sequences of characters used to store and...
FINAL REVIEW FOR COPD DIANOSIS FOR PULMONARY DISEASE.pptx
Mohammad Mahdi Farshadian CV - Prospective PhD Student 2026
Embodied AI: Ushering in the Next Era of Intelligent Systems
UNIT 4 Total Quality Management .pptx
bas. eng. economics group 4 presentation 1.pptx
CYBER-CRIMES AND SECURITY A guide to understanding
MCN 401 KTU-2019-PPE KITS-MODULE 2.pptx
Construction Project Organization Group 2.pptx
Engineering Ethics, Safety and Environment [Autosaved] (1).pptx
Evaluating the Democratization of the Turkish Armed Forces from a Normative P...
Foundation to blockchain - A guide to Blockchain Tech
OOP with Java - Java Introduction (Basics)
UNIT-1 - COAL BASED THERMAL POWER PLANTS
Sustainable Sites - Green Building Construction
Welding lecture in detail for understanding
CH1 Production IntroductoryConcepts.pptx

C programming day#2.

  • 2. 2 Lecture Notes:  Set your phone to vibration mode.  Ask any time.  During labs feel free to check any materials or internet.
  • 4. 4 Arrays.  which can store a fixed-size sequential collection of elements of the same type.  All arrays consist of contiguous memory locations (Single Block).  The size of array, once declared, is fixed and cannot be modified.  Single Dimension Array.  Multi Dimension Array (Array of Arrays). EX: char a[5]; //Array of 5 characters. EX: char a[3][4]; /*Array of three arrays and each array has 4 characters.*/
  • 5. 5 Examples: double d[100]={1.5,2.7}; //first two elements initialized and remaining ones set to zero. short num[]={1,2,3,4,5,6}; //compiler fixes size at 7 elements. short num[]={1,2,3,4,5,6}; //compiler fixes size at 7 elements.
  • 6. 6 Take Care !!!! #define size 10 int a[size]; char size=10; int a[size]; const char size=10; int a[size]; int a[5.3]; //size must be an integer int a[5]; a[5]=50; //in this case it will overwrite some data. a[-1]=5; //the index must be an integer.
  • 7. 7 Control Statements.  For Statements. for (initial value;condition;update) { //statements } OR initial value; for(;condition;) { //statements update; }
  • 8. 8 Take Care !!!! char x; for(x=0;x<200;x++) { printf(“c programming”); } for(;;) { printf(“c programming”); }
  • 9. 9  while loop. while (condition) { //statements }  do while loop. do { //statements } while (condition) Control Statements.
  • 10. 10 Break & Continue. • Break and continue are used to modify the execution of loops.  break. When the break statement is encountered inside a loop, the loop is immediately terminated and program control resumes at the next statement following the loop.  Continue. continue forces the next iteration of the loop to take place, skipping any code in between.
  • 11. 11 Examples: //program to print even numbers between(0:50) int main() { char x=0; while (x<=50) { if(x%2){ x++; continue; } printf("%dn",x++); } return 0; }
  • 12. 12 Pointers. Why pointers? • Achieve call by reference with functions. • Arrays and structures are difficult without pointers. • Create linked list, trees and graph. Note: We must take care in using pointers since, there are no safety features.
  • 13. 13 Declaring pointers. • Pointers are declared using “*”. Int x; //declaring an integer Int* x; //declaring pointer to an integer char* m; //declare pointer to character Notes: • Pointers may only point to variables of the same type as the pointer has been declared. • A pointer to an int may only point to int. • A pointer to a double may only point to double not float or long double. • (&) stands for “address of….” • (*) stands for “content of…”
  • 16. 16 Pointers and arrays. • The name of array is a pointer to the 0th place of array. • You cannot apply increment or decrement on array name. • Pointer is useful for passing array to function and safe stack memory. EX#1: double *ptr; double arr[10]; Ptr=arr; //ptr=&arr[0] arr++; //not allowed ptr++; //allowed EX#2: void print_arr(char *ptr){ printf(“%dn”,*(ptr++)); } . . . . . . . . . . . char arr[10]; Print_arr(arr); Take Care !!!! char *ptr[5]; //array of five pointers to char char (*ptr)[5]; //pointer to array of 5 elements
  • 17. 17 EX#3: char (*ptr)[5]; char arr[5] {5,6,7,8,9}; Ptr=arr; ptr++; 5 0 1 2 3 4 5 6 7 8 9 Garbage value
  • 18. 18 Functions. • Functions are blocks of code that perform a number of pre-defined commands to accomplish something productive. You can either use the built-in library functions or you can create your own functions. • Functions that a programmer writes will generally require a prototype. • It tells the compiler what the function will return, what the function will be called, as well as what arguments the function can be passed. return-type <function-name> (arg_type arg1, arg_type arg2,…); Exs. void fun (void); //function which take nothing and return nothing void fun (int x,int y,…); //function take arguments and return nothing int fun (void); //function take nothing and return int int fun (int x,int y,…); //function take arguments and return int
  • 19. 19 Ex#1. #include <stdio.h> int mult ( int x, int y ); //prototype of function int main() { int x,int y,int result; printf( "Please input two numbers to be multiplied: " ); scanf( "%d", &x ); scanf( "%d", &y ); result= mult(x,y); //calling the function printf( "The product of two numbers is %dn",result); return 0; } //implementation of function int mult (int x, int y) { return x * y; } Functions. cont‟d Pointer to function.
  • 20. 20 What happened in calling function? Save some data in memory segment called stack. • the value of PC (Program Counter). • A copy of parameters passed to function. • The value which returned from function. Note: If size of data stored on stack is larger than whole stack size it will cause Common error called “Stack Overflow”. Problem: What if we need to pass a huge data to function to be processed. Solution: We can pass by reference because any pointer only occupy 4 bytes. Functions. cont‟d Calling function.
  • 21. 21 Write a c program to calculate the largest number in passed array. #include <studio.h> char calc_largest (char *ptr,char siz) { char largest=*ptr; char i=0; for (i=0;i<siz;i++) { if (*(ptr+i) >largest) largest=*(ptr+i); else continue; } return largest; } int main(){ char arr[]={45,12,5,44,6,8,60} printf(“the largest value is %d”,calc_largest(arr,sizeof(arr)); } Functions. cont‟d
  • 22. 22 Functions. cont‟d Pointer to function. • Pointer to function allow programmers to pass a function as a parameter to another function. • Function pointer syntax. <return data_type> (* pointer_name)(arguments passed to function); EX#1: void (*ptr)(int arg1,int arg2); /* Pointer to function which return nothing and take two integers parameters. */ Note: Don't be confused between pointer to function and pointer to array.
  • 23. 23 Functions. cont‟d Initializing Pointer to function. EX#2: void my_int_func(int x) { printf( "%dn", x ); } int main() { void (*ptr)(int); /* the ampersand is actually optional */ ptr = &my_int_func; (*ptr)( 2 ); //or ptr(2); return 0; }
  • 25. Email: mo7amed.fawzy33@gmail.com Phone: 01006032792 Facebook: mo7amed_fawzy33@yahoo.com Contact me: 25
  • 26. 26