SlideShare a Scribd company logo
1 of 26
Download to read offline
Lecture 5
C
Language
ENG : HATEM ABD EL-SALAM
• String
• Pointers
Agenda
String
• String is array of characters. It is terminated by a null character 0
• Here, "c string tutorial" is a string. When, compiler encounter strings,
it appends a null character 0 at the end of string
String (Cont.)
• Initialization of strings
Reading Strings from user
Using scanf()
• The scanf() function only takes the first entered word. The function
terminates when it encounters a white space (or just space).
Reading Strings from user
• Example
Reading Strings from user
Using getchar()
• In the program above, using the function getchar(), ch gets a single
character from the user each time.
Reading Strings from user
Using standard library function
• To make life easier, there are predefined functions gets() and puts() in C
language to read and display string respectively.
String (Cont.)
Passing Strings to Functions
String (Cont.)
• Strings handling functions are defined under "string.h" header file.
EXERCISES
Exercises
• C Program to Find the Length of a String
• C Program to Find the Frequency of Characters in a String
• C Program to Find the Number of Vowels, Consonants, Digits and White space in a
String
• C program to Concatenate Two Strings
• C Program to Copy a String
• C Program to Convert string to lowercase
• C Program to Convert string to uppercase
Pointers
Address in C
• If you have a variable var in your program, &var will give you its address
in the memory, where & is commonly called the reference operator.
• You must have seen this notation while using scanf() function. It was
used in the function to store the user inputted value in the address of
var.
Pointers (Cont.)
Pointer variables
• In C, there is a special variable that stores just the address of another
variable. It is called Pointer variable or, simply, a pointer.
• Above statement defines, p as pointer variable of type int.
• Note: The * sign when declaring a pointer is not a dereference
operator. It is just a similar notation that creates a pointer.
Pointers (Cont.)
Reference operator (&) and Dereference operator (*)
• & is called reference operator. It gives you the address of a variable.
• Likewise, there is another operator that gets you the value from the
address, it is called a dereference operator (*).
Explanation of program
1. int* pc; creates a pointer pc and int c; creates a normal variable c. Since pc and c are both not
initialized, pointer pc points to either no address or a random address. Likewise, variable c is
assigned an address but contains a random/garbage value.
2. c=22; assigns 22 to the variable c, i.e.,22 is stored in the memory location of variable c. Note
that, when printing &c (address of c), we use %u rather than %d since address is usually
expressed as an unsigned integer (always positive).
3. pc=&c; assigns the address of variable to c to the pointer pc. When printing, you see value of pc
is the same as the address of c and the content of pc (*pc) is 22 as well.
4. c=11; assigns 11 to variable c. We assign a new value to c to see its effect on pointer pc.
5. Since, pointer pc points to the same address as c, value pointed by pointer pc is 11 as well.
Printing the address and content of pc shows the updated content as 11.
6. *pc=2; changes the contents of the memory location pointed by pointer pc to 2. Since the
address of pointer pc is same as address of c, value of c also changes to 2.
Pointers (Cont.)
Common mistakes when working with pointers
• In both cases, pointer pc is not pointing to the address of c.
Pointers (Cont.)
Pointers and Arrays
• Consider an array:
• In C programming, name of the array always points to address of the
first element of an array.
• In the above example, arr and &arr[0] points to the address of the first
element
Pointers (Cont.)
Pointers and Arrays
• Since, the addresses of both are the same, the values of arr and &arr[0]
are also the same.
• In C, you can declare an array and can use pointer to alter the data of an
array.
Pointers (Cont.)
Call by Reference
• When a pointer is passed as an argument to a function, address of the
memory location is passed instead of the value.
• This is because, pointer stores the location of the memory, and not the
value.
Explanation of program
1. The address of memory location num1 and num2 are passed to the function swap and the
pointers *n1 and *n2 accept those values.
2. So, now the pointer n1 and n2 points to the address of num1 and num2 respectively.
3. When, the value of pointers are changed, the value in the pointed memory location also changes
correspondingly.
4. Hence, changes made to *n1 and *n2 are reflected in num1 and num2 in the main function.
C- language Lecture 5

