How to Say “Hello, World!” in C

The “Hello, World!” program is a classic way to start learning any programming language. It simply prints the text:

Hello, World!

✏️ The Code:

#include <stdio.h>  // Include the Standard Input Output library

int main() {
    printf("Hello, World!\n");  // Print the text
    return 0;  // End of program
}

πŸ” Explanation:

LineWhat It Does
#include <stdio.h>Tells the compiler to include the Standard I/O library (needed for printf).
int main() { ... }The main function where the program starts running.
printf("Hello, World!\n");Prints the text. \n moves to a new line after printing.
return 0;Tells the system the program finished successfully.

2️⃣ What Are Values in C?

In C, a value is any piece of data your program uses, like:

  • A number: 10, 3.14
  • A character: 'A', 'z'
  • A text string: "Hello"
  • A boolean-like value (though C doesn’t have built-in booleans in its original version)

πŸ‘‰ Examples of values:

42              // An integer value
3.14            // A floating-point (decimal) value
'A'             // A character value
"Good Morning"  // A string value

3️⃣ What Are Variables in C?

A variable is like a container that holds a value. You give it a name so you can store and reuse data in your program.

πŸ›  How to Create (Declare) a Variable:

In C, you must: 1️⃣ Tell the computer what type of data you want to store. 2️⃣ Give it a name.

πŸ”‘ Common Types:

TypeWhat It StoresExample
intInteger numbers (whole numbers)int age = 25;
floatDecimal numbers (single precision)float pi = 3.14;
doubleDecimal numbers (double precision)double g = 9.8;
charSingle characterschar grade = 'A';

✏️ Example Code:

#include <stdio.h>

int main() {
    int age = 25;               // an integer variable
    float height = 5.9;         // a float variable
    char grade = 'A';           // a character variable

    printf("Age: %d\n", age);
    printf("Height: %.1f\n", height);
    printf("Grade: %c\n", grade);

    return 0;
}

πŸ” What’s Happening:

  • int age = 25; β†’ Stores the value 25 in a variable called age.
  • float height = 5.9; β†’ Stores 5.9 in height.
  • char grade = 'A'; β†’ Stores 'A' in grade.
  • %d, %.1f, %c β†’ These are format specifiers used in printf to print integers, floats, and characters.

βœ… Rules for Naming Variables:

βœ”οΈ Must start with a letter (or _ underscore).
βœ”οΈ Can contain letters, numbers, and underscores.
βœ”οΈ Cannot be a C keyword (like int, return).
βœ”οΈ Case-sensitive (score β‰  Score).

Examples of valid names: totalMarks, user_age, PI_value.


πŸ”— Quick Comparison: C vs. Python Variables

In CIn Python
Must declare type: int a = 5;No need to declare type: a = 5
Needs format specifiers for printingDirect printing: print(a)
Static typing (fixed type)Dynamic typing (type can change)

πŸ”š Conclusion

  • You learned to write a simple Hello, World! program.
  • You now understand values (like numbers and text) and how to create variables to store them.
  • C is strict about types, which teaches you how data is stored and managed at a low level.

πŸ’‘ Next step: Try creating your own C program that asks the user for input and prints it back!

Feedback Display

Learner Reviews