Unit 1 Introduction To Programming
Unit 1 Introduction To Programming
Unit 1
Programming for Problem Solving
07-04-2024 2
Types of Programming Language
• There are three types of programming language:
– Machine language (Low-level language)
– Assembly language (Low-level language)
– High-level language
• Low-level languages are closer to the language used by a computer, while high-
level languages are closer to human languages
07-04-2024 3
Machine Language
• Machine language is a collection of binary digits or bits that the computer
reads and interprets.
07-04-2024 4
Assembly Language
• Symbolic representation(mnemonics) of machine code.
• Example:
– Assembly language :
mov a1, #061h (Move the hexadecimal value 61 (97 decimal) into the
processor register named "a1".)
07-04-2024 5
High Level Language
• User friendly, Similar to natural languages.
• Platform independent.
07-04-2024 6
Assembler
• The assembler is used to translate the program written in assembly language
into machine code.
• Basic Function:
• Translating mnemonic operation codes to their machine language equivalents
07-04-2024 7
Compiler
• A high level source program must be translated into a form machine
can understand. This done by software called the compiler.
• During the process of translation, the compiler reads the source programs
statement-wise and checks for syntax errors.
• In case of any error, the computer generates message about the error.
• Ex: C, C++, Java etc.
07-04-2024 8
Interpreter
• Like compiler, Interpreter is also a translator which translates high level to
machine level language.
• Each line is checked for syntax error and then converted to the equivalent
machine code.
07-04-2024 9
Difference between Compiler and Interpreter
07-04-2024 10
Linker
• A linker is special program that combines the object files, generated by
compiler/assembler, and other pieces of codes to make an executable file
have .exe extension.
• In the object file, linker searches and append all libraries needed for
execution of file.
• It also merges two or more separate object programs and establishes link
among them.
07-04-2024 11
Function of Linker
07-04-2024 12
Loader
• The loader is special program that takes input of object code from linker,
loads it to main memory, and prepares this code for execution by computer.
07-04-2024 13
Function of Loader
07-04-2024 14
Introduction to C Programming Language
• C is a procedural programming language as well as a general-purpose programming
language.
• Created in 1972 to write operating systems (Unix in particular)
• By Dennis Ritchie
• At AT & T's Bell Laboratories
• C language was created for a specific purpose i.e. designing the UNIX operating system
(which is currently base of many UNIX based OS).
• C is very powerful; it has been used to develop operating systems, databases, applications,
etc.
• C Language is a middle-level programming language that has features of both low-level
programming language and high-level programming languages.
15
07-04-2024 C. V. Raman Global University, Odisha
Why is C Programming Language so popular
• C language is a good language to introduce yourself to the programming world, because it
is simple, easy to learn, and covers all the basic concepts of programming.
• The scope of C language is limited, so it is good for beginners to start programming.
• Initially, C language used for writing system-level programs, like designing Operating
Systems, OS kernels, etc.
• C language can be used to develop simple software, like Text Editors, Compilers, Network
Drivers, and many traditional PoS (Point of Sale) software like Restaurant Billing systems,
etc.
• Some popular applications build using C language are Adobe Photoshop, MySQL
Database, Chromium browser, etc.
• If you are in college, you can clear college placement interviews by learning C language,
and get a job in good service-based companies like TCS, Infosys, IBM, Accenture, etc.
16
07-04-2024
Features of C Language
07-04-2024 17
My first C Program
/* This is my first program*/
#include <stdio.h>
printf(“Hello World\n”);
return0;
}
07-04-2024 18
Documentation Section
• The documentation section is the part of the program where the programmer gives the
details associated with the program.
• Provides name of program, the author, algorithms, methods used and other details.
• Ex:-
07-04-2024 20
Link Section (Preprocessor directives)
• This part of the code is used to declare all the header files that will be used
in the program.
• This leads to the compiler being told to link the header files to the system
libraries.
07-04-2024 21
Definition Section (Preprocessor directives)
• Preprocess directives is executed before the actual compilation is done.
07-04-2024 22
Global Declaration Section
• The variables which are used in more than one functions or blocks are called
global variables.
07-04-2024 23
main() Function Section
• Every C program starts with a main() function . Every program must have exactly one main function. C permits different
form of main statements.eg. main(), int main(),void main(),main(void),void main(void), int main(void).
• Declaration part declares all the variables used in the execution part. The initialization has been done in this part.
• Variable s declared inside main() are called local variable and they are used only in the block in which they are declared.
• int n1;
• int n2=5;
• n1= n1 +1;
• n2=n1*5
07-04-2024 24
Subprogram Section
ram()
07-04-2024 25
Comments
• Comments in C language are used to provide information about lines of code. It is
widely used for documenting code. There are 2 types of comments in the C
language.
• Single Line Comments
07-04-2024 26
Preprocessor in C
• One of the unique feature of C language is preprocessor.
• Preprocessors are programs that process our source code before compilation.
• The preprocessor works on the source code and creates “expanded source code”.
07-04-2024 27
Preprocessor in C
07-04-2024 28
File inclusion
• This type of preprocessor directive tells the compiler to include a file in the source code
program.
• #include<stdio.h>
• #include<conio.h>
• These files contains definition of pre-defined functions like printf(), scanf(), getch(),
clrscr() etc.
07-04-2024 29
printf() and scanf() in C
• The printf() and scanf() functions are used for input and output in C language.
• Both functions are inbuilt library functions, defined in stdio.h (header file).
• printf() function
• The printf() function is used for output. It prints the given statement to the console.
• Eg printf(“………………..”); or printf(“%d”,number);
• scanf() function
• The scanf() function is used for input. It reads the input data from the console.
• Eg scanf (“……”,&variable);
07-04-2024 30
C Format Specifier
• The Format specifier is a string used in the formatted input and output functions. The
format string determines the format of the input and output. The format string always
starts with a '%' character.
• The commonly used format specifiers in printf() function are:
Specifier Description
07-04-2024 31
Example of Program
#include<stdio.h>
int main()
{
int a=10, b=20;
int sum;
sum=a+b;
printf("Sum: %d", sum);
return 0;
}
07-04-2024 32
Macro expansion
• Macros are a piece of code in a program which is given some name.
• Whenever this name is encountered by the compiler the compiler replaces
the name with the actual piece of code.
• The ‘#define’ directive is used to define a macro.
• Ex #include <stdio.h>
#define height 100
#define number 3.14
int main()
{
printf("value of height : %d \n", height );
printf("value of number : %f \n", number );
return 0;
07-04-2024 33
}
Escape Sequence in C
• C have escape sequences as a standard feature. They enable the inclusion of special
characters and control patterns in strings and character constants that are difficult to
represent or write directly. Whenever this name is encountered by the compiler the
compiler replaces the name with the actual piece of code.
• Ex \b(Back space),\n(new line),\t(horizontal tab),\\backslash,\0(null character)
• The character set in C are grouped into following catagories.
1. Letters: upper case and lower case(Alphabets from A to Z and a to z)
2. Digits(Numbers, from 0 to 9)
3. Special characters(comma , semicolon etc)
4. Delimeters: Language pattern of C uses special kind of symbols which are called delimeters.
E.g. ;(Terminal Statement),()(Useful in function and expression),[](used for array declaration),{ }(Scope of
Statement),:(Used for labels),#(Preprocessor Directive),Comma(variable separator)
07-04-2024 34
Escape Sequence in C
07-04-2024 35
Data types in C
• Data type specifies which type of data that we are storing in a variable.
• Data types in c refer to an extensive system used for declaring variables or
functions of different types.
• The type of a variable determines how much space it occupies in storage and
how the bit pattern stored is interpreted .
• The data-type in a programming language is the collection of data with
values having fixed meaning as well as characteristics.
• A data type is used to
• Identify the type of a variable when the variable is declared
• Identify the type of the return value of a function
• Identify the type of a parameter expected by a function
07-04-2024 36
Classification of Data Types
07-04-2024 37
Integer data types
• Integer is nothing but a whole number.
• Keyword int is used for declaring the variable with integer type.
• For example-- int var1;
• Ex: int age;
• Here, age is a variable of an integer data type which can be used to store integer values.
• 'C' provides three different classes of integers.
• a) int
• b) short int
• c) long int
07-04-2024 38
Standard Data types with their storage
sizes and value ranges
07-04-2024 39
C. V. Raman Global University, Odisha
Floating point data type
• All numeric data type items with fractional part belong to float
07-04-2024 40
Standard floating-point types with
their storage sizes and value ranges
07-04-2024 41
Character data type
• Character data types are used to store a single character value enclosed in
single quotes.
• In C, char type takes 1 byte of memory and it supports ASCII characters.
• We can store only one character using character data type.
• “char” keyword is used to refer character data type.
• Example: char b = 'G‘, char group = ‘A’
• A signed char is same as an ordinary char and has a range from -128 to +127
• An unsigned char has a range from 0 to 255
07-04-2024 42
Data Types Range and Sizes
07-04-2024 43
C. V. Raman Global University, Odisha
Variables
• Variables are memory locations(storage area) in the C programming language.
• A variable is an identifier which is used to store some value.
• Variables can change during the execution of a program and update the value stored
inside it.
• It is a way to represent memory location through symbol so that it can be easily
identified.
• Variable Declaration: data_type VariableName
• Example: int num; float area; char name
• Variable Definition: assigning a value to the declared variable
• Example: int num = 5; float area=10.5; char name=‘a’;
• Variable Initialization:
• Variable declaration is the act of informing the compiler about the existence and data type of a
variable. It informs the compiler that a variable with a specific name and data type will be used in
the program, but that memory for the variable still needs to be allocated. 44
Rules to declare the variables
• They must begin with letter or underscore(_)
• First character may be followed by letters or digits.
• Both lowercase and uppercase letters are distinct.
• Example: Total and total are not same
• It should not be a keyword
• White space, Dollar sign are not allowed.
• Example: John, delhi, First_tag, int_type ---- valid
• Price$, char, group one, 123, (area) ------- Invalid
07-04-2024 45
Scope of the variables
• The scope of variable determines the area of the program where that variable is valid i.e the part of the
program that has access to that variable
• The variables may also be categorized, depending on the place of their declaration as :
a)Global variable b)Local variable
a)Global variable
• They are declared outside the function .
• Its scope is through out the program .
• Its life span is through out the entire program.
• They are automatically initialized to zero.
b)Local variable
• The declaration is placed after the opening curly braces of any function including main() and before any
function statement.
• Scope of the variable is limited to the function in which it is declared.
• Life07-04-2024
span of the local variable is within the block in which it is declared. 46
Example of Scope of the variables
•
07-04-2024 47
Thank You
78