C++ vs C

How to Code "I Love You" in C

Learn to print I love you in C with printf or puts. Includes a complete program, compile steps, common mistakes, and the C++ version using std::cout.

On this page
  1. The short answer
  2. What each line does
  3. Printing quotation marks
  4. A simpler option: puts
  5. The same message in C++
  6. Common mistakes

The short answer

Use printf from the C standard library. This complete program prints the message:

#include <stdio.h>

int main(void) {
    printf("I love you\n");
    return 0;
}

Save the file as love.c, compile it with gcc love.c -o love, and run it with ./love. The output is:

I love you

What each line does

  • #include <stdio.h> loads the standard input and output header, which declares printf.
  • int main(void) starts the function where the program begins.
  • printf("I love you\n"); prints the text inside the double quotes. The \n at the end is an escape sequence for a newline, so anything printed next starts on a new line.
  • return 0; tells the operating system the program finished without error.

Printing quotation marks

To include quotation marks in the output, put a backslash before each one:

printf("\"I love you\"\n");

This prints "I love you" with the quotes included.

A simpler option: puts

The puts function prints a string and adds a newline on its own:

#include <stdio.h>

int main(void) {
    puts("I love you");
    return 0;
}

Both versions print the same text. When you need to mix text with values, printf format specifiers such as %s and %d do that work. The difference between them is explained in what is %s and %d in c.

The same message in C++

C++ programs usually print with std::cout from the <iostream> header:

#include <iostream>

int main() {
    std::cout << "I love you\n";
    return 0;
}

Compile it with g++ love.cpp -o love. The << operator sends the text to the output stream. C++ lets you leave out return 0; in main, but keeping it makes the program match the C version.

Common mistakes

  • Single quotes. In C, single quotes make a character constant, not a string. 'I love you' does not store the text as a string, and most compilers warn about it. Use double quotes for text.
  • Missing semicolon. A missing ; after the printf line causes a syntax error. The compiler may report it on the line after.
  • Missing include. Without #include <stdio.h>, the compiler may not know what printf is, and it may warn or fail.
  • Missing newline. Without \n, the next output stays on the same line.

The complete guide to c++ vs c

Is C++ Better Than C?
Read the guide

Keep exploring

Guides from other topics

All topics →