If you want to know how to write “I love you” in C language, the answer takes only a few lines. C has no built-in string type. Instead, a string is an array of char values that ends with a null character, written '\0'. You can print the text directly with printf, or store it in an array first.
Print the text with printf
#include <stdio.h>
int main(void) {
printf("I love you\n");
return 0;
}
The \n at the end moves the cursor to a new line. The output is:
I love you
If the text itself needs quotation marks, escape each one with a backslash:
printf("She wrote \"I love you\"\n");
This prints She wrote "I love you".
Store the text in a char array
#include <stdio.h>
int main(void) {
char message[] = "I love you";
printf("%s\n", message);
printf("%zu\n", sizeof(message));
return 0;
}
%s tells printf to read characters until it reaches the null character. The output is:
I love you
11
The text has 10 visible characters, counting the spaces. The array needs one more byte for the null character, so it holds 11 bytes in total.
When you let the compiler size the array from the string literal, this is handled for you. If you choose a fixed size yourself, make it at least 11. An array that is too small leaves no room for the null character, and printf("%s") will keep reading past the end of the array, which is undefined behavior.
Use a pointer to a string literal
#include <stdio.h>
int main(void) {
const char *message = "I love you";
puts(message);
return 0;
}
The text here is a string literal, and changing a string literal is undefined behavior. The const keyword makes the compiler reject attempts to change the text through message. puts prints the string and adds a newline for you.
Compile and run
gcc -std=c11 -Wall -Wextra love.c -o love
./love
A note for C++ readers
C++ has std::string, which manages its own size, so most C++ code does not need to count bytes by hand. The C examples above are the way to do it in plain C.