C Programming Unit-1

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.
C Programming Unit-1
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.
C Programming Unit-1
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
1 de 41

Recomendados

C programming presentation for university por
C programming presentation for universityC programming presentation for university
C programming presentation for universitySheikh Monirul Hasan
1.2K visualizações10 slides
Basics of c++ por
Basics of c++Basics of c++
Basics of c++Huba Akhtar
1.1K visualizações37 slides
Introduction to c programming por
Introduction to c programmingIntroduction to c programming
Introduction to c programmingABHISHEK fulwadhwa
4.6K visualizações74 slides
C basics por
C   basicsC   basics
C basicsthirumalaikumar3
1.7K visualizações25 slides
Introduction to problem solving in C por
Introduction to problem solving in CIntroduction to problem solving in C
Introduction to problem solving in CDiwakar Pratap Singh 'Deva'
26.9K visualizações23 slides
C fundamental por
C fundamentalC fundamental
C fundamentalSelvam Edwin
3.1K visualizações79 slides

Mais conteúdo relacionado

Mais procurados

Unit 4 Foc por
Unit 4 FocUnit 4 Foc
Unit 4 FocJAYA
3.1K visualizações164 slides
C Tokens por
C TokensC Tokens
C TokensRipon Hossain
983 visualizações13 slides
Programming in c por
Programming in cProgramming in c
Programming in cindra Kishor
10.9K visualizações157 slides
Introduction to c++ ppt 1 por
Introduction to c++ ppt 1Introduction to c++ ppt 1
Introduction to c++ ppt 1Prof. Dr. K. Adisesha
2.8K visualizações65 slides
Loops in C por
Loops in CLoops in C
Loops in CKamal Acharya
10.9K visualizações34 slides
Structure of a C program por
Structure of a C programStructure of a C program
Structure of a C programDavid Livingston J
22.4K visualizações16 slides

Mais procurados(20)

Unit 4 Foc por JAYA
Unit 4 FocUnit 4 Foc
Unit 4 Foc
JAYA3.1K visualizações
C Tokens por Ripon Hossain
C TokensC Tokens
C Tokens
Ripon Hossain983 visualizações
Programming in c por indra Kishor
Programming in cProgramming in c
Programming in c
indra Kishor10.9K visualizações
Introduction to c++ ppt 1 por Prof. Dr. K. Adisesha
Introduction to c++ ppt 1Introduction to c++ ppt 1
Introduction to c++ ppt 1
Prof. Dr. K. Adisesha2.8K visualizações
Loops in C por Kamal Acharya
Loops in CLoops in C
Loops in C
Kamal Acharya10.9K visualizações
Structure of a C program por David Livingston J
Structure of a C programStructure of a C program
Structure of a C program
David Livingston J22.4K visualizações
C Programming: Control Structure por Sokngim Sa
C Programming: Control StructureC Programming: Control Structure
C Programming: Control Structure
Sokngim Sa14.1K visualizações
Introduction to c programming por Manoj Tyagi
Introduction to c programmingIntroduction to c programming
Introduction to c programming
Manoj Tyagi4.7K visualizações
Unit 1. Problem Solving with Computer por Ashim Lamichhane
Unit 1. Problem Solving with Computer   Unit 1. Problem Solving with Computer
Unit 1. Problem Solving with Computer
Ashim Lamichhane35.3K visualizações
Looping statements in C por Jeya Lakshmi
Looping statements in CLooping statements in C
Looping statements in C
Jeya Lakshmi9K visualizações
C++ ppt por parpan34
C++ pptC++ ppt
C++ ppt
parpan3445.7K visualizações
Introduction to C Language por Tarun Sharma
Introduction to C LanguageIntroduction to C Language
Introduction to C Language
Tarun Sharma1.4K visualizações
Variables in C Programming por programming9
Variables in C ProgrammingVariables in C Programming
Variables in C Programming
programming99.1K visualizações
Lecture 01 introduction to compiler por Iffat Anjum
Lecture 01 introduction to compilerLecture 01 introduction to compiler
Lecture 01 introduction to compiler
Iffat Anjum19.9K visualizações
Jumping statements por Suneel Dogra
Jumping statementsJumping statements
Jumping statements
Suneel Dogra7.1K visualizações
Algorithm and flowchart por Rabin BK
Algorithm and flowchartAlgorithm and flowchart
Algorithm and flowchart
Rabin BK10.8K visualizações
Loops in C Programming Language por Mahantesh Devoor
Loops in C Programming LanguageLoops in C Programming Language
Loops in C Programming Language
Mahantesh Devoor21.8K visualizações

Similar a C Programming Unit-1

