0% found this document useful (0 votes)
13 views

C notes - PIMS

Uploaded by

shreyas19052006
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
13 views

C notes - PIMS

Uploaded by

shreyas19052006
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 20

C C C C Dennis Ritchie

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

Programming languages can be categorized into three main levels based on


how close they are to the computer's hardware and how easy they are for
humans to use:
Machine-Level Language Assembly-Level Language High-Level Language.
1. Machine-Level Language
This is the most basic programming language, consisting entirely of
binary code—1s and 0s—which the computer's hardware can directly execute.
Characteristics:
1. It’s very fast and efficient because the computer can understand it directly
without any translation.
2. It’s extremely difficult for humans to read or write because it’s so low-
level.Every type of computer has its own machine language, so code written in
one machine language won’t work on a different type of computer.
10110000 00000001
10110001 0000001000000011 0

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.

.Features or Characteristics of C Language

1. Simple and Efficient


· Simple: C has a basic, easy-to-understand style of writing code. It doesn't have
too many complex rules, making it easier for beginners to grasp.
· Efficient: C is designed to run programs quickly. It can use the computer's
hardware very effectively, which helps programs run fast and use resources
well.
2. Structured Programming Language
· Description: C allows you to write code in an organized way by breaking it into
smaller parts called functions. This makes it easier to write, read, and manage
your code.
· Example: Instead of writing a huge block of code all at once, you can create
separate functions for different tasks, like one function for adding numbers and
another for printing results.
3. Portability
· Description: Programs written in C can be moved from one type of computer to
another without needing major changes. This is because C follows standard
rules that work on many different systems.
· Example: You can write a C program on a Windows computer and then run it on
a Mac or Linux computer without changing the code.
C C C C Dennis Ritchie
4. Middle-Level Language
· Description: C is a mix of high-level and low-level programming. It's high-level
because it uses simple commands, but it's also low-level because it can interact
directly with the computer's hardware.
· Example: You can write simple code in C to do everyday tasks, but you can also
use it to control hardware directly, like managing memory.
5. General-Purpose Language
· Description: C is used for many different kinds of programming, not just one
specific type. It's flexible and can handle various programming needs.
· Example: C can be used to create everything from a simple calculator to
complex software like an operating system.
6. Speed
· Description: C programs run very quickly because they are compiled into
machine code, which is the language that the computer understands directly.
· Example: Games and real-time systems often use C because it helps them run
smoothly and quickly.
7. Powerful
· Description: C provides strong features for controlling and managing computer
hardware. It gives you a lot of power to do things that other languages might
not.
· Example: C lets you manipulate memory directly, which is useful for developing
complex software and systems.
8. Rich Standard Library
· Description: C comes with a set of built-in functions that help with common
tasks like input/output, handling strings, and math calculations.
· Example: You can use functions like printf() to print messages and scanf() to
read user input without having to write those functions yourself.
9. Syntax-Based Language
· Description: C has specific rules for how code should be written. Following
these rules is necessary for the code to work correctly.
· Example: You need to use the correct punctuation and keywords, like using
curly braces {} to define a block of code.
C C C C Dennis Ritchie
10. Format-Free Language
· Description: C doesn't enforce strict rules about how your code should be
spaced or formatted. However, good formatting makes your code easier to
read.
· Example: You can write your code with different styles of indentation and
spacing, but consistent formatting helps others understand your code better.
11. Compiled Language
· Description: C code is turned into machine code by a compiler before it runs.
This makes the program run faster compared to languages that are interpreted
line-by-line.
· Example: When you compile a C program, it's converted into a file that the
computer can execute directly.
12. Case-Sensitive Language
· Description: In C, uppercase and lowercase letters are treated differently. This
means Variable and variable are considered separate.
· Example: If you declare a variable as int count;, using Count or COUNT
elsewhere will refer to different variables.
13. Easy to Extend
· Description: You can easily add new features or functions to your C programs.
It's flexible and allows for growth and changes.
· Example: You can write additional functions or include libraries to add new
capabilities to your program without starting from scratch.
C C C C Dennis Ritchie
Structure of C
The basic structure of a C program is divided into 6 parts which makes it easy
to read, modify, document, and understand in a particular format. C program must
follow the below-mentioned outline in order to successfully compile and execute.
Debugging is easier in a well-structured C program.

Sections of the C Program

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:

int sum(int x, int y)


