C Programming is a general-purpose programming language used to develop software, system applications, embedded programs, utilities, and many other types of applications.
C is known for its speed, efficiency, structured programming features, and close interaction with computer memory. Learning C helps students understand important programming concepts such as variables, data types, operators, input/output, conditions, loops, and functions.
1. What is C Programming?
C is a general-purpose, procedural programming language developed in the early 1970s.
It is widely used for learning programming fundamentals because its syntax introduces concepts that are important in many other programming languages.
Common Uses of C
System software
Embedded systems
Device software
Utilities
Compilers
Operating-system components
Database and networking software
Programming education
2. Features of C
Important features of C include:
Simple and structured syntax
Fast execution
Portable programs
Supports functions
Supports pointers
Provides operators for different types of calculations
Provides direct memory-related programming features
Suitable for system-level programming
Supports modular programming
3. Structure of a C Program
A basic C program generally contains:
Preprocessor directives
Functions
Statements
Variables
Comments
Example
#include <stdio.h>
int main()
{
printf(“Hello, World!”);
return 0;
}
Explanation
Part Purpose
#include <stdio.h> Includes standard input/output functions
int main() Main function where program execution begins
{ } Defines the function body
printf() Displays output
return 0; Indicates successful program completion
4. The main() Function
The main() function is the starting point of a normal C program.
int main()
{
// Program statements
return 0;
}
When a C program is executed, control normally begins from main().
5. Comments in C
Comments are used to explain code. They are ignored by the compiler.
Single-Line Comment
// This is a comment
Multi-Line Comment
/*
This is a
multi-line comment
*/
Comments make programs easier to understand and maintain.
6. C Tokens
A token is one of the basic elements of a C program.
Common types of tokens include:
Keywords
Identifiers
Constants
Strings
Operators
Special symbols
Example
int age = 20;
Here:
int → keyword
age → identifier
20 → constant
= → operator
; → special symbol
7. Keywords
Keywords are reserved words that have predefined meanings in C.
Examples
int
float
char
if
else
for
while
return
void
switch
case
break
continue
Keywords cannot normally be used as variable or function names.
8. Identifiers
Identifiers are names given to programming elements such as:
Variables
Functions
Arrays
Structures
Example
int studentAge;
float marks;
Here, studentAge and marks are identifiers.
Rules for Identifiers
Can contain letters, digits, and underscore.
Cannot start with a digit.
Cannot contain spaces.
Cannot be a reserved keyword.
C is case-sensitive.
Valid
student
student_name
marks1
totalMarks
Invalid
1student
student name
float
9. Variables
A variable is a named memory location used to store a value that can change during program execution.
Example
int age = 20;
Here:
int is the data type.
age is the variable.
20 is the initial value.
Another Example
float salary = 25000.50;
char grade = ‘A’;
10. Constants
A constant is a value that does not change during program execution.
Examples:
10
25.5
‘A’
“Hello”
C also supports named constants using const.
const int MAX = 100;
The value of MAX should not be modified after initialization.
11. Data Types in C
A data type specifies what kind of value a variable can store.
Common Basic Data Types
Data Type Typical Use Example
int Whole numbers 25
float Decimal numbers 25.5
double Higher-precision decimal values 25.5678
char Single character ‘A’
void No value void
The exact size and range of some C data types can depend on the compiler and system.
12. Declaring Variables
Variables can be declared before they are used.
int age;
float marks;
char grade;
Values can then be assigned:
age = 20;
marks = 85.5;
grade = ‘A’;
They can also be declared and initialized together:
int age = 20;
float marks = 85.5;
char grade = ‘A’;
13. Input and Output
C provides functions for taking input and displaying output.
The stdio.h header contains commonly used input/output functions.
Output Using printf()
#include <stdio.h>
int main()
{
printf(“Welcome to C Programming”);
return 0;
}
Output
Welcome to C Programming
14. Taking Input Using scanf()
The scanf() function can be used to read formatted input.
Example
#include <stdio.h>
int main()
{
int age;
printf(“Enter your age: “);
scanf(“%d”, &age);
printf(“Your age is %d”, age);
return 0;
}
Explanation
%d is used for an int.
&age supplies the address where the input should be stored.
15. Common Format Specifiers
Specifier Common Use
%d int
%f float
%lf double with scanf()
%c Character
%s String
Example
int age = 20;
float marks = 85.5;
char grade = ‘A’;
printf(“%dn”, age);
printf(“%fn”, marks);
printf(“%cn”, grade);
16. Escape Sequences
Escape sequences are special character combinations used inside strings.
Escape Sequence Meaning
n New line
t Tab
Backslash
” Double quotation mark
‘ Single quotation mark
Example
printf(“Name:tAjaynCourse:tADCA”);
17. Operators in C
Operators are symbols used to perform operations on values and variables.
Main Categories
Arithmetic operators
Relational operators
Logical operators
Assignment operators
Increment/decrement operators
Conditional operator
Bitwise operators
18. Arithmetic Operators
Operator Operation Example
+ Addition a + b
– Subtraction a – b
* Multiplication a * b
/ Division a / b
% Remainder a % b
Example
int a = 10;
int b = 3;
printf(“%d”, a + b);
Output:
13
19. Assignment Operators
The basic assignment operator is:
=
Example
int marks = 80;
Other compound assignment operators include:
+=
-=
*=
/=
%=
Example
int x = 10;
x += 5;
Now x becomes 15.
20. Relational Operators
Relational operators compare values.
Operator Meaning
== Equal to
!= Not equal to
> Greater than
< Less than
>= Greater than or equal to
<= Less than or equal to
These operators produce a true/false result used in decision-making.
21. Logical Operators
Logical operators combine conditions.
Operator Meaning
&& Logical AND
`
! Logical NOT
Example
if (age >= 18 && age <= 60)
{
printf(“Eligible”);
}
22. Increment and Decrement
Increment
x++;
Increases the value of x by 1.
Decrement
x–;
Decreases the value of x by 1.
They can also be written as:
++x;
–x;
The position can affect the value used within an expression, which will be studied in more detail later.
23. Type Conversion
Type conversion means converting a value from one data type to another.
Example
int a = 10;
float b;
b = (float)a;
Here, a is explicitly converted to float.
Two Common Forms
Implicit conversion
Explicit conversion using a cast
24. Operator Precedence
When an expression contains multiple operators, C follows rules that determine the order of evaluation.
Example
int result = 10 + 5 * 2;
Multiplication is evaluated before addition, so the result is:
20
Parentheses can be used to make the intended order clear:
int result = (10 + 5) * 2;
Result:
30
25. Basic C Program: Addition of Two Numbers
#include <stdio.h>
int main()
{
int a, b, sum;
printf(“Enter first number: “);
scanf(“%d”, &a);
printf(“Enter second number: “);
scanf(“%d”, &b);
sum = a + b;
printf(“Sum = %d”, sum);
return 0;
}
Example Output
Enter first number: 25
Enter second number: 15
Sum = 40
26. Basic C Program: Student Marks
#include <stdio.h>
int main()
{
float marks;
printf(“Enter marks: “);
scanf(“%f”, &marks);
printf(“Marks = %.2f”, marks);
return 0;
}
The %.2f format displays the value with two digits after the decimal point.
27. Errors in C Programs
Programming errors can prevent a program from working correctly.
1. Syntax Error
Occurs when the rules of the C language are violated.
Example:
printf(“Hello”)
The semicolon is missing.
2. Runtime Error
Occurs while the program is running.
3. Logical Error
The program runs but produces an incorrect result because the logic is wrong.
Practical Activity
Activity 1: Hello World Program
Write and execute:
#include <stdio.h>
int main()
{
printf(“Welcome to ADCA C Programming”);
return 0;
}
Activity 2: Basic Calculator
Create a program that accepts two numbers and displays:
Addition
Subtraction
Multiplication
Division
Remainder
Example:
Enter first number: 20
Enter second number: 5
Addition = 25
Subtraction = 15
Multiplication = 100
Division = 4
Remainder = 0
Activity 3: Student Information
Create a C program that accepts:
Student Name
Roll Number
Course
Marks
and displays the information in a formatted manner.
Key Terms
C Programming – A general-purpose procedural programming language.
Program – A set of instructions written to perform a task.
Compiler – Software that translates C source code into machine-executable form.
Variable – A named storage location whose value can change.
Constant – A value intended not to change during program execution.
Data Type – Specifies the type of data a variable can store.
Keyword – Reserved word with a predefined meaning.
Identifier – Name given to a programming element.
Operator – Symbol used to perform an operation.
printf() – Function used for formatted output.
scanf() – Function used for formatted input.
Syntax Error – Error caused by incorrect language syntax.
Runtime Error – Error occurring during program execution.
Logical Error – Error in program logic that produces an incorrect result.
Type Conversion – Conversion of a value from one data type to another.
Leave a Reply