Unit 2 introduction to c programming por
Unit 2   introduction to c programmingUnit 2   introduction to c programming
Unit 2 introduction to c programmingMithun DSouza
302 visualizações39 slides
Introduction%20C.pptx por
Introduction%20C.pptxIntroduction%20C.pptx
Introduction%20C.pptx20EUEE018DEEPAKM
10 visualizações101 slides
Introduction to C Unit 1 por
Introduction to C Unit 1Introduction to C Unit 1
Introduction to C Unit 1SURBHI SAROHA
282 visualizações32 slides
1. introduction to computer por
1. introduction to computer1. introduction to computer
1. introduction to computerShankar Gangaju
105 visualizações8 slides
C language por
C language C language
C language COMSATS Institute of Information Technology
946 visualizações38 slides
C programming por
C programmingC programming
C programmingPralhadKhanal1
85 visualizações63 slides

Similar a C Programming Unit-1(20)

Unit 2 introduction to c programming por Mithun DSouza
Unit 2   introduction to c programmingUnit 2   introduction to c programming
Unit 2 introduction to c programming
Mithun DSouza302 visualizações
Introduction%20C.pptx por 20EUEE018DEEPAKM
Introduction%20C.pptxIntroduction%20C.pptx
Introduction%20C.pptx
20EUEE018DEEPAKM10 visualizações
Introduction to C Unit 1 por SURBHI SAROHA
Introduction to C Unit 1Introduction to C Unit 1
Introduction to C Unit 1
SURBHI SAROHA282 visualizações
1. introduction to computer por Shankar Gangaju
1. introduction to computer1. introduction to computer
1. introduction to computer
Shankar Gangaju105 visualizações
C programming por PralhadKhanal1
C programmingC programming
C programming
PralhadKhanal185 visualizações
Introduction to C Programming por MOHAMAD NOH AHMAD
Introduction to C ProgrammingIntroduction to C Programming
Introduction to C Programming
MOHAMAD NOH AHMAD1.9K visualizações
C prog ppt por xinoe
C prog pptC prog ppt
C prog ppt
xinoe1.8K visualizações
C language introduction geeksfor geeks por AashutoshChhedavi
C language introduction   geeksfor geeksC language introduction   geeksfor geeks
C language introduction geeksfor geeks
AashutoshChhedavi328 visualizações
Chapter3 por Kamran
Chapter3Chapter3
Chapter3
Kamran1.3K visualizações
C++ programming language basic to advance level por sajjad ali khan
C++ programming language basic to advance levelC++ programming language basic to advance level
C++ programming language basic to advance level
sajjad ali khan83 visualizações
C programming por Rounak Samdadia
C programmingC programming
C programming
Rounak Samdadia677 visualizações
C programming notes.pdf por AdiseshaK
C programming notes.pdfC programming notes.pdf
C programming notes.pdf
AdiseshaK1.7K visualizações
C basics 4 std11(GujBoard) por indrasir
C basics 4 std11(GujBoard)C basics 4 std11(GujBoard)
C basics 4 std11(GujBoard)
indrasir1.8K visualizações
C programming Training in Ambala ! Batra Computer Centre por jatin batra
C programming Training in Ambala ! Batra Computer CentreC programming Training in Ambala ! Batra Computer Centre
C programming Training in Ambala ! Batra Computer Centre
jatin batra259 visualizações
C programming course material por Ranjitha Murthy
C programming course materialC programming course material
C programming course material
Ranjitha Murthy277 visualizações
CProgrammingTutorial por Muthuselvam RS
CProgrammingTutorialCProgrammingTutorial
CProgrammingTutorial
Muthuselvam RS4K visualizações

Mais de Vikram Nandini

IoT: From Copper strip to Gold Bar por
IoT: From Copper strip to Gold BarIoT: From Copper strip to Gold Bar
IoT: From Copper strip to Gold BarVikram Nandini
16 visualizações50 slides
Design Patterns por
Design PatternsDesign Patterns
Design PatternsVikram Nandini
131 visualizações32 slides
Linux File Trees and Commands por
Linux File Trees and CommandsLinux File Trees and Commands
Linux File Trees and CommandsVikram Nandini
120 visualizações17 slides
Introduction to Linux & Basic Commands por
Introduction to Linux & Basic CommandsIntroduction to Linux & Basic Commands
Introduction to Linux & Basic CommandsVikram Nandini
343 visualizações25 slides
INTRODUCTION to OOAD por
INTRODUCTION to OOADINTRODUCTION to OOAD
INTRODUCTION to OOADVikram Nandini
482 visualizações23 slides
Ethics por
EthicsEthics
EthicsVikram Nandini
47 visualizações12 slides

Mais de Vikram Nandini(20)

