Your technical depth starts here.
Introduction to C, Syntax, Variables, and Output
Module 1 – Introduction to C, Syntax, Variables, and Output
1.1 Module Overview
This module is your starting point. You will learn what C is, how a C program is written, compiled and run, and how to store data in variables and print it on the screen.
By the end of this module, you should be able to:
- Explain what C is and why it still matters.
- Write, compile, and run your first C program.
- Describe the structure of a C program.
- Declare variables and constants.
- Write comments.
- Print text and variables using
printf()and format specifiers. - Use basic data types and number systems.
- Format output neatly.
1.2 What is C?
Definition
C is a general-purpose, procedural programming language used for system programming, embedded systems, operating systems, and performance-critical applications.
Why C matters
- It is fast and efficient.
- It gives direct control over memory.
- It is the foundation for many modern languages.
- It is widely used in embedded and low-level programming.
1.3 Your First C Program
Code
#include <stdio.h>
int main(void) {
printf("Hello World!\n");
return 0;
}Explanation
#include <stdio.h>includes the standard input/output library.int main(void)is the entry point of the program.printf()prints output to the screen.\nprints a new line.return 0;indicates successful program execution.
Compile and Run
gcc hello.c -o hello
./helloOutput
Hello World!1.4 Structure of a C Program
A typical C program has these parts:
- Preprocessor directives
- Main function
- Variable declarations
- Statements
- Return statement
Example
#include <stdio.h>
int main(void) {
int num = 5;
printf("%d\n", num);
return 0;
}1.5 Variables
Definition
A variable is a named storage location used to store data that may change during program execution.
Example
int myNum = 15;
int myNum2;
myNum2 = 15;
int myNum3 = 15;
myNum3 = 10;Explanation
myNumis initialized immediately.myNum2is declared first and assigned later.myNum3is updated after its initial assignment.
Naming rules
- Names may contain letters, digits, and underscores.
- Names cannot start with a digit.
- Names are case-sensitive (
ageandAgeare different). - Keywords such as
intorreturncannot be used as names.
1.6 Constants
Definition
A constant is a value that cannot be changed after initialization.
Example
const int minutesPerHour = 60;
const float PI = 3.14;
const int BIRTHYEAR = 1980;Best Practice
Use uppercase names for constants to make them easy to identify.
1.7 Comments
Definition
Comments are notes in the source code that are ignored by the compiler.
Single-line comment
// this is a comment
printf("Hello World!"); // comment at the end of a lineMulti-line comment
/* Multi-line comment,
print Hello World!
to the screen, it's awesome */1.8 Printing Text and Variables
Print text
printf("I am learning C.");Print variables
int testInteger = 5;
printf("Number = %d", testInteger);Common format specifiers
%dfor integers%ffor float%lffor double%cfor character%sfor string
1.9 Data Types
Definition
Data types tell the compiler what kind of data a variable can store and how much memory it needs.
Basic types
charshortintlongfloatdoublevoid
Example
#include <stdio.h>
int main(void) {
int myNum = 5;
float myFloatNum = 5.99;
char myLetter = 'D';
double myDouble = 3.2325467;
printf("%d\n", myNum);
printf("%f\n", myFloatNum);
printf("%c\n", myLetter);
printf("%lf\n", myDouble);
return 0;
}1.10 Number Systems
C can store numbers in different bases.
Binary (prefix 0b, supported by GCC/Clang and C23)
short a = 0b1010110;Octal (prefix 0)
int b = 02713;Hexadecimal (prefix 0x)
long c = 0X1DAB83;Print in different formats
printf("a=%ho, b=%o, c=%lo\n", a, b, c); /* octal */
printf("a=%hd, b=%d, c=%ld\n", a, b, c); /* decimal */
printf("a=%hx, b=%x, c=%lx\n", a, b, c); /* hex, lowercase */
printf("a=%hX, b=%X, c=%lX\n", a, b, c); /* hex, uppercase */1.11 Formatting Output
Example
int a1 = 20, a2 = 345, a3 = 700;
int b1 = 56720, b2 = 9999, b3 = 20098;
printf("%-9d %-9d %-9d\n", a1, a2, a3);
printf("%-9d %-9d %-9d\n", b1, b2, b3);Explanation
%dprints decimal integers.9is the minimum field width.-means left alignment.
Decimals
printf("%.2f\n", 3.14159); /* prints 3.14 */1.12 Best Practices
- Use meaningful variable names.
- Use constants for fixed values.
- Format code properly with consistent indentation.
- Keep comments short and useful.
- Prefer clear, readable output formatting.
- Initialize variables when you declare them.
1.13 Practice Exercises
- Write a program to print your name.
- Write a program to store and print your age.
- Declare three variables and print their sum.
- Print a float and a double using correct format specifiers.
- Print a number in decimal, octal, and hex form.
- Print a small table of numbers using field widths.
1.14 Module Recap
In this module, you learned:
- What C is and why it matters.
- How a C program is structured, compiled, and run.
- Variables, constants, comments, and output.
- Data types and format specifiers.
- Different number systems and output formatting.