More Related Content

What's hot

Code generation in Compiler Design
Code generation in Compiler DesignCode generation in Compiler Design
Code generation in Compiler DesignKuppusamy P
 
Register allocation and assignment
Register allocation and assignmentRegister allocation and assignment
Register allocation and assignmentKarthi Keyan
 
Lecture 16 17 code-generation
Lecture 16 17 code-generationLecture 16 17 code-generation
Lecture 16 17 code-generationIffat Anjum
 
Chapter Seven(1)
Chapter Seven(1)Chapter Seven(1)
Chapter Seven(1)bolovv
 
Chapter Seven(2)
Chapter Seven(2)Chapter Seven(2)
Chapter Seven(2)bolovv
 
Advanced procedures in assembly language Full chapter ppt
Advanced procedures in assembly language Full chapter pptAdvanced procedures in assembly language Full chapter ppt
Advanced procedures in assembly language Full chapter pptMuhammad Sikandar Mustafa
 
C programming Ms. Pranoti Doke
C programming Ms. Pranoti DokeC programming Ms. Pranoti Doke
C programming Ms. Pranoti DokePranoti Doke
 
Learning New Semi-Supervised Deep Auto-encoder Features for Statistical Machi...
Learning New Semi-Supervised Deep Auto-encoder Features for Statistical Machi...Learning New Semi-Supervised Deep Auto-encoder Features for Statistical Machi...
Learning New Semi-Supervised Deep Auto-encoder Features for Statistical Machi...Vimukthi Wickramasinghe
 
Cupdf.com introduction to-data-structures-and-algorithm
Cupdf.com introduction to-data-structures-and-algorithmCupdf.com introduction to-data-structures-and-algorithm
Cupdf.com introduction to-data-structures-and-algorithmTarikuDabala1
 
Peephole optimization techniques in compiler design
Peephole optimization techniques in compiler designPeephole optimization techniques in compiler design
Peephole optimization techniques in compiler designAnul Chaudhary
 
220 runtime environments
220 runtime environments220 runtime environments
220 runtime environmentsJ'tong Atong
 
C programming session 01
C programming session 01C programming session 01
C programming session 01Dushmanta Nath
 
Unit v memory & programmable logic devices
Unit v   memory & programmable logic devicesUnit v   memory & programmable logic devices
Unit v memory & programmable logic devicesKanmaniRajamanickam
 

What's hot (20)

Compiler unit 5
Compiler  unit 5Compiler  unit 5
Compiler unit 5
 
Code generation in Compiler Design
Code generation in Compiler DesignCode generation in Compiler Design
Code generation in Compiler Design
 
Register allocation and assignment
Register allocation and assignmentRegister allocation and assignment
Register allocation and assignment
 
Lecture 16 17 code-generation
Lecture 16 17 code-generationLecture 16 17 code-generation
Lecture 16 17 code-generation
 
Chapter Seven(1)
Chapter Seven(1)Chapter Seven(1)
Chapter Seven(1)
 
Chapter Seven(2)
Chapter Seven(2)Chapter Seven(2)
Chapter Seven(2)
 
Advanced procedures in assembly language Full chapter ppt
Advanced procedures in assembly language Full chapter pptAdvanced procedures in assembly language Full chapter ppt
Advanced procedures in assembly language Full chapter ppt
 
Code Generation
Code GenerationCode Generation
Code Generation
 
C programming Ms. Pranoti Doke
C programming Ms. Pranoti DokeC programming Ms. Pranoti Doke
C programming Ms. Pranoti Doke
 
Learning New Semi-Supervised Deep Auto-encoder Features for Statistical Machi...
Learning New Semi-Supervised Deep Auto-encoder Features for Statistical Machi...Learning New Semi-Supervised Deep Auto-encoder Features for Statistical Machi...
Learning New Semi-Supervised Deep Auto-encoder Features for Statistical Machi...
 
Code generation
Code generationCode generation
Code generation
 
Compiler unit 2&3
Compiler unit 2&3Compiler unit 2&3
Compiler unit 2&3
 
