String in C Programming

String in C Programming

In this tutorial, we will study what is a string in c programming, what was the type of declaring and initializing method of string, and the get() and puts() function with the help of examples. So let’s go start.

String in C Programming

There is no data type available to create strings in C programming. If we have to create a string, then we have to create a char array, hence the character array in C language is also called a string. And the strings in it terminate with a null character ().

In C language we can declare and initialize with strings two types.

First:-

In this, we create an array of finite numbers and store one-one character of a string in its all index. It stores the null character () in its last index. It is necessary to do this.

char myArray[4] = {‘A’, ‘ L’, ‘I’, ‘M’, ‘A’, ‘M’};

Second:-

In this, we create one undefined char array and assign it one string and Array automatically becomes the same size as the string size. With this method, the null character () is automatically added to the initialization.

char myArray[] = “ALIMAM”;

Example:-

#include<stdio.h>
int main()
{
  /* Declaring string in array format */
  char myArrayF[5] = {‘A’, ‘ L’, ‘I’, ‘M’, ‘A’, ‘M’};

  /* Declaring string in simple format */
  char myArrayS[] = “ALIMAM”;

  printf(“First Way: %sn”,myArrayF);
  printf(“Second Way: %s”, myArrayS);

return 0;
}

Output

First Way: ALIMAM
Second Way: ALIMAM

gets() and puts() Functions

gets() function:-

The get () function is used to read from the string user at run time. This function is defined for this purpose. In this, we pass the name of the array in which we want to store the string.

gets(char_Array_Name);

On passing the string, it is stored in the char array. If the array is to be made a string point, the loop will have to be used.

puts() function provide:-

The puts () function is used to print the Char array as a complete string.

puts(char_Array_Name);

Example

#include<stdio.h>
#include <stdio.h>
int main()
{
char answer[50];
printf(“Who is the founder of Use My Notes?”);

/* Taking string input at run time using gets() function */
gets(answer);
printf(“\n Answer is : “);

/* Printing string to the screen using puts() function */
puts(answer);

return 0;
}

Output:-

Who is the founder of Use My Notes?
Alimam Miya (input)
Answer is: Alimam Miya

Originally posted on - String in C Programming

To view or add a comment, sign in

More articles by Alimam Miya

  • CAT Toppers List 2025 - Check Topper’s Name and %ile Here

    The CAT 2025 result has already been released by the Indian Institute of Management Kozhikode, and candidates can…

  • Elevate Your CAT Preparation with iQuanta's CAT Online Course

    Are you preparing for CAT 2025 and looking for the best online course to boost your chances of success? iQuanta’s CAT…

  • Best CAT Score Calculator 2024

    The Common Admission Test (CAT) is a crucial milestone for MBA aspirants aiming for prestigious institutes like IIMs…

  • Top 10 Pharma Companies in India 2024

    In this will talk about the Top 10 Pharma Companies in India. India’s pharmaceutical assiduity has surfaced as a global…

  • Top 10 Credit Card Companies in India 2025

    This article will talk about the Top 10 Credit Card Companies in India. In the dynamic geography of India’s fiscal…

  • Top Real Estate Companies in India 2024

    This article will talk about the Top Real Estate Companies in India 2024. The real estate region in India has witnessed…

    1 Comment
  • Top Real Estate Companies in Kolkata 2024

    This article will talk about the Top Real Estate Companies in Kolkata. Kolkata, the artistic locus of India, is going…

  • Top Real Estate Companies in Delhi 2025

    This article will talk about the Top Real Estate Companies in Delhi. Delhi, the heart of India, not only serves as the…

    1 Comment
  • Hostinger Coupon Codes 2024 [60% Plan Discount]

    Looking for an affordable yet powerful hosting solution? Hostinger is one of the leading web hosting providers known…

  • 5 Best CAT Online Coaching in Gurugram

    Preparing for the Common Admission Test (CAT) can be a daunting task, especially with the myriad of coaching options…

Others also viewed

Explore content categories