Programming C Module 1 Notes
Programming C Module 1 Notes
In simple words: Hardware is the "body" of the computer, and Software is the "mind" that tells the body what to
do.
• Operating System (OS): Manages hardware resources and provides services to application programs.
Example: Windows, Linux, macOS, Android.
• Device Drivers: Small programs that allow the OS to communicate with hardware devices like printers,
keyboards, and graphic cards.
• Utility Software: Helps maintain and optimize the computer, such as antivirus programs, disk cleanup
tools, and file compression tools.
• Language Translators/Processors: Convert programs written in high-level languages into machine
language (compilers, interpreters, assemblers).
• Word Processors: Used to create and edit text documents. Example: Microsoft Word.
• Spreadsheet Software: Used for calculations and data analysis. Example: Microsoft Excel.
• Web Browsers: Used to access the internet. Example: Google Chrome, Mozilla Firefox.
• Media Players: Used to play audio and video files. Example: VLC Media Player.
• Games and Educational Software: Used for entertainment and learning.
Purpose Manages computer hardware and Helps user perform specific tasks
resources
Dependency Can run independently of application Cannot run without system software
software
Interaction Works mostly in the background Directly used by the end user
3. Programming Languages
A programming language is a set of rules and symbols used to write instructions (programs) that a computer can
understand and execute. Just as humans use languages like English or Hindi to communicate, programmers use
programming languages to communicate with computers.
Programming languages are broadly classified into three categories based on their closeness to human language or
machine language:
10110000 01100001
This binary code might instruct the processor to move a value into a register, but it is nearly impossible for a
human to understand at a glance.
MOV A, 5
ADD A, 3
This means: move the value 5 into register A, then add 3 to it.
printf("Hello, World!");
This single line simply tells the computer to display the message "Hello, World!" on the screen. It reads almost
like a plain English instruction.
4.2 Flowchart
A flowchart is the pictorial (diagrammatic) representation of an algorithm. It uses standard symbols/shapes to
represent different types of steps, and arrows to show the flow of control from one step to the next. Flowcharts
make it easier to visualize the logic of a program before actually writing the code.
Common Flowchart Symbols
Symbol Shape Name Purpose
Example: Flowchart to add two numbers (described in words, since a flowchart is drawn using shapes)
• Start (Oval)
• Input A, B (Parallelogram)
• Compute SUM = A + B (Rectangle)
• Display SUM (Parallelogram)
• End (Oval)
Each of these steps would be connected by arrows in a real flowchart, showing that the program moves from
Start, to taking input, to processing, to showing output, and finally to End.
Basics of C Programming
1. Introduction to C
C is a general-purpose, high-level programming language that was developed by Dennis Ritchie at AT & TBell
Laboratories in 1972. It was originally created to develop the UNIX operating system. C is often called the
"mother of all programming languages" because many later languages such as C++, Java, and Python borrowed
heavily from its syntax and concepts.
1.1 Characteristics of C
• Simple and Efficient: C has a small set of keywords and a simple syntax, making it easy to learn and fast
to execute.
• Structured Language: A C program can be broken down into smaller parts called functions, making the
code organized and easy to manage.
• Portable: A C program written on one type of computer can be run on another type of computer with little
or no modification.
• Rich Library Support: C provides a large number of built-in functions (like printf, scanf) to perform
common tasks.
• Middle-Level Language: C combines the features of both high-level languages (easy to understand) and
low-level languages (direct hardware access), which is why it is often called a middle-level language.
• Case-Sensitive: In C, "Sum" and "sum" are treated as two completely different names.
1.2 Uses of C
Mainly C Language is used to Develop Desktop application and system software. Some
applications of C language are given below.
• C programming language can be used to design the system software like operating system
and Compiler.
• To develop application software like database and spread sheets.
• For Develop Graphical related application like computer and mobile games.
• To evaluate any kind of mathematical equation, use c language.
• C programming language can be used to design the compilers.
• UNIX Kernel is completely developed in C Language.
• For Creating Compilers of different Languages which can take input from other language
and convert it into lower-level machine dependent language.
• C programming language can be used to design Operating System.
• C programming language can be used to design Network Devices.
• To design GUI Applications. Adobe Photoshop, one of the most popularly used photo editors since
olden times, was created with the help of C.
2. Structure of a C Program
Every C program follows a fixed general structure. Understanding this structure helps beginners know exactly
where to write each part of their code.
Section Purpose
Local Declarations & Statements Variables and instructions inside main() or other functions
return Statement Ends the function and optionally sends a value back
Output:
Hello, World!
Here, #include <stdio.h> tells the compiler to include the Standard Input Output library so that functions like
printf() can be used. The main() function is where the program execution starts and ends. Everything inside the
curly braces { } is the body of the function.
Comment line
It indicates the purpose of the program. It is represented as /*……………………………..*/ Comment line is used
for increasing the readability of the program. It is useful in explaining the program and generally used for
documentation. It is enclosed within the decimeters. Comment line can be single or multiple line but should not
be nested. It can be anywhere in the program except inside string constant & character constant.
Preprocessor Directive:
#include tells the compiler to include information about the standard input/output library. It is also used in
symbolic constant such as #define PI 3.14(value). The stdio.h (standard input output header file) contains
definition &declaration of system defined function such as printf( ), scanf( ), pow( ) etc. Generally printf()
function used to display and scanf() function used to read value.
Global Declaration:
This is the section where variables are declared globally so that it can be access by all the functions used in the
program. And it is generally declared outside the function.
main() It is the user defined function and every function has one main() function from where actually program is
started and it is encloses within the pair of curly braces. The main( ) function can be anywhere in the program but
in general practice it is placed in the first position. Syntax : main() { …….. …….. …….. } The main( ) function
return value when it declared by data type as int main( ) { return 0
} The main function does not return any value when void (means null/empty) as void main(void ) or void main() {
printf (“C language”); } Output: C language
The program execution starts with opening braces and end with closing brace.
/*First c program with return statement*/ #include int main (void) { printf ("welcome to c Programming
language.\n"); return 0; } Output: welcome to c programming language.
4. Preprocessor Directives
Preprocessor directives are special instructions that are processed by the preprocessor before the actual
compilation of the program begins. They always start with a hash symbol (#) and do not end with a semicolon.
• #include: Used to include the contents of a header file (a file containing predefined functions) into the
program. Example: #include <stdio.h>
• #define: Used to define a constant or a macro. Example: #define PI 3.14
#include <stdio.h>
#define PI 3.14 // PI is now a constant value
int main()
{
float radius = 5;
float area = PI * radius * radius;
printf("Area = %f", area);
return 0;
}
Output:
Area = 78.500000
Wherever PI appears in the program, the preprocessor simply replaces it with 3.14 before compilation starts.
5.1 Compilation
Compilation is the process of converting the entire high-level source code (written by the programmer) into
machine code (object code) all at once, before execution begins. A special program called the Compiler performs
this task. If there is even one error in the code, the compiler will not produce the final output until it is fixed. C
programs are typically compiled languages.
5.2 Interpretation
Interpretation is an alternative method where the source code is translated and executed line-by-line, rather than
all at once. A program called an Interpreter reads one instruction, executes it immediately, then moves to the next.
This makes debugging easier but generally makes execution slower compared to compiled programs. (Languages
like Python typically use interpretation, while C mainly uses compilation.)
5.3 Loading
Loading is the process of copying the executable program from secondary storage (like a hard disk) into the
computer's main memory (RAM) so that the CPU can access and execute its instructions. A system program
called the Loader performs this job.
5.4 Linking
Linking is the process of combining the object code of the program with the object code of any library functions it
uses (such as printf or scanf), to create a single executable file. A program called the Linker performs this task.
For example, when a program uses printf(), the linker connects the program's object code with the pre-compiled
code of the printf() function from the C standard library.
• Single-line comment: starts with // and continues till the end of the line.
• Multi-line comment: starts with /* and ends with */, and can span multiple lines.
#include <stdio.h>
int main()
{
printf("Learning C is fun!");
return 0;
}
6.2 Keywords
Keywords are reserved words that have a special, fixed meaning in the C language. They cannot be used as
variable names because the compiler already understands them as part of the language's syntax. C has 32
keywords in total. Some commonly used ones are:
Keyword Meaning
6.3 Identifiers
Identifiers are the names given by the programmer to variables, functions, arrays, and other user-defined items.
They must follow certain rules:
• Can contain letters, digits, and underscore (_), but must start with a letter or underscore, not a digit.
• Cannot be a keyword (e.g., you cannot name a variable int).
• Cannot contain spaces or special symbols like @, #, %.
• C is case-sensitive, so Total and total are different identifiers.
• first characters should be alphabet or underscore
Identifiers are generally given in some meaningful name such as value, net_salary, age, data etc. An identifier name
may be long, some implementation recognizes only first eight characters, most recognize 31 characters. ANSI
standard compiler recognize 31 characters. Some invalid identifiers are 5cb, int, res#, avg no etc.
Data types in the C programming language are used to specify what kind of value can be stored in a variable.
The memory size and type of the value of a variable are determined by the variable data type.
In the c programming language, data types are classified as
follows...
Primary Data Types: The primary/primitive data types in the C programming language are the basic data
types. All the primary data types are already defined in the system. Primary data types are also called as Built-
In data types.
Integer: We use the keyword "int" to represent integer data type in c.
• We use the keyword int to declare the variables and to specify the return type of a function.
• The integer data type is used with different type modifiers like Short (2bytes), long (4 bytes), signed (2bytes)
and unsigned (2bytes).
The access specifier for integer data type is %d.
Float: Floating-point data types are a set of numbers with the decimal value. Every floating-point value must
contain the decimal value. The floating-point data type has two variants: Float and double
We use the keyword "float" to represent floating-point (4 bytes) datatype and "double" to represent double (8
bytes) data type in c.
The float value contains 6 decimal places whereas double value contains 15 or 19 decimal places.
The access specifier for float is %f and double is %ld
8. Variables
A variable is an object whose value can change while a programme is running. A variable is a name given to a
memory location that is used to store a value which can change (vary) during program execution. Before using a
variable, it must be declared with a data type, telling the compiler what kind of value it will hold.
Each variable has a memory location or address assigned to it, and the value of that variable is stored there. Each
variable has its own name, data type, size, and value. All variables must have a type so that the compiler can
record all necessary information about them, generate the appropriate code during translation, and allocate the
necessary memory space.
Rules for Constructing Variable Name in C language are listed below:
1. Variable name may be a combination of alphabets, digits or underscores. Sometimes, an additional constraint
on the number of characters in the name is imposed by compilers in which case its length should not exceed 8
characters.
2. First character must be an alphabet or an underscore (_).
3. No commas or blank spaces are allowed in a variable name.
4. Among the special symbols, only underscore can be used in a variable name. E.g.: emp_age, item_4, etc.
5. No word, having a reserved meaning in C can be used for variable name
Syntax:
data_type variable_name;
Example
int marks, total;
float salary;
char ch;
Example: Declaring and using variables
#include <stdio.h>
int main()
{
int marks; // declaration
marks = 85; // assigning a value
printf("Marks = %d", marks);
return 0;
}
Output:
Marks = 85
Here, marks is a variable of type int. It first holds no meaningful value (declaration), and then we store 85 into it
(assignment). Later in the program, this value can even be changed, for example, marks = 90;
9. Constants
A constant is a value that does not change during the execution of the program. Unlike a variable, once a constant
is set, it cannot be modified later. Constants can be created in two common ways:
#include <stdio.h>
int main()
{
const int daysInWeek = 7;
printf("Days in a week = %d", daysInWeek);
// daysInWeek = 10; // This line would cause an ERROR
return 0;
}
Output:
Days in a week = 7
Formatted Input/Formatted Output In this category, we have functions that allow input and output operations to be
performed in a fixed format.
Printf() and scanf() are the two most commonly used functions for formatted I/O. (). The printf() function is used
to view formatted data items on a standard output device, such as a monitor, while the scanf() function is used to
read formatted data input from a standard input device, such as a keyboard.
Where format string refers to a string in enclosed in double quotes that contain formatting information and arg1,
arg2, ...., argn are arguments (may be constants, variable, or other complex expressions) whose values are
formatted and printed according to the specification of the format string.
10.2 scanf() - for Input
scanf() is used to take input from the user through the keyboard and store it in a variable. It requires an ampersand
(&) before the variable name, which tells the compiler the memory address where the entered value should be
stored.
#include <stdio.h>
int main()
{
int age;
printf("Enter your age: "); // output: asking for input
scanf("%d", &age); // input: storing entered value
printf("You are %d years old.", age); // output: showing result
return 0;
}
Output:
Enter your age: 19
You are 19 years old.
%d Integer (int)
#include <stdio.h>
void main( )
{
int c;
printf("Enter a character");
c = getchar();
putchar(c);
}
Character I/O functions have a drawback in that they can only handle one character at a time.
Strings, on the other hand, are commonly used in real-world programmes. A string is nothing more
than a set of characters. String I/O functions are functions that make it easier to move strings
between a computer and regular I/O devices. Following function can be used for handling strings
I/O:
1. gets()
2. puts()
Program for gets ()
#include<stdio.h>
void main ()
{
char s[25];
printf("Enter the string? ");
gets(s);
printf("You entered %s",s);
}
#include <stdio.h>
void main( )
{
char name[10];
printf(“Enter Name”);
gets(name);
printf("Entered name is");
puts(name);
return 0;
}
11. Operators in C
An operator is a symbol that tells the compiler to perform a specific mathematical, relational, logical, or other
operation between operands (values or variables). C provides several categories of operators. Each type is
explained below with its own table and a complete, runnable example program.
Unary operator
Unary operators are operators that act upon a single operand to produce a new value.
Binary operators
Binary operators are those that operate on two operands. For example, a + b—the addition operator
(+) surrounded by two operands—is a popular binary expression. Arithmetic, relational, logical,
and assignment operators are all subsets of binary operators.
11.1 Arithmetic Operators
Used to perform basic mathematical calculations between two operands.
+ Addition a+b 13
- Subtraction a-b 7
* Multiplication a*b 30
#include <stdio.h>
int main()
{
int a = 10, b = 3;
printf("Sum = %d\n", a + b);
printf("Difference = %d\n", a - b);
printf("Product = %d\n", a * b);
printf("Quotient = %d\n", a / b);
printf("Remainder = %d\n", a % b);
return 0;
}
Output:
Sum = 13
Difference = 7
Product = 30
Quotient = 3
Remainder = 1
== Equal to a == b 0 (false)
#include <stdio.h>
int main()
{
int a = 10, b = 3;
printf("a == b : %d\n", a == b);
printf("a != b : %d\n", a != b);
printf("a > b : %d\n", a > b);
printf("a < b : %d\n", a < b);
printf("a >= b : %d\n", a >= b);
printf("a <= b : %d\n", a <= b);
return 0;
}
Output:
a == b : 0
a != b : 1
a>b :1
a<b :0
a >= b : 1
a <= b : 0
Note: relational operators are most often used inside if statements and loops to make decisions, e.g., if (a > b)
{ ... }
#include <stdio.h>
int main()
{
int age = 20;
int hasID = 1; // 1 means true, 0 means false
Output:
AND : 1
OR : 1
NOT : 0
Here, age >= 18 is true (1) and hasID == 1 is true (1), so AND gives 1. Since at least one side of OR is true, OR
also gives 1. NOT simply flips the true result of (age >= 18) to 0.
#include <stdio.h>
int main()
{
int a = 10;
a += 5; printf("After += : %d\n", a); // 15
a -= 3; printf("After -= : %d\n", a); // 12
a *= 2; printf("After *= : %d\n", a); // 24
a /= 4; printf("After /= : %d\n", a); // 6
a %= 4; printf("After %%= : %d\n", a); // 2
return 0;
}
Output:
After += : 15
After -= : 12
After *= : 24
After /= : 6
After %= : 2
• Pre-increment/decrement (++a or --a): the value is changed first, then used in the expression.
• Post-increment/decrement (a++ or a--): the value is used in the expression first, then changed.
Example: Program showing the difference between pre and post increment
#include <stdio.h>
int main()
{
int a = 5, b = 5;
printf("Post-increment: %d\n", a++); // prints 5, then a becomes 6
printf("a after post-increment: %d\n", a);
Output:
Post-increment: 5
a after post-increment: 6
Pre-increment: 6
b after pre-increment: 6
#include <stdio.h>
int main()
{
int a = 6, b = 3; // a = 0110, b = 0011 in binary
printf("a & b = %d\n", a & b);
printf("a | b = %d\n", a | b);
printf("a ^ b = %d\n", a ^ b);
printf("~a = %d\n", ~a);
printf("a << 1 = %d\n", a << 1);
printf("a >> 1 = %d\n", a >> 1);
return 0;
}
Output:
a&b =2
a|b =7
a^b =5
~a = -7
a << 1 = 12
a >> 1 = 3
Example: Program using the ternary operator to find the largest of two numbers
#include <stdio.h>
int main()
{
int a = 10, b = 20, largest;
largest = (a > b) ? a : b; // if a>b is true, largest=a, else largest=b
printf("Largest = %d", largest);
return 0;
}
Output:
Largest = 20
#include <stdio.h>
int main()
{
int a = 10;
printf("Size of int = %lu bytes\n", sizeof(a));
printf("Value of a = %d\n", a);
printf("Address of a = %p\n", &a);
return 0;
}
Output:
Size of int = 4 bytes
Value of a = 10
Address of a = 0x7ffe4a3c9b1c (this will vary each time you run it)
sizeof() is very useful when writing code that needs to work correctly regardless of how much memory a data type
occupies on a particular machine. The address-of operator (&) is essential when using scanf() and pointers, since
it tells the compiler exactly where in memory a value should be stored.
2. Multiplicative *, /, %
Precedence (High to Low) Operators
3. Additive +, -
6. Equality ==, !=
9. Conditional ?:
#include <stdio.h>
int main()
{
int result = 10 + 5 * 2; // * has higher precedence than +
printf("Result = %d", result); // 5*2 is done first, then +10
return 0;
}
Output:
Result = 20
#include <stdio.h>
int main()
{
int a = 5;
float b = 2.5;
float result = a + b; // int 'a' is automatically converted to float
printf("Result = %f", result);
return 0;
}
Output:
Result = 7.500000
Here, the compiler automatically converts the integer value of a into a floating-point value before adding it to b,
since the result is being stored in a float variable.
#include <stdio.h>
int main()
{
int a = 7, b = 2;
float result;
result = (float) a / b; // manually converting 'a' to float
printf("Result = %f", result);
return 0;
}
Output:
Result = 3.500000
Without the (float) cast, both a and b would be treated as integers, and the division 7 / 2 would give 3 (losing the
decimal part). By casting a to float first, the division correctly produces 3.5.
Control Less control for the programmer Full control for the programmer