Unit: 2:1:: Introduction To Computer and C Programming

Download as pdf or txt
Download as pdf or txt
You are on page 1of 26

ICCPL (Introduction to Computing and C- Programming Language) : Notes

Introduction to Computer and C Programming.


Unit: 2 :1:
Introduction to Programming Languages
Introduction to Programming Languages:
• Programming languages allow us to communicate with computers through
computer code.
• Each programming language is simply a means to tell a computer what it should
do.
• It’s a language your computer understands and knows how to process.

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.

The need of Programming Language:

• language is medium for communication. If two parties like to communicate or


exchange the thoughts they must know a language.
• Language should be understandable by both the Parties.
▪ For example, A wants to talk to B. A only knows English. B only knows
Spanish. A and B can’t communicate till both A and B know a common
language.
• Computer is a machine and we are human beings.
• Human wants to talk to Computer. Both must know a common language.
• These languages which are understandable by both Human and Computer are called
Programming languages.
• C, C++, JAVA, FORTRAN, COBOL, Python etc. are the Programming languages.
• A person interacts with Computer using instructions. A set of instruction is called
program.
• The art of writing or developing a program is called programming.
• So programming language is medium using which we can communicate with the
Computer.

Types of programming languages:


There are three major categories of of computer
programming languages namely:
• low level (Assembly, Machine code).
• Mid-level (C, Assembly).

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)

Low level languages:


▪ Low level languages (LLL) are languages which are machine/processor dependent e.g
machine code(binary) and assembly code.
▪ They are mostly used to write programs which will work on a specific processor
architecture and computer hardware.
▪ Writing low level code is a daunting task and programs written are difficult to read,
understand or debug,
▪ furthermore, they cannot be executed on any other processor however they are very
fast and memory efficient
▪ In most cases they are used for coding operating systems, compilers, databases, malware,
drivers or any application which requires direct hardware access.

The Low-Level Languages is divided into two categories:

▪ 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.

▪ These mnemonics are then translated to machine language specific to a processor


architecture by an assembler.

▪ 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.

• No need for compilers or interpreters since code is ready to be executed by a


processor.

• Provides better use of memory and processing power.

• They provide direct manipulation of computer registers and storage.

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.

• Coding in low level languages exposes one to a lot of errors.

• Programming in a low-level language results in poor programming productivity.

• Low level programs are not portable.

Mid level languages.


▪ MLL (Mid-level Language) are programming languages which exhibit features of both
high level and low-level programming languages.
▪ The C programming language is a good example of a mid-level programming
language since it has features of both.
▪ We can also place assembly language in this category since it is also readable and can
be coded and maintained relatively easily by an expert in a specific processor
architecture.

High level languages:


• The HL- languages are machine independent meaning a program written in one
system can be executed on any other system.

• These languages are human readable,


• some are close to written english e.g python and hence easily understandable so
much so that code written by a different programmer can be changed to perform
a different task,
• they are also easier to debug and maintain.

• They provide a high level of abstraction from machine languages.


• The focus with high level languages is to be able to solve complex arithmetic
operations, increase programmer productivity and optimized programs.
• High level programs require compilers and interpreters which translate their
source code into low level machine executable code.
▪ For example gcc compiler for C or python interpreter for python

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.

High level languages are divided into two categories:

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:

▪ These are languages which are directly converted


into machine code which can be executed by a
processor and thus they are faster and more
efficient compared to interpreted languages.

▪ When we compile a C/C++ program we expect an executable which we then execute


with the appropriate inputs. Examples include C, C++, Rust, Haskell.

High level languages can be also categorised by their programming paradigms: such as: logical,
procedural, functional, object oriented, event driven, flow driven etc.

The major paradigms of Programming are:


Procedural Languages.

▪ 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.

▪ Examples include Prolog.

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.

▪ Examples include, Javascript, Haskell, Scala.

Object Oriented Languages.


▪ The OOL Programming paradigm represent real world entities as objects
which are instances of classes and therefore each object will encapsulate
a state and particular behavior.

▪ 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.

Pros/benefits/advantage of high level languages:


• Easy to read, write, debug and maintain.

• They are portable.

• 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.

• Using high level programming languages results in better programmer productivity,


