SlideShare una empresa de Scribd logo
1 de 26
Descargar para leer sin conexión
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

Más contenido relacionado

La actualidad más candente

#OOP_D_ITS - 2nd - C++ Getting Started
#OOP_D_ITS - 2nd - C++ Getting Started#OOP_D_ITS - 2nd - C++ Getting Started
#OOP_D_ITS - 2nd - C++ Getting Started
Hadziq Fabroyir
 
C programming session 05
C programming session 05C programming session 05
C programming session 05
Dushmanta Nath
 

La actualidad más candente (20)

Pointers in C
Pointers in CPointers in C
Pointers in C
 
Hooking signals and dumping the callstack
Hooking signals and dumping the callstackHooking signals and dumping the callstack
Hooking signals and dumping the callstack
 
Pointers in C
Pointers in CPointers in C
Pointers in C
 
C pointers
C pointersC pointers
C pointers
 
Pointers - DataStructures
Pointers - DataStructuresPointers - DataStructures
Pointers - DataStructures
 
C pointer basics
C pointer basicsC pointer basics
C pointer basics
 
Pointers
PointersPointers
Pointers
 
Pointers in C/C++ Programming
Pointers in C/C++ ProgrammingPointers in C/C++ Programming
Pointers in C/C++ Programming
 
Pointers+(2)
Pointers+(2)Pointers+(2)
Pointers+(2)
 
Python-02| Input, Output & Import
Python-02| Input, Output & ImportPython-02| Input, Output & Import
Python-02| Input, Output & Import
 
Basics of pointer, pointer expressions, pointer to pointer and pointer in fun...
Basics of pointer, pointer expressions, pointer to pointer and pointer in fun...Basics of pointer, pointer expressions, pointer to pointer and pointer in fun...
Basics of pointer, pointer expressions, pointer to pointer and pointer in fun...
 
C programming(part 3)
C programming(part 3)C programming(part 3)
C programming(part 3)
 
#OOP_D_ITS - 2nd - C++ Getting Started
#OOP_D_ITS - 2nd - C++ Getting Started#OOP_D_ITS - 2nd - C++ Getting Started
#OOP_D_ITS - 2nd - C++ Getting Started
 
Pointers
 Pointers Pointers
Pointers
 
Void pointer in c
Void pointer in cVoid pointer in c
Void pointer in c
 
Ponters
PontersPonters
Ponters
 
Lecture 17 - Strings
Lecture 17 - StringsLecture 17 - Strings
Lecture 17 - Strings
 
C programming session 05
C programming session 05C programming session 05
C programming session 05
 
Pointers in C
Pointers in CPointers in C
Pointers in C
 
Pointer in C
Pointer in CPointer in C
Pointer in C
 

Destacado

Lecture 15 ryuzo okada - vision processors for embedded computer vision
Lecture 15   ryuzo okada - vision processors for embedded computer visionLecture 15   ryuzo okada - vision processors for embedded computer vision
Lecture 15 ryuzo okada - vision processors for embedded computer vision
mustafa sarac
 

Destacado (20)

C programming day#3.
C programming day#3.C programming day#3.
C programming day#3.
 
02 Interfacing High Power Devices.2016
02 Interfacing High Power Devices.201602 Interfacing High Power Devices.2016
02 Interfacing High Power Devices.2016
 
Towards Embedded Computer Vision邁向嵌入式電腦視覺
Towards Embedded Computer Vision邁向嵌入式電腦視覺Towards Embedded Computer Vision邁向嵌入式電腦視覺
Towards Embedded Computer Vision邁向嵌入式電腦視覺
 
Ximea - the pc camera, 90 gflps smart camera
Ximea  - the pc camera, 90 gflps smart cameraXimea  - the pc camera, 90 gflps smart camera
Ximea - the pc camera, 90 gflps smart camera
 
Embedded Systems: Lecture 6: Linux & GNU
Embedded Systems: Lecture 6: Linux & GNUEmbedded Systems: Lecture 6: Linux & GNU
Embedded Systems: Lecture 6: Linux & GNU
 
