Full Definitions

Simple Programs for Printing Text

Example 1 – Hello World

#include <stdio.h>

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

This is the most basic C program. #include <stdio.h> allows us to use the printf() function. The main() function is the entry point of the program. The printf() prints "Hello, World!" on the screen. return 0; indicates that the program executed successfully.

Output:
Hello, World!

Example 2 – Print Your Name

#include <stdio.h>

int main() {
    printf("My name is Salman.");
    return 0;
}

This program prints your name. The string inside printf() can be any text. It shows how you can display personalized information using C.

Output:
My name is Salman.

Example 3 – Print Multiple Lines

#include <stdio.h>

int main() {
    printf("Pakistan\nZindabad");
    return 0;
}

The \n is an escape sequence that moves the cursor to a new line. This program prints "Pakistan" on the first line and "Zindabad" on the second line.

Output:
Pakistan
Zindabad

Example 4 – Print a Quote

#include <stdio.h>

int main() {
    printf("C Programming is Fun!");
    return 0;
}

This program prints a motivational message. It demonstrates that printf() can print any text string enclosed in double quotes.

Output:
C Programming is Fun!

Example 5 – Print a Poem (2 Lines)

#include <stdio.h>

int main() {
    printf("Twinkle, twinkle, little star\nHow I wonder what you are!");
    return 0;
}

This example shows how to print multiple lines of a poem. The \n ensures the second line starts below the first line.

Output:
Twinkle, twinkle, little star
How I wonder what you are!

Practice Statements

  • Print your name, age, city, and full address on the screen.
  • Print your school/college/university name and your roll number with marks.
  • Print your favorite quote and also print Pakistan Zindabad.
  • Print your contact number and email address on the screen.
  • Print the following exactly as it looks:
    C Programming is Fun!

MCQs – Printing Text with printf

  1. Which of the following will correctly print: Hello World
    a) printf(Hello World);
    b) printf("Hello World");
    c) printf('Hello World');
    d) print("Hello World");

    Answer: b) printf("Hello World");

  2. Which escape sequence is used to print text on a new line?
    a) \t
    b) \n
    c) \\
    d) \b

    Answer: b) \n

  3. What will be the output of the following?
    printf("Pakistan\nZindabad");

    a) Pakistan Zindabad
    b) PakistanZindabad
    c) Pakistan
    Zindabad
    d) Error

    Answer: c) Pakistan
    Zindabad

  4. To print double quotes around a text, which of the following is correct?
    a) printf(""C Programming"");
    b) printf("\"C Programming\"");
    c) printf("'C Programming'");
    d) printf("C Programming");

    Answer: b) printf("\"C Programming\"");

  5. Which statement correctly prints a 2-line poem?
    a) printf("Twinkle, twinkle, little star How I wonder what you are");
    b) printf("Twinkle, twinkle, little star\nHow I wonder what you are");
    c) printf('Twinkle, twinkle, little star\nHow I wonder what you are');
    d) print("Twinkle, twinkle, little star\nHow I wonder what you are");

    Answer: b) printf("Twinkle, twinkle, little star\nHow I wonder what you are");

  6. Which of the following is incorrect?
    a) printf("My name is Ali");
    b) printf("Age: 20\n");
    c) printf('Pakistan');
    d) printf("City: Lahore");

    Answer: c) printf('Pakistan');

Frequently Asked Questions (FAQs)

1. What is the purpose of printf() in C?

The printf() function is used to print text, numbers, and variables on the screen. It is essential for displaying output to the user.

2. How do I print text on multiple lines?

Use the newline escape sequence \n within the string, e.g., printf("Line1\nLine2");.

3. Can I print both text and variables together?

Yes. You can mix text and variables using format specifiers like %d for integers, %f for floats, and %c for characters.

4. How to print double quotes inside text?

Use the escape character \ before double quotes, e.g., printf("\"C Programming\"");.

5. Is printf() only for text?

No. printf() can print numbers, characters, variables, and formatted output as well.

6. How do I print a tab space?

Use the tab escape sequence \t inside the string, e.g., printf("A\tB\tC");.

7. Can I print Unicode or special symbols?

Yes, using the \u or \x escape sequences, depending on the compiler support.

8. What is the difference between printf() and puts()?

printf() allows formatted output, while puts() prints a string and automatically adds a newline at the end.

9. Do I need to include any library to use printf()?

Yes. You must include #include <stdio.h> at the top of your program.

10. Can printf() be used in loops?

Absolutely. You can use printf() inside loops to print text or variables multiple times efficiently.