SlideShare ist ein Scribd-Unternehmen logo
1 von 49
Downloaden Sie, um offline zu lesen
By/Mohamed Fawzy
C-Programming Language
1 Day #3
2
Lecture Notes:
 Set your phone to vibration mode.
 Ask any time.
 During labs feel free to check any materials or internet.
Agenda:
3
Strings.
User defined data types (Structures, Union, Enum).
C-scope rules.
Typedef.
Modularity.
C Header File.
C storage classes.
C preprocessor.
Compiling C code.
Agenda:
4
Strings.
User defined data types (Structures, Union, Enum).
C-scope rules.
Typedef.
Modularity.
C Header File.
C storage classes.
C preprocessor.
Compiling C code.
5
• C doesn't contain data type called String.
• String is array of characters with NULL terminator „0‟.
EX#1:
char first_name[5]={„m‟,‟o‟,‟h‟,‟a‟,‟m‟,‟e‟,‟d‟,‟0‟};
Note:
In this case, the compiler isn't smart enough to figure we are constructing a string
And we must add NULL „0‟ terminator.
EX#2:
char first_name[8]=“Mohamed”;
Notes:
• In this case, the compiler realizes that we are constructing a string and
automatically adds the NULL terminator.
• The size of array is 8 not 7.
Strings.
6
EX#3:
Char name[]=“mohamed fawzy”;
Note:
In this case, compiler deduces the size of array from the initial value
Strings. cont‟d
7
Printing Strings.
EX#1:
char name[]=“Ahmed Osama”;
int i=0;
while(name[i] != „0‟)
printf(“%c”,name[i++]);
Using Pointers:
char name[]=“Ahmed Osama”;
Char *ptr;
Ptr=name;
while(*ptr != „0‟)
printf(“%c”,*ptr++);
• Printing Using String Feature.
printf(“%s”,name);
Strings. cont‟d
• Strings may be printed manually.
8
Strings. cont‟d
Take Care !!!
char name[7]=“Mohamed”;
Here, the compiler deliberately excludes the NULL terminator and it become array
of characters not a String. (try to print it by previous methods).
In printing EX#1, if we did not use string (NULL terminator), printf would continue
Printing characters randomly from memory until by chance found a byte containing
zero.
EX#2:
char my_name[]=“Mohamed fawzy”;
printf(“%s”,my_name);
my_name[8]=„0‟;
printf(“%s”,my_name);
9
Assigning to string.
• Strings may be initialized with “=” but not assigned to with “=”.
• The name of any array is a constant pointer to the zero element and cannot
be changes.
strcpy(destination,source);
EX#3:
char who[]=“Mohamed”;
who=“Ahmed”; //not allowed
strcpy(who,”Ahmed”);
Note:
Don't forget to include string header file.
#include <string.h>
Strings. cont‟d
10
Strings. cont‟d
Take Care !!!
char name[]=“Mohamed”;
strcpy(name, ” a really very large string indeed”);
This would overflow the array “name” and corrupt the memory Around it, this
would very likely cause a program to crash.
Solution
For solving this problem we would use another routine
strcpy(destination,source,size of destination);
EX#4:
strcpy(name , ”large string”, size of (name));
11
Strings. cont‟d
Take Care !!!
The previous solution will cause another problem, because it will copy no of elements
Equal to size (it wouldn't copy NULL terminator).
Solution
For solving this problem we would add just one line of code.
destination [ sizeof (destination)-1]=„0‟;
EX#5:
strcpy(name , ”large string”, sizeof (who));
name[sizeof(name)-1]=„0‟;
12
Strings. cont‟d
Pointers and strings.
We can store strings by two methods:
• char str[]=“Hello”;
• char *strptr=“Hello”;
Notes:
• In second method we ask C compiler to store it in some location in memory and
assign the address of the string in a char pointer.
• The difference between the two forms is that in 1st method, we cannot assign
string to another but, in 2nd method we can assign a char pointer to another char
pointer.
EX#6:
char str1[]=“Hello”;
char str2[10];
char *strptr=“Good Morning”;
char *q;
str2=str1; //giving an error
q=strptr; //no error
EX#7:
char str1[]=“Hello”;
char *strptr=“Hello”;
Str1=“Bye”; //give an error
Strptr=“Bye”; //give no error
13
Strings. cont‟d
Accepting string from user.
char my_name [45];
printf(“Enter your name n”);
scanf(“%s”,& my_name);
printf(“n you entered %s”,my_name);
Take Care !!!
In previous example, if we entered “Mohamed Fawzy” it would print “Mohamed” only
Because scanf function stores characters until we press space.
Solutions
We can use fgets function.
fgets included in <stdio.h>
fgets(string , size , stdin);
scanf(“%[^n]s”,my_name);
Modify scanf function.
14
Strings. cont‟d
EX#8:
char string [45];
printf(“Enter your name”);
fgets(string,45,stdin);
printf(“your name is %s”, string);
15
Strings. cont‟d
<string.h> functions
strcmp (str_1 , str_2);
This function take two strings as parameters from user, compare between them, it
return a value referred to result.
• If (str_1>str_2) it return 1
• If (str_1<str_2) it return -1
• If (str_1=str_2) it return 0
EX#9:
int x;
x=strcmp(“A”,”a”); //”a” > ”A”
printf(“%d”,x);
Note:
The most common APP for this function is comparing password.
1616
Strings. cont‟d
<string.h> functions
strcat (str_1 , str_2);
This function take two strings as parameters from user, concatenate them to
each other.
EX#9:
char str_1[30]=“Mohamed”;
puts(strcat (str_1,”_Fawzy”));
Note:
puts function equivalent to printf(“n”)
printf(“%sn”, puts(strcat (str_1,”_Fawzy”))
171717
Strings. cont‟d
<string.h> functions
strlen (str_1);
This function take one strings as parameter from user, return its length (no of characters
excluding NULL terminator).
EX#9:
int x;
char str_1[30]=“Mohamed”;
printf(“%d”, strlen (str_1));
Agenda:
18
Strings.
User defined data types (Structures, Union, Enum).
C-scope rules.
Typedef.
Modularity.
C Header File.
C storage classes.
C preprocessor.
Compiling C code.
19
User defined data types.
Structure.
 A structure is a collection of one or more variables with different data types
grouped together under a single name for convenient handling.
 Variables in structures are called members and may have any type including
arrays or other structures.
 Defining structure:
struct <structure name>
{
<variable_1>;
<variable_2>;
...........
};
Note:
• structure type defining doesn't tell the compiler to reserve any space in memory,
it only define a template.
• Defining structure is written in program header (before main).
User defined data types.
Structure. cont‟d
Method#1: in defining structure.
EX#2:
struct student_info
{
char name[10]; //10 bytes
int id; //2 bytes
char class; //1 byte
}ahmed , mohamed , osama;
Method#2:in main function.
EX#3:
struct student_info
{
char name[10];
int id;
char class;
};
In main
struct student_info ahmed,
mohamed;
 Declaring a structure:
Note:
• Now, you reserved 13 byte for each declared variable.
• Structure elements stored in continuous memory locations.
21
EX#1:
struct student_info
{
char name[10];
int id;
char class;
} ;
name Id class
struct student_info ahmed , mohamed ;
name id class
Structure. cont‟d
Note:
• We can initialize structures instances with declaring it.
struct student_info ahmed={“Ahmed”,20,3};
 Accessing Structure Elements:
ahmed.name=“Ahmed”;
ahmed.id=5;
ahmed.class=2;
mohamed.name=“Mohamed”;
mohamed.id=10;
mohamed.class=1;
name id class
Ahmed 5 2
Mohamed 10 1
Structure. cont‟d
Structure. cont‟d
 Array of structure.
EX#:
struct student_info arr[5];
Note:
• Here, struct student_info is the data type.
• In very large programs they are usually put in a separate header file,
and the file included in main program.
Structure. cont‟d
 Structures and pointers.
struct rectangle
{
int length;
int width;
};
int main()
{
struct rectangle rect_1;
struct rectangle *ptr;
Ptr=&rect_1;
Ptr->length=5;
Ptr->widdth=10;
}
Note:
• Ptr->length is equivalent to (*ptr).length
Take Care !!!
• *ptr.length not equivalent to (*ptr).length.
• *ptr.length =*(ptr.length) because “.” has
higher precedence than “*”.
Structure. cont‟d
 Passing structure by value:
struct rectangle
{
int length;
int width;
};
int area(struct rectangle rect)
{
return (rect.lengh*rect.width);
}
int main()
{
struct rectangle rect_1;
int rect_area;
rect_1.length=10;
rect_1.width=5;
rect_area=area(rect_1);
printf(“%d”,rect_area);
}
Structure. cont‟d
 Passing structure by reference:
struct rectangle
{
int length;
int width;
};
int area(struct rectangle *rectptr)
{
return (rectptr->lengh*rectptr->width);
}
int main()
{
struct rectangle rect_1;
struct rectangle *ptr;
int rect_area;
ptr->length=10;
ptr->width=5;
rect_area=area(&rect_1);
printf(“%d”,rect_area);
}
Structure. cont‟d
 Passing structure by reference: cont‟d
Notes:
• Passing structures by reference (using pointers) is better than passing
by value;
• Passing by value place struct in stack and structure may include arrays
or other structures.(may cause stack overflow error).
• It is better to use constant pointer to structure. (why)
const struct student_info *ptr.
const int *ptr; //pointer to constant integer
p++; //allowed
*p=15; //not allowed
//the value at the end of pointer cannot be changed
This helps you to avoid a common mistake
If (ptr->length = 0)
• The values of a structure variables can be assigned to another structure
variable of the same type using assignment operator.
ahmed=mohamed;
Agenda:
28
Strings.
User defined data types (Structures, Union, Enum).
C-scope rules.
Typedef.
Modularity.
C Header File.
C storage classes.
C preprocessor.
Compiling C code.
C-scope rules.
• A scope in any programming is a region of the program where a defined variable
can have its existence and beyond that variable can not be accessed. There are
three places where variables can be declared in C programming language:
1) Inside a function or a block which is called local variables.
2) Outside of all functions which is called global variables.
3) In the definition of function parameters which is called formal parameters.
Agenda:
30
Strings.
User defined data types (Structures, Union, Enum).
C-scope rules.
Typedef.
Modularity.
C Header File.
C storage classes.
C preprocessor.
Compiling C code.
Typedef
you can use to give a type a new name.
EX#:
typedef unsigned char BYTE;
BYTE b1,b2;
you can use typedef to give a name to user defined data type as well.
EX#:
typedef struct book
{
char title[50];
char author[50];
char subject[100];
int book_id;
} Book;
Book b1,b2;
32
• The typedef is limited to giving symbolic names to types only where as
#define can be used to define alias for values as well, like you can define
1 as ONE etc.
• The typedef interpretation is performed by the compiler where as
#define statements are processed by the pre-processor.
Typedef Vs. #define
Agenda:
33
Strings.
User defined data types (Structures, Union, Enum).
C-scope rules.
Typedef.
Modularity.
C Header File.
C storage classes.
C preprocessor.
Compiling C code.
Modularity.
file.h
file.c
Agenda:
35
Strings.
User defined data types (Structures, Union, Enum).
C-scope rules.
Typedef.
Modularity.
C Header File.
C storage classes.
C preprocessor.
Compiling C code.
C Header File.
• A header file is a file with extension .h which contains C function declarations and
macro definitions and to be shared between several source files. There are two
types of header files: the files that the programmer writes and the files that come
with your compiler.
• You request the use of a header file in your program by including it, with the C
preprocessing directive #include like you have seen inclusion of stdio.h header
file, which comes along with your compiler.
• A simple practice in C or C++ programs is that we keep all the constants, macros,
system wide global variables, and function prototypes in header files and include
that header file wherever it is required.
#include <file.h>
#include “file.h”
Agenda:
37
Strings.
User defined data types (Structures, Union, Enum).
C-scope rules.
Typedef.
Modularity.
C Header File.
C storage classes.
C preprocessor.
Compiling C code.
C storage classes.
• A storage class defines the scope (visibility) and life-time of variables and/or
functions within a C Program. These specifiers precede the type that they
modify. There are the following storage classes, which can be used in a C
Program:
1. auto
2. register
3. static
4. extern
 The auto Storage Class