that is, coming up with a program in python is much quicker and productive than
coming up with the same in assembly code.

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.

• They need to be compiled or interpreted and this takes additional time.

• They cannot communicate with the hardware directly.

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.

Early Implementations and Language Standard of C:


• The development of C was to become the basis for Unix.
• According to the Bell Labs paper, “By early 1973, the essentials of modern C were
complete. The language and compiler were strong enough to permit us to rewrite the
Unix kernel for the PDP-11 in C during the summer of the year.”
• This now meant that C was becoming a strong language that could, and would be,
implemented across many systems. By the middle of the 1970s, the C-based Unix was
used in many projects within the Bell System as well as
• “a small group of research-oriented industrial, academic, and government
organizations outside [Bell Labs]".

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.

Types Data Types

Basic Data Type int, char, float, double

Derived Data Type array, pointer, structure, union

Enumeration Data Type enum

Void Data Type void

The basic data types. Its size is given according to 32-bit architecture.

Data Types Memory Range


Size

char 1 byte −128 to 127 %c

signed char 1 byte −128 to 127 %c

unsigned char 1 byte 0 to 255 %c

short 2 byte −32,768 to 32,767 %d

signed short 2 byte −32,768 to 32,767 %d

unsigned short 2 byte 0 to 65,535 %hu

int 2 byte −32,768 to 32,767 %d

By, Mritunjay Kr. Ranjan. & Amit Gadekar (SOET-Sandip University, Nashik.)
ICCPL (Introduction to Computing and C- Programming Language) : Notes

signed int 2 byte −32,768 to 32,767 %d

unsigned int 2 byte 0 to 65,535 %u

short int 2 byte −32,768 to 32,767 %hd

signed short int 2 byte −32,768 to 32,767 %d

unsigned short int 2 byte 0 to 65,535 %hu

long int 4 byte -2,147,483,648 to 2,147,483,647 %d

signed long int 4 byte -2,147,483,648 to 2,147,483,647 %d

unsigned long int 4 byte 0 to 4,294,967,295 %u

float 4 byte 3.4E-38 to 3.4E+38 %f

double 8 byte 1.7E-308 to 1.7E+308 %lf

long double 10 byte 3.4E-4932 to 1.1E+4932. %Lf

Note: Data types given in Red, are not for


beginners:
Variables in C:
• A variable is a name of the memory location. It is used to store data. Its value can be
changed, and it can be reused many times.
• It is a way to represent memory location through symbol so that it can be easily
identified.

The syntax to declare a variable:

type variable list;

example: Here, a, b, c are variables.


int a; The int, float, char are the
float b; . data types.
char c;

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';

Rules for defining variables:

• A variable can have alphabets, digits, and underscore.


• A variable name can start with the alphabet, and underscore only. It can't start with a
digit.
• No whitespace is allowed within the variable name.
• A variable name must not be any reserved word or keyword, e.g. int, float, etc.

Example: Valid variable names: Invalid variable names:


1. int a; 1. int 2;
2. int _ab; 2. int a b;
3. int a30; 3. int long;

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.

Note: It must be declared at the start of the block.

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()

int x=10;//local variable

static int y=10;//static variable


x=x+1;
y=y+1;
printf("%d,%d",x,y);

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.

Automatic Variable: Note: Not For Beginners:


• All variables in C that are declared inside the block, are automatic variables by default.
• We can explicitly declare an automatic variable using auto keyword.
Example: void main ( )

int x=10; //local variable (also automatic)

auto int y=20; //automatic variable


}

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

extern int x=10;//external variable (also global)


program1.c

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

Decimal Constant 10, 20, 450 etc.

Real or Floating-point Constant 10.3, 20.2, 450.6 etc.

Octal Constant 021, 033, 046 etc.

Hexadecimal Constant 0x2a, 0x7b, 0xaa etc.

Character Constant 'a', 'b', 'x' etc.

String Constant "c", "c program", etc.

Note: Constant given in Red, are not for


beginners:
By, Mritunjay Kr. Ranjan. & Amit Gadekar (SOET-Sandip University, Nashik.)
ICCPL (Introduction to Computing and C- Programming Language) : Notes
There are two ways to define constant in C programming.