Cupdf.com introduction to-data-structures-and-algorithm
Cupdf.com introduction to-data-structures-and-algorithmCupdf.com introduction to-data-structures-and-algorithm
Cupdf.com introduction to-data-structures-and-algorithm
 
Peephole optimization techniques in compiler design
Peephole optimization techniques in compiler designPeephole optimization techniques in compiler design
Peephole optimization techniques in compiler design
 
Run time storage
Run time storageRun time storage
Run time storage
 
220 runtime environments
220 runtime environments220 runtime environments
220 runtime environments
 
Optimization
OptimizationOptimization
Optimization
 
Clanguage
ClanguageClanguage
Clanguage
 
C programming session 01
C programming session 01C programming session 01
C programming session 01
 
Unit v memory & programmable logic devices
Unit v   memory & programmable logic devicesUnit v   memory & programmable logic devices
Unit v memory & programmable logic devices
 

Similar to C- language Lecture 5

COM1407: Type Casting, Command Line Arguments and Defining Constants
COM1407: Type Casting, Command Line Arguments and Defining Constants COM1407: Type Casting, Command Line Arguments and Defining Constants
COM1407: Type Casting, Command Line Arguments and Defining Constants Hemantha Kulathilake
 
FYBSC(CS)_UNIT-1_Pointers in C.pptx
FYBSC(CS)_UNIT-1_Pointers in C.pptxFYBSC(CS)_UNIT-1_Pointers in C.pptx
FYBSC(CS)_UNIT-1_Pointers in C.pptxsangeeta borde
 
C programming tutorial for Beginner
C programming tutorial for BeginnerC programming tutorial for Beginner
C programming tutorial for Beginnersophoeutsen2
 
M.Florence Dayana / Basics of C Language
M.Florence Dayana / Basics of C LanguageM.Florence Dayana / Basics of C Language
M.Florence Dayana / Basics of C LanguageDr.Florence Dayana
 
Basic Concepts of C Language.pptx
Basic Concepts of C Language.pptxBasic Concepts of C Language.pptx
Basic Concepts of C Language.pptxKorbanMaheshwari
 
(4) cpp automatic arrays_pointers_c-strings
(4) cpp automatic arrays_pointers_c-strings(4) cpp automatic arrays_pointers_c-strings
(4) cpp automatic arrays_pointers_c-stringsNico Ludwig
 
Pointers and single &multi dimentionalarrays.pptx
Pointers and single &multi dimentionalarrays.pptxPointers and single &multi dimentionalarrays.pptx
Pointers and single &multi dimentionalarrays.pptxRamakrishna Reddy Bijjam
 
Learn C LANGUAGE at ASIT
Learn C LANGUAGE at ASITLearn C LANGUAGE at ASIT
Learn C LANGUAGE at ASITASIT
 
C programming basic concepts of mahi.pptx
C programming basic concepts of mahi.pptxC programming basic concepts of mahi.pptx
C programming basic concepts of mahi.pptxKorbanMaheshwari
 
Pointers in C Language
Pointers in C LanguagePointers in C Language
Pointers in C Languagemadan reddy
 
EASY UNDERSTANDING OF POINTERS IN C LANGUAGE.pdf
EASY UNDERSTANDING OF POINTERS IN C LANGUAGE.pdfEASY UNDERSTANDING OF POINTERS IN C LANGUAGE.pdf
EASY UNDERSTANDING OF POINTERS IN C LANGUAGE.pdfsudhakargeruganti
 
Character Arrays and strings in c language
Character Arrays and strings in c languageCharacter Arrays and strings in c language
Character Arrays and strings in c languagemallikavin
 
03-arrays-pointers.ppt
03-arrays-pointers.ppt03-arrays-pointers.ppt
03-arrays-pointers.pptArifKamal36
 

Similar to C- language Lecture 5 (20)

Pointers
PointersPointers
Pointers
 
COM1407: Working with Pointers
COM1407: Working with PointersCOM1407: Working with Pointers
COM1407: Working with Pointers
 