EX:
int student_no;
auto int student no;
C storage classes. cont‟d
 The register Storage Class
The register storage class is used to define local variables that should be stored
in a register instead of RAM. This means that the variable has a maximum size
equal to the register size (usually one word) and can't have the unary '&' operator
applied to it (as it does not have a memory location).
EX:
register int student no;
Note:
defining 'register' does not mean that the variable will be stored in a
register. It means that it MIGHT be stored in a register depending on
hardware and implementation restrictions.
The auto storage class is the default storage class for all local variables.
C storage classes. cont‟d
 The static Storage Class
With Local variables:
The static storage class instructs the compiler to keep a local variable in existence
during the life-time of the program instead of creating and destroying it each time it
comes into and goes out of scope.
With Global variables:
it causes that variable's scope to be restricted to the file in which it is declared.
C storage classes. cont‟d
 The extern Storage Class
When you have multiple files and you define a global variable or function, which
will be used in other files also, then extern will be used in another file to give
reference of defined variable or function.
Agenda:
43
Strings.
User defined data types (Structures, Union, Enum).
C-scope rules.
Typedef.
Modularity.
C Header File.
C storage classes.
C preprocessor.
Compiling C code.
C - Preprocessors
Agenda:
45
Strings.
User defined data types (Structures, Union, Enum).
C-scope rules.
Typedef.
Modularity.
C Header File.
C storage classes.
C preprocessor.
Compiling C code.
Compiling C Program.
47
Hands ON
Email: mo7amed.fawzy33@gmail.com
Phone: 01006032792
Facebook: mo7amed_fawzy33@yahoo.com
Contact me:
48
49

