CodeNewbie Community

Cover image for String in C Programming

String in C Programming

Alimam Miya
I'm not a great programmer, I'm a good programmer, with great habits.
・2 min read

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.


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[6] = {‘A’, ‘ L’, ‘I’, ‘M’, ‘A’, ‘M’};
Enter fullscreen mode Exit fullscreen mode


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”;
Enter fullscreen mode Exit fullscreen mode


int main(void)
  /* Declaring string in array format */
  char myArrayF[5] = {‘A’, ‘ L’, ‘I’, ‘M’, ‘A’, ‘M’, ‘\0’};

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

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

return 0;
Enter fullscreen mode Exit fullscreen mode


First Way: ALIMAM
Second Way: ALIMAM
Enter fullscreen mode Exit fullscreen mode

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.

Enter fullscreen mode Exit fullscreen mode

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.

Enter fullscreen mode Exit fullscreen mode


#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 */
printf(“\n Answer is : “);

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

return 0;
Enter fullscreen mode Exit fullscreen mode


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

Originally posted on - String in C Programming

Discussion (2)

ac000 profile image
Andrew Clayton


Please allow me to correct a few things here.

1) The NUL (not to be confused with NULL) character is usually written as \0

2) Your single quotes are strange \342 characters and not the usual single quote (ASCII 39).

3) Your first array example

char myArray[4] = { 'A', 'L', 'I', 'M', 'A', 'M' };
Enter fullscreen mode Exit fullscreen mode

hasn't allocated enough space. For the above it would need a size of 6, however it wouldn't be NUL terminated. You probably meant something like

char myArray[7] = { 'A', 'L', 'I', 'M', 'A', 'M', '\0' };
Enter fullscreen mode Exit fullscreen mode

4) Please don't use gets(3). To quote the man page

  Never use gets().  Because it is impossible to tell without knowing the
  data  in  advance  how  many  characters  gets() will read, and because
  gets() will continue to store characters past the end of the buffer, it
  is  extremely dangerous to use.  It has been used to break computer se‐
  curity.  Use fgets() instead.

  For more information, see CWE-242 (aka  "Use  of  Inherently  Dangerous
  Function") at

In fact gets(3) has been marked as obsolescent in POSIX.1-2008 and has been removed from C11.

puts(3) is of course fine to use.

5) Oh, and in C if your not passing any parameters into a function you should declare it as void otherwise it will be (...) which is something completely different. So your main should look like

int main(void)
Enter fullscreen mode Exit fullscreen mode


alimammiya profile image
Alimam Miya Author

Thanks for suggest