Course 101: Lecture 6: Installing Ubuntu
Course 101: Lecture 6: Installing Ubuntu Course 101: Lecture 6: Installing Ubuntu
Course 101: Lecture 6: Installing Ubuntu
 
Embedded Systems: Lecture 8: The Raspberry Pi as a Linux Box
Embedded Systems: Lecture 8: The Raspberry Pi as a Linux BoxEmbedded Systems: Lecture 8: The Raspberry Pi as a Linux Box
Embedded Systems: Lecture 8: The Raspberry Pi as a Linux Box
 
Lecture 15 ryuzo okada - vision processors for embedded computer vision
Lecture 15   ryuzo okada - vision processors for embedded computer visionLecture 15   ryuzo okada - vision processors for embedded computer vision
Lecture 15 ryuzo okada - vision processors for embedded computer vision
 
Intelligent Video Surveillance - Synesis integrated hardware and software sol...
Intelligent Video Surveillance - Synesis integrated hardware and software sol...Intelligent Video Surveillance - Synesis integrated hardware and software sol...
Intelligent Video Surveillance - Synesis integrated hardware and software sol...
 
Embedded Systems: Lecture 7: Unwrapping the Raspberry Pi
Embedded Systems: Lecture 7: Unwrapping the Raspberry PiEmbedded Systems: Lecture 7: Unwrapping the Raspberry Pi
Embedded Systems: Lecture 7: Unwrapping the Raspberry Pi
 
Embedded Systems: Lecture 7: Lab 1: Preparing the Raspberry Pi
Embedded Systems: Lecture 7: Lab 1: Preparing the Raspberry PiEmbedded Systems: Lecture 7: Lab 1: Preparing 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 5: A Tour in RTOS LandEmbedded Systems: Lecture 5: A Tour in RTOS Land
Embedded Systems: Lecture 5: A Tour in RTOS Land
 
Embedded Systems: Lecture 8: Lab 1: Building a Raspberry Pi Based WiFi AP
Embedded Systems: Lecture 8: Lab 1: Building a Raspberry Pi Based WiFi APEmbedded Systems: Lecture 8: Lab 1: Building a Raspberry Pi Based WiFi AP
Embedded Systems: Lecture 8: Lab 1: Building a Raspberry Pi Based WiFi AP
 
Course 102: Lecture 16: Process Management (Part 2)
Course 102: Lecture 16: Process Management (Part 2) Course 102: Lecture 16: Process Management (Part 2)
Course 102: Lecture 16: Process Management (Part 2)
 
Embedded Systems: Lecture 2: Introduction to Embedded Systems
Embedded Systems: Lecture 2: Introduction to Embedded SystemsEmbedded Systems: Lecture 2: Introduction to Embedded Systems
Embedded Systems: Lecture 2: Introduction to Embedded Systems
 
Embedded Systems: Lecture 4: Selecting the Proper RTOS
Embedded Systems: Lecture 4: Selecting the Proper RTOSEmbedded Systems: Lecture 4: Selecting the Proper RTOS
Embedded Systems: Lecture 4: Selecting the Proper RTOS
 
Course 102: Lecture 11: Environment Variables
Course 102: Lecture 11: Environment VariablesCourse 102: Lecture 11: Environment Variables
Course 102: Lecture 11: Environment Variables
 
Embedded Systems: Lecture 10: Introduction to Git & GitHub (Part 1)
Embedded Systems: Lecture 10: Introduction to Git & GitHub (Part 1)Embedded Systems: Lecture 10: Introduction to Git & GitHub (Part 1)
Embedded Systems: Lecture 10: Introduction to Git & GitHub (Part 1)
 
Embedded Systems: Lecture 1: Course Overview
Embedded Systems: Lecture 1: Course OverviewEmbedded Systems: Lecture 1: Course Overview
Embedded Systems: Lecture 1: Course Overview
 
Course 102: Lecture 28: Virtual FileSystems
Course 102: Lecture 28: Virtual FileSystems Course 102: Lecture 28: Virtual FileSystems
Course 102: Lecture 28: Virtual FileSystems
 

