SlideShare une entreprise Scribd logo
1  sur  41
Introduction to Algorithms
Algorithm:
 In programming, algorithm is a set of well
defined instructions in sequence to solve the
problem
Key features of algorithms are:
 Easy to grasp - Algorithms are there to help
humans to understand the solution.
 Correctness - This is a must have feature for all
algorithms.
 Precision - the steps are precisely stated(defined).
 Finiteness - the algorithm stops after a finite number
of steps.
 Generality - the algorithm applies to all possible
distribution of inputs as stated.
Example:
Write an algorithm to add two numbers entered by
user.
Step 1: Start
Step 2: Declare variables num1, num2 and sum.
Step 3: Read values num1 and num2.
Step 4: Add num1 and num2 and assign the result to
sum.
sum←num1+num2
Step 5: Display sum
Step 6: Stop
 Write an algorithm to find the largest among three
different numbers entered by user.
Step 1: Start
Step 2: Declare variables a,b and c.
Step 3: Read variables a,b and c.
Step 4: If a>b
If a>c
Display a is the largest number.
Else
Display c is the largest number.
Else
If b>c
Display b is the largest number.
Else
Display c is the greatest number.
Step 5: Stop
 FlowChart:
Flowchart is a diagrammatic representation of an algorithm. Flowchart are
very helpful in writing program and explaining program to others.
Symbol Purpose Description
Flow line
Used to indicate the flow of logic by connecting
symbols.
Terminal(Stop/Start) Used to represent start and end of flowchart.
Input/Output Used for input and output operation.
Processing
Used for airthmetic operations and data-
manipulations.
Desicion
Used to represent the operation in which there
are two alternatives, true and false.
On-page Connector Used to join different flowline
Off-page Connector
Used to connect flowchart portion on different
page.
Predefined
Process/Function
Used to represent a group of statements
performing one processing task.
 Example:
Draw a flowchart to add two numbers entered by
user
Pseudocode
Pseudocode is a detailed yet readable description of
what a computer program or algorithm must do,
expressed in a formally-styled natural language rather
than in a programming language. Pseudocode is
sometimes used as a detailed step in the process of
developing a program. It allows designers or lead
programmers to express the design in great detail and
provides programmers a detailed template for the next
step of writing code in a specific programming
language.
 Generations of programming language
1. First generation languages (1GL)
2. Second generation languages (2GL)
3. Third generation languages (3GL)
4. Fourth generation languages (4GL)
5. Fifth generation languages (5GL)
Structured Programming Approach
Structured Programming Approach, as the word
suggests, can be defined as a programming approach
in which the program is made as a single structure. It
means that the code will execute the instruction by
instruction one after the other. It doesn’t support the
possibility of jumping from one instruction to some
other with help of any statement like GOTO, etc.
Therefore, the instructions in this approach will be
executed in a serial and structured manner. The
languages that support Structured programming
approach are:
 C
 C++
 Java
INTRODUCTION TO C
Introduction to C:
 'C' is a programming language that was developed in
the early 1970s by Dennis Ritchie at Bell
Laboratories.
 It was designed for implementing system software
and used for developing the portable application
software.
 It is one of the most popular languages.
 C++ and Java are based on C programming which
means that if you are well versed with C, then the
above two programming languages can be learnt
very easily.
 It is primarily used for the system programming. It
has the portability, efficiency and the ability to access
the specific hardware addresses and low runtime
demand on system resources which makes it a good
choice for implementation of operating systems and
 Structure of a C program
Example: First C Program
Program:
#include <stdio.h>
void main()
{
printf(“n Welcome ”);
}
Output:
Welcome
Process of compilation and execution:
 The process starts with the source file and ends with
the executable file.
 A source file is created which consists of statements
written in C.
 The source file is then processed by the compiler.
 Compiler will translate the source code into object
code. Object code contains the machine instructions
for the CPU and calls the operating system API.
 The object file is not an executable file.
 The object file is then processed with a linker.
 There can be different compilers for the program but
has a same linker for the object files.
 The output of this linker will be an executable file.
