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

Vishram Singh - Textbook of Anatomy Upper Limb and Thorax.. Volume 1 (1).pdf
Vishram Singh - Textbook of Anatomy  Upper Limb and Thorax.. Volume 1 (1).pdfVishram Singh - Textbook of Anatomy  Upper Limb and Thorax.. Volume 1 (1).pdf
Vishram Singh - Textbook of Anatomy Upper Limb and Thorax.. Volume 1 (1).pdfssuserdda66b
 
Key note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdfKey note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdfAdmir Softic
 
This PowerPoint helps students to consider the concept of infinity.
This PowerPoint helps students to consider the concept of infinity.This PowerPoint helps students to consider the concept of infinity.
This PowerPoint helps students to consider the concept of infinity.christianmathematics
 
How to Manage Global Discount in Odoo 17 POS
How to Manage Global Discount in Odoo 17 POSHow to Manage Global Discount in Odoo 17 POS
How to Manage Global Discount in Odoo 17 POSCeline George
 
Salient Features of India constitution especially power and functions
Salient Features of India constitution especially power and functionsSalient Features of India constitution especially power and functions
Salient Features of India constitution especially power and functionsKarakKing
 
Spellings Wk 3 English CAPS CARES Please Practise
Spellings Wk 3 English CAPS CARES Please PractiseSpellings Wk 3 English CAPS CARES Please Practise
Spellings Wk 3 English CAPS CARES Please PractiseAnaAcapella
 
Mixin Classes in Odoo 17 How to Extend Models Using Mixin Classes
Mixin Classes in Odoo 17  How to Extend Models Using Mixin ClassesMixin Classes in Odoo 17  How to Extend Models Using Mixin Classes
Mixin Classes in Odoo 17 How to Extend Models Using Mixin ClassesCeline George
 
Micro-Scholarship, What it is, How can it help me.pdf
Micro-Scholarship, What it is, How can it help me.pdfMicro-Scholarship, What it is, How can it help me.pdf
Micro-Scholarship, What it is, How can it help me.pdfPoh-Sun Goh
 
Accessible Digital Futures project (20/03/2024)
Accessible Digital Futures project (20/03/2024)Accessible Digital Futures project (20/03/2024)
Accessible Digital Futures project (20/03/2024)Jisc
 
Python Notes for mca i year students osmania university.docx
Python Notes for mca i year students osmania university.docxPython Notes for mca i year students osmania university.docx
Python Notes for mca i year students osmania university.docxRamakrishna Reddy Bijjam
 
Application orientated numerical on hev.ppt
Application orientated numerical on hev.pptApplication orientated numerical on hev.ppt
Application orientated numerical on hev.pptRamjanShidvankar
 
Food safety_Challenges food safety laboratories_.pdf
Food safety_Challenges food safety laboratories_.pdfFood safety_Challenges food safety laboratories_.pdf
Food safety_Challenges food safety laboratories_.pdfSherif Taha
 
Dyslexia AI Workshop for Slideshare.pptx
Dyslexia AI Workshop for Slideshare.pptxDyslexia AI Workshop for Slideshare.pptx
Dyslexia AI Workshop for Slideshare.pptxcallscotland1987
 
Unit-V; Pricing (Pharma Marketing Management).pptx
Unit-V; Pricing (Pharma Marketing Management).pptxUnit-V; Pricing (Pharma Marketing Management).pptx
Unit-V; Pricing (Pharma Marketing Management).pptxVishalSingh1417
 
SKILL OF INTRODUCING THE LESSON MICRO SKILLS.pptx
SKILL OF INTRODUCING THE LESSON MICRO SKILLS.pptxSKILL OF INTRODUCING THE LESSON MICRO SKILLS.pptx
SKILL OF INTRODUCING THE LESSON MICRO SKILLS.pptxAmanpreet Kaur
 
On National Teacher Day, meet the 2024-25 Kenan Fellows
On National Teacher Day, meet the 2024-25 Kenan FellowsOn National Teacher Day, meet the 2024-25 Kenan Fellows
On National Teacher Day, meet the 2024-25 Kenan FellowsMebane Rash
 
Sociology 101 Demonstration of Learning Exhibit
Sociology 101 Demonstration of Learning ExhibitSociology 101 Demonstration of Learning Exhibit
Sociology 101 Demonstration of Learning Exhibitjbellavia9
 
General Principles of Intellectual Property: Concepts of Intellectual Proper...
General Principles of Intellectual Property: Concepts of Intellectual  Proper...General Principles of Intellectual Property: Concepts of Intellectual  Proper...
General Principles of Intellectual Property: Concepts of Intellectual Proper...Poonam Aher Patil
 
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...Nguyen Thanh Tu Collection
 