COM1407: Type Casting, Command Line Arguments and Defining Constants
COM1407: Type Casting, Command Line Arguments and Defining Constants COM1407: Type Casting, Command Line Arguments and Defining Constants
COM1407: Type Casting, Command Line Arguments and Defining Constants
 
FYBSC(CS)_UNIT-1_Pointers in C.pptx
FYBSC(CS)_UNIT-1_Pointers in C.pptxFYBSC(CS)_UNIT-1_Pointers in C.pptx
FYBSC(CS)_UNIT-1_Pointers in C.pptx
 
C programming tutorial for Beginner
C programming tutorial for BeginnerC programming tutorial for Beginner
C programming tutorial for Beginner
 
M.Florence Dayana / Basics of C Language
M.Florence Dayana / Basics of C LanguageM.Florence Dayana / Basics of C Language
M.Florence Dayana / Basics of C Language
 
Basic Concepts of C Language.pptx
Basic Concepts of C Language.pptxBasic Concepts of C Language.pptx
Basic Concepts of C Language.pptx
 
20.C++Pointer.pptx
20.C++Pointer.pptx20.C++Pointer.pptx
20.C++Pointer.pptx
 
Lecture2.ppt
Lecture2.pptLecture2.ppt
Lecture2.ppt
 
(4) cpp automatic arrays_pointers_c-strings
(4) cpp automatic arrays_pointers_c-strings(4) cpp automatic arrays_pointers_c-strings
(4) cpp automatic arrays_pointers_c-strings
 
Pointers and single &multi dimentionalarrays.pptx
Pointers and single &multi dimentionalarrays.pptxPointers and single &multi dimentionalarrays.pptx
Pointers and single &multi dimentionalarrays.pptx
 
C Programming Unit-4
C Programming Unit-4C Programming Unit-4
C Programming Unit-4
 
Learn C LANGUAGE at ASIT
Learn C LANGUAGE at ASITLearn C LANGUAGE at ASIT
Learn C LANGUAGE at ASIT
 
C programming basic concepts of mahi.pptx
C programming basic concepts of mahi.pptxC programming basic concepts of mahi.pptx
C programming basic concepts of mahi.pptx
 
Pointers in C Language
Pointers in C LanguagePointers in C Language
Pointers in C Language
 
EASY UNDERSTANDING OF POINTERS IN C LANGUAGE.pdf
EASY UNDERSTANDING OF POINTERS IN C LANGUAGE.pdfEASY UNDERSTANDING OF POINTERS IN C LANGUAGE.pdf
EASY UNDERSTANDING OF POINTERS IN C LANGUAGE.pdf
 
Character Arrays and strings in c language
Character Arrays and strings in c languageCharacter Arrays and strings in c language
Character Arrays and strings in c language
 
Pointers.pdf
Pointers.pdfPointers.pdf
Pointers.pdf
 
Pointers
PointersPointers
Pointers
 
03-arrays-pointers.ppt
03-arrays-pointers.ppt03-arrays-pointers.ppt
03-arrays-pointers.ppt
 

More from Hatem Abd El-Salam

More from Hatem Abd El-Salam (13)

Java- language Lecture 7
Java- language Lecture 7Java- language Lecture 7
Java- language Lecture 7
 
Java- language Lecture 6
Java- language Lecture 6Java- language Lecture 6
Java- language Lecture 6
 
Java- language Lecture 5
Java- language Lecture 5Java- language Lecture 5
Java- language Lecture 5
 
Java- language Lecture 4
Java- language Lecture 4Java- language Lecture 4
Java- language Lecture 4
 
Java- language Lecture 3
Java- language Lecture 3Java- language Lecture 3
Java- language Lecture 3
 
Java- Language Lecture 2
Java- Language Lecture 2Java- Language Lecture 2
Java- Language Lecture 2
 
Java- language Lecture 1
Java- language Lecture 1Java- language Lecture 1
Java- language Lecture 1
 
introduction to embedded systems part 2
introduction to embedded systems part 2introduction to embedded systems part 2
introduction to embedded systems part 2
 
introduction to embedded systems part 1
introduction to embedded systems part 1introduction to embedded systems part 1
introduction to embedded systems part 1
 
