Print String In C Programming

The string is used to store single or more characters in a single variable in the C programming language. The string is a very popular data type that is used in different cases like storing name, surname, address, and other text data. One of the most popular operations with the string type is printing the string. A string can be printed to the different output types like standard output, console, terminal, file, etc. In this tutorial, we examine how to print strings in C.

Print String with printf() Method

The printf() method is the defacto method in order to print different data types and variables. The string type can be also printed with the printf() method. The string value can be directly provided to the printf() method as a parameter. The provided string value printed to the standard output which is the terminal or command line interface by default.

#include <stdio.h>

int main()
{
   printf("I like wisetut.com");

   return 0;
}

Another way to print a string is setting the string value into a string variable and then provide the string variable to the print() method as parameter.

#include <stdio.h>

int main()
{

   char sentence[]="I like wisetut.com";

   printf("%s",sentence);

   return 0;
}

Also the string may contain multiple lines by using the “\n”. The printf() method can be used to print multiple lines without problem.

#include <stdio.h>

int main()
{

   char sentence[]="I like wisetut.com\nI like wisetut.com\n";

   printf("%s",sentence);

   return 0;
}
I like wisetut.com
I like wisetut.com

Print String with puts() Method

As a feature-rich programming language C provides puts() method in order to print string. The puts name comes from “put string”. The string variable provided as a parameter to the puts() method. A string value can not be printed with the puts method because the puts method only accepts a string pointer.

#include <stdio.h>

int main()
{

   char sentence[]="I like wisetut.com\nI like wisetut.com\n";

   puts(sentence);

   return 0;
}

Print String with fputs() Method

fputs() method is another alternative way to print string in C. The fputs() method is very similar to the puts() method where only string variables can be printed to the standard output. The fputs() method also requires the output type which is stdout by default. This prints given string to the terminal or command line interface if default properties are used.

#include <stdio.h>

int main()
{

   char sentence[]="I like wisetut.com\nI like wisetut.com\n";

   fputs(sentence);

   return 0;
}

Leave a Comment