Recently uploaded (20)

Vishram Singh - Textbook of Anatomy Upper Limb and Thorax.. Volume 1 (1).pdf
Vishram Singh - Textbook of Anatomy  Upper Limb and Thorax.. Volume 1 (1).pdfVishram Singh - Textbook of Anatomy  Upper Limb and Thorax.. Volume 1 (1).pdf
Vishram Singh - Textbook of Anatomy Upper Limb and Thorax.. Volume 1 (1).pdf
 
Key note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdfKey note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdf
 
This PowerPoint helps students to consider the concept of infinity.
This PowerPoint helps students to consider the concept of infinity.This PowerPoint helps students to consider the concept of infinity.
This PowerPoint helps students to consider the concept of infinity.
 
How to Manage Global Discount in Odoo 17 POS
How to Manage Global Discount in Odoo 17 POSHow to Manage Global Discount in Odoo 17 POS
How to Manage Global Discount in Odoo 17 POS
 
Salient Features of India constitution especially power and functions
Salient Features of India constitution especially power and functionsSalient Features of India constitution especially power and functions
Salient Features of India constitution especially power and functions
 
Spellings Wk 3 English CAPS CARES Please Practise
Spellings Wk 3 English CAPS CARES Please PractiseSpellings Wk 3 English CAPS CARES Please Practise
Spellings Wk 3 English CAPS CARES Please Practise
 
Mixin Classes in Odoo 17 How to Extend Models Using Mixin Classes
Mixin Classes in Odoo 17  How to Extend Models Using Mixin ClassesMixin Classes in Odoo 17  How to Extend Models Using Mixin Classes
Mixin Classes in Odoo 17 How to Extend Models Using Mixin Classes
 
Micro-Scholarship, What it is, How can it help me.pdf
Micro-Scholarship, What it is, How can it help me.pdfMicro-Scholarship, What it is, How can it help me.pdf
Micro-Scholarship, What it is, How can it help me.pdf
 
Accessible Digital Futures project (20/03/2024)
Accessible Digital Futures project (20/03/2024)Accessible Digital Futures project (20/03/2024)
Accessible Digital Futures project (20/03/2024)
 
Python Notes for mca i year students osmania university.docx
Python Notes for mca i year students osmania university.docxPython Notes for mca i year students osmania university.docx
Python Notes for mca i year students osmania university.docx
 
Mehran University Newsletter Vol-X, Issue-I, 2024
Mehran University Newsletter Vol-X, Issue-I, 2024Mehran University Newsletter Vol-X, Issue-I, 2024
Mehran University Newsletter Vol-X, Issue-I, 2024
 
Application orientated numerical on hev.ppt
Application orientated numerical on hev.pptApplication orientated numerical on hev.ppt
Application orientated numerical on hev.ppt
 
Food safety_Challenges food safety laboratories_.pdf
Food safety_Challenges food safety laboratories_.pdfFood safety_Challenges food safety laboratories_.pdf
Food safety_Challenges food safety laboratories_.pdf
 
Dyslexia AI Workshop for Slideshare.pptx
Dyslexia AI Workshop for Slideshare.pptxDyslexia AI Workshop for Slideshare.pptx
Dyslexia AI Workshop for Slideshare.pptx
 
Unit-V; Pricing (Pharma Marketing Management).pptx
Unit-V; Pricing (Pharma Marketing Management).pptxUnit-V; Pricing (Pharma Marketing Management).pptx
Unit-V; Pricing (Pharma Marketing Management).pptx
 
SKILL OF INTRODUCING THE LESSON MICRO SKILLS.pptx
SKILL OF INTRODUCING THE LESSON MICRO SKILLS.pptxSKILL OF INTRODUCING THE LESSON MICRO SKILLS.pptx
SKILL OF INTRODUCING THE LESSON MICRO SKILLS.pptx
 
On National Teacher Day, meet the 2024-25 Kenan Fellows
On National Teacher Day, meet the 2024-25 Kenan FellowsOn National Teacher Day, meet the 2024-25 Kenan Fellows
On National Teacher Day, meet the 2024-25 Kenan Fellows
 
Sociology 101 Demonstration of Learning Exhibit
Sociology 101 Demonstration of Learning ExhibitSociology 101 Demonstration of Learning Exhibit
Sociology 101 Demonstration of Learning Exhibit
 
General Principles of Intellectual Property: Concepts of Intellectual Proper...
General Principles of Intellectual Property: Concepts of Intellectual  Proper...General Principles of Intellectual Property: Concepts of Intellectual  Proper...
General Principles of Intellectual Property: Concepts of Intellectual Proper...
 
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
 

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