IoT: From Copper strip to Gold Bar por Vikram Nandini
IoT: From Copper strip to Gold BarIoT: From Copper strip to Gold Bar
IoT: From Copper strip to Gold Bar
Vikram Nandini16 visualizações
Design Patterns por Vikram Nandini
Design PatternsDesign Patterns
Design Patterns
Vikram Nandini131 visualizações
Linux File Trees and Commands por Vikram Nandini
Linux File Trees and CommandsLinux File Trees and Commands
Linux File Trees and Commands
Vikram Nandini120 visualizações
Introduction to Linux & Basic Commands por Vikram Nandini
Introduction to Linux & Basic CommandsIntroduction to Linux & Basic Commands
Introduction to Linux & Basic Commands
Vikram Nandini343 visualizações
INTRODUCTION to OOAD por Vikram Nandini
INTRODUCTION to OOADINTRODUCTION to OOAD
INTRODUCTION to OOAD
Vikram Nandini482 visualizações
Ethics por Vikram Nandini
EthicsEthics
Ethics
Vikram Nandini47 visualizações
Manufacturing - II Part por Vikram Nandini
Manufacturing - II PartManufacturing - II Part
Manufacturing - II Part
Vikram Nandini39 visualizações
Manufacturing por Vikram Nandini
ManufacturingManufacturing
Manufacturing
Vikram Nandini29 visualizações
Business Models por Vikram Nandini
Business ModelsBusiness Models
Business Models
Vikram Nandini56 visualizações
Prototyping Online Components por Vikram Nandini
Prototyping Online ComponentsPrototyping Online Components
Prototyping Online Components
Vikram Nandini59 visualizações
Artificial Neural Networks por Vikram Nandini
Artificial Neural NetworksArtificial Neural Networks
Artificial Neural Networks
Vikram Nandini105 visualizações
IoT-Prototyping por Vikram Nandini
IoT-PrototypingIoT-Prototyping
IoT-Prototyping
Vikram Nandini140 visualizações
Design Principles for Connected Devices por Vikram Nandini
Design Principles for Connected DevicesDesign Principles for Connected Devices
Design Principles for Connected Devices
Vikram Nandini215 visualizações
Introduction to IoT por Vikram Nandini
Introduction to IoTIntroduction to IoT
Introduction to IoT
Vikram Nandini284 visualizações
Embedded decices por Vikram Nandini
Embedded decicesEmbedded decices
Embedded decices
Vikram Nandini66 visualizações
Communication in the IoT por Vikram Nandini
Communication in the IoTCommunication in the IoT
Communication in the IoT
Vikram Nandini69 visualizações
Introduction to Cyber Security por Vikram Nandini
Introduction to Cyber SecurityIntroduction to Cyber Security
Introduction to Cyber Security
Vikram Nandini444 visualizações
cloud computing UNIT-2.pdf por Vikram Nandini
cloud computing UNIT-2.pdfcloud computing UNIT-2.pdf
cloud computing UNIT-2.pdf
Vikram Nandini525 visualizações
Introduction to Web Technologies por Vikram Nandini
Introduction to Web TechnologiesIntroduction to Web Technologies
Introduction to Web Technologies
Vikram Nandini200 visualizações
Cascading Style Sheets por Vikram Nandini
Cascading Style SheetsCascading Style Sheets
Cascading Style Sheets
Vikram Nandini116 visualizações

Último

JQUERY.pdf por
JQUERY.pdfJQUERY.pdf
JQUERY.pdfArthyR3
103 visualizações22 slides
ANGULARJS.pdf por
ANGULARJS.pdfANGULARJS.pdf
ANGULARJS.pdfArthyR3
49 visualizações10 slides
MercerJesse2.1Doc.pdf por
MercerJesse2.1Doc.pdfMercerJesse2.1Doc.pdf
MercerJesse2.1Doc.pdfjessemercerail
301 visualizações5 slides
Mineral nutrition and Fertilizer use of Cashew por
 Mineral nutrition and Fertilizer use of Cashew Mineral nutrition and Fertilizer use of Cashew
Mineral nutrition and Fertilizer use of CashewAruna Srikantha Jayawardana
53 visualizações107 slides
Java Simplified: Understanding Programming Basics por
Java Simplified: Understanding Programming BasicsJava Simplified: Understanding Programming Basics
Java Simplified: Understanding Programming BasicsAkshaj Vadakkath Joshy
625 visualizações155 slides
A Guide to Applying for the Wells Mountain Initiative Scholarship 2023 por
A Guide to Applying for the Wells Mountain Initiative Scholarship 2023A Guide to Applying for the Wells Mountain Initiative Scholarship 2023
A Guide to Applying for the Wells Mountain Initiative Scholarship 2023Excellence Foundation for South Sudan
79 visualizações26 slides

Último(20)