C- language Lecture 8
C- language Lecture 8C- language Lecture 8
C- language Lecture 8
 
C- language Lecture 3
C- language Lecture 3C- language Lecture 3
C- language Lecture 3
 
C- Language Lecture 2
C- Language Lecture 2C- Language Lecture 2
C- Language Lecture 2
 
C-language Lecture 1
C-language Lecture 1C-language Lecture 1
C-language Lecture 1
 

Recently uploaded

_Math 4-Q4 Week 5.pptx Steps in Collecting Data
_Math 4-Q4 Week 5.pptx Steps in Collecting Data_Math 4-Q4 Week 5.pptx Steps in Collecting Data
_Math 4-Q4 Week 5.pptx Steps in Collecting DataJhengPantaleon
 
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPT
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPTECONOMIC CONTEXT - LONG FORM TV DRAMA - PPT
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPTiammrhaywood
 
Introduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher EducationIntroduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher Educationpboyjonauth
 
Hybridoma Technology ( Production , Purification , and Application )
Hybridoma Technology  ( Production , Purification , and Application  ) Hybridoma Technology  ( Production , Purification , and Application  )
Hybridoma Technology ( Production , Purification , and Application ) Sakshi Ghasle
 
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptxSOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptxiammrhaywood
 
EPANDING THE CONTENT OF AN OUTLINE using notes.pptx
EPANDING THE CONTENT OF AN OUTLINE using notes.pptxEPANDING THE CONTENT OF AN OUTLINE using notes.pptx
EPANDING THE CONTENT OF AN OUTLINE using notes.pptxRaymartEstabillo3
 
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️9953056974 Low Rate Call Girls In Saket, Delhi NCR
 
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptxPOINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptxSayali Powar
 
Presiding Officer Training module 2024 lok sabha elections
Presiding Officer Training module 2024 lok sabha electionsPresiding Officer Training module 2024 lok sabha elections
Presiding Officer Training module 2024 lok sabha electionsanshu789521
 
Class 11 Legal Studies Ch-1 Concept of State .pdf
Class 11 Legal Studies Ch-1 Concept of State .pdfClass 11 Legal Studies Ch-1 Concept of State .pdf
Class 11 Legal Studies Ch-1 Concept of State .pdfakmcokerachita
 
ENGLISH5 QUARTER4 MODULE1 WEEK1-3 How Visual and Multimedia Elements.pptx
ENGLISH5 QUARTER4 MODULE1 WEEK1-3 How Visual and Multimedia Elements.pptxENGLISH5 QUARTER4 MODULE1 WEEK1-3 How Visual and Multimedia Elements.pptx
ENGLISH5 QUARTER4 MODULE1 WEEK1-3 How Visual and Multimedia Elements.pptxAnaBeatriceAblay2
 
Final demo Grade 9 for demo Plan dessert.pptx
Final demo Grade 9 for demo Plan dessert.pptxFinal demo Grade 9 for demo Plan dessert.pptx
Final demo Grade 9 for demo Plan dessert.pptxAvyJaneVismanos
 
Painted Grey Ware.pptx, PGW Culture of India
Painted Grey Ware.pptx, PGW Culture of IndiaPainted Grey Ware.pptx, PGW Culture of India
Painted Grey Ware.pptx, PGW Culture of IndiaVirag Sontakke
 
Crayon Activity Handout For the Crayon A
Crayon Activity Handout For the Crayon ACrayon Activity Handout For the Crayon A
Crayon Activity Handout For the Crayon AUnboundStockton
 
Solving Puzzles Benefits Everyone (English).pptx
Solving Puzzles Benefits Everyone (English).pptxSolving Puzzles Benefits Everyone (English).pptx
Solving Puzzles Benefits Everyone (English).pptxOH TEIK BIN
 
Pharmacognosy Flower 3. Compositae 2023.pdf
Pharmacognosy Flower 3. Compositae 2023.pdfPharmacognosy Flower 3. Compositae 2023.pdf
Pharmacognosy Flower 3. Compositae 2023.pdfMahmoud M. Sallam
 