Weitere ähnliche Inhalte

Was ist angesagt?

C programming session 02
C programming session 02C programming session 02
C programming session 02
Dushmanta Nath
 
C programming session 05
C programming session 05C programming session 05
C programming session 05
Dushmanta Nath
 
structure and union
structure and unionstructure and union
structure and union
student
 

Was ist angesagt? (19)

C programming session 02
C programming session 02C programming session 02
C programming session 02
 
Strings-Computer programming
Strings-Computer programmingStrings-Computer programming
Strings-Computer programming
 
Python Programming
Python ProgrammingPython Programming
Python Programming
 
C programming session 05
C programming session 05C programming session 05
C programming session 05
 
STRINGS IN C MRS.SOWMYA JYOTHI.pdf
STRINGS IN C MRS.SOWMYA JYOTHI.pdfSTRINGS IN C MRS.SOWMYA JYOTHI.pdf
STRINGS IN C MRS.SOWMYA JYOTHI.pdf
 
Cbasic
CbasicCbasic
Cbasic
 
C# String
C# StringC# String
C# String
 
C tutorial
C tutorialC tutorial
C tutorial
 
C tutorial
C tutorialC tutorial
C tutorial
 
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)
 
MANAGING INPUT AND OUTPUT OPERATIONS IN C MRS.SOWMYA JYOTHI.pdf
MANAGING INPUT AND OUTPUT OPERATIONS IN C    MRS.SOWMYA JYOTHI.pdfMANAGING INPUT AND OUTPUT OPERATIONS IN C    MRS.SOWMYA JYOTHI.pdf
MANAGING INPUT AND OUTPUT OPERATIONS IN C MRS.SOWMYA JYOTHI.pdf
 