Comments in C:
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.
1. Single Line Comments
2. Multi-Line Comments
Single Line Comments:
Single line comments are represented by double slash . Let's see an example of a single line comment in C.
#include<stdio.h>
int main(){
//printing information
printf("Hello C");
return 0;
}
Output:
Hello C
Even you can place the comment after the statement. For example:
printf("Hello C");//printing information
Mult Line Comments:
Multi-Line comments are represented by slash asterisk * ... *. It can occupy many lines of code, but it can't be nested. Syntax:
/*
code
to be commented
*/
Let's see an example of a multi-Line comment in C.
#include<stdio.h>
int main(){
/*printing information
Multi-Line Comment*/
Keywords in C:
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
Identifiers
 Identifier refers to name given to entities such as
variables, functions, structures etc.
 Identifier must be unique. They are created to give
unique name to a entity to identify it during the
execution of the program. For example:
 int money; double accountBalance;
 Here, money and accountBalance are identifiers.
Data Types in C:
 A data type specifies the type of data that a variable
can store such as integer, floating, character, etc.
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
Basic Data Types:
 The basic data types are integer-based and floating-
point based. C language supports both signed and
unsigned literals.
 The memory size of the basic data types may
change according to 32 or 64-bit operating system.
Variable:
 C variable is a named location in a memory where a
program can manipulate the data. This location is
used to hold the value of the variable.
 The value of the C variable may get change in the
program.
 C variable might be belonging to any of the data type
like int, float, char etc.
RULES FOR NAMING C VARIABLE:
 Variable name must begin with letter or underscore.
 Variables are case sensitive
 They can be constructed with digits, letters.
 No special symbols are allowed other than
underscore.
 sum, height, _value are some examples for variable
Constants:
 Constants refer to fixed values that the program may
not alter during its execution. These fixed values are
also called literals.
 Constants can be of any of the basic data types
like an integer constant, a floating constant, a
character constant, or a string literal. There are
enumeration constants as well.
 Constants are treated just like regular variables
except that their values cannot be modified after
their definition
Input : In any programming language input means to
feed some data into program. This can be given in the
form of file or from command line. C programming
language provides a set of built-in functions to read
given input and feed it to the program as per
requirement.
Output : In any programming language output means
to display some data on screen, printer or in any file. C
programming language provides a set of built-in
functions to output required data.
 Here we will discuss only one input function and one
output function just to understand the meaning of
input and output. Rest of the functions are given
into C –Built-in Function
printf() function
 This is one of the most frequently used functions in
C for output.
scanf() function
 This is the function which can be used to to read an
input from the command line.
C Operators:
 An operator is a symbol which operates on a value
or a variable. For example: + is an operator to
perform addition.
 C has wide range of operators to perform various
operations.
Operator Meaning of Operator
+ addition or unary plus
- subtraction or unary minus
* Multiplication
/ Division
% remainder after division( modulo division)
// C Program to demonstrate the working of arithmetic operators
#include <stdio.h>
int main()
{
int a = 9,b = 4, c;
c = a+b;
printf("a+b = %d n",c);
c = a-b;
printf("a-b = %d n",c);
c = a*b;
printf("a*b = %d n",c);
c=a/b;
printf("a/b = %d n",c);
c=a%b;
printf("Remainder when a divided by b = %d n",c);
return 0;
}
Output
a+b = 13
a-b = 5
a*b = 36
C Assignment Operators
 An assignment operator is used for assigning a
value to a variable. The most common assignment
operator is =
Operator Example Same as
= a = b a = b
+= a += b a = a+b
-= a -= b a = a-b
*= a *= b a = a*b
/= a /= b a = a/b
%= a %= b a = a%b
// C Program to demonstrate the working of assignment operators
#include <stdio.h>
int main()
{
int a = 5, c;
c = a;
printf("c = %d n", c);
c += a; // c = c+a
printf("c = %d n", c);
c -= a; // c = c-a
printf("c = %d n", c);
c *= a; // c = c*a
printf("c = %d n", c);
c /= a; // c = c/a
printf("c = %d n", c);
c %= a; // c = c%a
printf("c = %d n", c);
return 0;
}
c = 5
c = 10
c = 5
c = 25
c = 5
c = 0
OUTPUT
Relational operators are used in decision making and
loops:
Operator Meaning of Operator Example
== Equal to 5 == 3 returns 0
> Greater than 5 > 3 returns 1
< Less than 5 < 3 returns 0
!= Not equal to 5 != 3 returns 1
>= Greater than or equal to 5 >= 3 returns 1
<= Less than or equal to 5 <= 3 return 0
Logical operators :
Operator Meaning of Operator Example
&&
Logial AND. True only if all
operands are true
If c = 5 and d = 2 then, expression ((c == 5)
&& (d > 5)) equals to 0.
||
Logical OR. True only if either
one operand is true
If c = 5 and d = 2 then, expression ((c == 5)
|| (d > 5)) equals to 1.
!
Logical NOT. True only if the
operand is 0
If c = 5 then, expression ! (c == 5)equals to
0.
Bitwise Operators:
Operators Meaning of operators
& Bitwise AND
| Bitwise OR
^ Bitwise exclusive OR
~ Bitwise complement
<< Shift left
>> Shift right
C Ternary Operator (?:)
Ternary operator is a conditional operator that works
on 3 operands.
Conditional Operator Syntax:
conditionalExpression ? expression1 : expression2
The conditional operator works as follows:
 The first expression conditionalExpression is
evaluated first. This expression evaluates to 1 if it's
true and evaluates to 0 if it's false.
 If conditionalExpression is true, expression1 is
evaluated.
 If conditionalExpression is false, expression2 is
evaluated.
Type Conversion in 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
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.
Implicit Type Conversion:
 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 lose of data.
 All the data types of the variables are upgraded to the data
type of the variable with largest data type.
 bool -> char -> short int -> int ->
unsigned int -> long -> unsigned ->
long long -> float -> double -> long
double
 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:
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.
Example:
// C program to demonstrate explicit type casting
#include<stdio.h>
int main()
{
double x = 1.2;
// Explicit conversion from double to int
int sum = (int)x + 1;
printf("sum = %d", sum);
return 0;
}
Output:
sum=2

Contenu connexe

Tendances (20)

Introduction to c++ ppt
Introduction to c++ pptIntroduction to c++ ppt
Introduction to c++ ppt
 
SPL 9 | Scope of Variables in C
SPL 9 | Scope of Variables in CSPL 9 | Scope of Variables in C
SPL 9 | Scope of Variables in C
 
C basics 4 std11(GujBoard)
C basics 4 std11(GujBoard)C basics 4 std11(GujBoard)
C basics 4 std11(GujBoard)
 
C presentation
C presentationC presentation
C presentation
 
Data Types In C
Data Types In CData Types In C
Data Types In C
 
String functions in C
String functions in CString functions in C
String functions in C
 
Presentation on C++ Programming Language
Presentation on C++ Programming LanguagePresentation on C++ Programming Language
Presentation on C++ Programming Language
 
Data types in C
Data types in CData types in C
Data types in C
 
Introduction to c++
Introduction to c++Introduction to c++
Introduction to c++
 
Storage class in c
Storage class in cStorage class in c
Storage class in c
 
C fundamental
C fundamentalC fundamental
C fundamental
 
Loops in C
Loops in CLoops in C
Loops in C
 
Functions in c language
Functions in c language Functions in c language
Functions in c language
 
Elements of programming
Elements of programmingElements of programming
Elements of programming
 
Lecture 1- History of C Programming
Lecture 1- History of C Programming Lecture 1- History of C Programming
Lecture 1- History of C Programming
 
Data types in c++
Data types in c++Data types in c++
Data types in c++
 
Pointers C programming
Pointers  C programmingPointers  C programming
Pointers C programming
 
Introduction to c programming
Introduction to c programmingIntroduction to c programming
Introduction to c programming
 
Introduction of c programming
Introduction of c programmingIntroduction of c programming
Introduction of c programming
 
Call by value
Call by valueCall by value
Call by value
 

Similaire à C Programming Unit-1

Unit 2 introduction to c programming
Unit 2   introduction to c programmingUnit 2   introduction to c programming
Unit 2 introduction to c programmingMithun DSouza
 
Introduction to C Unit 1
Introduction to C Unit 1Introduction to C Unit 1
Introduction to C Unit 1SURBHI SAROHA
 
1. introduction to computer
1. introduction to computer1. introduction to computer
1. introduction to computerShankar Gangaju
 
IIM.Com-FIT-Unit2(14.9.2021 TO 30.9.2021).pptx
IIM.Com-FIT-Unit2(14.9.2021 TO 30.9.2021).pptxIIM.Com-FIT-Unit2(14.9.2021 TO 30.9.2021).pptx
IIM.Com-FIT-Unit2(14.9.2021 TO 30.9.2021).pptxrajkumar490591
 
Introduction to C Programming
Introduction to C ProgrammingIntroduction to C Programming
Introduction to C ProgrammingMOHAMAD NOH AHMAD
 
C prog ppt
C prog pptC prog ppt
C prog pptxinoe
 
C programming language tutorial for beginers.pdf
C programming language tutorial for beginers.pdfC programming language tutorial for beginers.pdf
C programming language tutorial for beginers.pdfComedyTechnology
 
C language introduction geeksfor geeks
C language introduction   geeksfor geeksC language introduction   geeksfor geeks
C language introduction geeksfor geeksAashutoshChhedavi
 
Chapter3
Chapter3Chapter3
Chapter3Kamran
 
C++ programming language basic to advance level
C++ programming language basic to advance levelC++ programming language basic to advance level
C++ programming language basic to advance levelsajjad ali khan
 
C programming notes.pdf
C programming notes.pdfC programming notes.pdf
C programming notes.pdfAdiseshaK
 
C programming Training in Ambala ! Batra Computer Centre
C programming Training in Ambala ! Batra Computer CentreC programming Training in Ambala ! Batra Computer Centre
C programming Training in Ambala ! Batra Computer Centrejatin batra
 
C programming course material
C programming course materialC programming course material
C programming course materialRanjitha Murthy
 

Similaire à C Programming Unit-1 (20)

Unit 2 introduction to c programming
Unit 2   introduction to c programmingUnit 2   introduction to c programming
Unit 2 introduction to c programming
 
Introduction%20C.pptx
Introduction%20C.pptxIntroduction%20C.pptx
Introduction%20C.pptx
 
Introduction to C Unit 1
Introduction to C Unit 1Introduction to C Unit 1
Introduction to C Unit 1
 
1. introduction to computer
1. introduction to computer1. introduction to computer
1. introduction to computer
 
IIM.Com-FIT-Unit2(14.9.2021 TO 30.9.2021).pptx
IIM.Com-FIT-Unit2(14.9.2021 TO 30.9.2021).pptxIIM.Com-FIT-Unit2(14.9.2021 TO 30.9.2021).pptx
IIM.Com-FIT-Unit2(14.9.2021 TO 30.9.2021).pptx
 
C language
C language C language
C language
 
C programming
C programmingC programming
C programming
 
Introduction to C Programming
Introduction to C ProgrammingIntroduction to C Programming
Introduction to C Programming
 
C prog ppt
C prog pptC prog ppt
C prog ppt
 
C programming language tutorial for beginers.pdf
C programming language tutorial for beginers.pdfC programming language tutorial for beginers.pdf
C programming language tutorial for beginers.pdf
 
C language introduction geeksfor geeks
C language introduction   geeksfor geeksC language introduction   geeksfor geeks
C language introduction geeksfor geeks
 
C programming notes
C programming notesC programming notes
C programming notes
 
Chapter3
Chapter3Chapter3
Chapter3
 
C++ programming language basic to advance level
C++ programming language basic to advance levelC++ programming language basic to advance level
C++ programming language basic to advance level
 
C programming
C programmingC programming
C programming
 
C programming notes.pdf
C programming notes.pdfC programming notes.pdf
C programming notes.pdf
 
C programming Training in Ambala ! Batra Computer Centre
C programming Training in Ambala ! Batra Computer CentreC programming Training in Ambala ! Batra Computer Centre
C programming Training in Ambala ! Batra Computer Centre
 
C programming course material
C programming course materialC programming course material
C programming course material
 
Introduction to Procedural Programming in C++
Introduction to Procedural Programming in C++Introduction to Procedural Programming in C++
Introduction to Procedural Programming in C++
 
UNIT 1 NOTES.docx
UNIT 1 NOTES.docxUNIT 1 NOTES.docx
UNIT 1 NOTES.docx
 

Plus de Vikram Nandini

IoT: From Copper strip to Gold Bar
IoT: From Copper strip to Gold BarIoT: From Copper strip to Gold Bar
IoT: From Copper strip to Gold BarVikram Nandini
 
Linux File Trees and Commands
Linux File Trees and CommandsLinux File Trees and Commands
Linux File Trees and CommandsVikram Nandini
 
Introduction to Linux & Basic Commands
Introduction to Linux & Basic CommandsIntroduction to Linux & Basic Commands
Introduction to Linux & Basic CommandsVikram Nandini
 
Manufacturing - II Part
Manufacturing - II PartManufacturing - II Part
Manufacturing - II PartVikram Nandini
 
Prototyping Online Components
Prototyping Online ComponentsPrototyping Online Components
Prototyping Online ComponentsVikram Nandini
 
Artificial Neural Networks
Artificial Neural NetworksArtificial Neural Networks
Artificial Neural NetworksVikram Nandini
 
Design Principles for Connected Devices
Design Principles for Connected DevicesDesign Principles for Connected Devices
Design Principles for Connected DevicesVikram Nandini
 
Communication in the IoT
Communication in the IoTCommunication in the IoT
Communication in the IoTVikram Nandini
 
Introduction to Cyber Security
Introduction to Cyber SecurityIntroduction to Cyber Security
Introduction to Cyber SecurityVikram Nandini
 
cloud computing UNIT-2.pdf
cloud computing UNIT-2.pdfcloud computing UNIT-2.pdf
cloud computing UNIT-2.pdfVikram Nandini
 
Introduction to Web Technologies
Introduction to Web TechnologiesIntroduction to Web Technologies
Introduction to Web TechnologiesVikram Nandini
 
Cascading Style Sheets
Cascading Style SheetsCascading Style Sheets
Cascading Style SheetsVikram Nandini
 

Plus de Vikram Nandini (20)

IoT: From Copper strip to Gold Bar
IoT: From Copper strip to Gold BarIoT: From Copper strip to Gold Bar
IoT: From Copper strip to Gold Bar
 
Design Patterns
Design PatternsDesign Patterns
Design Patterns
 
Linux File Trees and Commands
Linux File Trees and CommandsLinux File Trees and Commands
Linux File Trees and Commands
 
Introduction to Linux & Basic Commands
Introduction to Linux & Basic CommandsIntroduction to Linux & Basic Commands
Introduction to Linux & Basic Commands
 
INTRODUCTION to OOAD
INTRODUCTION to OOADINTRODUCTION to OOAD
INTRODUCTION to OOAD
 
Ethics
EthicsEthics
Ethics
 
Manufacturing - II Part
Manufacturing - II PartManufacturing - II Part
Manufacturing - II Part
 
Manufacturing
ManufacturingManufacturing
Manufacturing
 
Business Models
Business ModelsBusiness Models
Business Models
 
Prototyping Online Components
Prototyping Online ComponentsPrototyping Online Components
Prototyping Online Components
 
Artificial Neural Networks
Artificial Neural NetworksArtificial Neural Networks
Artificial Neural Networks
 
IoT-Prototyping
IoT-PrototypingIoT-Prototyping
IoT-Prototyping
 
Design Principles for Connected Devices
Design Principles for Connected DevicesDesign Principles for Connected Devices
Design Principles for Connected Devices
 
Introduction to IoT
Introduction to IoTIntroduction to IoT
Introduction to IoT
 
Embedded decices
Embedded decicesEmbedded decices
Embedded decices
 
Communication in the IoT
Communication in the IoTCommunication in the IoT
Communication in the IoT
 
Introduction to Cyber Security
Introduction to Cyber SecurityIntroduction to Cyber Security
Introduction to Cyber Security
 
cloud computing UNIT-2.pdf
cloud computing UNIT-2.pdfcloud computing UNIT-2.pdf
cloud computing UNIT-2.pdf
 
Introduction to Web Technologies
Introduction to Web TechnologiesIntroduction to Web Technologies
Introduction to Web Technologies
 
Cascading Style Sheets
Cascading Style SheetsCascading Style Sheets
Cascading Style Sheets
 

Dernier

Blowin' in the Wind of Caste_ Bob Dylan's Song as a Catalyst for Social Justi...
Blowin' in the Wind of Caste_ Bob Dylan's Song as a Catalyst for Social Justi...Blowin' in the Wind of Caste_ Bob Dylan's Song as a Catalyst for Social Justi...
Blowin' in the Wind of Caste_ Bob Dylan's Song as a Catalyst for Social Justi...DhatriParmar
 
Textual Evidence in Reading and Writing of SHS
Textual Evidence in Reading and Writing of SHSTextual Evidence in Reading and Writing of SHS
Textual Evidence in Reading and Writing of SHSMae Pangan
 
Measures of Position DECILES for ungrouped data
Measures of Position DECILES for ungrouped dataMeasures of Position DECILES for ungrouped data
Measures of Position DECILES for ungrouped dataBabyAnnMotar
 
Grade Three -ELLNA-REVIEWER-ENGLISH.pptx
Grade Three -ELLNA-REVIEWER-ENGLISH.pptxGrade Three -ELLNA-REVIEWER-ENGLISH.pptx
Grade Three -ELLNA-REVIEWER-ENGLISH.pptxkarenfajardo43
 
ROLES IN A STAGE PRODUCTION in arts.pptx
ROLES IN A STAGE PRODUCTION in arts.pptxROLES IN A STAGE PRODUCTION in arts.pptx
ROLES IN A STAGE PRODUCTION in arts.pptxVanesaIglesias10
 
Narcotic and Non Narcotic Analgesic..pdf
Narcotic and Non Narcotic Analgesic..pdfNarcotic and Non Narcotic Analgesic..pdf
Narcotic and Non Narcotic Analgesic..pdfPrerana Jadhav
 
How to Make a Duplicate of Your Odoo 17 Database
How to Make a Duplicate of Your Odoo 17 DatabaseHow to Make a Duplicate of Your Odoo 17 Database
How to Make a Duplicate of Your Odoo 17 DatabaseCeline George
 
4.16.24 21st Century Movements for Black Lives.pptx
4.16.24 21st Century Movements for Black Lives.pptx4.16.24 21st Century Movements for Black Lives.pptx
4.16.24 21st Century Movements for Black Lives.pptxmary850239
 
MS4 level being good citizen -imperative- (1) (1).pdf
MS4 level   being good citizen -imperative- (1) (1).pdfMS4 level   being good citizen -imperative- (1) (1).pdf
MS4 level being good citizen -imperative- (1) (1).pdfMr Bounab Samir
 
ESP 4-EDITED.pdfmmcncncncmcmmnmnmncnmncmnnjvnnv
ESP 4-EDITED.pdfmmcncncncmcmmnmnmncnmncmnnjvnnvESP 4-EDITED.pdfmmcncncncmcmmnmnmncnmncmnnjvnnv
ESP 4-EDITED.pdfmmcncncncmcmmnmnmncnmncmnnjvnnvRicaMaeCastro1
 
Scientific Writing :Research Discourse
Scientific  Writing :Research  DiscourseScientific  Writing :Research  Discourse
Scientific Writing :Research DiscourseAnita GoswamiGiri
 
Beauty Amidst the Bytes_ Unearthing Unexpected Advantages of the Digital Wast...
Beauty Amidst the Bytes_ Unearthing Unexpected Advantages of the Digital Wast...Beauty Amidst the Bytes_ Unearthing Unexpected Advantages of the Digital Wast...
Beauty Amidst the Bytes_ Unearthing Unexpected Advantages of the Digital Wast...DhatriParmar
 
Grade 9 Quarter 4 Dll Grade 9 Quarter 4 DLL.pdf
Grade 9 Quarter 4 Dll Grade 9 Quarter 4 DLL.pdfGrade 9 Quarter 4 Dll Grade 9 Quarter 4 DLL.pdf
Grade 9 Quarter 4 Dll Grade 9 Quarter 4 DLL.pdfJemuel Francisco
 
Mental Health Awareness - a toolkit for supporting young minds
Mental Health Awareness - a toolkit for supporting young mindsMental Health Awareness - a toolkit for supporting young minds
Mental Health Awareness - a toolkit for supporting young mindsPooky Knightsmith
 
BIOCHEMISTRY-CARBOHYDRATE METABOLISM CHAPTER 2.pptx
BIOCHEMISTRY-CARBOHYDRATE METABOLISM CHAPTER 2.pptxBIOCHEMISTRY-CARBOHYDRATE METABOLISM CHAPTER 2.pptx
BIOCHEMISTRY-CARBOHYDRATE METABOLISM CHAPTER 2.pptxSayali Powar
 
Daily Lesson Plan in Mathematics Quarter 4
Daily Lesson Plan in Mathematics Quarter 4Daily Lesson Plan in Mathematics Quarter 4
Daily Lesson Plan in Mathematics Quarter 4JOYLYNSAMANIEGO
 
Using Grammatical Signals Suitable to Patterns of Idea Development
Using Grammatical Signals Suitable to Patterns of Idea DevelopmentUsing Grammatical Signals Suitable to Patterns of Idea Development
Using Grammatical Signals Suitable to Patterns of Idea Developmentchesterberbo7
 
week 1 cookery 8 fourth - quarter .pptx
week 1 cookery 8  fourth  -  quarter .pptxweek 1 cookery 8  fourth  -  quarter .pptx
week 1 cookery 8 fourth - quarter .pptxJonalynLegaspi2
 

Dernier (20)

Blowin' in the Wind of Caste_ Bob Dylan's Song as a Catalyst for Social Justi...
Blowin' in the Wind of Caste_ Bob Dylan's Song as a Catalyst for Social Justi...Blowin' in the Wind of Caste_ Bob Dylan's Song as a Catalyst for Social Justi...
Blowin' in the Wind of Caste_ Bob Dylan's Song as a Catalyst for Social Justi...
 
Textual Evidence in Reading and Writing of SHS
Textual Evidence in Reading and Writing of SHSTextual Evidence in Reading and Writing of SHS
Textual Evidence in Reading and Writing of SHS
 
Measures of Position DECILES for ungrouped data
Measures of Position DECILES for ungrouped dataMeasures of Position DECILES for ungrouped data
Measures of Position DECILES for ungrouped data
 
Grade Three -ELLNA-REVIEWER-ENGLISH.pptx
Grade Three -ELLNA-REVIEWER-ENGLISH.pptxGrade Three -ELLNA-REVIEWER-ENGLISH.pptx
Grade Three -ELLNA-REVIEWER-ENGLISH.pptx
 
ROLES IN A STAGE PRODUCTION in arts.pptx
ROLES IN A STAGE PRODUCTION in arts.pptxROLES IN A STAGE PRODUCTION in arts.pptx
ROLES IN A STAGE PRODUCTION in arts.pptx
 
Narcotic and Non Narcotic Analgesic..pdf
Narcotic and Non Narcotic Analgesic..pdfNarcotic and Non Narcotic Analgesic..pdf
Narcotic and Non Narcotic Analgesic..pdf
 
How to Make a Duplicate of Your Odoo 17 Database
How to Make a Duplicate of Your Odoo 17 DatabaseHow to Make a Duplicate of Your Odoo 17 Database
How to Make a Duplicate of Your Odoo 17 Database
 
prashanth updated resume 2024 for Teaching Profession
prashanth updated resume 2024 for Teaching Professionprashanth updated resume 2024 for Teaching Profession
prashanth updated resume 2024 for Teaching Profession
 
4.16.24 21st Century Movements for Black Lives.pptx
4.16.24 21st Century Movements for Black Lives.pptx4.16.24 21st Century Movements for Black Lives.pptx
4.16.24 21st Century Movements for Black Lives.pptx
 
MS4 level being good citizen -imperative- (1) (1).pdf
MS4 level   being good citizen -imperative- (1) (1).pdfMS4 level   being good citizen -imperative- (1) (1).pdf
MS4 level being good citizen -imperative- (1) (1).pdf
 
Mattingly "AI & Prompt Design: Large Language Models"
Mattingly "AI & Prompt Design: Large Language Models"Mattingly "AI & Prompt Design: Large Language Models"
Mattingly "AI & Prompt Design: Large Language Models"
 
ESP 4-EDITED.pdfmmcncncncmcmmnmnmncnmncmnnjvnnv
ESP 4-EDITED.pdfmmcncncncmcmmnmnmncnmncmnnjvnnvESP 4-EDITED.pdfmmcncncncmcmmnmnmncnmncmnnjvnnv
ESP 4-EDITED.pdfmmcncncncmcmmnmnmncnmncmnnjvnnv
 
Scientific Writing :Research Discourse
Scientific  Writing :Research  DiscourseScientific  Writing :Research  Discourse
Scientific Writing :Research Discourse
 
Beauty Amidst the Bytes_ Unearthing Unexpected Advantages of the Digital Wast...
Beauty Amidst the Bytes_ Unearthing Unexpected Advantages of the Digital Wast...Beauty Amidst the Bytes_ Unearthing Unexpected Advantages of the Digital Wast...
Beauty Amidst the Bytes_ Unearthing Unexpected Advantages of the Digital Wast...
 
Grade 9 Quarter 4 Dll Grade 9 Quarter 4 DLL.pdf
Grade 9 Quarter 4 Dll Grade 9 Quarter 4 DLL.pdfGrade 9 Quarter 4 Dll Grade 9 Quarter 4 DLL.pdf
Grade 9 Quarter 4 Dll Grade 9 Quarter 4 DLL.pdf
 
Mental Health Awareness - a toolkit for supporting young minds
Mental Health Awareness - a toolkit for supporting young mindsMental Health Awareness - a toolkit for supporting young minds
Mental Health Awareness - a toolkit for supporting young minds
 
BIOCHEMISTRY-CARBOHYDRATE METABOLISM CHAPTER 2.pptx
BIOCHEMISTRY-CARBOHYDRATE METABOLISM CHAPTER 2.pptxBIOCHEMISTRY-CARBOHYDRATE METABOLISM CHAPTER 2.pptx
BIOCHEMISTRY-CARBOHYDRATE METABOLISM CHAPTER 2.pptx
 
Daily Lesson Plan in Mathematics Quarter 4
Daily Lesson Plan in Mathematics Quarter 4Daily Lesson Plan in Mathematics Quarter 4
Daily Lesson Plan in Mathematics Quarter 4
 
Using Grammatical Signals Suitable to Patterns of Idea Development
Using Grammatical Signals Suitable to Patterns of Idea DevelopmentUsing Grammatical Signals Suitable to Patterns of Idea Development
Using Grammatical Signals Suitable to Patterns of Idea Development
 
week 1 cookery 8 fourth - quarter .pptx
week 1 cookery 8  fourth  -  quarter .pptxweek 1 cookery 8  fourth  -  quarter .pptx
week 1 cookery 8 fourth - quarter .pptx
 

C Programming Unit-1

  • 2. Algorithm:  In programming, algorithm is a set of well defined instructions in sequence to solve the problem Key features of algorithms are:  Easy to grasp - Algorithms are there to help humans to understand the solution.  Correctness - This is a must have feature for all algorithms.  Precision - the steps are precisely stated(defined).  Finiteness - the algorithm stops after a finite number of steps.  Generality - the algorithm applies to all possible distribution of inputs as stated.
  • 3. Example: Write an algorithm to add two numbers entered by user. Step 1: Start Step 2: Declare variables num1, num2 and sum. Step 3: Read values num1 and num2. Step 4: Add num1 and num2 and assign the result to sum. sum←num1+num2 Step 5: Display sum Step 6: Stop
  • 4.  Write an algorithm to find the largest among three different numbers entered by user. Step 1: Start Step 2: Declare variables a,b and c. Step 3: Read variables a,b and c. Step 4: If a>b If a>c Display a is the largest number. Else Display c is the largest number. Else If b>c Display b is the largest number. Else Display c is the greatest number. Step 5: Stop
  • 5.  FlowChart: Flowchart is a diagrammatic representation of an algorithm. Flowchart are very helpful in writing program and explaining program to others. Symbol Purpose Description Flow line Used to indicate the flow of logic by connecting symbols. Terminal(Stop/Start) Used to represent start and end of flowchart. Input/Output Used for input and output operation. Processing Used for airthmetic operations and data- manipulations. Desicion Used to represent the operation in which there are two alternatives, true and false. On-page Connector Used to join different flowline Off-page Connector Used to connect flowchart portion on different page. Predefined Process/Function Used to represent a group of statements performing one processing task.
  • 6.  Example: Draw a flowchart to add two numbers entered by user
  • 7. Pseudocode Pseudocode is a detailed yet readable description of what a computer program or algorithm must do, expressed in a formally-styled natural language rather than in a programming language. Pseudocode is sometimes used as a detailed step in the process of developing a program. It allows designers or lead programmers to express the design in great detail and provides programmers a detailed template for the next step of writing code in a specific programming language.
  • 8.  Generations of programming language 1. First generation languages (1GL) 2. Second generation languages (2GL) 3. Third generation languages (3GL) 4. Fourth generation languages (4GL) 5. Fifth generation languages (5GL)
  • 9. Structured Programming Approach Structured Programming Approach, as the word suggests, can be defined as a programming approach in which the program is made as a single structure. It means that the code will execute the instruction by instruction one after the other. It doesn’t support the possibility of jumping from one instruction to some other with help of any statement like GOTO, etc. Therefore, the instructions in this approach will be executed in a serial and structured manner. The languages that support Structured programming approach are:  C  C++  Java
  • 11. Introduction to C:  'C' is a programming language that was developed in the early 1970s by Dennis Ritchie at Bell Laboratories.  It was designed for implementing system software and used for developing the portable application software.  It is one of the most popular languages.  C++ and Java are based on C programming which means that if you are well versed with C, then the above two programming languages can be learnt very easily.  It is primarily used for the system programming. It has the portability, efficiency and the ability to access the specific hardware addresses and low runtime demand on system resources which makes it a good choice for implementation of operating systems and
  • 12.  Structure of a C program
  • 13. Example: First C Program Program: #include <stdio.h> void main() { printf(“n Welcome ”); } Output: Welcome
  • 14. Process of compilation and execution:  The process starts with the source file and ends with the executable file.  A source file is created which consists of statements written in C.  The source file is then processed by the compiler.  Compiler will translate the source code into object code. Object code contains the machine instructions for the CPU and calls the operating system API.  The object file is not an executable file.  The object file is then processed with a linker.  There can be different compilers for the program but has a same linker for the object files.  The output of this linker will be an executable file.
  • 15.
  • 16. Comments in C: 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. 1. Single Line Comments 2. Multi-Line Comments Single Line Comments: Single line comments are represented by double slash . Let's see an example of a single line comment in C. #include<stdio.h> int main(){ //printing information printf("Hello C"); return 0; } Output: Hello C Even you can place the comment after the statement. For example: printf("Hello C");//printing information Mult Line Comments: Multi-Line comments are represented by slash asterisk * ... *. It can occupy many lines of code, but it can't be nested. Syntax: /* code to be commented */ Let's see an example of a multi-Line comment in C. #include<stdio.h> int main(){ /*printing information Multi-Line Comment*/
  • 17. Keywords in C: 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
  • 18. Identifiers  Identifier refers to name given to entities such as variables, functions, structures etc.  Identifier must be unique. They are created to give unique name to a entity to identify it during the execution of the program. For example:  int money; double accountBalance;  Here, money and accountBalance are identifiers.
  • 19. Data Types in C:  A data type specifies the type of data that a variable can store such as integer, floating, character, etc.
  • 20. 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
  • 21. Basic Data Types:  The basic data types are integer-based and floating- point based. C language supports both signed and unsigned literals.  The memory size of the basic data types may change according to 32 or 64-bit operating system.
  • 22.
  • 23. Variable:  C variable is a named location in a memory where a program can manipulate the data. This location is used to hold the value of the variable.  The value of the C variable may get change in the program.  C variable might be belonging to any of the data type like int, float, char etc. RULES FOR NAMING C VARIABLE:  Variable name must begin with letter or underscore.  Variables are case sensitive  They can be constructed with digits, letters.  No special symbols are allowed other than underscore.  sum, height, _value are some examples for variable
  • 24. Constants:  Constants refer to fixed values that the program may not alter during its execution. These fixed values are also called literals.  Constants can be of any of the basic data types like an integer constant, a floating constant, a character constant, or a string literal. There are enumeration constants as well.  Constants are treated just like regular variables except that their values cannot be modified after their definition
  • 25. Input : In any programming language input means to feed some data into program. This can be given in the form of file or from command line. C programming language provides a set of built-in functions to read given input and feed it to the program as per requirement. Output : In any programming language output means to display some data on screen, printer or in any file. C programming language provides a set of built-in functions to output required data.  Here we will discuss only one input function and one output function just to understand the meaning of input and output. Rest of the functions are given into C –Built-in Function
  • 26. printf() function  This is one of the most frequently used functions in C for output. scanf() function  This is the function which can be used to to read an input from the command line.
  • 27. C Operators:  An operator is a symbol which operates on a value or a variable. For example: + is an operator to perform addition.  C has wide range of operators to perform various operations.
  • 28. Operator Meaning of Operator + addition or unary plus - subtraction or unary minus * Multiplication / Division % remainder after division( modulo division)
  • 29. // C Program to demonstrate the working of arithmetic operators #include <stdio.h> int main() { int a = 9,b = 4, c; c = a+b; printf("a+b = %d n",c); c = a-b; printf("a-b = %d n",c); c = a*b; printf("a*b = %d n",c); c=a/b; printf("a/b = %d n",c); c=a%b; printf("Remainder when a divided by b = %d n",c); return 0; } Output a+b = 13 a-b = 5 a*b = 36
  • 30. C Assignment Operators  An assignment operator is used for assigning a value to a variable. The most common assignment operator is = Operator Example Same as = a = b a = b += a += b a = a+b -= a -= b a = a-b *= a *= b a = a*b /= a /= b a = a/b %= a %= b a = a%b
  • 31. // C Program to demonstrate the working of assignment operators #include <stdio.h> int main() { int a = 5, c; c = a; printf("c = %d n", c); c += a; // c = c+a printf("c = %d n", c); c -= a; // c = c-a printf("c = %d n", c); c *= a; // c = c*a printf("c = %d n", c); c /= a; // c = c/a printf("c = %d n", c); c %= a; // c = c%a printf("c = %d n", c); return 0; } c = 5 c = 10 c = 5 c = 25 c = 5 c = 0 OUTPUT
  • 32. Relational operators are used in decision making and loops: Operator Meaning of Operator Example == Equal to 5 == 3 returns 0 > Greater than 5 > 3 returns 1 < Less than 5 < 3 returns 0 != Not equal to 5 != 3 returns 1 >= Greater than or equal to 5 >= 3 returns 1 <= Less than or equal to 5 <= 3 return 0
  • 33. Logical operators : Operator Meaning of Operator Example && Logial AND. True only if all operands are true If c = 5 and d = 2 then, expression ((c == 5) && (d > 5)) equals to 0. || Logical OR. True only if either one operand is true If c = 5 and d = 2 then, expression ((c == 5) || (d > 5)) equals to 1. ! Logical NOT. True only if the operand is 0 If c = 5 then, expression ! (c == 5)equals to 0.
  • 34. Bitwise Operators: Operators Meaning of operators & Bitwise AND | Bitwise OR ^ Bitwise exclusive OR ~ Bitwise complement << Shift left >> Shift right
  • 35. C Ternary Operator (?:) Ternary operator is a conditional operator that works on 3 operands. Conditional Operator Syntax: conditionalExpression ? expression1 : expression2 The conditional operator works as follows:  The first expression conditionalExpression is evaluated first. This expression evaluates to 1 if it's true and evaluates to 0 if it's false.  If conditionalExpression is true, expression1 is evaluated.  If conditionalExpression is false, expression2 is evaluated.
  • 36. Type Conversion in 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 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.
  • 38.  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 lose of data.  All the data types of the variables are upgraded to the data type of the variable with largest data type.  bool -> char -> short int -> int -> unsigned int -> long -> unsigned -> long long -> float -> double -> long double  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).
  • 40. 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.
  • 41. Example: // C program to demonstrate explicit type casting #include<stdio.h> int main() { double x = 1.2; // Explicit conversion from double to int int sum = (int)x + 1; printf("sum = %d", sum); return 0; } Output: sum=2