Biting mechanism of poisonous snakes.pdf
Biting mechanism of poisonous snakes.pdfBiting mechanism of poisonous snakes.pdf
Biting mechanism of poisonous snakes.pdfadityarao40181
 
A Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy ReformA Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy ReformChameera Dedduwage
 
Sanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdfSanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdfsanyamsingh5019
 

Recently uploaded (20)

_Math 4-Q4 Week 5.pptx Steps in Collecting Data
_Math 4-Q4 Week 5.pptx Steps in Collecting Data_Math 4-Q4 Week 5.pptx Steps in Collecting Data
_Math 4-Q4 Week 5.pptx Steps in Collecting Data
 
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPT
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPTECONOMIC CONTEXT - LONG FORM TV DRAMA - PPT
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPT
 
Staff of Color (SOC) Retention Efforts DDSD
Staff of Color (SOC) Retention Efforts DDSDStaff of Color (SOC) Retention Efforts DDSD
Staff of Color (SOC) Retention Efforts DDSD
 
Introduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher EducationIntroduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher Education
 
Hybridoma Technology ( Production , Purification , and Application )
Hybridoma Technology  ( Production , Purification , and Application  ) Hybridoma Technology  ( Production , Purification , and Application  )
Hybridoma Technology ( Production , Purification , and Application )
 
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptxSOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
 
EPANDING THE CONTENT OF AN OUTLINE using notes.pptx
EPANDING THE CONTENT OF AN OUTLINE using notes.pptxEPANDING THE CONTENT OF AN OUTLINE using notes.pptx
EPANDING THE CONTENT OF AN OUTLINE using notes.pptx
 
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
 
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptxPOINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
 
Presiding Officer Training module 2024 lok sabha elections
Presiding Officer Training module 2024 lok sabha electionsPresiding Officer Training module 2024 lok sabha elections
Presiding Officer Training module 2024 lok sabha elections
 
Class 11 Legal Studies Ch-1 Concept of State .pdf
Class 11 Legal Studies Ch-1 Concept of State .pdfClass 11 Legal Studies Ch-1 Concept of State .pdf
Class 11 Legal Studies Ch-1 Concept of State .pdf
 
ENGLISH5 QUARTER4 MODULE1 WEEK1-3 How Visual and Multimedia Elements.pptx
ENGLISH5 QUARTER4 MODULE1 WEEK1-3 How Visual and Multimedia Elements.pptxENGLISH5 QUARTER4 MODULE1 WEEK1-3 How Visual and Multimedia Elements.pptx
ENGLISH5 QUARTER4 MODULE1 WEEK1-3 How Visual and Multimedia Elements.pptx
 
Final demo Grade 9 for demo Plan dessert.pptx
Final demo Grade 9 for demo Plan dessert.pptxFinal demo Grade 9 for demo Plan dessert.pptx
Final demo Grade 9 for demo Plan dessert.pptx
 
Painted Grey Ware.pptx, PGW Culture of India
Painted Grey Ware.pptx, PGW Culture of IndiaPainted Grey Ware.pptx, PGW Culture of India
Painted Grey Ware.pptx, PGW Culture of India
 
Crayon Activity Handout For the Crayon A
Crayon Activity Handout For the Crayon ACrayon Activity Handout For the Crayon A
Crayon Activity Handout For the Crayon A
 
Solving Puzzles Benefits Everyone (English).pptx
Solving Puzzles Benefits Everyone (English).pptxSolving Puzzles Benefits Everyone (English).pptx
Solving Puzzles Benefits Everyone (English).pptx
 
Pharmacognosy Flower 3. Compositae 2023.pdf
Pharmacognosy Flower 3. Compositae 2023.pdfPharmacognosy Flower 3. Compositae 2023.pdf
Pharmacognosy Flower 3. Compositae 2023.pdf
 
Biting mechanism of poisonous snakes.pdf
Biting mechanism of poisonous snakes.pdfBiting mechanism of poisonous snakes.pdf
Biting mechanism of poisonous snakes.pdf
 
A Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy ReformA Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy Reform
 
Sanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdfSanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdf
 

