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

PDF
C Language Lecture 13
PDF
C Language Lecture 14
PPTX
PHP string-part 1
PPTX
Array in (C) programing
PDF
PHP object calisthenics
DOC
Arrays and Strings
PDF
Cs3430 lecture 17
PDF
SPL 10 | One Dimensional Array in C
C Language Lecture 13
C Language Lecture 14
PHP string-part 1
Array in (C) programing
PHP object calisthenics
Arrays and Strings
Cs3430 lecture 17
SPL 10 | One Dimensional Array in C

What's hot (19)

PPTX
PHP Functions & Arrays
PPT
Smarty Template
PPT
Smarty Template
PDF
Arrays in PHP
PPT
Class 4 - PHP Arrays
PPTX
Array in php
PPTX
Array in c
PPTX
Array Of Pointers
PPTX
Arrays in c language
PPT
PDF
Sorting arrays in PHP
PDF
PHP Unit 4 arrays
PPTX
2CPP06 - Arrays and Pointers
PPTX
PHP array 1
PPTX
arrays in c
PPT
Introduction to perl_lists
PPTX
strings in c language and its importance
PPT
Session 7 En
PHP Functions & Arrays
Smarty Template
Smarty Template
Arrays in PHP
Class 4 - PHP Arrays
Array in php
Array in c
Array Of Pointers
Arrays in c language
Sorting arrays in PHP
PHP Unit 4 arrays
2CPP06 - Arrays and Pointers
PHP array 1
arrays in c
Introduction to perl_lists
strings in c language and its importance
Session 7 En
Ad

Similar to C Language Lecture 12 (20)

PDF
[ITP - Lecture 17] Strings in C/C++
PDF
String notes
PPTX
pps unit 3.pptx
PPTX
ARRAY's in C Programming Language PPTX.
PPT
Cfbcgdhfghdfhghggfhghghgfhgfhgfhhapter11.PPT
PPT
Chapterabcdefghijklmnopqrdstuvwxydanniipo
PPT
THE FORMAT AND USAGE OF STRINGS IN C.PPT
PPTX
Handling of character strings C programming
PPT
Strings
PDF
Strings IN C
PDF
Character Array and String
PPT
14 strings
PPTX
Week6_P_String.pptx
PPTX
COm1407: Character & Strings
PPT
Lecture 05 2017
PPTX
Lecture_on_string_manipulation_functions.pptx
PPTX
Review of C.pptx
PDF
C programming & data structure [character strings & string functions]
PPTX
Introduction about Low Level Programming using C
DOC
String in c
[ITP - Lecture 17] Strings in C/C++
String notes
pps unit 3.pptx
ARRAY's in C Programming Language PPTX.
Cfbcgdhfghdfhghggfhghghgfhgfhgfhhapter11.PPT
Chapterabcdefghijklmnopqrdstuvwxydanniipo
THE FORMAT AND USAGE OF STRINGS IN C.PPT
Handling of character strings C programming
Strings
Strings IN C
Character Array and String
14 strings
Week6_P_String.pptx
COm1407: Character & Strings
Lecture 05 2017
Lecture_on_string_manipulation_functions.pptx
Review of C.pptx
C programming & data structure [character strings & string functions]
Introduction about Low Level Programming using C
String in c
Ad

More from Shahzaib Ajmal (20)

PDF
Galatica Smart Energy Infrastructure Startup Pitch Deck
PDF
C Language Lecture 22
PDF
C Language Lecture 21
PDF
C Language Lecture 20
PDF
C Language Lecture 19
PDF
C Language Lecture 18
PDF
C Language Lecture 17
PDF
C Language Lecture 16
PDF
C Language Lecture 15
PDF
C Language Lecture 11
PDF
C Language Lecture 10
PDF
C Language Lecture 9
PDF
C Language Lecture 8
PDF
C Language Lecture 7
PDF
C Language Lecture 6
PDF
C Language Lecture 5
PDF
C Language Lecture 4
PDF
C Language Lecture 3
PDF
C Language Lecture 2
PDF
C Language Lecture 1
Galatica Smart Energy Infrastructure Startup Pitch Deck
C Language Lecture 22
C Language Lecture 21
C Language Lecture 20
C Language Lecture 19
C Language Lecture 18
C Language Lecture 17
C Language Lecture 16
C Language Lecture 15
C Language Lecture 11
C Language Lecture 10
C Language Lecture 9
C Language Lecture 8
C Language Lecture 7
C Language Lecture 6
C Language Lecture 5
C Language Lecture 4
C Language Lecture 3
C Language Lecture 2
C Language Lecture 1

Recently uploaded (20)

PPTX
Final Presentation General Medicine 03-08-2024.pptx
PDF
STATICS OF THE RIGID BODIES Hibbelers.pdf
PPTX
human mycosis Human fungal infections are called human mycosis..pptx
PDF
Classroom Observation Tools for Teachers
PPTX
Microbial diseases, their pathogenesis and prophylaxis
PPTX
IMMUNITY IMMUNITY refers to protection against infection, and the immune syst...
PDF
01-Introduction-to-Information-Management.pdf
PPTX
Tissue processing ( HISTOPATHOLOGICAL TECHNIQUE
PDF
3rd Neelam Sanjeevareddy Memorial Lecture.pdf
PDF
ANTIBIOTICS.pptx.pdf………………… xxxxxxxxxxxxx
PPTX
school management -TNTEU- B.Ed., Semester II Unit 1.pptx
PDF
Supply Chain Operations Speaking Notes -ICLT Program
PDF
GENETICS IN BIOLOGY IN SECONDARY LEVEL FORM 3
PPTX
1st Inaugural Professorial Lecture held on 19th February 2020 (Governance and...
PPTX
202450812 BayCHI UCSC-SV 20250812 v17.pptx
PPTX
GDM (1) (1).pptx small presentation for students
PPTX
Presentation on HIE in infants and its manifestations
PDF
VCE English Exam - Section C Student Revision Booklet
PDF
Saundersa Comprehensive Review for the NCLEX-RN Examination.pdf
PPTX
Pharma ospi slides which help in ospi learning
Final Presentation General Medicine 03-08-2024.pptx
STATICS OF THE RIGID BODIES Hibbelers.pdf
human mycosis Human fungal infections are called human mycosis..pptx
Classroom Observation Tools for Teachers
Microbial diseases, their pathogenesis and prophylaxis
IMMUNITY IMMUNITY refers to protection against infection, and the immune syst...
01-Introduction-to-Information-Management.pdf
Tissue processing ( HISTOPATHOLOGICAL TECHNIQUE
3rd Neelam Sanjeevareddy Memorial Lecture.pdf
ANTIBIOTICS.pptx.pdf………………… xxxxxxxxxxxxx
school management -TNTEU- B.Ed., Semester II Unit 1.pptx
Supply Chain Operations Speaking Notes -ICLT Program
GENETICS IN BIOLOGY IN SECONDARY LEVEL FORM 3
1st Inaugural Professorial Lecture held on 19th February 2020 (Governance and...
202450812 BayCHI UCSC-SV 20250812 v17.pptx
GDM (1) (1).pptx small presentation for students
Presentation on HIE in infants and its manifestations
VCE English Exam - Section C Student Revision Booklet
Saundersa Comprehensive Review for the NCLEX-RN Examination.pdf
Pharma ospi slides which help in ospi learning

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