# C program to print a string C program to print a string using various functions such as printf, puts. Consider the following code:

printf("Hi there! How are you doing?");

Output:
Hi there! How are you doing?

The printf function prints the argument passed to it (a string). Next, we will see how to print it if it's stored in a character array.

#include <stdio.h>

int main()
{
char z = "I am learning C programming language.";

printf("%s", z); // %s is format specifier

return 0;
}

Output:
I am learning C programming language.

To input a string, we can use scanf and gets functions.

## C program

#include <stdio.h>

int main()
{
char array;

printf("Enter a string\n");
scanf("%s", array);

return 0;
}

Output:
Enter a string
We love C.

Only "We" is printed because function scanf can only be used to input strings without any spaces, to input strings containing spaces use gets function.

### Input string containing spaces

#include <stdio.h>

int main()
{
char z;

printf("Enter a string\n");
gets(z);

printf("The string: %s\n", z);
return 0;
}

Enter a string
Practice makes a person perfect.
The string: Practice makes a person perfect.

## Print a string using a loop

We can print a string using a loop by printing its characters one at a time. It terminates with '\0' (NULL character), which marks its end.

#include <stdio.h>

int main()
{
char s;
int c = 0;

gets(s);

while (s[c] != '\0') {
printf("%c", s[c]);
c++;
}

return 0;
}

You can also use a for loop:

for (c = 0; s[c] != '\0'; c++)
printf("%c", s[c]);

Using a do-while loop:

#include <stdio.h>

int main()
{
char s;

int c = 0;

gets(s);

if (s[c] != '\0') { // Used to check empty string
do {
printf("%c", s[c]);
c++;
} while (s[c] != '\0');
}
return 0;
}

## C program to print a string using recursion

#include <stdio.h>

void print(char*);

int main() {
char s;
gets(s);
print(s);
return 0;
}

void print(char *t) {
if (*t == '\0') // Base case
return;
printf("%c", *t);
print(++t);
}
{"ft_lang_label":"__label__en","ft_lang_prob":0.61735636,"math_prob":0.92996347,"size":1768,"snap":"2023-40-2023-50","text_gpt3_token_len":488,"char_repetition_ratio":0.1558957,"word_repetition_ratio":0.080906145,"special_character_ratio":0.3388009,"punctuation_ratio":0.18639798,"nsfw_num_words":0,"has_unicode_error":false,"math_prob_llama3":0.9722371,"pos_list":[0,1,2],"im_url_duplicate_count":[null,2,null],"WARC_HEADER":"{\"WARC-Type\":\"response\",\"WARC-Date\":\"2023-11-30T04:05:38Z\",\"WARC-Record-ID\":\"<urn:uuid:9981d28c-e73d-4b75-bc92-25f9f219030e>\",\"Content-Length\":\"36112\",\"Content-Type\":\"application/http; msgtype=response\",\"WARC-Warcinfo-ID\":\"<urn:uuid:ba448b64-cb41-4a77-817a-369bd2ed8f71>\",\"WARC-Concurrent-To\":\"<urn:uuid:2aeb02e5-f140-4228-9ed6-ddb0a6423d24>\",\"WARC-IP-Address\":\"65.0.25.56\",\"WARC-Target-URI\":\"https://www.programmingsimplified.com/c/program/print-string\",\"WARC-Payload-Digest\":\"sha1:EV6FH6ALNIRUUGMTHIQSHQFMPEM7OAES\",\"WARC-Block-Digest\":\"sha1:6H766NZL6COMWHOYY7AYIG5CWROM3RBV\",\"WARC-Identified-Payload-Type\":\"text/html\",\"warc_filename\":\"/cc_download/warc_2023/CC-MAIN-2023-50/CC-MAIN-2023-50_segments_1700679100164.87_warc_CC-MAIN-20231130031610-20231130061610-00358.warc.gz\"}"}