C notes - PIMS
C notes - PIMS
Program
A program is a detailed set of instructions written in a specific
programming language that tells a computer how to perform a particular task.
It’s like rules that a computer follows to achieve a desired outcome. Programs
can range from very simple, performing basic tasks like calculations, to highly
complex, managing entire systems or applications.
Why Are Programs Important?
Programs are the foundation of all software. Every application you use on
your computer or smartphone—from web browsers to games, from operating
systems to mobile apps—is a program or a collection of programs working
together. In your studies, you'll learn how to write programs to solve problems,
automate processes, and create software that others can use.
Programming Language
A programming language is a way to talk to computers. Just like we use
English or other languages to talk to people, we use programming languages to
give instructions to a computer. These instructions tell the computer what we
want it to do, like showing a picture, adding numbers, or playing a
video.Because computers don't understand human languages, we use
programming languages to make our instructions clear and precise. The
computer then follows these instructions to do exactly what we want. There are
different programming languages, like Python, JavaScript, and Java, each with
its own way of writing these instructions, depending on what we want the
computer to do.
1
C C C C Dennis Ritchie
Types of Programming Language
2. Assembly-Level Language
Assembly language is a step above machine language. It uses symbolic code,
which is more understandable for humans, but still closely tied to the machine’s
hardware.
Characteristics:
1. It’s easier to read and write than machine language but still requires
knowledge of the computer’s architecture.
2. An assembler translates the assembly code into machine code that the
computer can execute.
C C C C C
C C C C Dennis Ritchie
3. It’s often used in situations where you need direct control over hardware, like in
embedded systems or when optimizing performance.
MOV AL, 1
MOV BL, 2
ADD AL, BL
3. High-Level Language
High-level languages are much closer to human languages, making them
easier to learn and use. They abstract away the details of the computer’s
hardware, so you can focus more on solving problems.
Characteristics:
1. They’re easier to read, write, and maintain, which makes them great for
developing software, websites, and apps.
2. A compiler or interpreter translates the high-level code into machine code that
the computer can understand.
Examples include Python, Java, and C++.
num1 = 1
num2 = 2
sum = num1 + num2
printf("The sum is: %d", sum)
HISTORY OF C
1. Fortran
Full Name: Formula Translation
Developed: 1950s
Purpose: Designed for scientific and engineering calculations. It was one of the
C C C C Dennis Ritchie
earliest high-level programming languages.
Impact on C: While not directly related, Fortran set the stage for high-level
programming languages by introducing many concepts used in later
languages, including C.
2. ALGOL
Full Name: Algorithmic Language
Developed: 1958
Purpose: Created for expressing algorithms and had a significant impact on
the development of later programming languages.
Impact on C: ALGOL introduced block structure and scope, which influenced
many subsequent languages, including C.
3. BCPL
Full Name: Basic Combined Programming Language
Developed: 1966
Purpose: Designed for writing system software and compilers, it was simpler
and more flexible than earlier languages.
4. B
Developed: Late 1960s
Creators: Ken Thompson and Dennis Ritchie at Bell Labs
Purpose: An early programming language that simplified and extended
BCPL's concepts. It was used primarily for system programming and
developing the UNIX operating system.
Impact on C: C was developed as an improvement over B, adding features and
capabilities to address some of B’s limitations.
C C C C Dennis Ritchie
5. Traditional C
Developed: Early 1970sCreator: Dennis Ritchie
Purpose: A new programming language designed to improve on B and to be
used for system programming, particularly for rewriting the UNIX operating
system.
Characteristics: Traditional C introduced more structured and powerful
programming features compared to B, such as data types and more complex
control structures.
Documentation
Preprocessor Section
Definition
Global Declaration
Main() Function
Sub Programs
1. Documentation
This section consists of the description of the program, the name of the
program, and the creation date and time of the program. It is specified at the start of
the program in the form of comments. Documentation can be represented as:
/*
description, name of the program, programmer name, date, time etc.
*/
Anything written as comments will be treated as documentation of the program and
this will not interfere with the given code. Basically, it gives an overview to the reader
of the program.
2. Preprocessor Section
All the header files of the program will be declared in the preprocessor section
of the program. Header files help us to access other’s improved code into our code.
A copy of these multiple files is inserted into our program before the process of
compilation.
Example:
#include<stdio.h>
#include<math.h>
3. Definition
Preprocessor directives start with the ‘#’ symbol. The #define preprocessor is
used to create a constant throughout the program. Whenever this name is
encountered by the compiler, it is replaced by the actual piece of defined code.
C C C C Dennis Ritchie
Example:
#define PI 3.14
4. Global Declaration
The global declaration section contains global variables, function
declaration, and static variables. Variables and functions which are declared in this
scope can be used anywhere in the program.
Example:
int num = 18;
5. Main() Function
Every C program must have a main function. The main() function of the
program is written in this section. Operations like declaration and execution are
performed inside the curly braces of the main program.
The return type of the main() function can be int as well as void too.
void() main tells the compiler that the program will not return any value.
The int main() tells the compiler that the program will return an integer value.
Example:
void main()
or
int main()
6. Sub Programs
User-defined functions are called in this section of the program. The control
of the program is shifted to the called function whenever they are called from the
main or outside the main() function. These are specified as per the requirements of
the programmer.
Example:
1. Alphabet
· An alphabet refers to the set of letters that make up a language.
· In programming, it typically refers to the collection of uppercase and lowercase
letters used in a character set.
· In the English alphabet, this includes:
o Uppercase letters: A to Z
o Lowercase letters: a to z
· Examples: A, b, M, x.
2. Digits
· Digits are characters that represent numbers.
· In most programming character sets, these are the characters 0 through 9.
· They are used to represent numerical values in programs and digital data.
· Examples: 0, 1, 2, 9.
3. White Spaces
· White space characters are characters that do not produce visible symbols but
serve to separate elements in text.
· Common white space characters include:
o Space: Regular space between words ( ).
o Tab: A wider space, often used for indentation (\t).
o Newline: Represents the end of a line and starts a new one (\n).
o Carriage return: Moves the cursor to the beginning of the line (\r).
· These characters help in formatting text or organizing code for readability.
5. Special Symbols
· Special symbols (or special characters) are characters that are not alphanumeric
(i.e., they are neither letters nor digits).
· They are often used for operations, punctuation, or other specific purposes in
programming or text.
o Punctuation marks: !, ?, ., ,
o Mathematical symbols: +, -, =, /
o Other symbols: @, #, %, &, *, (
C C C C Dennis Ritchie
Tokens
Tokens are the smallest pieces or building blocks of a program. When you write
code, the computer breaks it down into these tokens so it can understand what you
want it to do.
Types of Tokens (in an easy way):
1. Keywords:
o Special words that are already reserved by the programming language.
o Example: if, else, while, return.
2. Identifiers:
o Names you give to things like variables, functions, or objects.
o Example: age, total, calculateSum.
3. Literals:
o Actual values that appear in the code.
o Example: numbers like 10, 3.14 or text like "Hello".
4. Operators:
o Symbols that tell the computer to do some kind of operation.
o Example: + (add), - (subtract), = (assign a value).
5. Punctuation (Delimiters):
o Special characters that organize code and separate different parts.
o Example: ; (end of a statement), , (separate items).
Programming symbols: {, }, [, ], <, >, ;, :
Keywords
C language supports case-sensitive language, there is a lot of difference between
a, A
These keywords are simple english words has specific meaning or tasks that
compiler understands
All the keywords should be written in lower case
There are 32 keywords in C programming
C C C C Dennis Ritchie
Identifiers
A name given to the variable, function or any other user defined functions
Rules for identifiers
1. The first character must be alphabet or an underscore(_)
2. Identifiers must contain of any alphabet, digit or underscore
3. The maximum length is 31 characters
4. Keywords should not be used as identifiers
5. Identifiers should be single word i.e no blank space is allowed
Variable
A variable is a symbolic name for a storage location in memory that holds a value. In
programming, variables are used to store data (like numbers, strings, etc.) that can be used
and manipulated throughout a program. The value stored in a variable can change over
time, which is why they are called variables.
Variable is like a container that holds a value in a program. Just like how you might
use a labeled box to store things, a variable has a name (the label) and stores data (the
value). You can change the value in the variable whenever you want while the program
runs.
C C C C Dennis Ritchie
Syntax of Declaring Variables
data_type variable_name = value;
int age = 25;
float salary = 3000.50;
char grade = 'A';
Types of Variables
Local Variable:
A local variable is a variable that is declared inside a function or a block of
code. It can only be accessed within that specific function or block, meaning it
exists temporarily while the function or block is executing. Once the function
finishes, the local variable is destroyed, and its value is no longer available.
#include <stdio.h>
int main() {
int num1, num2, sum;
printf("Enter first number: ");
scanf("%d", &num1);
printf("Enter second number: ");
scanf("%d", &num2);
sum = num1 + num2;
printf("The sum of %d and %d is: %d\n", num1, num2, sum);
return 0;
}
Global Variable
A global variable in C is a variable that is declared outside of all functions and is
accessible from any function within the same program. Global variables have a program-
wide scope, meaning they can be used and modified by any function after their
declaration
#include <stdio.h>
int sum;
int main() {
int num1, num2;
printf("Enter first number: ");
scanf("%d", &num1);
printf("Enter second number: ");
scanf("%d", &num2);
sum = num1 + num2;
printf("The sum of %d and %d is: %d\n", num1, num2, sum);
return 0;
}
C C C C Dennis Ritchie
Data Type
In C, a data type defines the kind of data that a variable can hold. It's a way of
telling the computer what kind of value you want to store and how much space that
value will take in memory.
Integer Qualifier
The integer data type is used to modify using long and short integer data type
known as qualifiers. That means the qualifiers of integer data type is long and short
1. Long
It is twice of Integer data type so that the maximum size of this data type is 4
bytes of data and also used for long keyword.
Example : long a; or long int a;
2. Short
It is half of its integer data type known as short integer and also used for short
keyword
Example : short x; or short int a;
The integer data type is used for signed and unsigned integer values
1. Signed
It takes positive and negative integer values
Example : signed int x=-10;
2. Unsigned
It takes only positive numbers and do not allow negative values
Example: unsigned int x=10;
Floating Qualifiers
The modifier of floating point data type is double. The double is twice of
floating point data type and its maximum size is 8 bytes of data and also used for
double keyword
Example : double a,b,c
C C C C Dennis Ritchie
Format Specifiers
int %d
float %f
char %c
double %lf