Introduction to C Programming
- 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?
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:
- Store the first number.
- Store the second number.
- Add the two numbers.
- Display the result.
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?
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
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
Programming
& Applications
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.
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
#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. |
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
#include <stdio.h>
int main(void)
{
printf("Welcome to C Programming");
return 0;
}
Output
9. Common Parts of a C Program
A C program can contain several important elements. Three commonly encountered elements are:
- Statements
- Comments
- Functions
10. Statements
;.
Examples
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
# symbol.
Examples include #include and #define.
Example 1 – #include
#include <stdio.h>
This tells the preprocessor to include the declarations provided by the standard input/output header.
Example 2 – #define
#define MAX 100
This defines the symbolic constant MAX as
100 for preprocessing purposes.
12. Compound Statement / Block
{ }.
Example
{
int x;
x = 10;
printf("%d", x);
}
Blocks are commonly used as the bodies of functions, loops, conditional statements and other control structures.
13. Functions
Functions help divide a large program into smaller, manageable parts.
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:
14. Comments in C
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
// This program displays a welcome message
#include <stdio.h>
int main(void)
{
printf("Welcome to C Programming");
return 0;
}
14.2 Multi-Line Comment
/*
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 |
16. Anatomy of a Simple C Program
// 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
18. Practice Questions
A. Multiple Choice Questions
- A computer’s physical component
- A set of instructions given to a computer
- An operating system only
- A storage device
- James Gosling
- Dennis Ritchie
- Guido van Rossum
- Bill Gates
- ;
- //
- #
- $
- start()
- run()
- main()
- begin()
- /*
- //
- #
- <!–
B. Short Answer Questions
C. Programming Practice
Welcome to C Programming.
D. Identify the Parts
// Addition of two numbers
#include <stdio.h>
int main(void)
{
int a, b;
a = 10;
b = 20;
printf("%d", a + b);
return 0;
}
Identify:
- The comment
- The preprocessor directive
- The function
- The declaration statement
- The assignment statements
- The output statement
- The return statement
- The compound statement/block
19. Debugging Practice
Find and correct the errors in the following program.
#include <stdio.h>
int main(void)
{
int x
x = 10
printf("%d", x);
return 0;
}
;.
20. Quick Revision
- 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*/.