Data type2 c
Data type2 cData type2 c
Data type2 c
 
C Tutorials
C TutorialsC Tutorials
C Tutorials
 
Synapseindia dot net development
Synapseindia dot net developmentSynapseindia dot net development
Synapseindia dot net development
 
14 strings
14 strings14 strings
14 strings
 
Generic Programming
Generic ProgrammingGeneric Programming
Generic Programming
 
structure and union
structure and unionstructure and union
structure and union
 
Bc0037
Bc0037Bc0037
Bc0037
 
Structures
StructuresStructures
Structures
 

Andere mochten auch

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
 
C language (Collected By Dushmanta)
C language  (Collected By Dushmanta)C language  (Collected By Dushmanta)
C language (Collected By Dushmanta)
Dushmanta Nath
 

Andere mochten auch (20)

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
 
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...
 
C programming day#2.
C programming day#2.C programming day#2.
C programming day#2.
 
Imaging on embedded GPUs
Imaging on embedded GPUsImaging on embedded GPUs
Imaging on embedded GPUs
 
CCTV
CCTVCCTV
CCTV
 
Synesis Embedded Video Analytics
Synesis Embedded Video AnalyticsSynesis Embedded Video Analytics
Synesis Embedded Video Analytics
 
Introduction to Basic C programming 01
Introduction to Basic C programming 01Introduction to Basic C programming 01
Introduction to Basic C programming 01
 
Embedded systems and robotics by scmandota
Embedded systems and robotics by scmandotaEmbedded systems and robotics by scmandota
Embedded systems and robotics by scmandota
 
introduction to Embedded System
introduction to Embedded Systemintroduction to Embedded System
introduction to Embedded System
 
Interfacing using ِAtmega16/32
Interfacing using ِAtmega16/32 Interfacing using ِAtmega16/32
Interfacing using ِAtmega16/32
 
Building a Network IP Camera using Erlang
Building a Network IP Camera using ErlangBuilding a Network IP Camera using Erlang
Building a Network IP Camera using Erlang
 
Prog lang-c
Prog lang-cProg lang-c
Prog lang-c
 
Introduction to Basic C programming 02
Introduction to Basic C programming 02Introduction to Basic C programming 02
Introduction to Basic C programming 02
 
C language (Collected By Dushmanta)
C language  (Collected By Dushmanta)C language  (Collected By Dushmanta)
C language (Collected By Dushmanta)
 
Programming in c
Programming in cProgramming in c
Programming in c
 
Automatic Number Plate Recognition (ANPR)
Automatic Number Plate Recognition (ANPR)Automatic Number Plate Recognition (ANPR)
Automatic Number Plate Recognition (ANPR)
 
Embedded C programming based on 8051 microcontroller
Embedded C programming based on 8051 microcontrollerEmbedded C programming based on 8051 microcontroller
Embedded C programming based on 8051 microcontroller
 

Ähnlich wie C programming day#3.

C++ Unit 1PPT which contains the Introduction and basic o C++ with OOOps conc...
C++ Unit 1PPT which contains the Introduction and basic o C++ with OOOps conc...C++ Unit 1PPT which contains the Introduction and basic o C++ with OOOps conc...
C++ Unit 1PPT which contains the Introduction and basic o C++ with OOOps conc...
ANUSUYA S
 
Lecture 15_Strings and Dynamic Memory Allocation.pptx
Lecture 15_Strings and  Dynamic Memory Allocation.pptxLecture 15_Strings and  Dynamic Memory Allocation.pptx
Lecture 15_Strings and Dynamic Memory Allocation.pptx
JawadTanvir
 
CS 23001 Computer Science II Data Structures & AbstractionPro.docx
CS 23001 Computer Science II Data Structures & AbstractionPro.docxCS 23001 Computer Science II Data Structures & AbstractionPro.docx
CS 23001 Computer Science II Data Structures & AbstractionPro.docx
faithxdunce63732
 

Ähnlich wie C programming day#3. (20)

C++ Unit 1PPT which contains the Introduction and basic o C++ with OOOps conc...
C++ Unit 1PPT which contains the Introduction and basic o C++ with OOOps conc...C++ Unit 1PPT which contains the Introduction and basic o C++ with OOOps conc...
C++ Unit 1PPT which contains the Introduction and basic o C++ with OOOps conc...
 
270_1_CIntro_Up_To_Functions.ppt
270_1_CIntro_Up_To_Functions.ppt270_1_CIntro_Up_To_Functions.ppt
270_1_CIntro_Up_To_Functions.ppt
 
270_1_CIntro_Up_To_Functions.ppt
270_1_CIntro_Up_To_Functions.ppt270_1_CIntro_Up_To_Functions.ppt
270_1_CIntro_Up_To_Functions.ppt
 
Survey of programming language getting started in C
Survey of programming language getting started in CSurvey of programming language getting started in C
Survey of programming language getting started in C
 
270 1 c_intro_up_to_functions
270 1 c_intro_up_to_functions270 1 c_intro_up_to_functions
270 1 c_intro_up_to_functions
 
270_1_CIntro_Up_To_Functions.ppt
270_1_CIntro_Up_To_Functions.ppt270_1_CIntro_Up_To_Functions.ppt
270_1_CIntro_Up_To_Functions.ppt
 
Structures-2
Structures-2Structures-2
Structures-2
 
