Unit: 2:1:: Introduction To Computer and C Programming
Unit: 2:1:: Introduction To Computer and C Programming
Unit: 2:1:: Introduction To Computer and C Programming
When you learn a specific programming language, it’s a lot like learning any other language.
You need to understand how the language works and follow a certain set of rules, much like
grammar in spoken languages.
By, Mritunjay Kr. Ranjan. & Amit Gadekar (SOET-Sandip University, Nashik.)
ICCPL (Introduction to Computing and C- Programming Language) : Notes
• High level (Java, Haskell, Prolog, FORTRAN)
▪ Machine language
▪ Assembly language
Machine Language.
▪ This consists a set of instructions which are executed directly by the CPU where each
instruction performs a small specific task.
▪ These instructions are specific to a machine and therefore will differ from machine to
machine.
Assembly Language.
▪ This is a better version of
machine language whereby instead of using raw binary sequences to write
instructions, we use mnemonics.
▪ Assembly code has the best of both worlds, it is fast and can be read and understood
and also communicates with the hardware directly.
Pros /benefits/advantage of low level languages.
• They are in direct communication with hardware.
By, Mritunjay Kr. Ranjan. & Amit Gadekar (SOET-Sandip University, Nashik.)
ICCPL (Introduction to Computing and C- Programming Language) : Notes
• Programs developed with low level programming languages are fast and memory
efficient.
• Cons/Loss/ disadvantage of low-level languages:
• Difficult to write, read, debug or maintain.
• They are processor architecture specific and therefore a programmer needs to have
a specific knowledge of particular processor architecture in order to write code for it.
By, Mritunjay Kr. Ranjan. & Amit Gadekar (SOET-Sandip University, Nashik.)
ICCPL (Introduction to Computing and C- Programming Language) : Notes
• Using compilers, we can compile source code written in a high-level language into
machine code that is specific to any processor architecture and therefore is
machine independent.
• All this comes at a cost of being slow compared to low level languages since
translation of this high level language to low level language takes additional time.
1. Interpreted languages
2. Compiled languages
Interpreted Languages:
▪ Programs developed by interpreted
languages are executed by an interpreter
line by line where instructions are directly
translated into a sequence of subroutines
and then into machine code.
▪ They are very slow compared to compiled languages however there are methods to
increase their speed such as, Just-In-Time compilation. They include, Python,
Javascript, PHP.
Compiled Languages:
High level languages can be also categorised by their programming paradigms: such as: logical,
procedural, functional, object oriented, event driven, flow driven etc.
▪ These are also known as imperative languages. They are based on the concept
of procedure calls whereby statements are grouped into procedures better
known as routines and subroutines.
▪ Generally, they are a list of instructions which instruct the computer on what
steps to take until a task is accomplished.
Examples include COBOL, FORTRAN.
By, Mritunjay Kr. Ranjan. & Amit Gadekar (SOET-Sandip University, Nashik.)
ICCPL (Introduction to Computing and C- Programming Language) : Notes
Logic Languages.
▪ Logic programming paradigm has its roots in mathematical logic whereby
program statements are used to express facts and rules about problems within a
system.
▪ These rules are written as clauses and follow a declarative approach rather than
an imperative one.
Functional Languages.
▪ The functional programming paradigm views all subprograms as functions, code
written in this paradigm will consist of pure functions which take an argument list
as input and produce output.
▪ States can be fields, attributes and behaviors define what we do with the
state of the object which is defined by a method.
Examples include, Java, Python, C++.
Note: that the languages listed in each paradigm are not limited to the said paradigm for
example javascript can also be written in an object oriented paradigm even though we
grouped it as functional.
• They are machine independent e.g, a java program can be executed in any processor
architecture.
• They are less prone to errors, for example, syntax highlighting and compilers help to
spot errors.
By, Mritunjay Kr. Ranjan. & Amit Gadekar (SOET-Sandip University, Nashik.)
ICCPL (Introduction to Computing and C- Programming Language) : Notes
• They provide a higher level of abstraction from machine languages for example we
don't handle memory allocation or deallocation.
Cons/Loss/disadvantage of high-level languages.
• They are not memory efficient compared to low level languages.
• They are slower - they go through a lot of steps before they can actually be executed
on a machine.
Unit: 2 :2:
C -Programming Basic.
History of C – Programming:
The Beginning:
• The C programming language came out of Bell Labs in the early 1970s.
• According to the Bell Labs paper The Development of the C Language by Dennis
Ritchie,
• “The C programming language was devised in the early 1970s as a system
implementation language for the nascent Unix operating system. Derived from the
typeless language BCPL, it evolved a type structure; created on a tiny machine as a tool
to improve a major programming environment.”
• Originally, Ken Thompson, a Bell Labs employee, desired to make a programming
language for the new Unix platform.
• Thompson modified the BCPL system language and created B. However, not many
utilities were ever written in B due to its slow nature and inability to take advantage
of PDP-11 features in the operating system.
• This led to Ritchie improving on B, and thus creating C.
By, Mritunjay Kr. Ranjan. & Amit Gadekar (SOET-Sandip University, Nashik.)
ICCPL (Introduction to Computing and C- Programming Language) : Notes
• In 1978, Brian Kernighan and Dennis Ritchie published The C Programming Language,
which would serve as the language reference until a formal standard was adopted.
• Five years later, the American National Standard Institute (ANSI) formed the
committee, X3J11, to establish the formal standard of C.
• The C standard was ratified as ANSI X3.159-1989 “Programming Language C”.
• This was the first formal standard of C. Currently, we are on the fourth standard of C,
known as C18 as it was published in June of 2018.
Data Types in C
• A data type specifies the type of
data that a variable can store
such as integer, floating,
character, etc.
• here are the following data
types in C language.
The basic data types. Its size is given according to 32-bit architecture.
By, Mritunjay Kr. Ranjan. & Amit Gadekar (SOET-Sandip University, Nashik.)
ICCPL (Introduction to Computing and C- Programming Language) : Notes
We can also provide values while declaring the variables as given below:
int a=10, b=20; //declaring 2 variable of integer type
float f=20.8;
By, Mritunjay Kr. Ranjan. & Amit Gadekar (SOET-Sandip University, Nashik.)
ICCPL (Introduction to Computing and C- Programming Language) : Notes
char c='A';
Types of Variables in C:
There are basically 4- types of variables in c:
• Local variable
• Global variable
• Static variable
• Automatic variable
Local Variable:
• A variable that is declared inside the function or block is called a local variable.
• It must be declared at the start of the block.
Example:
void function1()
{
int x=10; //local variable
}
Note: You must have to initialize the local variable before it is used.
Global Variable:
• A variable that is declared outside the function or block is called a global variable.
• Any function can change the value of the global variable. It is available to all the
functions.
By, Mritunjay Kr. Ranjan. & Amit Gadekar (SOET-Sandip University, Nashik.)
ICCPL (Introduction to Computing and C- Programming Language) : Notes
Example:
int value=20; //global variable
void function1( )
{
int x=10;//local variable
}
Static Variable:
• A variable that is declared with the static keyword is called static variable.
• It retains its value between multiple function calls.
Example:
void function1()
Note : If you call this function many times, the local variable will print the same value for
each function call, e.g, 11,11,11 and so on. But the static variable will print the incremented
value in each function call, e.g. 11, 12, 13 and so on.
By, Mritunjay Kr. Ranjan. & Amit Gadekar (SOET-Sandip University, Nashik.)
ICCPL (Introduction to Computing and C- Programming Language) : Notes
External Variable: Note: Not For Beginners:
• We can share a variable in multiple C source files by using an external
variable. To declare an external variable, you need to use extern
keyword.
myfile.h
include "myfile.h"
#include <stdio.h>
void printValue()
{
printf("Global variable: %d", global_variable);
}
Constants in C:
• A constant is a value or variable that can't be changed in the program, for example:
10, 20, 'a', 3.4, "c programming" etc.
• There are different types of constants in C programming.
List of Constants in C
Constant Example
1. const keyword
2. #define preprocessor
1) C const keyword
#include<stdio.h>
#include<conio.h>
void main()
{
const float PI=3.14;
clrscr();
printf("The value of PI is: %f",PI);
getch();
}
Output:
2) C #define preprocessor
Syntax:
By, Mritunjay Kr. Ranjan. & Amit Gadekar (SOET-Sandip University, Nashik.)
ICCPL (Introduction to Computing and C- Programming Language) : Notes
}
Keywords in C:
• Keywords are the reserved words which are used for specific purpose. It cannot be
used as variable, constants.
• Keywords in C: are 32:
Comments:
• It is used to apply the comment on a single line. To apply single line comment // is
used.
e.g.
printf(“hello Rohit"); // It will print the string hello Rohit
Multi Line comment:
• It is used to apply the comment on multiple lines. To apply multi line use
/*comment */
e.g.
printf("hello Rohit"); /*It will print the on out-window
hello Rohit*/
Operators in C-language:
• An operator is a symbol that tells the compiler to perform specific mathematical or
logical functions.
C language has is 6 (Six) Types of operators.
By, Mritunjay Kr. Ranjan. & Amit Gadekar (SOET-Sandip University, Nashik.)
ICCPL (Introduction to Computing and C- Programming Language) : Notes
1. Arithmetic Operators
2. Relational Operators
3. Logical Operators
4. Bitwise Operators
5. Assignment Operators
6. Misc Operators
Arithmetic Operators:
• The following table shows all the arithmetic operators supported by the C language.
Assume variable A holds 10 and variable B holds 20 then −
Examples:
Relational Operators
• The following table shows all the relational operators supported by C. Assume
variable A holds 10 and variable B holds 20 then −
Examples
By, Mritunjay Kr. Ranjan. & Amit Gadekar (SOET-Sandip University, Nashik.)
ICCPL (Introduction to Computing and C- Programming Language) : Notes
Operator Description Example
== Checks if the values of two operands are equal or not. If (A == B) is not true.
yes, then the condition becomes true.
> Checks if the value of left operand is greater than the (A > B) is not true.
value of right operand. If yes, then the condition
becomes true.
< Checks if the value of left operand is less than the value (A < B) is true.
of right operand. If yes, then the condition becomes
true.
>= Checks if the value of left operand is greater than or (A >= B) is not true.
equal to the value of right operand. If yes, then the
condition becomes true.
<= Checks if the value of left operand is less than or equal (A <= B) is true.
to the value of right operand. If yes, then the condition
becomes true.
Logical Operators
• Following table shows all the logical operators supported by C language. Assume
variable A holds 1 and variable B holds 0, then −
Examples:
By, Mritunjay Kr. Ranjan. & Amit Gadekar (SOET-Sandip University, Nashik.)
ICCPL (Introduction to Computing and C- Programming Language) : Notes
|| Called Logical OR Operator. If any of the two (A || B) is true.
operands is non-zero, then the condition
becomes true.
• Bitwise operator works on bits and perform bit-by-bit operation. The truth tables for
&, |, and ^ is as follows −
0 0 0 0 0
0 1 0 1 1
1 1 1 1 0
1 0 0 1 1
By, Mritunjay Kr. Ranjan. & Amit Gadekar (SOET-Sandip University, Nashik.)
ICCPL (Introduction to Computing and C- Programming Language) : Notes
Operator Description Example
& Binary AND Operator copies a bit to the (A & B) = 12, i.e., 0000 1100
result if it exists in both operands.
^ Binary XOR Operator copies the bit if it is (A ^ B) = 49, i.e., 0011 0001
set in one operand but not both.
Assignment Operators:
• The following table lists the assignment operators supported by the C language −
Examples
By, Mritunjay Kr. Ranjan. & Amit Gadekar (SOET-Sandip University, Nashik.)
ICCPL (Introduction to Computing and C- Programming Language) : Notes
-= Subtract AND assignment operator. It
subtracts the right operand from the left
C -= A is equivalent to C = C - A
operand and assigns the result to the left
operand.
• Besides the operators discussed above, there are a few other important operators
including sizeof and ? : supported by the C Language.
Examples
By, Mritunjay Kr. Ranjan. & Amit Gadekar (SOET-Sandip University, Nashik.)
ICCPL (Introduction to Computing and C- Programming Language) : Notes
Operator Description Example
& Returns the address of a &a; returns the actual address of the variable.
variable.
Precedence of Operators in - C
❖ Here, operators with the highest precedence appear at the top of the table,
❖ those with the lowest appear at the bottom.
❖ Within an expression, higher precedence operators will be evaluated first.
Examples:
By, Mritunjay Kr. Ranjan. & Amit Gadekar (SOET-Sandip University, Nashik.)
ICCPL (Introduction to Computing and C- Programming Language) : Notes
Additive +- Left to right
Type Conversion C
By, Mritunjay Kr. Ranjan. & Amit Gadekar (SOET-Sandip University, Nashik.)
ICCPL (Introduction to Computing and C- Programming Language) : Notes
Implicit Type Conversion
• Also known as ‘automatic type conversion’.
• Done by the compiler on its own, without any
external trigger from the user.
• Generally takes place when in an expression more
than one data type is present.
• In such condition type conversion (type promotion)
takes place to avoid loss of data.
• All the data types of the variables are upgraded to
the data type of the variable with largest data type.
Example:
bool -> char -> short int -> int ->
unsigned int -> long -> unsigned ->
long long -> float -> double -> long double
Note: It is possible for implicit conversions to lose information, signs can be lost (when
signed is implicitly converted to unsigned), and overflow can occur (when long long is
implicitly converted to float).
By, Mritunjay Kr. Ranjan. & Amit Gadekar (SOET-Sandip University, Nashik.)
ICCPL (Introduction to Computing and C- Programming Language) : Notes
double x = 1.2;
getch();
}
Output:
sum = 2
Advantages of Type Conversion
• This is done to take advantage of certain features of type hierarchies or type
representations.
• It helps us to compute expressions containing variables of different data types.
printf and scanf function in C Programming
printf () and scanf () function / Output and input function :
• The printf () and scanf () functions are the most popular input and output functions in
C language.
• Both functions are inbuilt library functions which is part of stdio.h header file. Before
using both functions in our program, we must include stdio.h header file as shown
below.
#include <stdio.h>
Printf () Function in C
• The printf function of stdio C Library writes a formatted string to stdout.
• If format contains format specifiers (subsequences beginning with %),
• the additional arguments following format are inserted after formatting in the
resulting string by replacing their respective format specifiers.
Syntax:
Printf(“ message + argument you want to print + control specifier if needed followed by variable“, v1,v2,… vn);
Example:
printf(“ Rohit “); // out put: Rohit
By, Mritunjay Kr. Ranjan. & Amit Gadekar (SOET-Sandip University, Nashik.)
ICCPL (Introduction to Computing and C- Programming Language) : Notes
a=4;
printf(“ the value of a = %d “,a); // out put: the value of a= 4
scanf () Function in C
• The scanf function of stdio C Library function reads formatted data from
stdin and stores them in the variables pointed by the additional
arguments.
Syntax:
scanf(“ contoll specifier”, address of operator with required variable);
example:
scanf(“%d”,&a); // it will get the input from user
Pre-processors – Directive in C
• As the name suggests Pre-processors are programs that process our source code
before compilation.
• There are a number of steps involved between writing a program and executing a
program in C.
There are 4 main types of preprocessor directives:
1. Macros
2. File Inclusion
3. Conditional Compilation
4. Other directives
By, Mritunjay Kr. Ranjan. & Amit Gadekar (SOET-Sandip University, Nashik.)
ICCPL (Introduction to Computing and C- Programming Language) : Notes
• Header File or Standard files: These files contains definition of pre-defined
functions like printf(), scanf() etc.
• These files must be included for working with these functions.
• Different function are declared in different header files.
For example standard I/O functions are in ‘stdio.h’ file whereas functions which perform
maths related operational function are in ‘math.h’ file.
Syntax:
#include< file_name >
where file_name is the name of file to be included. The ‘<‘ and ‘>’ brackets tells the compiler
to look for the file in standard directory.
Example:
#include<stdio.h>
#include<math.h>
• user defined files: When a program becomes very large, it is good practice to divide it
into smaller files and include whenever needed. These types of files are user defined
files. These files can be included as:
#include"filename"
By, Mritunjay Kr. Ranjan. & Amit Gadekar (SOET-Sandip University, Nashik.)
ICCPL (Introduction to Computing and C- Programming Language) : Notes
statementN;
#endif
If the macro with name as ‘macroname‘ is defined then the block of statements will execute
normally but if it is not defined, the compiler will simply skip this block of statements.
Out-put
By, Mritunjay Kr. Ranjan. & Amit Gadekar (SOET-Sandip University, Nashik.)
ICCPL (Introduction to Computing and C- Programming Language) : Notes
1. Write a program in C to get the addition of any two integer type number with
out-put.
Program:
Out -Put
By, Mritunjay Kr. Ranjan. & Amit Gadekar (SOET-Sandip University, Nashik.)