C Output: How to Print Text and Values Using printf()

Learn how to output values and print text in C programming using the versatile printf() function. This guide provides basic examples and demonstrates how to display strings, variables, and formatted data effectively.



C - Output (Print Text)

In C, you can output values or print text using the printf() function. This function is versatile and allows you to display strings, variables, and formatted data.

Basic Example

Here's a simple example of using printf() to print a message:


#include 

int main() {
  printf("Hello World!");
  return 0;
}

Double Quotes

When working with text in C, remember that the text must be enclosed in double quotation marks "". If you forget to include the quotes, it will result in a compilation error:


printf("This sentence will work!");
printf(This sentence will produce an error.);

Multiple printf() Functions

You can use multiple printf() functions in a program. However, note that it does not automatically insert a new line at the end of each output unless specified:


#include 

int main() {
  printf("Hello World!");
  printf("I am learning C.");
  printf("And it is awesome!");
  return 0;
}

Output:

Hello World!I am learning C.And it is awesome!

To ensure each output is on a new line, you can use the newline escape character \n within your strings:


#include 

int main() {
  printf("Hello World!\n");
  printf("I am learning C.\n");
  printf("And it is awesome!\n");
  return 0;
}

Output:

Hello World!
I am learning C.
And it is awesome!