C programming language tutorial
C programming language tutorial C programming language tutorial
C programming language tutorial
 
Lecture 15_Strings and Dynamic Memory Allocation.pptx
Lecture 15_Strings and  Dynamic Memory Allocation.pptxLecture 15_Strings and  Dynamic Memory Allocation.pptx
Lecture 15_Strings and Dynamic Memory Allocation.pptx
 
c++ referesher 1.pdf
c++ referesher 1.pdfc++ referesher 1.pdf
c++ referesher 1.pdf
 
Chapter1.pptx
Chapter1.pptxChapter1.pptx
Chapter1.pptx
 
C programming day#1
C programming day#1C programming day#1
C programming day#1
 
Lecture 3.mte 407
Lecture 3.mte 407Lecture 3.mte 407
Lecture 3.mte 407
 
C prog ppt
C prog pptC prog ppt
C prog ppt
 
Complete C programming Language Course
Complete C programming Language CourseComplete C programming Language Course
Complete C programming Language Course
 
C programming language
C programming languageC programming language
C programming language
 
Unit 3
Unit 3 Unit 3
Unit 3
 
C Programming Tutorial - www.infomtec.com
C Programming Tutorial - www.infomtec.comC Programming Tutorial - www.infomtec.com
C Programming Tutorial - www.infomtec.com
 
CS 23001 Computer Science II Data Structures & AbstractionPro.docx
CS 23001 Computer Science II Data Structures & AbstractionPro.docxCS 23001 Computer Science II Data Structures & AbstractionPro.docx
CS 23001 Computer Science II Data Structures & AbstractionPro.docx
 
C++primer
C++primerC++primer
C++primer
 

Mehr von Mohamed Fawzy (9)

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
 

Kürzlich hochgeladen

XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
ssuser89054b
 
Call Girls in Netaji Nagar, Delhi 💯 Call Us 🔝9953056974 🔝 Escort Service
Call Girls in Netaji Nagar, Delhi 💯 Call Us 🔝9953056974 🔝 Escort ServiceCall Girls in Netaji Nagar, Delhi 💯 Call Us 🔝9953056974 🔝 Escort Service
Call Girls in Netaji Nagar, Delhi 💯 Call Us 🔝9953056974 🔝 Escort Service
9953056974 Low Rate Call Girls In Saket, Delhi NCR
 
Call Girls In Bangalore ☎ 7737669865 🥵 Book Your One night Stand
Call Girls In Bangalore ☎ 7737669865 🥵 Book Your One night StandCall Girls In Bangalore ☎ 7737669865 🥵 Book Your One night Stand
Call Girls In Bangalore ☎ 7737669865 🥵 Book Your One night Stand
amitlee9823
 
VIP Call Girls Palanpur 7001035870 Whatsapp Number, 24/07 Booking
VIP Call Girls Palanpur 7001035870 Whatsapp Number, 24/07 BookingVIP Call Girls Palanpur 7001035870 Whatsapp Number, 24/07 Booking
VIP Call Girls Palanpur 7001035870 Whatsapp Number, 24/07 Booking
dharasingh5698
 
Cara Menggugurkan Sperma Yang Masuk Rahim Biyar Tidak Hamil
Cara Menggugurkan Sperma Yang Masuk Rahim Biyar Tidak HamilCara Menggugurkan Sperma Yang Masuk Rahim Biyar Tidak Hamil
Cara Menggugurkan Sperma Yang Masuk Rahim Biyar Tidak Hamil
Cara Menggugurkan Kandungan 087776558899
 
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
 

Kürzlich hochgeladen (20)

Intze Overhead Water Tank Design by Working Stress - IS Method.pdf
Intze Overhead Water Tank  Design by Working Stress - IS Method.pdfIntze Overhead Water Tank  Design by Working Stress - IS Method.pdf
Intze Overhead Water Tank Design by Working Stress - IS Method.pdf
 
University management System project report..pdf
University management System project report..pdfUniversity management System project report..pdf
University management System project report..pdf
 
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
 
Call Girls in Netaji Nagar, Delhi 💯 Call Us 🔝9953056974 🔝 Escort Service
Call Girls in Netaji Nagar, Delhi 💯 Call Us 🔝9953056974 🔝 Escort ServiceCall Girls in Netaji Nagar, Delhi 💯 Call Us 🔝9953056974 🔝 Escort Service
Call Girls in Netaji Nagar, Delhi 💯 Call Us 🔝9953056974 🔝 Escort Service
 
Intro To Electric Vehicles PDF Notes.pdf
Intro To Electric Vehicles PDF Notes.pdfIntro To Electric Vehicles PDF Notes.pdf
Intro To Electric Vehicles PDF Notes.pdf
 
Block diagram reduction techniques in control systems.ppt
Block diagram reduction techniques in control systems.pptBlock diagram reduction techniques in control systems.ppt
Block diagram reduction techniques in control systems.ppt
 
Call Girls In Bangalore ☎ 7737669865 🥵 Book Your One night Stand
Call Girls In Bangalore ☎ 7737669865 🥵 Book Your One night StandCall Girls In Bangalore ☎ 7737669865 🥵 Book Your One night Stand
Call Girls In Bangalore ☎ 7737669865 🥵 Book Your One night Stand
 
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...
 
