Table of Contents
Introduction
In this article, we will learn how to print an individual character in C.
Program
#include<stdio.h> int main() { char name[]="Saumya"; printf("%c\n",name[0]); printf("%c\n",name[6]); printf("%c\n",name[5]); return 0; }
Output
Explanation
printf(“%c\n”,name[0]); ,by just passing the index value we can access that particular character.
In the output window, there is a blank space between the character ‘S’ and ‘a’ which shows the null character which is the output of the statement printf(“%c\n”,name[6]);, since the null character is present at 6th index value.
Print an Individual Character in C using Loop
In the previous examples, we use %s to print that string as a whole but in the above example, we use %c to print characters only.
The above code can be made simple with the use of the ‘for’ loop. Using for loop we can access all characters in a single step.
Program
#include<stdio.h> int main() { char name[]="Saumya"; int i; for(i=0;i<7;i++) { printf("%c\n",name[i]); } return 0; }
Output
The iterator ‘i’ iterate from 0 to 6 and print each character individually including ‘null’ in the last.
0 Comments