BeginnersBook

  • Home
  • Java
    • Java OOPs
    • Java Collections
    • Java Examples
  • C
    • C Examples
  • C++
    • C++ Examples
  • DBMS
  • Computer Network
  • Python
    • Python Examples
  • More…
    • jQuery
    • Kotlin
    • WordPress
    • SEO
    • JSON
    • JSP
    • JSTL
    • Servlet
    • MongoDB
    • XML
    • Perl

C Program to Print String using Pointer

Last Updated: February 24, 2019 by Chaitanya Singh | Filed Under: C Programs

In this tutorial, we will write a C program to print a String character by character using a pointer variable. To understand this program you should have basic knowledge of the following topics:

  • C Pointers
  • C Array

Program to print a String using Pointer

In the following program we have declared a char array to hold the input string and we have declared a char pointer. We have assigned the array base address (address of the first element of the array) to the pointer and then we have displayed the every element of the char array by incrementing the pointer in the while loop.

#include <stdio.h>
int main()
{
    char str[100];
    char *p;

    printf("Enter any string: ");
    fgets(str, 100, stdin);

    /* Assigning the base address str[0] to pointer
     * p. p = str is same as p = str[0]
     */
    p=str;

    printf("The input string is: ");
    //'\0' signifies end of the string
    while(*p!='\0')
        printf("%c",*p++);

    return 0;
}

Output:
C Program to Print String using Pointer

Related C Examples

1. C program to swap two numbers using pointers
2. C program to create, initialize and access a pointer variable
3. C program to find sum of first n natural numbers
4. C program to find average of two numbers

Top Related Articles:

  1. C Program to concatenate two strings without using strcat
  2. C Program to Swap two numbers using Pointers
  3. C Program to find greatest of three numbers
  4. C Program to Calculate Average Using Array
  5. C Program to Access Array Elements Using Pointer

About the Author

I have 15 years of experience in the IT industry, working with renowned multinational corporations. Additionally, I have dedicated over a decade to teaching, allowing me to refine my skills in delivering information in a simple and easily understandable manner.

– Chaitanya

Leave a Reply Cancel reply

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

Copyright © 2012 – 2025 BeginnersBook . Privacy Policy . Sitemap