Work-Permit-Receiver-in-Saudi-Aramco.pptx
Work-Permit-Receiver-in-Saudi-Aramco.pptxWork-Permit-Receiver-in-Saudi-Aramco.pptx
Work-Permit-Receiver-in-Saudi-Aramco.pptx
 
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
 
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
 
FEA Based Level 3 Assessment of Deformed Tanks with Fluid Induced Loads
FEA Based Level 3 Assessment of Deformed Tanks with Fluid Induced LoadsFEA Based Level 3 Assessment of Deformed Tanks with Fluid Induced Loads
FEA Based Level 3 Assessment of Deformed Tanks with Fluid Induced Loads
 
VIP Call Girls Palanpur 7001035870 Whatsapp Number, 24/07 Booking
VIP Call Girls Palanpur 7001035870 Whatsapp Number, 24/07 BookingVIP Call Girls Palanpur 7001035870 Whatsapp Number, 24/07 Booking
VIP Call Girls Palanpur 7001035870 Whatsapp Number, 24/07 Booking
 
Booking open Available Pune Call Girls Koregaon Park 6297143586 Call Hot Ind...
Booking open Available Pune Call Girls Koregaon Park  6297143586 Call Hot Ind...Booking open Available Pune Call Girls Koregaon Park  6297143586 Call Hot Ind...
Booking open Available Pune Call Girls Koregaon Park 6297143586 Call Hot Ind...
 
Cara Menggugurkan Sperma Yang Masuk Rahim Biyar Tidak Hamil
Cara Menggugurkan Sperma Yang Masuk Rahim Biyar Tidak HamilCara Menggugurkan Sperma Yang Masuk Rahim Biyar Tidak Hamil
Cara Menggugurkan Sperma Yang Masuk Rahim Biyar Tidak Hamil
 