1. const keyword
2. #define preprocessor

1) C const keyword

const float PI=3.14;

Now, the value of PI variable can't be changed.

#include<stdio.h>
#include<conio.h>
void main()
{
const float PI=3.14;
clrscr();
printf("The value of PI is: %f",PI);
getch();
}

Output:

The value of PI is: 3.140000

2) C #define preprocessor

• The #define preprocessor is also used to define constant.


• The #define preprocessor directive is used to define constant or micro substitution. It
can use any basic data type.

Syntax:

#define token value

Example of #define to define a constant.

#include <stdio.h> // example with header file


#define PI 3.14 // example with constant generally in beginning stage it is not used.
main()
{
printf("%f",PI);

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:

auto break case char const continue default do


double else enum extern float for goto if
int long register return short signed sizeof static
struct switch typedef union unsigned void volatile while

Comments:

• It is useful to place a comment in place to indicate what you are doing.


• It is added into the program for making the program easier to understand and these
are not compiled by the compiler or interpreter.
• It is useful if you want someone else to be able to ever read your code.

There are two ways to use comment in C :

1. Single Line comment


2. Multi Line comment

Single Line comment:

• 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:

Operator Description Example

+ Adds two operands. A + B = 30

− Subtracts second operand from the first. A − B = -10

* Multiplies both operands. A * B = 200

/ Divides numerator by de-numerator. B/A=2

% Modulus Operator and remainder of after an integer division. B%A=0

++ Increment operator increases the integer value by one. A++ = 11

-- Decrement operator decreases the integer value by one. A-- = 9

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 values of two operands are equal or not. If (A != B) is true.


the values are not equal, 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:

Operator Description Example

&& Called Logical AND operator. If both the (A && B) is false.


operands are non-zero, then the condition
becomes true.

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.

! Called Logical NOT Operator. It is used to reverse !(A && B) is true.


the logical state of its operand. If a condition is
true, then Logical NOT operator will make it
false.

Bitwise Operator: Note : Not for beginners.

• Bitwise operator works on bits and perform bit-by-bit operation. The truth tables for
&, |, and ^ is as follows −

p q p&q p|q p^q

0 0 0 0 0

0 1 0 1 1

1 1 1 1 0

1 0 0 1 1

Assume A = 60 and B = 13 in binary format, they will be as follows −


A = 0011 1100
B = 0000 1101
-----------------
A&B = 0000 1100
A|B = 0011 1101
A^B = 0011 0001
~A = 1100 0011
The following table lists the bitwise operators supported by C. Assume variable 'A' holds 60
and variable 'B' holds 13, then −
Examples

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 OR Operator copies a bit if it (A | B) = 61, i.e., 0011 1101


exists in either operand.

^ Binary XOR Operator copies the bit if it is (A ^ B) = 49, i.e., 0011 0001
set in one operand but not both.

~ Binary One's Complement Operator is (~A ) = ~(60), i.e,. -0111101


unary and has the effect of 'flipping'
bits.

<< Binary Left Shift Operator. The left


operands value is moved left by the
A << 2 = 240 i.e., 1111 0000
number of bits specified by the right
operand.

>> Binary Right Shift Operator. The left


operands value is moved right by the
A >> 2 = 15 i.e., 0000 1111
number of bits specified by the right
operand.

Assignment Operators:

• The following table lists the assignment operators supported by the C language −
Examples

Operator Description Example

= Simple assignment operator. Assigns values C = A + B will assign the value of A + B


from right side operands to left side operand to C

+= Add AND assignment operator. It adds the


right operand to the left operand and assign C += A is equivalent to C = C + A
the result to the left operand.

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.

*= Multiply AND assignment operator. It


multiplies the right operand with the left
C *= A is equivalent to C = C * A
operand and assigns the result to the left
operand.

/= Divide AND assignment operator. It divides


the left operand with the right operand and C /= A is equivalent to C = C / A
assigns the result to the left operand.

%= Modulus AND assignment operator. It takes


modulus using two operands and assigns the C %= A is equivalent to C = C % A
result to the left operand.

<<= Left shift AND assignment operator. C <<= 2 is same as C = C << 2

>>= Right shift AND assignment operator. C >>= 2 is same as C = C >> 2

&= Bitwise AND assignment operator. C &= 2 is same as C = C & 2

^= Bitwise exclusive OR and assignment C ^= 2 is same as C = C ^ 2


operator.

|= Bitwise inclusive OR and assignment C |= 2 is same as C = C | 2


operator.

Note: The assignment operator written in Red Text are not


for beginners
Misc Operators ↦ sizeof & ternary

• 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

sizeof () Returns the size of a sizeof(a), where a is integer, will return 4.


variable.

& Returns the address of a &a; returns the actual address of the variable.
variable.

* Pointer to a variable. *a;

?: Conditional Expression. If Condition is true ? then value X : otherwise value Y

Precedence of Operators in - C

• Operator precedence determines the grouping of terms in an expression and decides


how an expression is evaluated.
• Certain operators have higher precedence than others; for example, the
multiplication operator has a higher precedence than the addition operator.
For example,
x = 7 + 3 * 2;
here, x is assigned 13, not 20 because operator * has a higher
precedence than +,
so, it first gets multiplied with 3*2 and then adds into 7.

❖ 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:

Category Operator Associativity

Postfix () [] -> . ++ - - Left to right

Unary + - ! ~ ++ - - (type)* & sizeof Right to left

Multiplicative */% Left to right

By, Mritunjay Kr. Ranjan. & Amit Gadekar (SOET-Sandip University, Nashik.)
ICCPL (Introduction to Computing and C- Programming Language) : Notes
Additive +- Left to right

Shift << >> Left to right

Relational < <= > >= Left to right

Equality == != Left to right

Bitwise AND & Left to right

Bitwise XOR ^ Left to right

Bitwise OR | Left to right

Logical AND && Left to right

Logical OR || Left to right

Conditional ?: Right to left

Assignment = += -= *= /= %=>>= <<= &= ^= |= Right to left

Comma , Left to right

Type Conversion C

• A type cast is basically a conversion from one type to another.

There are two types of type conversion:


1. Implicit Type Conversion
2. Explicit Type Conversion

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).