C- language Lecture 5

  • 1. Lecture 5 C Language ENG : HATEM ABD EL-SALAM
  • 3. String • String is array of characters. It is terminated by a null character 0 • Here, "c string tutorial" is a string. When, compiler encounter strings, it appends a null character 0 at the end of string
  • 5. Reading Strings from user Using scanf() • The scanf() function only takes the first entered word. The function terminates when it encounters a white space (or just space).
  • 6. Reading Strings from user • Example
  • 7. Reading Strings from user Using getchar() • In the program above, using the function getchar(), ch gets a single character from the user each time.
  • 8. Reading Strings from user Using standard library function • To make life easier, there are predefined functions gets() and puts() in C language to read and display string respectively.
  • 10. String (Cont.) • Strings handling functions are defined under "string.h" header file.
  • 12. Exercises • C Program to Find the Length of a String • C Program to Find the Frequency of Characters in a String • C Program to Find the Number of Vowels, Consonants, Digits and White space in a String • C program to Concatenate Two Strings • C Program to Copy a String • C Program to Convert string to lowercase • C Program to Convert string to uppercase
  • 13.
  • 14. Pointers Address in C • If you have a variable var in your program, &var will give you its address in the memory, where & is commonly called the reference operator. • You must have seen this notation while using scanf() function. It was used in the function to store the user inputted value in the address of var.
  • 15. Pointers (Cont.) Pointer variables • In C, there is a special variable that stores just the address of another variable. It is called Pointer variable or, simply, a pointer. • Above statement defines, p as pointer variable of type int. • Note: The * sign when declaring a pointer is not a dereference operator. It is just a similar notation that creates a pointer.
  • 16. Pointers (Cont.) Reference operator (&) and Dereference operator (*) • & is called reference operator. It gives you the address of a variable. • Likewise, there is another operator that gets you the value from the address, it is called a dereference operator (*).
  • 17.
  • 18. Explanation of program 1. int* pc; creates a pointer pc and int c; creates a normal variable c. Since pc and c are both not initialized, pointer pc points to either no address or a random address. Likewise, variable c is assigned an address but contains a random/garbage value. 2. c=22; assigns 22 to the variable c, i.e.,22 is stored in the memory location of variable c. Note that, when printing &c (address of c), we use %u rather than %d since address is usually expressed as an unsigned integer (always positive). 3. pc=&c; assigns the address of variable to c to the pointer pc. When printing, you see value of pc is the same as the address of c and the content of pc (*pc) is 22 as well. 4. c=11; assigns 11 to variable c. We assign a new value to c to see its effect on pointer pc. 5. Since, pointer pc points to the same address as c, value pointed by pointer pc is 11 as well. Printing the address and content of pc shows the updated content as 11. 6. *pc=2; changes the contents of the memory location pointed by pointer pc to 2. Since the address of pointer pc is same as address of c, value of c also changes to 2.
  • 19. Pointers (Cont.) Common mistakes when working with pointers • In both cases, pointer pc is not pointing to the address of c.
  • 20. Pointers (Cont.) Pointers and Arrays • Consider an array: • In C programming, name of the array always points to address of the first element of an array. • In the above example, arr and &arr[0] points to the address of the first element
  • 21. Pointers (Cont.) Pointers and Arrays • Since, the addresses of both are the same, the values of arr and &arr[0] are also the same. • In C, you can declare an array and can use pointer to alter the data of an array.
  • 22.
  • 23. Pointers (Cont.) Call by Reference • When a pointer is passed as an argument to a function, address of the memory location is passed instead of the value. • This is because, pointer stores the location of the memory, and not the value.
  • 24.
  • 25. Explanation of program 1. The address of memory location num1 and num2 are passed to the function swap and the pointers *n1 and *n2 accept those values. 2. So, now the pointer n1 and n2 points to the address of num1 and num2 respectively. 3. When, the value of pointers are changed, the value in the pointed memory location also changes correspondingly. 4. Hence, changes made to *n1 and *n2 are reflected in num1 and num2 in the main function.

Editor's Notes

  1. Address of c= 2686784