Similar a C programming day#2.

Functions And Header Files In C++ | Bjarne stroustrup
Functions And Header Files In C++ | Bjarne stroustrupFunctions And Header Files In C++ | Bjarne stroustrup
Functions And Header Files In C++ | Bjarne stroustrup
SyedHaroonShah4
 

Similar a C programming day#2. (20)

C programming language tutorial
C programming language tutorial C programming language tutorial
C programming language tutorial
 
ch08.ppt
ch08.pptch08.ppt
ch08.ppt
 
C++_notes.pdf
C++_notes.pdfC++_notes.pdf
C++_notes.pdf
 
Embedded C - Lecture 2
Embedded C - Lecture 2Embedded C - Lecture 2
Embedded C - Lecture 2
 
Programming in C sesion 2
Programming in C sesion 2Programming in C sesion 2
Programming in C sesion 2
 
Unit 3
Unit 3 Unit 3
Unit 3
 
C tutorial
C tutorialC tutorial
C tutorial
 
C tutorial
C tutorialC tutorial
C tutorial
 
C tutorial
C tutorialC tutorial
C tutorial
 
C programming language
C programming languageC programming language
C programming language
 
Programming in C (part 2)
Programming in C (part 2)Programming in C (part 2)
Programming in C (part 2)
 
Functions And Header Files In C++ | Bjarne stroustrup
Functions And Header Files In C++ | Bjarne stroustrupFunctions And Header Files In C++ | Bjarne stroustrup
Functions And Header Files In C++ | Bjarne stroustrup
 
C
CC
C
 
C Tutorials
C TutorialsC Tutorials
C Tutorials
 
Improve Your Edge on Machine Learning - Day 1.pptx
Improve Your Edge on Machine Learning - Day 1.pptxImprove Your Edge on Machine Learning - Day 1.pptx
Improve Your Edge on Machine Learning - Day 1.pptx
 
C cheat sheet for varsity (extreme edition)
C cheat sheet for varsity (extreme edition)C cheat sheet for varsity (extreme edition)
C cheat sheet for varsity (extreme edition)
 
C Programming - Refresher - Part IV
C Programming - Refresher - Part IVC Programming - Refresher - Part IV
C Programming - Refresher - Part IV
 
dynamic-allocation.pdf
dynamic-allocation.pdfdynamic-allocation.pdf
dynamic-allocation.pdf
 
Pointers and Dynamic Memory Allocation
Pointers and Dynamic Memory AllocationPointers and Dynamic Memory Allocation
Pointers and Dynamic Memory Allocation
 
46630497 fun-pointer-1
46630497 fun-pointer-146630497 fun-pointer-1
46630497 fun-pointer-1
 

Más de Mohamed Fawzy (10)

07 Analogue to Digital Converter(ADC).2016
07 Analogue to Digital Converter(ADC).201607 Analogue to Digital Converter(ADC).2016
07 Analogue to Digital Converter(ADC).2016
 
06 Interfacing Keypad 4x4.2016
06 Interfacing Keypad 4x4.201606 Interfacing Keypad 4x4.2016
06 Interfacing Keypad 4x4.2016
 
05 EEPROM memory.2016
05 EEPROM memory.201605 EEPROM memory.2016
05 EEPROM memory.2016
 
04 Interfacing LCD Displays.2016
04 Interfacing LCD Displays.201604 Interfacing LCD Displays.2016
04 Interfacing LCD Displays.2016
 
01 GPIO||General Purpose Input Output.2016
01 GPIO||General Purpose Input Output.201601 GPIO||General Purpose Input Output.2016
01 GPIO||General Purpose Input Output.2016
 
00 let us get started.2016
00 let us get started.201600 let us get started.2016
00 let us get started.2016
 
أزاى تروح فى داهيه !!!
أزاى تروح فى داهيه !!!أزاى تروح فى داهيه !!!
أزاى تروح فى داهيه !!!
 
