Print an Individual Character in C

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

Print an Individual Character in C

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

Print an Individual Character in C using Loop

The iterator ‘i’ iterate from 0 to 6 and print each character individually including ‘null’ in the last.

 

Author

  • Barry Allen

    A Full Stack Developer with 10+ years of experience in different domain including SAP, Blockchain, AI and Web Development.

    View all posts

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.