Explicit Type Conversion / Type Casting


• This process is also called type casting and it is
user defined.
• Here the user can type cast the result to make
it of a particular data type.
The syntax in C:
(type) expression
Type indicated the data type to which the final result is converted.

// C program to demonstrate explicit type casting


#include<stdio.h>
#include<conio.h>
void main()
{

By, Mritunjay Kr. Ranjan. & Amit Gadekar (SOET-Sandip University, Nashik.)
ICCPL (Introduction to Computing and C- Programming Language) : Notes
double x = 1.2;

// Explicit conversion from double to int


int sum = (int)x + 1;

printf("sum = %d", sum);

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.

• Additional arguments must point to variables of the same type as


specified in the format.

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

Macros: Note: Not For Beginners


• 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.
File Inclusion:
• This type of pre-processor directive tells the compiler to include a file in the source
code program.
There are two types of files which can be included by the user in the program:

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"

Conditional Compilation: Note: Not for Beginners


• Conditional Compilation directives are type of directives which helps to
compile a specific portion of the program or to skip compilation of some
specific part of the program based on some conditions.
• This can be done with the help of two pre-processing commands
‘ifdef‘ and ‘endif‘.
Syntax:
#ifdef macro_name
statement1;
statement2;
statement3;
.
.
.

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.

Other directives: Note: Not for Beginners


• Apart from the above directives there are two more directives which are not
commonly used. These are:
• #undef Directive: The #undef directive is used to undefine an existing macro. This
directive works as:
#undef LIMIT
Using this statement will undefine the existing macro LIMIT.
After this statement every “#ifdef LIMIT” statement will evaluate to
false.
• #pragma Directive: This directive is a special purpose directive and is used to turn on
or off some features. This type of directives are compiler-specific, i.e., they vary from
compiler to compiler. Some of the #pragma directives are discussed below:
• #pragma startup and #pragma exit: These directives helps us to specify the functions
that are needed to run before program startup (before the control passes to main() )
and just before program exit (just before the control returns from main()).
Simple Programme with C:
Write a program C to get the addition of two integer type number 4 and 6 with out-put.
Program

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.)

You might also like