(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
 
Thermal Engineering -unit - III & IV.ppt
Thermal Engineering -unit - III & IV.pptThermal Engineering -unit - III & IV.ppt
Thermal Engineering -unit - III & IV.ppt
 
UNIT - IV - Air Compressors and its Performance
UNIT - IV - Air Compressors and its PerformanceUNIT - IV - Air Compressors and its Performance
UNIT - IV - Air Compressors and its Performance
 
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
 
KubeKraft presentation @CloudNativeHooghly
KubeKraft presentation @CloudNativeHooghlyKubeKraft presentation @CloudNativeHooghly
KubeKraft presentation @CloudNativeHooghly
 

C programming day#3.

  • 2. 2 Lecture Notes:  Set your phone to vibration mode.  Ask any time.  During labs feel free to check any materials or internet.
  • 3. Agenda: 3 Strings. User defined data types (Structures, Union, Enum). C-scope rules. Typedef. Modularity. C Header File. C storage classes. C preprocessor. Compiling C code.
  • 4. Agenda: 4 Strings. User defined data types (Structures, Union, Enum). C-scope rules. Typedef. Modularity. C Header File. C storage classes. C preprocessor. Compiling C code.
  • 5. 5 • C doesn't contain data type called String. • String is array of characters with NULL terminator „0‟. EX#1: char first_name[5]={„m‟,‟o‟,‟h‟,‟a‟,‟m‟,‟e‟,‟d‟,‟0‟}; Note: In this case, the compiler isn't smart enough to figure we are constructing a string And we must add NULL „0‟ terminator. EX#2: char first_name[8]=“Mohamed”; Notes: • In this case, the compiler realizes that we are constructing a string and automatically adds the NULL terminator. • The size of array is 8 not 7. Strings.
  • 6. 6 EX#3: Char name[]=“mohamed fawzy”; Note: In this case, compiler deduces the size of array from the initial value Strings. cont‟d
  • 7. 7 Printing Strings. EX#1: char name[]=“Ahmed Osama”; int i=0; while(name[i] != „0‟) printf(“%c”,name[i++]); Using Pointers: char name[]=“Ahmed Osama”; Char *ptr; Ptr=name; while(*ptr != „0‟) printf(“%c”,*ptr++); • Printing Using String Feature. printf(“%s”,name); Strings. cont‟d • Strings may be printed manually.
  • 8. 8 Strings. cont‟d Take Care !!! char name[7]=“Mohamed”; Here, the compiler deliberately excludes the NULL terminator and it become array of characters not a String. (try to print it by previous methods). In printing EX#1, if we did not use string (NULL terminator), printf would continue Printing characters randomly from memory until by chance found a byte containing zero. EX#2: char my_name[]=“Mohamed fawzy”; printf(“%s”,my_name); my_name[8]=„0‟; printf(“%s”,my_name);
  • 9. 9 Assigning to string. • Strings may be initialized with “=” but not assigned to with “=”. • The name of any array is a constant pointer to the zero element and cannot be changes. strcpy(destination,source); EX#3: char who[]=“Mohamed”; who=“Ahmed”; //not allowed strcpy(who,”Ahmed”); Note: Don't forget to include string header file. #include <string.h> Strings. cont‟d
  • 10. 10 Strings. cont‟d Take Care !!! char name[]=“Mohamed”; strcpy(name, ” a really very large string indeed”); This would overflow the array “name” and corrupt the memory Around it, this would very likely cause a program to crash. Solution For solving this problem we would use another routine strcpy(destination,source,size of destination); EX#4: strcpy(name , ”large string”, size of (name));
  • 11. 11 Strings. cont‟d Take Care !!! The previous solution will cause another problem, because it will copy no of elements Equal to size (it wouldn't copy NULL terminator). Solution For solving this problem we would add just one line of code. destination [ sizeof (destination)-1]=„0‟; EX#5: strcpy(name , ”large string”, sizeof (who)); name[sizeof(name)-1]=„0‟;
  • 12. 12 Strings. cont‟d Pointers and strings. We can store strings by two methods: • char str[]=“Hello”; • char *strptr=“Hello”; Notes: • In second method we ask C compiler to store it in some location in memory and assign the address of the string in a char pointer. • The difference between the two forms is that in 1st method, we cannot assign string to another but, in 2nd method we can assign a char pointer to another char pointer. EX#6: char str1[]=“Hello”; char str2[10]; char *strptr=“Good Morning”; char *q; str2=str1; //giving an error q=strptr; //no error EX#7: char str1[]=“Hello”; char *strptr=“Hello”; Str1=“Bye”; //give an error Strptr=“Bye”; //give no error
  • 13. 13 Strings. cont‟d Accepting string from user. char my_name [45]; printf(“Enter your name n”); scanf(“%s”,& my_name); printf(“n you entered %s”,my_name); Take Care !!! In previous example, if we entered “Mohamed Fawzy” it would print “Mohamed” only Because scanf function stores characters until we press space. Solutions We can use fgets function. fgets included in <stdio.h> fgets(string , size , stdin); scanf(“%[^n]s”,my_name); Modify scanf function.
  • 14. 14 Strings. cont‟d EX#8: char string [45]; printf(“Enter your name”); fgets(string,45,stdin); printf(“your name is %s”, string);
  • 15. 15 Strings. cont‟d <string.h> functions strcmp (str_1 , str_2); This function take two strings as parameters from user, compare between them, it return a value referred to result. • If (str_1>str_2) it return 1 • If (str_1<str_2) it return -1 • If (str_1=str_2) it return 0 EX#9: int x; x=strcmp(“A”,”a”); //”a” > ”A” printf(“%d”,x); Note: The most common APP for this function is comparing password.
  • 16. 1616 Strings. cont‟d <string.h> functions strcat (str_1 , str_2); This function take two strings as parameters from user, concatenate them to each other. EX#9: char str_1[30]=“Mohamed”; puts(strcat (str_1,”_Fawzy”)); Note: puts function equivalent to printf(“n”) printf(“%sn”, puts(strcat (str_1,”_Fawzy”))
  • 17. 171717 Strings. cont‟d <string.h> functions strlen (str_1); This function take one strings as parameter from user, return its length (no of characters excluding NULL terminator). EX#9: int x; char str_1[30]=“Mohamed”; printf(“%d”, strlen (str_1));
  • 18. Agenda: 18 Strings. User defined data types (Structures, Union, Enum). C-scope rules. Typedef. Modularity. C Header File. C storage classes. C preprocessor. Compiling C code.
  • 19. 19 User defined data types. Structure.  A structure is a collection of one or more variables with different data types grouped together under a single name for convenient handling.  Variables in structures are called members and may have any type including arrays or other structures.  Defining structure: struct <structure name> { <variable_1>; <variable_2>; ........... }; Note: • structure type defining doesn't tell the compiler to reserve any space in memory, it only define a template. • Defining structure is written in program header (before main).
  • 20. User defined data types. Structure. cont‟d Method#1: in defining structure. EX#2: struct student_info { char name[10]; //10 bytes int id; //2 bytes char class; //1 byte }ahmed , mohamed , osama; Method#2:in main function. EX#3: struct student_info { char name[10]; int id; char class; }; In main struct student_info ahmed, mohamed;  Declaring a structure: Note: • Now, you reserved 13 byte for each declared variable. • Structure elements stored in continuous memory locations.
  • 21. 21 EX#1: struct student_info { char name[10]; int id; char class; } ; name Id class struct student_info ahmed , mohamed ; name id class Structure. cont‟d Note: • We can initialize structures instances with declaring it. struct student_info ahmed={“Ahmed”,20,3};
  • 22.  Accessing Structure Elements: ahmed.name=“Ahmed”; ahmed.id=5; ahmed.class=2; mohamed.name=“Mohamed”; mohamed.id=10; mohamed.class=1; name id class Ahmed 5 2 Mohamed 10 1 Structure. cont‟d
  • 23. Structure. cont‟d  Array of structure. EX#: struct student_info arr[5]; Note: • Here, struct student_info is the data type. • In very large programs they are usually put in a separate header file, and the file included in main program.
  • 24. Structure. cont‟d  Structures and pointers. struct rectangle { int length; int width; }; int main() { struct rectangle rect_1; struct rectangle *ptr; Ptr=&rect_1; Ptr->length=5; Ptr->widdth=10; } Note: • Ptr->length is equivalent to (*ptr).length Take Care !!! • *ptr.length not equivalent to (*ptr).length. • *ptr.length =*(ptr.length) because “.” has higher precedence than “*”.
  • 25. Structure. cont‟d  Passing structure by value: struct rectangle { int length; int width; }; int area(struct rectangle rect) { return (rect.lengh*rect.width); } int main() { struct rectangle rect_1; int rect_area; rect_1.length=10; rect_1.width=5; rect_area=area(rect_1); printf(“%d”,rect_area); }
  • 26. Structure. cont‟d  Passing structure by reference: struct rectangle { int length; int width; }; int area(struct rectangle *rectptr) { return (rectptr->lengh*rectptr->width); } int main() { struct rectangle rect_1; struct rectangle *ptr; int rect_area; ptr->length=10; ptr->width=5; rect_area=area(&rect_1); printf(“%d”,rect_area); }
  • 27. Structure. cont‟d  Passing structure by reference: cont‟d Notes: • Passing structures by reference (using pointers) is better than passing by value; • Passing by value place struct in stack and structure may include arrays or other structures.(may cause stack overflow error). • It is better to use constant pointer to structure. (why) const struct student_info *ptr. const int *ptr; //pointer to constant integer p++; //allowed *p=15; //not allowed //the value at the end of pointer cannot be changed This helps you to avoid a common mistake If (ptr->length = 0) • The values of a structure variables can be assigned to another structure variable of the same type using assignment operator. ahmed=mohamed;
  • 28. Agenda: 28 Strings. User defined data types (Structures, Union, Enum). C-scope rules. Typedef. Modularity. C Header File. C storage classes. C preprocessor. Compiling C code.
  • 29. C-scope rules. • A scope in any programming is a region of the program where a defined variable can have its existence and beyond that variable can not be accessed. There are three places where variables can be declared in C programming language: 1) Inside a function or a block which is called local variables. 2) Outside of all functions which is called global variables. 3) In the definition of function parameters which is called formal parameters.
  • 30. Agenda: 30 Strings. User defined data types (Structures, Union, Enum). C-scope rules. Typedef. Modularity. C Header File. C storage classes. C preprocessor. Compiling C code.
  • 31. Typedef you can use to give a type a new name. EX#: typedef unsigned char BYTE; BYTE b1,b2; you can use typedef to give a name to user defined data type as well. EX#: typedef struct book { char title[50]; char author[50]; char subject[100]; int book_id; } Book; Book b1,b2;
  • 32. 32 • The typedef is limited to giving symbolic names to types only where as #define can be used to define alias for values as well, like you can define 1 as ONE etc. • The typedef interpretation is performed by the compiler where as #define statements are processed by the pre-processor. Typedef Vs. #define
  • 33. Agenda: 33 Strings. User defined data types (Structures, Union, Enum). C-scope rules. Typedef. Modularity. C Header File. C storage classes. C preprocessor. Compiling C code.
  • 35. Agenda: 35 Strings. User defined data types (Structures, Union, Enum). C-scope rules. Typedef. Modularity. C Header File. C storage classes. C preprocessor. Compiling C code.
  • 36. C Header File. • A header file is a file with extension .h which contains C function declarations and macro definitions and to be shared between several source files. There are two types of header files: the files that the programmer writes and the files that come with your compiler. • You request the use of a header file in your program by including it, with the C preprocessing directive #include like you have seen inclusion of stdio.h header file, which comes along with your compiler. • A simple practice in C or C++ programs is that we keep all the constants, macros, system wide global variables, and function prototypes in header files and include that header file wherever it is required. #include <file.h> #include “file.h”
  • 37. Agenda: 37 Strings. User defined data types (Structures, Union, Enum). C-scope rules. Typedef. Modularity. C Header File. C storage classes. C preprocessor. Compiling C code.
  • 38. C storage classes. • A storage class defines the scope (visibility) and life-time of variables and/or functions within a C Program. These specifiers precede the type that they modify. There are the following storage classes, which can be used in a C Program: 1. auto 2. register 3. static 4. extern
  • 39.  The auto Storage Class EX: int student_no; auto int student no; C storage classes. cont‟d  The register Storage Class The register storage class is used to define local variables that should be stored in a register instead of RAM. This means that the variable has a maximum size equal to the register size (usually one word) and can't have the unary '&' operator applied to it (as it does not have a memory location). EX: register int student no; Note: defining 'register' does not mean that the variable will be stored in a register. It means that it MIGHT be stored in a register depending on hardware and implementation restrictions. The auto storage class is the default storage class for all local variables.
  • 40. C storage classes. cont‟d  The static Storage Class With Local variables: The static storage class instructs the compiler to keep a local variable in existence during the life-time of the program instead of creating and destroying it each time it comes into and goes out of scope. With Global variables: it causes that variable's scope to be restricted to the file in which it is declared.
  • 41. C storage classes. cont‟d  The extern Storage Class When you have multiple files and you define a global variable or function, which will be used in other files also, then extern will be used in another file to give reference of defined variable or function.
  • 42.
  • 43. Agenda: 43 Strings. User defined data types (Structures, Union, Enum). C-scope rules. Typedef. Modularity. C Header File. C storage classes. C preprocessor. Compiling C code.
  • 45. Agenda: 45 Strings. User defined data types (Structures, Union, Enum). C-scope rules. Typedef. Modularity. C Header File. C storage classes. C preprocessor. Compiling C code.
  • 48. Email: mo7amed.fawzy33@gmail.com Phone: 01006032792 Facebook: mo7amed_fawzy33@yahoo.com Contact me: 48
  • 49. 49