{
return x+y;
}
C C C C Dennis Ritchie
Structure of C Program with example
Example: Below C program to find the sum of 2 numbers:
// Documentation
/*
file: sum.c
author: you
description: program to find sum.
*/
#include <stdio.h> /* Link*/
#define X 20 /* Definition */
int sum(int y); /* Global Declaration */
int main(void) /*Main() Function*/
{
int y = 55;
printf("Sum: %d", sum(y));
return 0;
}
int sum(int y) /* Subprogram */
{
return y + X;
}
Output
Sum: 75
C C C C Dennis Ritchie
1. Writing the Program
· You start by writing the C program using a text editor like Notepad, Visual Studio
Code, or any IDE (Integrated Development Environment).
· The code contains human-readable instructions, such as variables, functions, loops,
and conditional statements.
· This code tells the computer to print "Hello, World!" on the screen.
· The program is saved with a .c extension (e.g., program.c).
2. Preprocessing
· The preprocessor scans the code for lines that start with #, such as #include or
#define.
o #include <stdio.h> tells the preprocessor to include the stdio.h header file,
which contains the definition for printf.
o #define can be used to create macros or constants that get substituted before
the actual compilation.
· The preprocessor resolves these directives and makes replacements or inclusions.
o For example, it replaces the #include directive with the actual contents of the
stdio.h file.
· The preprocessed code is then sent to the compiler for the next step.
· The preprocessed output is often called a translation unit.
3. Compilation
· The compiler converts the preprocessed code into machine code (low-level code
that the CPU can understand).
· It checks for syntax errors like:
o Missing semicolons (;),
o Undefined variables,
o Incorrect function calls.
· If the code passes all syntax checks, the compiler generates an object file (e.g.,
program.o or program.obj).
· The object file contains the machine code but isn't yet a complete program because
it might rely on external libraries or other functions.
· If there are errors, the compiler provides error messages that help you debug the
code.
4. Linking
· The linker takes the object file and links it with other necessary object files and
libraries.
· If you used any external functions, such as printf, the linker connects your code with
C C C C Dennis Ritchie
the library where printf is defined (in this case, the C standard library).
· The linker ensures all function calls are resolved and the necessary code is
included.
· It may also link multiple object files if your program is split into multiple source
files.
· The result of linking is an executable file:
o On Windows, it might be program.exe.
o On Linux or macOS, the default name might be a.out.
· If there are missing functions or libraries, the linker will generate errors.
5. Loading
· The loader is part of the operating system, and its job is to load the executable file
into the computer's memory (RAM) before execution.
· The loader assigns memory addresses for all variables, functions, and code.
· It also sets up the execution environment for the program, including the program
counter (which tells the CPU where to start executing).
· Any dynamic libraries (such as shared libraries or DLLs) are also loaded during this
step.
· Once everything is ready, the loader hands over control to the CPU.
6. Execution
· The CPU begins executing the instructions in the program, starting from the main()
function.
· In the example, the CPU processes the printf("Hello, World!\n"); statement, which
causes "Hello, World!" to appear on the screen.
· The program continues executing instructions in sequence until it reaches the
end.
· When the program encounters return 0;, it finishes execution and returns control
to the operating system.
· If the program completes successfully, the operating system cleans up and
releases any resources used by the program (memory, file handles, etc.).
C C C C Dennis Ritchie
C C C C Dennis Ritchie
C Character Set
· A character is any symbol or letter that can be typed or represented digitally. This
includes letters, numbers, punctuation marks, spaces, and control characters.
· Characters are fundamental building blocks in computing
· Examples: A, b, 7, *, @, \n (newline character).

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

Valid Identifier Invalid Identifier


ADD 3add
Add add+123
_add for
Student_Name ABC XYZ
Name Student%123
A3123B _Abc*153

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.

Basically there are three types of fundamental data type


1. Integer data type
2. Floating point data type
3. Character data type

1. Integer Data Type


The integer data type is used to represent only the whole numbers such as
10, 20,30,40, and so on. The integer data type is used to declare using ‘int’ keyword
and the maximum size is 2 bytes of data.
Example: int a;
int x,y,z;

2. Floating data Type


It is used to represent real numbers or fractional numbers are used in
program execution. This data type maximum size is 4 bytes of data and used for
float keyword.
Example: float x;

3. Character data type


It is used to represent a single character only and its maximum size is only
one byte of data and it is used for char keyword
Example : char ch;
When we assign the character datatype variable it must be enclosed with quotes (’
‘).
char ch =’A’;
C C C C Dennis Ritchie
Qualifiers
In C, qualifiers are keywords that you can use with data types to modify the
behavior of variables. They give additional information about the variable, such as
whether its value can change or the range of values it can hold. Qualifiers are used
to add more control over the way variables are stored and accessed 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

Data Type Format Specifiers

int %d
float %f
char %c
double %lf

You might also like