Back to TechDepth
TechDepth / C foundationsFREE COURSE

CODESTUDIO / GO BEYOND SYNTAX

C ProgrammingUnderstand the language.
Think closer to the machine.

From your first line of C to confident technical explanations. Build real depth in syntax, pointers, memory and systems — one concept at a time.

14 learning modules40 interview questions80 practice drillsSelf-paced · Free
C / learning workspace

Your technical depth starts here.

MODULE 1 / 14

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

c
#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.
  • \n prints a new line.
  • return 0; indicates successful program execution.

Compile and Run

bash
gcc hello.c -o hello
./hello

Output

text
Hello World!

1.4 Structure of a C Program

A typical C program has these parts:

  1. Preprocessor directives
  2. Main function
  3. Variable declarations
  4. Statements
  5. Return statement

Example

c
#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

c
int myNum = 15;

int myNum2;
myNum2 = 15;

int myNum3 = 15;
myNum3 = 10;

Explanation

  • myNum is initialized immediately.
  • myNum2 is declared first and assigned later.
  • myNum3 is updated after its initial assignment.

Naming rules

  • Names may contain letters, digits, and underscores.
  • Names cannot start with a digit.
  • Names are case-sensitive (age and Age are different).
  • Keywords such as int or return cannot be used as names.

1.6 Constants

Definition

A constant is a value that cannot be changed after initialization.

Example

c
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

c
// this is a comment
printf("Hello World!"); // comment at the end of a line

Multi-line comment

c
/* Multi-line comment,
   print Hello World!
   to the screen, it's awesome */

1.8 Printing Text and Variables

Print text

c
printf("I am learning C.");

Print variables

c
int testInteger = 5;
printf("Number = %d", testInteger);

Common format specifiers

  • %d for integers
  • %f for float
  • %lf for double
  • %c for character
  • %s for 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

  • char
  • short
  • int
  • long
  • float
  • double
  • void

Example

c
#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)

c
short a = 0b1010110;

Octal (prefix 0)

c
int b = 02713;

Hexadecimal (prefix 0x)

c
long c = 0X1DAB83;

Print in different formats

c
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

c
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

  • %d prints decimal integers.
  • 9 is the minimum field width.
  • - means left alignment.

Decimals

c
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

  1. Write a program to print your name.
  2. Write a program to store and print your age.
  3. Declare three variables and print their sum.
  4. Print a float and a double using correct format specifiers.
  5. Print a number in decimal, octal, and hex form.
  6. 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.