Ce from a to z
Ce from a to zCe from a to z
Ce from a to z
 
Taqa
TaqaTaqa
Taqa
 
C programming day#1
C programming day#1C programming day#1
C programming day#1
 

Último

Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...
Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...
Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...
Christo Ananth
 
Call Girls in Ramesh Nagar Delhi 💯 Call Us 🔝9953056974 🔝 Escort Service
Call Girls in Ramesh Nagar Delhi 💯 Call Us 🔝9953056974 🔝 Escort ServiceCall Girls in Ramesh Nagar Delhi 💯 Call Us 🔝9953056974 🔝 Escort Service
Call Girls in Ramesh Nagar Delhi 💯 Call Us 🔝9953056974 🔝 Escort Service
9953056974 Low Rate Call Girls In Saket, Delhi NCR
 
UNIT-V FMM.HYDRAULIC TURBINE - Construction and working
UNIT-V FMM.HYDRAULIC TURBINE - Construction and workingUNIT-V FMM.HYDRAULIC TURBINE - Construction and working
UNIT-V FMM.HYDRAULIC TURBINE - Construction and working
rknatarajan
 
FULL ENJOY Call Girls In Mahipalpur Delhi Contact Us 8377877756
FULL ENJOY Call Girls In Mahipalpur Delhi Contact Us 8377877756FULL ENJOY Call Girls In Mahipalpur Delhi Contact Us 8377877756
FULL ENJOY Call Girls In Mahipalpur Delhi Contact Us 8377877756
dollysharma2066
 
VIP Call Girls Ankleshwar 7001035870 Whatsapp Number, 24/07 Booking
VIP Call Girls Ankleshwar 7001035870 Whatsapp Number, 24/07 BookingVIP Call Girls Ankleshwar 7001035870 Whatsapp Number, 24/07 Booking
VIP Call Girls Ankleshwar 7001035870 Whatsapp Number, 24/07 Booking
dharasingh5698
 
result management system report for college project
result management system report for college projectresult management system report for college project
result management system report for college project
Tonystark477637
 

Último (20)

Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...
Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...
Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...
 
UNIT-II FMM-Flow Through Circular Conduits
UNIT-II FMM-Flow Through Circular ConduitsUNIT-II FMM-Flow Through Circular Conduits
UNIT-II FMM-Flow Through Circular Conduits
 
Call Girls in Ramesh Nagar Delhi 💯 Call Us 🔝9953056974 🔝 Escort Service
Call Girls in Ramesh Nagar Delhi 💯 Call Us 🔝9953056974 🔝 Escort ServiceCall Girls in Ramesh Nagar Delhi 💯 Call Us 🔝9953056974 🔝 Escort Service
Call Girls in Ramesh Nagar Delhi 💯 Call Us 🔝9953056974 🔝 Escort Service
 
data_management_and _data_science_cheat_sheet.pdf
data_management_and _data_science_cheat_sheet.pdfdata_management_and _data_science_cheat_sheet.pdf
data_management_and _data_science_cheat_sheet.pdf
 
KubeKraft presentation @CloudNativeHooghly
KubeKraft presentation @CloudNativeHooghlyKubeKraft presentation @CloudNativeHooghly
KubeKraft presentation @CloudNativeHooghly
 
BSides Seattle 2024 - Stopping Ethan Hunt From Taking Your Data.pptx
BSides Seattle 2024 - Stopping Ethan Hunt From Taking Your Data.pptxBSides Seattle 2024 - Stopping Ethan Hunt From Taking Your Data.pptx
BSides Seattle 2024 - Stopping Ethan Hunt From Taking Your Data.pptx
 
UNIT-V FMM.HYDRAULIC TURBINE - Construction and working
UNIT-V FMM.HYDRAULIC TURBINE - Construction and workingUNIT-V FMM.HYDRAULIC TURBINE - Construction and working
UNIT-V FMM.HYDRAULIC TURBINE - Construction and working
 
chapter 5.pptx: drainage and irrigation engineering
chapter 5.pptx: drainage and irrigation engineeringchapter 5.pptx: drainage and irrigation engineering
chapter 5.pptx: drainage and irrigation engineering
 
