Skip to content
“`html Introduction to C Programming

Introduction to C Programming

Learning Objectives
  • Understand what a program is.
  • Understand the difference between a program and software.
  • Know the roles of an end-user, programmer and software developer.
  • Learn the history and major features of C.
  • Understand the basic structure of a C program.
  • Identify statements, functions, comments and preprocessor directives.
  • Learn single-line and multi-line comments.

1. What is a Program?

Definition: A program is a set of instructions or step-by-step guidance given to a computer to perform a particular task.

Computers cannot normally perform a task simply by understanding human language. We write instructions using a programming language, such as C, C++, Java or Python.

Simple Example

Suppose we want a computer to add two numbers. We can give it instructions such as:

  1. Store the first number.
  2. Store the second number.
  3. Add the two numbers.
  4. Display the result.
Example:

If the numbers are 10 and 20:
10 + 20 = 30

A C program can be written to perform these instructions automatically.

2. What is Software?

Software is a collection of programs and related components that work together to perform specific tasks and provide functionality to users.

A software application may contain many programs, files, libraries, menus and other components working together.

Program vs Software

Program Software
A set of instructions designed to perform a task. A broader collection of programs and related components that provides functionality to users.
Can be a small program performing one operation. Can be a complete application or system containing many components.

3. People Involved in Software

End-User
Programmer
Developer /
Software Engineer

End-User

An end-user is a person who uses a computer or software application to perform day-to-day tasks.

Example: A student using a word processor to prepare an assignment.

Programmer

A programmer writes programs using programming languages such as C, C++, Java or Python.

The programmer converts requirements or algorithms into computer programs.

Developer / Software Engineer

A software developer or software engineer develops software using programming and other software-development techniques.

Development may include design, coding, testing, debugging, deployment and maintenance.

4. History of C Language

C Language was developed by Dennis Ritchie at AT&T Bell Laboratories in the early 1970s. C became particularly important because it was used extensively in the development and rewriting of the UNIX operating system.
Earlier Systems
Programming
C Language
UNIX
Modern Systems
& Applications
Note: C was designed to provide efficient low-level access to computer resources while still offering many features associated with higher-level programming languages.

5. Features of C Language

1. High-Level Features

C provides features such as functions, variables, control statements and structured programming that make program development easier than programming directly in machine language.

2. Portable

C programs can often be moved to different hardware platforms with relatively few changes, provided a suitable C compiler is available.

3. Low-Level Capabilities

C provides features such as pointers, bit manipulation and direct memory-related operations.

4. Structured Programming

C supports functions and structured control constructs, helping programmers divide a large problem into smaller manageable parts.

Why is C called a middle-level language?

C combines many high-level programming features with low-level capabilities. This allows programmers to write relatively readable programs while also working closely with memory and hardware resources.

6. Hardware Platform and Software Platform

Hardware Platform

A hardware platform refers to the physical computer system on which software runs.

Important hardware components include:

  • Processor (CPU)
  • RAM
  • Motherboard
  • Storage devices
  • Other hardware components

Software Platform

A software platform commonly includes the operating system and related system software that provides an environment for programs to run.

Examples of operating systems include:

  • Windows
  • Linux
  • macOS
Platform Examples
Hardware Platform Processor, RAM, Motherboard
Software Platform Windows, Linux, macOS

7. Minimum C Program

A C program normally contains a main() function. Program execution begins from the main() function.

Modern Standard Form

Basic C Program
#include <stdio.h>

int main(void)
{
    /* Program statements go here */

    return 0;
}

Understanding the Program

Part Purpose
#include <stdio.h> Includes declarations for standard input/output functions such as printf().
int main(void) Defines the main function, where execution begins.
{ } Defines the body of the function.
return 0; Returns a success status to the operating system.
Important: You may see void main() in older textbooks or older C development environments. It is not the standard form for a hosted C program. Prefer int main(void) or int main() in modern C programs.

8. Example – A Simple C Program

Program
#include <stdio.h>

int main(void)
{
    printf("Welcome to C Programming");

    return 0;
}

Output

Welcome to C Programming

9. Common Parts of a C Program

A C program can contain several important elements. Three commonly encountered elements are:

  1. Statements
  2. Comments
  3. Functions
C Program
Statements
+
Functions
+
Comments

10. Statements

A statement is an instruction in a C program. Most executable and declaration statements end with a semicolon ;.

Examples

Statements
int x;
x = 10;
printf("%d", x);

Types of Program Statements

Type Example Purpose
Declaration Statement int x; Declares a variable.
Assignment Statement x = 10; Assigns a value to a variable.
Output Statement printf("%d", x); Displays output.
Input Statement scanf("%d", &x); Reads input from the user.
Preprocessor Directive #include <stdio.h> Provides instructions processed before compilation.

11. Preprocessor Directives

Preprocessor directives are instructions handled by the C preprocessor before the main compilation stage. They begin with the # symbol.

Examples include #include and #define.

Example 1 – #include

Example
#include <stdio.h>

This tells the preprocessor to include the declarations provided by the standard input/output header.

Example 2 – #define

Example
#define MAX 100

This defines the symbolic constant MAX as 100 for preprocessing purposes.

Remember: Preprocessor directives do not normally end with a semicolon.

12. Compound Statement / Block

A compound statement, also called a block, is a group of statements enclosed within curly braces { }.

Example

Block
{
    int x;
    x = 10;
    printf("%d", x);
}

Blocks are commonly used as the bodies of functions, loops, conditional statements and other control structures.

A single statement can sometimes be enclosed in braces even when braces are not strictly required, but using braces consistently can make program structure easier to read.

13. Functions

A function is a named section of a program that performs a particular task.

Functions help divide a large program into smaller, manageable parts.

Example

Function Example
#include <stdio.h>

void welcome(void)
{
    printf("Welcome!");
}

int main(void)
{
    welcome();

    return 0;
}

In this example, welcome() is a user-defined function. The main() function calls it using:

welcome();

14. Comments in C

Comments are notes written in the source code to describe the program or explain different sections of code. They are ignored by the compiler and are not executed as program instructions.

Comments are useful for programmers because they make source code easier to understand, maintain and modify.

Two Styles of Comments

Comment Type Syntax Use
Single-Line Comment // comment Used for a comment extending to the end of the current line.
Multi-Line Comment /* comment */ Used for comments that can span multiple lines.

14.1 Single-Line Comment

Example
// This program displays a welcome message

#include <stdio.h>

int main(void)
{
    printf("Welcome to C Programming");

    return 0;
}

14.2 Multi-Line Comment

Example
/*
    This program demonstrates
    the use of comments
    in a C program.
*/

#include <stdio.h>

int main(void)
{
    printf("Hello");

    return 0;
}

Why Use Comments?

  • Explain what a section of code does.
  • Make programs easier to read.
  • Help programmers maintain code.
  • Document important logic or assumptions.
  • Temporarily disable a section of code during debugging.

15. Comment Shortcuts in Code::Blocks

Action Shortcut
Add comment style Ctrl + Shift + C
Remove comment style Ctrl + Shift + X
Tip: Keyboard shortcuts can make commenting and uncommenting sections of code much faster while practicing in an IDE.

16. Anatomy of a Simple C Program

Program Structure
// Program to display a message

#include <stdio.h>

int main(void)
{
    printf("Hello, C Programming!");

    return 0;
}
Code Part Purpose
// Program to display a message Comment Describes the purpose of the program.
#include <stdio.h> Preprocessor directive Provides declarations for standard I/O functions.
int main(void) Function Entry point of the program.
{ ... } Block Contains the statements belonging to main().
printf(...); Statement / function call Displays the message.
return 0; Return statement Indicates successful program termination.

17. From Programmer to End-User

Requirement
Programmer
Program
Software
End-User

18. Practice Questions

A. Multiple Choice Questions

1. What is a program?
  1. A computer’s physical component
  2. A set of instructions given to a computer
  3. An operating system only
  4. A storage device
2. Who developed the C language?
  1. James Gosling
  2. Dennis Ritchie
  3. Guido van Rossum
  4. Bill Gates
3. Which symbol begins a preprocessor directive?
  1. ;
  2. //
  3. #
  4. $
4. Which function is normally the entry point of a C program?
  1. start()
  2. run()
  3. main()
  4. begin()
5. Which symbol is used for a single-line comment?
  1. /*
  2. //
  3. #
  4. <!–

B. Short Answer Questions

1. Define a program.
2. What is software?
3. Who developed C and at which organization was it developed?
4. Why is C often called a middle-level language?
5. What is a hardware platform?
6. What is a software platform?
7. What is a statement in C?
8. What is a preprocessor directive?
9. What is a compound statement?
10. What is the purpose of comments?

C. Programming Practice

1. Write a C program to display your name.
2. Write a C program to display the message Welcome to C Programming.
3. Write a C program containing a comment that describes what the program does.
4. Write a C program that declares two integer variables, assigns values to them, and displays their sum.
5. Write a C program that uses both a single-line comment and a multi-line comment.

D. Identify the Parts

Identify each part of the following program:
// Addition of two numbers

#include <stdio.h>

int main(void)
{
    int a, b;

    a = 10;
    b = 20;

    printf("%d", a + b);

    return 0;
}

Identify:

  1. The comment
  2. The preprocessor directive
  3. The function
  4. The declaration statement
  5. The assignment statements
  6. The output statement
  7. The return statement
  8. The compound statement/block

19. Debugging Practice

Find and correct the errors in the following program.

Find the Errors
#include <stdio.h>

int main(void)
{
    int x

    x = 10

    printf("%d", x);

    return 0;
}
Hint: Check the statements that should end with a semicolon ;.

20. Quick Revision

Program
Instructions
C Language
C Program
Remember these key points:
  • A program is a set of instructions for a computer.
  • Software is a broader collection of programs and related components.
  • C was developed by Dennis Ritchie at AT&T Bell Laboratories in the early 1970s.
  • C combines high-level programming features with low-level capabilities.
  • main() is the entry point of a normal C program.
  • Statements usually end with ;.
  • Preprocessor directives begin with #.
  • A block of statements is enclosed in { }.
  • Single-line comments begin with //.
  • Multi-line comments begin with /* and end with */.
Introduction to C Programming
Understand → Write → Compile → Debug → Run → Practice
“`