JQUERY.pdf por ArthyR3
JQUERY.pdfJQUERY.pdf
JQUERY.pdf
ArthyR3103 visualizações
ANGULARJS.pdf por ArthyR3
ANGULARJS.pdfANGULARJS.pdf
ANGULARJS.pdf
ArthyR349 visualizações
MercerJesse2.1Doc.pdf por jessemercerail
MercerJesse2.1Doc.pdfMercerJesse2.1Doc.pdf
MercerJesse2.1Doc.pdf
jessemercerail301 visualizações
Java Simplified: Understanding Programming Basics por Akshaj Vadakkath Joshy
Java Simplified: Understanding Programming BasicsJava Simplified: Understanding Programming Basics
Java Simplified: Understanding Programming Basics
Akshaj Vadakkath Joshy625 visualizações
Parts of Speech (1).pptx por mhkpreet001
Parts of Speech (1).pptxParts of Speech (1).pptx
Parts of Speech (1).pptx
mhkpreet00143 visualizações
Education of marginalized and socially disadvantages segments.pptx por GarimaBhati5
Education of marginalized and socially disadvantages segments.pptxEducation of marginalized and socially disadvantages segments.pptx
Education of marginalized and socially disadvantages segments.pptx
GarimaBhati540 visualizações
Meet the Bible por Steve Thomason
Meet the BibleMeet the Bible
Meet the Bible
Steve Thomason76 visualizações
Create a Structure in VBNet.pptx por Breach_P
Create a Structure in VBNet.pptxCreate a Structure in VBNet.pptx
Create a Structure in VBNet.pptx
Breach_P82 visualizações
Payment Integration using Braintree Connector | MuleSoft Mysore Meetup #37 por MysoreMuleSoftMeetup
Payment Integration using Braintree Connector | MuleSoft Mysore Meetup #37Payment Integration using Braintree Connector | MuleSoft Mysore Meetup #37
Payment Integration using Braintree Connector | MuleSoft Mysore Meetup #37
MysoreMuleSoftMeetup44 visualizações
Gross Anatomy of the Liver por obaje godwin sunday
Gross Anatomy of the LiverGross Anatomy of the Liver
Gross Anatomy of the Liver
obaje godwin sunday74 visualizações
Creative Restart 2023: Atila Martins - Craft: A Necessity, Not a Choice por Taste
Creative Restart 2023: Atila Martins - Craft: A Necessity, Not a ChoiceCreative Restart 2023: Atila Martins - Craft: A Necessity, Not a Choice
Creative Restart 2023: Atila Martins - Craft: A Necessity, Not a Choice
Taste41 visualizações
BÀI TẬP BỔ TRỢ TIẾNG ANH 11 THEO ĐƠN VỊ BÀI HỌC - CẢ NĂM - CÓ FILE NGHE (FRIE... por Nguyen Thanh Tu Collection
BÀI TẬP BỔ TRỢ TIẾNG ANH 11 THEO ĐƠN VỊ BÀI HỌC - CẢ NĂM - CÓ FILE NGHE (FRIE...BÀI TẬP BỔ TRỢ TIẾNG ANH 11 THEO ĐƠN VỊ BÀI HỌC - CẢ NĂM - CÓ FILE NGHE (FRIE...
BÀI TẬP BỔ TRỢ TIẾNG ANH 11 THEO ĐƠN VỊ BÀI HỌC - CẢ NĂM - CÓ FILE NGHE (FRIE...
Nguyen Thanh Tu Collection71 visualizações
12.5.23 Poverty and Precarity.pptx por mary850239
12.5.23 Poverty and Precarity.pptx12.5.23 Poverty and Precarity.pptx
12.5.23 Poverty and Precarity.pptx
mary850239162 visualizações
Jibachha publishing Textbook.docx por DrJibachhaSahVetphys
Jibachha publishing Textbook.docxJibachha publishing Textbook.docx
Jibachha publishing Textbook.docx
DrJibachhaSahVetphys54 visualizações
The Accursed House by Émile Gaboriau por DivyaSheta
The Accursed House  by Émile GaboriauThe Accursed House  by Émile Gaboriau
The Accursed House by Émile Gaboriau
DivyaSheta246 visualizações
11.30.23A Poverty and Inequality in America.pptx por mary850239
11.30.23A Poverty and Inequality in America.pptx11.30.23A Poverty and Inequality in America.pptx
11.30.23A Poverty and Inequality in America.pptx
mary85023986 visualizações
Pharmaceutical Analysis PPT (BP 102T) por yakshpharmacy009
Pharmaceutical Analysis PPT (BP 102T) Pharmaceutical Analysis PPT (BP 102T)
Pharmaceutical Analysis PPT (BP 102T)
yakshpharmacy009101 visualizações

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