SlideShare a Scribd company logo
Computing Fundamentals
Dr. Muhammad Yousaf Hamza
Deputy Chief Engineer, PIEAS
Characters and Strings
Dr. Yousaf, PIEAS
Characters
• Characters
– Building blocks of programs
• Every program is a sequence of meaningfully grouped
characters
Dr. Yousaf, PIEAS
#include <stdio.h>
int main()
{
char x = ‘h’;
printf(“Character x is %cn", x); // note the use of %c
getchar();
return 0;
}
Dr. Yousaf, PIEAS
// How to read a character and store in a variable
#include <stdio.h>
int main()
{
char x ;
printf(“Enter a character”);
scanf(“%c",&x); // note the use of %c and &
printf(“ You entered %cn", x); // note the use of %c
getchar();
return 0;
}
Dr. Yousaf, PIEAS
#include<stdio.h>
int main()
{
char x = 'h', y;
printf("Character x is %cn", x); // note the use of %c
printf("Please enter a character and press entern");
y = getchar(); // It is valid. It will give correct output
printf("Character y is %cn", y); // note the use of %c
getchar(); return 0; }
Note:
y = scanf("%c",&y); is not correct, as discussed in the int (or
float) case, previously. Though, it will run but will give you
incorrect output when you print the value of y.
Dr. Yousaf, PIEAS
Strings
• Strings
– When we want to store and print text in C we use a string
– Series of characters treated as a single unit
• Can include letters, digits and special characters (*, /, $)
– char strname[5]="abcd";
– String literal - written in double quotes
“abcd“
A string is an array of type char which has '0'
(named as NULL) as the last character.
– Strings are arrays of characters
• String a pointer to first character
• Value of string is the address of first character (Later)
Dr. Yousaf, PIEAS
String Declaration and Initialization
• String declarations
char c[5]={'a','b','c','d’};
char c[]={'a','b','c','d'}; // it works without mentioning the size
char c[5]="abcd";
char c[]="abcd";
– Declare as a character array or a variable of type char
char color[] = "blue";
– Remember that strings represented as character arrays end with
'0'
• color has 5 elements
• A string is an array of type char which has '0' as the last character.
• '0' is a special character (like 'n') which means "stop now".
– char s[10]="unix"; /* s[4] is '0'; */
– char s[ ]="unix"; /* s has five elements */
Dr. Yousaf, PIEAS
Printing Strings
– printf("Long long ago.");
– Use printf
– char word[] = “Hello”
printf("%s", word); // note the use of %s
Example:
char str[ ] = "A message to display";
printf ("%sn", str);
printf expects to receive a string as an additional
parameter when it sees %s in the format string
printf knows how much to print out because of the
NULL character at the end of all strings.
When it finds a 0, it knows to stop.
Dr. Yousaf, PIEAS
Accessing Individual Characters
• The first element of any array in C is at index 0. The
second is at index 1, and so on ...
char s[6] =“Hello”;
printf("%c", s[2]; // note the use of %c
• This notation can be used in all kinds of statements and
expressions in C:
• For example: char c;
c = s[1];
if (s[0] == '-') …
switch (s[1]) ...
s [0] s[1] s [2] s[3] s[4]
Dr. Yousaf, PIEAS
Example
#include<stdio.h>
int main()
{
char str[15] = "unix and c";
printf("%sn", str); // unix and c
getchar();
return 0;
}
Note: '0' is not printed in the output.
Dr. Yousaf, PIEAS
Example
#include<stdio.h>
int main()
{
char str[15] = "unix and c";
printf("%sn", str);
str[6]='0';
printf("%sn", str);
str[2]='%';
printf("%sn", str);
printf("n");
getchar();
return 0;
}
Dr. Yousaf, PIEAS
Example
#include<stdio.h>
int main()
{
char str[15] = "unix and c";
printf("%sn", str);
str[6]='0';
printf("%sn", str);
str[2]='%';
printf("%sn", str);
printf("n");
getchar();
return 0;
}
Dr. Yousaf, PIEAS
Example
#include<stdio.h>
int main()
{
char str[15] = "unix and c";
printf("%sn", str); // unix and c
str[6]='0';
printf("%sn", str); // unix a
str[2]='%';
printf("%sn", str); // un%x a
printf("n");
getchar();
return 0;
}
Dr. Yousaf, PIEAS

More Related Content

What's hot

PHP Functions & Arrays
PHP Functions & ArraysPHP Functions & Arrays
PHP Functions & Arrays
Henry Osborne
 
Arrays
ArraysArrays
Smarty Template
Smarty TemplateSmarty Template
Smarty Template
mussawir20
 
Smarty Template
Smarty TemplateSmarty Template
Smarty Template
guest48224
 
Arrays in PHP
Arrays in PHPArrays in PHP
Arrays in PHP
Vineet Kumar Saini
 
Class 4 - PHP Arrays
Class 4 - PHP ArraysClass 4 - PHP Arrays
Class 4 - PHP Arrays
Ahmed Swilam
 
Array in php
Array in phpArray in php
Array in php
Ashok Kumar
 
Array in c
Array in cArray in c
Array in c
AnIsh Kumar
 
Array Of Pointers
Array Of PointersArray Of Pointers
Array Of Pointers
Sharad Dubey
 
Arrays in c language
Arrays in c languageArrays in c language
Arrays in c language
tanmaymodi4
 
Arrays in PHP
Arrays in PHPArrays in PHP
Sorting arrays in PHP
Sorting arrays in PHPSorting arrays in PHP
Sorting arrays in PHP
Vineet Kumar Saini
 
PHP Unit 4 arrays
PHP Unit 4 arraysPHP Unit 4 arrays
PHP Unit 4 arrays
Kumar
 
2CPP06 - Arrays and Pointers
2CPP06 - Arrays and Pointers2CPP06 - Arrays and Pointers
2CPP06 - Arrays and Pointers
Michael Heron
 
PHP array 1
PHP array 1PHP array 1
PHP array 1
Mudasir Syed
 
arrays in c
arrays in carrays in c
arrays in c
vidhi mehta
 
Introduction to perl_lists
Introduction to perl_listsIntroduction to perl_lists
Introduction to perl_lists
Vamshi Santhapuri
 
strings in c language and its importance
strings in c language and its importancestrings in c language and its importance
strings in c language and its importance
sirmanohar
 
Session 7 En
Session 7 EnSession 7 En
Session 7 En
guest91d2b3
 

What's hot (19)

PHP Functions & Arrays
PHP Functions & ArraysPHP Functions & Arrays
PHP Functions & Arrays
 
Arrays
ArraysArrays
Arrays
 
Smarty Template
Smarty TemplateSmarty Template
Smarty Template
 
Smarty Template
Smarty TemplateSmarty Template
Smarty Template
 
Arrays in PHP
Arrays in PHPArrays in PHP
Arrays in PHP
 
Class 4 - PHP Arrays
Class 4 - PHP ArraysClass 4 - PHP Arrays
Class 4 - PHP Arrays
 
Array in php
Array in phpArray in php
Array in php
 
Array in c
Array in cArray in c
Array in c
 
Array Of Pointers
Array Of PointersArray Of Pointers
Array Of Pointers
 
Arrays in c language
Arrays in c languageArrays in c language
Arrays in c language
 
Arrays in PHP
Arrays in PHPArrays in PHP
Arrays in PHP
 
Sorting arrays in PHP
Sorting arrays in PHPSorting arrays in PHP
Sorting arrays in PHP
 
PHP Unit 4 arrays
PHP Unit 4 arraysPHP Unit 4 arrays
PHP Unit 4 arrays
 
2CPP06 - Arrays and Pointers
2CPP06 - Arrays and Pointers2CPP06 - Arrays and Pointers
2CPP06 - Arrays and Pointers
 
PHP array 1
PHP array 1PHP array 1
PHP array 1
 
arrays in c
arrays in carrays in c
arrays in c
 
Introduction to perl_lists
Introduction to perl_listsIntroduction to perl_lists
Introduction to perl_lists
 
strings in c language and its importance
strings in c language and its importancestrings in c language and its importance
strings in c language and its importance
 
Session 7 En
Session 7 EnSession 7 En
Session 7 En
 

Similar to C Language Lecture 12

Data structure week 3
Data structure week 3Data structure week 3
Data structure week 3
karmuhtam
 
introduction to strings in c programming
introduction to strings in c programmingintroduction to strings in c programming
introduction to strings in c programming
mikeymanjiro2090
 
COm1407: Character & Strings
COm1407: Character & StringsCOm1407: Character & Strings
COm1407: Character & Strings
Hemantha Kulathilake
 
[ITP - Lecture 17] Strings in C/C++
[ITP - Lecture 17] Strings in C/C++[ITP - Lecture 17] Strings in C/C++
[ITP - Lecture 17] Strings in C/C++
Muhammad Hammad Waseem
 
Strings in C language
Strings in C languageStrings in C language
Strings in C language
P M Patil
 
String in c
String in cString in c
String in c
Suneel Dogra
 
C Programming Language Part 11
C Programming Language Part 11C Programming Language Part 11
C Programming Language Part 11
Rumman Ansari
 
Handling of character strings C programming
Handling of character strings C programmingHandling of character strings C programming
Handling of character strings C programming
Appili Vamsi Krishna
 
Strings IN C
Strings IN CStrings IN C
Strings IN C
yndaravind
 
3 character strings and formatted input output
3  character strings and formatted input output3  character strings and formatted input output
3 character strings and formatted input output
MomenMostafa
 
Character array (strings)
Character array (strings)Character array (strings)
Character array (strings)
sangrampatil81
 
C Programming Strings.docx
C Programming Strings.docxC Programming Strings.docx
C Programming Strings.docx
8759000398
 
fundamentals of c programming_String.pptx
fundamentals of c programming_String.pptxfundamentals of c programming_String.pptx
fundamentals of c programming_String.pptx
JStalinAsstProfessor
 
string , pointer
string , pointerstring , pointer
string , pointer
Arafat Bin Reza
 
String
StringString
String
SANTOSH RATH
 
Array &strings
Array &stringsArray &strings
Array &strings
UMA PARAMESWARI
 
Arrays
ArraysArrays
Arrays
AnaraAlam
 
4. chapter iii
4. chapter iii4. chapter iii
4. chapter iii
Chhom Karath
 
Review of C.pptx
Review of C.pptxReview of C.pptx
Review of C.pptx
Satyanandaram Nandigam
 
Strings
StringsStrings
Strings
Mitali Chugh
 

Similar to C Language Lecture 12 (20)

Data structure week 3
Data structure week 3Data structure week 3
Data structure week 3
 
introduction to strings in c programming
introduction to strings in c programmingintroduction to strings in c programming
introduction to strings in c programming
 
COm1407: Character & Strings
COm1407: Character & StringsCOm1407: Character & Strings
COm1407: Character & Strings
 
[ITP - Lecture 17] Strings in C/C++
[ITP - Lecture 17] Strings in C/C++[ITP - Lecture 17] Strings in C/C++
[ITP - Lecture 17] Strings in C/C++
 
Strings in C language
Strings in C languageStrings in C language
Strings in C language
 
String in c
String in cString in c
String in c
 
C Programming Language Part 11
C Programming Language Part 11C Programming Language Part 11
C Programming Language Part 11
 
Handling of character strings C programming
Handling of character strings C programmingHandling of character strings C programming
Handling of character strings C programming
 
Strings IN C
Strings IN CStrings IN C
Strings IN C
 
3 character strings and formatted input output
3  character strings and formatted input output3  character strings and formatted input output
3 character strings and formatted input output
 
Character array (strings)
Character array (strings)Character array (strings)
Character array (strings)
 
C Programming Strings.docx
C Programming Strings.docxC Programming Strings.docx
C Programming Strings.docx
 
fundamentals of c programming_String.pptx
fundamentals of c programming_String.pptxfundamentals of c programming_String.pptx
fundamentals of c programming_String.pptx
 
string , pointer
string , pointerstring , pointer
string , pointer
 
String
StringString
String
 
Array &strings
Array &stringsArray &strings
Array &strings
 
Arrays
ArraysArrays
Arrays
 
4. chapter iii
4. chapter iii4. chapter iii
4. chapter iii
 
Review of C.pptx
Review of C.pptxReview of C.pptx
Review of C.pptx
 
Strings
StringsStrings
Strings
 

More from Shahzaib Ajmal

C Language Lecture 22
C Language Lecture 22C Language Lecture 22
C Language Lecture 22
Shahzaib Ajmal
 
C Language Lecture 21
C Language Lecture 21C Language Lecture 21
C Language Lecture 21
Shahzaib Ajmal
 
C Language Lecture 20
C Language Lecture 20C Language Lecture 20
C Language Lecture 20
Shahzaib Ajmal
 
C Language Lecture 19
C Language Lecture 19C Language Lecture 19
C Language Lecture 19
Shahzaib Ajmal
 
C Language Lecture 18
C Language Lecture 18C Language Lecture 18
C Language Lecture 18
Shahzaib Ajmal
 
C Language Lecture 17
C Language Lecture 17C Language Lecture 17
C Language Lecture 17
Shahzaib Ajmal
 
C Language Lecture 16
C Language Lecture 16C Language Lecture 16
C Language Lecture 16
Shahzaib Ajmal
 
C Language Lecture 15
C Language Lecture 15C Language Lecture 15
C Language Lecture 15
Shahzaib Ajmal
 
C Language Lecture 11
C Language Lecture  11C Language Lecture  11
C Language Lecture 11
Shahzaib Ajmal
 
C Language Lecture 10
C Language Lecture 10C Language Lecture 10
C Language Lecture 10
Shahzaib Ajmal
 
C Language Lecture 9
C Language Lecture 9C Language Lecture 9
C Language Lecture 9
Shahzaib Ajmal
 
C Language Lecture 8
C Language Lecture 8C Language Lecture 8
C Language Lecture 8
Shahzaib Ajmal
 
C Language Lecture 7
C Language Lecture 7C Language Lecture 7
C Language Lecture 7
Shahzaib Ajmal
 
C Language Lecture 6
C Language Lecture 6C Language Lecture 6
C Language Lecture 6
Shahzaib Ajmal
 
C Language Lecture 5
C Language Lecture  5C Language Lecture  5
C Language Lecture 5
Shahzaib Ajmal
 
C Language Lecture 4
C Language Lecture  4C Language Lecture  4
C Language Lecture 4
Shahzaib Ajmal
 
C Language Lecture 3
C Language Lecture  3C Language Lecture  3
C Language Lecture 3
Shahzaib Ajmal
 
C Language Lecture 2
C Language Lecture  2C Language Lecture  2
C Language Lecture 2
Shahzaib Ajmal
 
C Language Lecture 1
C Language Lecture  1C Language Lecture  1
C Language Lecture 1
Shahzaib Ajmal
 

More from Shahzaib Ajmal (19)

C Language Lecture 22
C Language Lecture 22C Language Lecture 22
C Language Lecture 22
 
C Language Lecture 21
C Language Lecture 21C Language Lecture 21
C Language Lecture 21
 
C Language Lecture 20
C Language Lecture 20C Language Lecture 20
C Language Lecture 20
 
C Language Lecture 19
C Language Lecture 19C Language Lecture 19
C Language Lecture 19
 
C Language Lecture 18
C Language Lecture 18C Language Lecture 18
C Language Lecture 18
 
C Language Lecture 17
C Language Lecture 17C Language Lecture 17
C Language Lecture 17
 
C Language Lecture 16
C Language Lecture 16C Language Lecture 16
C Language Lecture 16
 
C Language Lecture 15
C Language Lecture 15C Language Lecture 15
C Language Lecture 15
 
C Language Lecture 11
C Language Lecture  11C Language Lecture  11
C Language Lecture 11
 
C Language Lecture 10
C Language Lecture 10C Language Lecture 10
C Language Lecture 10
 
C Language Lecture 9
C Language Lecture 9C Language Lecture 9
C Language Lecture 9
 
C Language Lecture 8
C Language Lecture 8C Language Lecture 8
C Language Lecture 8
 
C Language Lecture 7
C Language Lecture 7C Language Lecture 7
C Language Lecture 7
 
C Language Lecture 6
C Language Lecture 6C Language Lecture 6
C Language Lecture 6
 
C Language Lecture 5
C Language Lecture  5C Language Lecture  5
C Language Lecture 5
 
C Language Lecture 4
C Language Lecture  4C Language Lecture  4
C Language Lecture 4
 
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

ISO/IEC 27001, ISO/IEC 42001, and GDPR: Best Practices for Implementation and...
ISO/IEC 27001, ISO/IEC 42001, and GDPR: Best Practices for Implementation and...ISO/IEC 27001, ISO/IEC 42001, and GDPR: Best Practices for Implementation and...
ISO/IEC 27001, ISO/IEC 42001, and GDPR: Best Practices for Implementation and...
PECB
 
How to Fix the Import Error in the Odoo 17
How to Fix the Import Error in the Odoo 17How to Fix the Import Error in the Odoo 17
How to Fix the Import Error in the Odoo 17
Celine George
 
NEWSPAPERS - QUESTION 1 - REVISION POWERPOINT.pptx
NEWSPAPERS - QUESTION 1 - REVISION POWERPOINT.pptxNEWSPAPERS - QUESTION 1 - REVISION POWERPOINT.pptx
NEWSPAPERS - QUESTION 1 - REVISION POWERPOINT.pptx
iammrhaywood
 
writing about opinions about Australia the movie
writing about opinions about Australia the moviewriting about opinions about Australia the movie
writing about opinions about Australia the movie
Nicholas Montgomery
 
How to Build a Module in Odoo 17 Using the Scaffold Method
How to Build a Module in Odoo 17 Using the Scaffold MethodHow to Build a Module in Odoo 17 Using the Scaffold Method
How to Build a Module in Odoo 17 Using the Scaffold Method
Celine George
 
ANATOMY AND BIOMECHANICS OF HIP JOINT.pdf
ANATOMY AND BIOMECHANICS OF HIP JOINT.pdfANATOMY AND BIOMECHANICS OF HIP JOINT.pdf
ANATOMY AND BIOMECHANICS OF HIP JOINT.pdf
Priyankaranawat4
 
How to Make a Field Mandatory in Odoo 17
How to Make a Field Mandatory in Odoo 17How to Make a Field Mandatory in Odoo 17
How to Make a Field Mandatory in Odoo 17
Celine George
 
Your Skill Boost Masterclass: Strategies for Effective Upskilling
Your Skill Boost Masterclass: Strategies for Effective UpskillingYour Skill Boost Masterclass: Strategies for Effective Upskilling
Your Skill Boost Masterclass: Strategies for Effective Upskilling
Excellence Foundation for South Sudan
 
Pollock and Snow "DEIA in the Scholarly Landscape, Session One: Setting Expec...
Pollock and Snow "DEIA in the Scholarly Landscape, Session One: Setting Expec...Pollock and Snow "DEIA in the Scholarly Landscape, Session One: Setting Expec...
Pollock and Snow "DEIA in the Scholarly Landscape, Session One: Setting Expec...
National Information Standards Organization (NISO)
 
Liberal Approach to the Study of Indian Politics.pdf
Liberal Approach to the Study of Indian Politics.pdfLiberal Approach to the Study of Indian Politics.pdf
Liberal Approach to the Study of Indian Politics.pdf
WaniBasim
 
Leveraging Generative AI to Drive Nonprofit Innovation
Leveraging Generative AI to Drive Nonprofit InnovationLeveraging Generative AI to Drive Nonprofit Innovation
Leveraging Generative AI to Drive Nonprofit Innovation
TechSoup
 
Life upper-Intermediate B2 Workbook for student
Life upper-Intermediate B2 Workbook for studentLife upper-Intermediate B2 Workbook for student
Life upper-Intermediate B2 Workbook for student
NgcHiNguyn25
 
Main Java[All of the Base Concepts}.docx
Main Java[All of the Base Concepts}.docxMain Java[All of the Base Concepts}.docx
Main Java[All of the Base Concepts}.docx
adhitya5119
 
Chapter wise All Notes of First year Basic Civil Engineering.pptx
Chapter wise All Notes of First year Basic Civil Engineering.pptxChapter wise All Notes of First year Basic Civil Engineering.pptx
Chapter wise All Notes of First year Basic Civil Engineering.pptx
Denish Jangid
 
Chapter 4 - Islamic Financial Institutions in Malaysia.pptx
Chapter 4 - Islamic Financial Institutions in Malaysia.pptxChapter 4 - Islamic Financial Institutions in Malaysia.pptx
Chapter 4 - Islamic Financial Institutions in Malaysia.pptx
Mohd Adib Abd Muin, Senior Lecturer at Universiti Utara Malaysia
 
Advanced Java[Extra Concepts, Not Difficult].docx
Advanced Java[Extra Concepts, Not Difficult].docxAdvanced Java[Extra Concepts, Not Difficult].docx
Advanced Java[Extra Concepts, Not Difficult].docx
adhitya5119
 
MARY JANE WILSON, A “BOA MÃE” .
MARY JANE WILSON, A “BOA MÃE”           .MARY JANE WILSON, A “BOA MÃE”           .
MARY JANE WILSON, A “BOA MÃE” .
Colégio Santa Teresinha
 
What is Digital Literacy? A guest blog from Andy McLaughlin, University of Ab...
What is Digital Literacy? A guest blog from Andy McLaughlin, University of Ab...What is Digital Literacy? A guest blog from Andy McLaughlin, University of Ab...
What is Digital Literacy? A guest blog from Andy McLaughlin, University of Ab...
GeorgeMilliken2
 
Présentationvvvvvvvvvvvvvvvvvvvvvvvvvvvv2.pptx
Présentationvvvvvvvvvvvvvvvvvvvvvvvvvvvv2.pptxPrésentationvvvvvvvvvvvvvvvvvvvvvvvvvvvv2.pptx
Présentationvvvvvvvvvvvvvvvvvvvvvvvvvvvv2.pptx
siemaillard
 
The History of Stoke Newington Street Names
The History of Stoke Newington Street NamesThe History of Stoke Newington Street Names
The History of Stoke Newington Street Names
History of Stoke Newington
 

Recently uploaded (20)

ISO/IEC 27001, ISO/IEC 42001, and GDPR: Best Practices for Implementation and...
ISO/IEC 27001, ISO/IEC 42001, and GDPR: Best Practices for Implementation and...ISO/IEC 27001, ISO/IEC 42001, and GDPR: Best Practices for Implementation and...
ISO/IEC 27001, ISO/IEC 42001, and GDPR: Best Practices for Implementation and...
 
How to Fix the Import Error in the Odoo 17
How to Fix the Import Error in the Odoo 17How to Fix the Import Error in the Odoo 17
How to Fix the Import Error in the Odoo 17
 
NEWSPAPERS - QUESTION 1 - REVISION POWERPOINT.pptx
NEWSPAPERS - QUESTION 1 - REVISION POWERPOINT.pptxNEWSPAPERS - QUESTION 1 - REVISION POWERPOINT.pptx
NEWSPAPERS - QUESTION 1 - REVISION POWERPOINT.pptx
 
writing about opinions about Australia the movie
writing about opinions about Australia the moviewriting about opinions about Australia the movie
writing about opinions about Australia the movie
 
How to Build a Module in Odoo 17 Using the Scaffold Method
How to Build a Module in Odoo 17 Using the Scaffold MethodHow to Build a Module in Odoo 17 Using the Scaffold Method
How to Build a Module in Odoo 17 Using the Scaffold Method
 
ANATOMY AND BIOMECHANICS OF HIP JOINT.pdf
ANATOMY AND BIOMECHANICS OF HIP JOINT.pdfANATOMY AND BIOMECHANICS OF HIP JOINT.pdf
ANATOMY AND BIOMECHANICS OF HIP JOINT.pdf
 
How to Make a Field Mandatory in Odoo 17
How to Make a Field Mandatory in Odoo 17How to Make a Field Mandatory in Odoo 17
How to Make a Field Mandatory in Odoo 17
 
Your Skill Boost Masterclass: Strategies for Effective Upskilling
Your Skill Boost Masterclass: Strategies for Effective UpskillingYour Skill Boost Masterclass: Strategies for Effective Upskilling
Your Skill Boost Masterclass: Strategies for Effective Upskilling
 
Pollock and Snow "DEIA in the Scholarly Landscape, Session One: Setting Expec...
Pollock and Snow "DEIA in the Scholarly Landscape, Session One: Setting Expec...Pollock and Snow "DEIA in the Scholarly Landscape, Session One: Setting Expec...
Pollock and Snow "DEIA in the Scholarly Landscape, Session One: Setting Expec...
 
Liberal Approach to the Study of Indian Politics.pdf
Liberal Approach to the Study of Indian Politics.pdfLiberal Approach to the Study of Indian Politics.pdf
Liberal Approach to the Study of Indian Politics.pdf
 
Leveraging Generative AI to Drive Nonprofit Innovation
Leveraging Generative AI to Drive Nonprofit InnovationLeveraging Generative AI to Drive Nonprofit Innovation
Leveraging Generative AI to Drive Nonprofit Innovation
 
Life upper-Intermediate B2 Workbook for student
Life upper-Intermediate B2 Workbook for studentLife upper-Intermediate B2 Workbook for student
Life upper-Intermediate B2 Workbook for student
 
Main Java[All of the Base Concepts}.docx
Main Java[All of the Base Concepts}.docxMain Java[All of the Base Concepts}.docx
Main Java[All of the Base Concepts}.docx
 
Chapter wise All Notes of First year Basic Civil Engineering.pptx
Chapter wise All Notes of First year Basic Civil Engineering.pptxChapter wise All Notes of First year Basic Civil Engineering.pptx
Chapter wise All Notes of First year Basic Civil Engineering.pptx
 
Chapter 4 - Islamic Financial Institutions in Malaysia.pptx
Chapter 4 - Islamic Financial Institutions in Malaysia.pptxChapter 4 - Islamic Financial Institutions in Malaysia.pptx
Chapter 4 - Islamic Financial Institutions in Malaysia.pptx
 
Advanced Java[Extra Concepts, Not Difficult].docx
Advanced Java[Extra Concepts, Not Difficult].docxAdvanced Java[Extra Concepts, Not Difficult].docx
Advanced Java[Extra Concepts, Not Difficult].docx
 
MARY JANE WILSON, A “BOA MÃE” .
MARY JANE WILSON, A “BOA MÃE”           .MARY JANE WILSON, A “BOA MÃE”           .
MARY JANE WILSON, A “BOA MÃE” .
 
What is Digital Literacy? A guest blog from Andy McLaughlin, University of Ab...
What is Digital Literacy? A guest blog from Andy McLaughlin, University of Ab...What is Digital Literacy? A guest blog from Andy McLaughlin, University of Ab...
What is Digital Literacy? A guest blog from Andy McLaughlin, University of Ab...
 
Présentationvvvvvvvvvvvvvvvvvvvvvvvvvvvv2.pptx
Présentationvvvvvvvvvvvvvvvvvvvvvvvvvvvv2.pptxPrésentationvvvvvvvvvvvvvvvvvvvvvvvvvvvv2.pptx
Présentationvvvvvvvvvvvvvvvvvvvvvvvvvvvv2.pptx
 
The History of Stoke Newington Street Names
The History of Stoke Newington Street NamesThe History of Stoke Newington Street Names
The History of Stoke Newington Street Names
 

C Language Lecture 12

  • 1. Computing Fundamentals Dr. Muhammad Yousaf Hamza Deputy Chief Engineer, PIEAS
  • 3. Characters • Characters – Building blocks of programs • Every program is a sequence of meaningfully grouped characters Dr. Yousaf, PIEAS
  • 4. #include <stdio.h> int main() { char x = ‘h’; printf(“Character x is %cn", x); // note the use of %c getchar(); return 0; } Dr. Yousaf, PIEAS
  • 5. // How to read a character and store in a variable #include <stdio.h> int main() { char x ; printf(“Enter a character”); scanf(“%c",&x); // note the use of %c and & printf(“ You entered %cn", x); // note the use of %c getchar(); return 0; } Dr. Yousaf, PIEAS
  • 6. #include<stdio.h> int main() { char x = 'h', y; printf("Character x is %cn", x); // note the use of %c printf("Please enter a character and press entern"); y = getchar(); // It is valid. It will give correct output printf("Character y is %cn", y); // note the use of %c getchar(); return 0; } Note: y = scanf("%c",&y); is not correct, as discussed in the int (or float) case, previously. Though, it will run but will give you incorrect output when you print the value of y. Dr. Yousaf, PIEAS
  • 7. Strings • Strings – When we want to store and print text in C we use a string – Series of characters treated as a single unit • Can include letters, digits and special characters (*, /, $) – char strname[5]="abcd"; – String literal - written in double quotes “abcd“ A string is an array of type char which has '0' (named as NULL) as the last character. – Strings are arrays of characters • String a pointer to first character • Value of string is the address of first character (Later) Dr. Yousaf, PIEAS
  • 8. String Declaration and Initialization • String declarations char c[5]={'a','b','c','d’}; char c[]={'a','b','c','d'}; // it works without mentioning the size char c[5]="abcd"; char c[]="abcd"; – Declare as a character array or a variable of type char char color[] = "blue"; – Remember that strings represented as character arrays end with '0' • color has 5 elements • A string is an array of type char which has '0' as the last character. • '0' is a special character (like 'n') which means "stop now". – char s[10]="unix"; /* s[4] is '0'; */ – char s[ ]="unix"; /* s has five elements */ Dr. Yousaf, PIEAS
  • 9. Printing Strings – printf("Long long ago."); – Use printf – char word[] = “Hello” printf("%s", word); // note the use of %s Example: char str[ ] = "A message to display"; printf ("%sn", str); printf expects to receive a string as an additional parameter when it sees %s in the format string printf knows how much to print out because of the NULL character at the end of all strings. When it finds a 0, it knows to stop. Dr. Yousaf, PIEAS
  • 10. Accessing Individual Characters • The first element of any array in C is at index 0. The second is at index 1, and so on ... char s[6] =“Hello”; printf("%c", s[2]; // note the use of %c • This notation can be used in all kinds of statements and expressions in C: • For example: char c; c = s[1]; if (s[0] == '-') … switch (s[1]) ... s [0] s[1] s [2] s[3] s[4] Dr. Yousaf, PIEAS
  • 11. Example #include<stdio.h> int main() { char str[15] = "unix and c"; printf("%sn", str); // unix and c getchar(); return 0; } Note: '0' is not printed in the output. Dr. Yousaf, PIEAS
  • 12. Example #include<stdio.h> int main() { char str[15] = "unix and c"; printf("%sn", str); str[6]='0'; printf("%sn", str); str[2]='%'; printf("%sn", str); printf("n"); getchar(); return 0; } Dr. Yousaf, PIEAS
  • 13. Example #include<stdio.h> int main() { char str[15] = "unix and c"; printf("%sn", str); str[6]='0'; printf("%sn", str); str[2]='%'; printf("%sn", str); printf("n"); getchar(); return 0; } Dr. Yousaf, PIEAS
  • 14. Example #include<stdio.h> int main() { char str[15] = "unix and c"; printf("%sn", str); // unix and c str[6]='0'; printf("%sn", str); // unix a str[2]='%'; printf("%sn", str); // un%x a printf("n"); getchar(); return 0; } Dr. Yousaf, PIEAS