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
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