UNIT-III FMM. DIMENSIONAL ANALYSIS
UNIT-III FMM.        DIMENSIONAL ANALYSISUNIT-III FMM.        DIMENSIONAL ANALYSIS
UNIT-III FMM. DIMENSIONAL ANALYSIS
 
FULL ENJOY Call Girls In Mahipalpur Delhi Contact Us 8377877756
FULL ENJOY Call Girls In Mahipalpur Delhi Contact Us 8377877756FULL ENJOY Call Girls In Mahipalpur Delhi Contact Us 8377877756
FULL ENJOY Call Girls In Mahipalpur Delhi Contact Us 8377877756
 
(INDIRA) Call Girl Bhosari Call Now 8617697112 Bhosari Escorts 24x7
(INDIRA) Call Girl Bhosari Call Now 8617697112 Bhosari Escorts 24x7(INDIRA) Call Girl Bhosari Call Now 8617697112 Bhosari Escorts 24x7
(INDIRA) Call Girl Bhosari Call Now 8617697112 Bhosari Escorts 24x7
 
Vivazz, Mieres Social Housing Design Spain
Vivazz, Mieres Social Housing Design SpainVivazz, Mieres Social Housing Design Spain
Vivazz, Mieres Social Housing Design Spain
 
Call Girls Pimpri Chinchwad Call Me 7737669865 Budget Friendly No Advance Boo...
Call Girls Pimpri Chinchwad Call Me 7737669865 Budget Friendly No Advance Boo...Call Girls Pimpri Chinchwad Call Me 7737669865 Budget Friendly No Advance Boo...
Call Girls Pimpri Chinchwad Call Me 7737669865 Budget Friendly No Advance Boo...
 
Call Girls Walvekar Nagar Call Me 7737669865 Budget Friendly No Advance Booking
Call Girls Walvekar Nagar Call Me 7737669865 Budget Friendly No Advance BookingCall Girls Walvekar Nagar Call Me 7737669865 Budget Friendly No Advance Booking
Call Girls Walvekar Nagar Call Me 7737669865 Budget Friendly No Advance Booking
 
(INDIRA) Call Girl Meerut Call Now 8617697112 Meerut Escorts 24x7
(INDIRA) Call Girl Meerut Call Now 8617697112 Meerut Escorts 24x7(INDIRA) Call Girl Meerut Call Now 8617697112 Meerut Escorts 24x7
(INDIRA) Call Girl Meerut Call Now 8617697112 Meerut Escorts 24x7
 
Top Rated Pune Call Girls Budhwar Peth ⟟ 6297143586 ⟟ Call Me For Genuine Se...
Top Rated  Pune Call Girls Budhwar Peth ⟟ 6297143586 ⟟ Call Me For Genuine Se...Top Rated  Pune Call Girls Budhwar Peth ⟟ 6297143586 ⟟ Call Me For Genuine Se...
Top Rated Pune Call Girls Budhwar Peth ⟟ 6297143586 ⟟ Call Me For Genuine Se...
 
Roadmap to Membership of RICS - Pathways and Routes
Roadmap to Membership of RICS - Pathways and RoutesRoadmap to Membership of RICS - Pathways and Routes
Roadmap to Membership of RICS - Pathways and Routes
 
Thermal Engineering-R & A / C - unit - V
Thermal Engineering-R & A / C - unit - VThermal Engineering-R & A / C - unit - V
Thermal Engineering-R & A / C - unit - V
 
VIP Call Girls Ankleshwar 7001035870 Whatsapp Number, 24/07 Booking
VIP Call Girls Ankleshwar 7001035870 Whatsapp Number, 24/07 BookingVIP Call Girls Ankleshwar 7001035870 Whatsapp Number, 24/07 Booking
VIP Call Girls Ankleshwar 7001035870 Whatsapp Number, 24/07 Booking
 
result management system report for college project
result management system report for college projectresult management system report for college project
result management system report for college project
 

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