Introduction to C Program
Introduction to C Programming
What is C?
C is a general-purpose, procedural programming language developed by Dennis Ritchie at Bell Labs in the early 1970s.
C is widely used for:
- System programming
- Operating systems
- Embedded systems
- Compilers
- Application development
- Learning fundamental programming concepts
C is known for being fast, efficient, portable, and close to hardware.
Features of C
Some important features of C are:
- Simple – C has a relatively small set of keywords and a straightforward syntax.
- Fast – C programs can execute very efficiently.
- Portable – C programs can be compiled on different platforms with relatively few changes.
- Procedural – Programs are organized using functions and procedures.
- Structured – C supports structured programming using conditions, loops, and functions.
- Middle-level language – C provides both high-level programming features and low-level memory manipulation.
- Extensible – Programs can be extended using functions and libraries.
Basic Structure of a C Program
A simple C program looks like this:
#include <stdio.h>
int main()
{
printf("Hello, World!");
return 0;
}
Let's understand each part.
1. #include <stdio.h>
This is a preprocessor directive.
#include <stdio.h>
stdio.h stands for Standard Input Output Header.
It provides functions such as:
printf()
scanf()
2. main() Function
Every C program must have a main() function because program execution starts from main().
int main()
{
// program statements
}
The int means that the function returns an integer value.
3. Statements
Statements are instructions executed by the program.
Example:
printf("Hello, World!");
Most C statements end with a semicolon ;.
4. return 0
return 0;
This indicates that the program has finished successfully.
Comments in C
Comments are notes written inside the source code. They are ignored by the compiler.
Single-line comment
// This is a comment
printf("Hello");
Multi-line comment
/*
This is a
multi-line comment
*/
printf("Hello");
Comments make programs easier to understand and maintain.
C Program Execution Process
A C program generally goes through several stages before it runs.
Source Code
↓
Preprocessor
↓
Compiler
↓
Assembler
↓
Linker
↓
Executable Program
↓
Execution
Source Code
The code written by the programmer is called source code.
Example:
#include <stdio.h>
int main()
{
printf("Hello");
return 0;
}
Preprocessor
The preprocessor handles directives such as:
#include
#define
Compiler
The compiler translates C source code into lower-level code and checks for syntax and other compilation errors.
Linker
The linker combines the required library code with the compiled program.
Executable
The final program can then be executed by the operating system.
Example: First C Program
#include <stdio.h>
int main()
{
printf("Welcome to C Programming!");
return 0;
}
Output
Welcome to C Programming!
Important Rules of C Syntax
1. C is case-sensitive
These are different:
age
Age
AGE
2. Statements normally end with ;
Correct:
printf("Hello");
Incorrect:
printf("Hello")
3. Curly braces define a block
int main()
{
printf("Hello");
return 0;
}
The { } define the body of the main() function.
4. Strings are written inside double quotes
printf("Hello");
5. Character values use single quotes
char grade = 'A';
Simple Program Example
#include <stdio.h>
int main()
{
printf("My name is Bhuwan.");
printf("I am learning C programming.");
return 0;
}
Output
My name is Bhuwan.
I am learning C programming.
Key Terms
| Term | Meaning |
|---|---|
| C | A general-purpose programming language |
| Compiler | Converts source code into machine/low-level code |
main() | Starting point of program execution |
#include | Includes a header file |
stdio.h | Standard input/output header |
printf() | Displays output |
| Statement | An instruction in a program |
| Comment | Non-executable explanatory text |
return 0 | Indicates successful program termination |
Summary
In this topic, we learned:
- What C programming is
- History and features of C
- Basic structure of a C program
- Purpose of
#include <stdio.h> - Role of the
main()function - Statements and semicolons
- Comments in C
- The C program compilation process
- Basic rules of C syntax
- How to write and execute a simple C program
Practice Questions
- What is C programming?
- Who developed the C programming language?
- List any five features of C.
- What is the purpose of the
main()function? - What is the use of
#include <stdio.h>? - What is a compiler?
- Why is
return 0used in a C program? - What is the difference between single-line and multi-line comments?
- Write a C program to display your name.
- Write a C program to display
"Hello, World!".