SlideShare a Scribd company logo
C-Programming
Walchand Institute of Technology (RC1131), Solapur Page 1
Handout#3
Assignment/Program Statement:
Write a C program using variables, constants, data types, expressions and applying
type casting and type conversion rules.
Learning Objectives:
Students will be able to
- explain basic concepts of C such as variables, constants, data types
- write C code using variables, constants, data types, expressions
- apply type casting and type conversion rules in C
Theory:
What is Program?
A program is a sequence of instructions (called programming statements),
executing one after another - usually in a sequential manner
Variable:
 In programming, a variable is a container (storage area) to hold data.
 To indicate the storage area, each variable should be given a unique name
(identifier).
 Variable names are just the symbolic representation of a memory location.
 For example: int marks = 65;
 In this example, "marks" is a variable of integer type. The variable is holding
value 65.
 The value of a variable can be changed, hence the name 'variable'.
 In C programming, you have to declare a variable before you can use it.
65
marks
Memory Representation
C-Programming
Walchand Institute of Technology (RC1131), Solapur Page 2
Constants/Literals:
 A constant is a value or an identifier whose value cannot be altered in a
program.
 For example:
const double PI = 3.14
Here, PI is a constant. Basically what it means is that, PI and 3.14 is same
for this program.
 Following are the types of constants
1) Integer constants
2) Floating-point constants
3) Character constants
4) String constants
[Reference: http://www.programiz.com/c-programming/c-variables-constants ]
Data Types in C:
 Data types simply refer to the type and size of data associated with variables
and functions.
 The type of a variable determines how much space it occupies in storage and
how the bit pattern stored is interpreted.
 C language supports 2 different type of data types – (1) Primary
(Fundamental) data types and (2) Derived data types
(1) Primary data types:
o These are fundamental data types in C namely integer(int),
floating(float), character(char) and void.
(2) Derived data types
o Derived data types are like arrays, pointers, structures and
enumeration.
[Reference: http://www.tutorialspoint.com/cprogramming/c_data_types.htm ,
http://www.programiz.com/c-programming/c-data-types and
http://www.studytonight.com/c/datatype-in-c.php ]
C-Programming
Walchand Institute of Technology (RC1131), Solapur Page 3
Operators in C:
 An operator is a symbol that tells the compiler to perform specific
mathematical or logical functions.
 C language provides the following types of operators -
1. Arithmetic Operators
2. Relational Operators
3. Logical Operators
4. Bitwise Operators
5. Assignment Operators
6. Misc Operators
[1] Arithmetic Operators:
 The following table shows all the arithmetic operators supported by the C
language.
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
 Assume variable A holds 10 and variable B holds 20.
Program:
#include<stdio.h>
void main()
{
C-Programming
Walchand Institute of Technology (RC1131), Solapur Page 4
int A = 10, B =20, C;
C = A + B;
printf(“n C=%d”, C);
}
Output: C=30
[2] Relational Operators:
The following table shows all the relational operators supported by C.
Operator Description Example
== Checks if the values of two operands are equal or
not. If yes, then the condition becomes true.
(A == B) is not
true.
!= Checks if the values of two operands are equal or
not. If the values are not equal, then the
condition becomes true.
(A != B) is true.
> Checks if the value of left operand is greater than
the value of right operand. If yes, then the
condition becomes true.
(A > B) is not true.
< Checks if the value of left operand is less than
the value of right operand. If yes, then the
condition becomes true.
(A < B) is true.
>= Checks if the value of left operand is greater than
or equal to the value of right operand. If yes,
then the condition becomes true.
(A >= B) is not
true.
<= Checks if the value of left operand is less than or
equal to the value of right operand. If yes, then
the condition becomes true.
(A <= B) is true.
[3] Logical Operators:
Following table shows all the logical operators supported by C language.
C-Programming
Walchand Institute of Technology (RC1131), Solapur Page 5
Operator Description Example
&& Called Logical AND operator. If both the operands are
non-zero, then the condition becomes true.
(A && B)
is false.
|| Called Logical OR Operator. If any of the two operands
is non-zero, then the condition becomes true.
(A || B) is
true.
! Called Logical NOT Operator. It is used to reverse the
logical state of its operand. If a condition is true, then
Logical NOT operator will make it false.
!(A && B)
is true.
[Reference: http://www.tutorialspoint.com/cprogramming/c_operators.htm ]
Type Conversion and Type casting in C
 Type conversion occurs when the expression has data of mixed data types.
 Examples of such expression include converting an integer value in to a float
value, or assigning the value of the expression to a variable with different
data type.
For example: int sum=17, count=5;
float mean;
mean = sum/count;
 In type conversion, the data type is promoted from lower to higher because
converting higher to lower involves loss of precision and value.
Forced Conversion:
 Forced conversion occurs when we are converting the value of the larger
data type to the value of the smaller data type or smaller data type to the
larger data type.
For example, consider the following assignment statement
int a;
float b;
a=5.5;
b=100;
C-Programming
Walchand Institute of Technology (RC1131), Solapur Page 6
 Case-1: a=5.5 ; a is declared as int so the float value 5.5 cannot be stored in
a. In such a case float is demoted to an int and then its value is stored. Hence
5 is stored in a.
 Case-2: b=100; since b is a float variable 100 is promoted to 100.000000
and then stored in b.
In general, the value of the expression is promoted or demoted depending on the
type of variable on left hand side of =.
Consider the following statement
int count = 5;
float sum = 17.5, mean;
mean = sum / count;
 In the above statement, one operand is int where as other is float. During
evaluation of the expression the int would be promoted to floats and the
result of the expression would be a float. And result float value is assigned to
float mean variable. If mean declared as int then result will be demoted to int
type.
 Forced conversion may decrease the precision.
 Type casting is the preferred method of forced conversion
[Reference: http://datastructuresprogramming.blogspot.in/2010/02/type-
conversion-and-type-casting-in-c.html]
Type Casting (or) Explicit Type conversion:
 Explicit type conversions can be forced in any expression, with a unary
operator called a cast.
 Type casting is a way to convert a variable from one data type to another
data type.
Syntax:
(type-name) expression;
Example:
int n=5.5;
C-Programming
Walchand Institute of Technology (RC1131), Solapur Page 7
float x;
x=(float)n;
printf(“n X=%f”,x);
The above statement will convert the value of n to a float value before assigning to
x but n is not altered.
Program:
main() {
int sum = 17, count = 5;
float mean;
mean = (float) sum / count;
printf("n Mean = %f ", mean );
}
Output: Mean = 3.000000 /*without typecasting*/
Output: Mean = 3.400000 /*with typecasting*/
Conclusion:
Thus C programs, using variables, constants, data types, expressions and applying
type casting and type conversion rules, is implemented.
Learning Outcomes:
At the end of this assignment, students are able to
- explain basic concepts of C such as variables, constants, data types
- write C code using variables, constants, data types, expressions
- apply type casting and type conversion rules in C

More Related Content

What's hot

CP Handout#7
CP Handout#7CP Handout#7
CP Handout#7
trupti1976
 
Learning the C Language
Learning the C LanguageLearning the C Language
Learning the C Language
nTier Custom Solutions
 
VTU PCD Model Question Paper - Programming in C
VTU PCD Model Question Paper - Programming in CVTU PCD Model Question Paper - Programming in C
VTU PCD Model Question Paper - Programming in C
Syed Mustafa
 
VTU 1ST SEM PROGRAMMING IN C & DATA STRUCTURES SOLVED PAPERS OF JUNE-2015 & ...
VTU 1ST SEM  PROGRAMMING IN C & DATA STRUCTURES SOLVED PAPERS OF JUNE-2015 & ...VTU 1ST SEM  PROGRAMMING IN C & DATA STRUCTURES SOLVED PAPERS OF JUNE-2015 & ...
VTU 1ST SEM PROGRAMMING IN C & DATA STRUCTURES SOLVED PAPERS OF JUNE-2015 & ...
vtunotesbysree
 
Learn C
Learn CLearn C
Learn C
kantila
 
C fundamentals
C fundamentalsC fundamentals
answer-model-qp-15-pcd13pcd
answer-model-qp-15-pcd13pcdanswer-model-qp-15-pcd13pcd
answer-model-qp-15-pcd13pcd
Syed Mustafa
 
Operators in C Programming
Operators in C ProgrammingOperators in C Programming
Operators in C Programming
Jasleen Kaur (Chandigarh University)
 
C# operators
C# operatorsC# operators
Ocs752 unit 2
Ocs752   unit 2Ocs752   unit 2
Ocs752 unit 2
mgrameshmail
 
Ocs752 unit 1
Ocs752   unit 1Ocs752   unit 1
Ocs752 unit 1
mgrameshmail
 
C Token’s
C Token’sC Token’s
C Token’s
Tarun Sharma
 
Presentation 2
Presentation 2Presentation 2
Ocs752 unit 3
Ocs752   unit 3Ocs752   unit 3
Ocs752 unit 3
mgrameshmail
 
Sample for Simple C Program - R.D.Sivakumar
Sample for Simple C Program - R.D.SivakumarSample for Simple C Program - R.D.Sivakumar
Sample for Simple C Program - R.D.Sivakumar
Sivakumar R D .
 
C Programming Unit-2
C Programming Unit-2C Programming Unit-2
C Programming Unit-2
Vikram Nandini
 
What is c
What is cWhat is c
What is c
Nitesh Saitwal
 
Ocs752 unit 4
Ocs752   unit 4Ocs752   unit 4
Ocs752 unit 4
mgrameshmail
 

What's hot (20)

CP Handout#7
CP Handout#7CP Handout#7
CP Handout#7
 
Learning the C Language
Learning the C LanguageLearning the C Language
Learning the C Language
 
VTU PCD Model Question Paper - Programming in C
VTU PCD Model Question Paper - Programming in CVTU PCD Model Question Paper - Programming in C
VTU PCD Model Question Paper - Programming in C
 
C Programming
C ProgrammingC Programming
C Programming
 
VTU 1ST SEM PROGRAMMING IN C & DATA STRUCTURES SOLVED PAPERS OF JUNE-2015 & ...
VTU 1ST SEM  PROGRAMMING IN C & DATA STRUCTURES SOLVED PAPERS OF JUNE-2015 & ...VTU 1ST SEM  PROGRAMMING IN C & DATA STRUCTURES SOLVED PAPERS OF JUNE-2015 & ...
VTU 1ST SEM PROGRAMMING IN C & DATA STRUCTURES SOLVED PAPERS OF JUNE-2015 & ...
 
Learn C
Learn CLearn C
Learn C
 
C fundamentals
C fundamentalsC fundamentals
C fundamentals
 
answer-model-qp-15-pcd13pcd
answer-model-qp-15-pcd13pcdanswer-model-qp-15-pcd13pcd
answer-model-qp-15-pcd13pcd
 
Operators in C Programming
Operators in C ProgrammingOperators in C Programming
Operators in C Programming
 
C# operators
C# operatorsC# operators
C# operators
 
Ocs752 unit 2
Ocs752   unit 2Ocs752   unit 2
Ocs752 unit 2
 
Ocs752 unit 1
Ocs752   unit 1Ocs752   unit 1
Ocs752 unit 1
 
Unit 1
Unit 1Unit 1
Unit 1
 
C Token’s
C Token’sC Token’s
C Token’s
 
Presentation 2
Presentation 2Presentation 2
Presentation 2
 
Ocs752 unit 3
Ocs752   unit 3Ocs752   unit 3
Ocs752 unit 3
 
Sample for Simple C Program - R.D.Sivakumar
Sample for Simple C Program - R.D.SivakumarSample for Simple C Program - R.D.Sivakumar
Sample for Simple C Program - R.D.Sivakumar
 
C Programming Unit-2
C Programming Unit-2C Programming Unit-2
C Programming Unit-2
 
What is c
What is cWhat is c
What is c
 
Ocs752 unit 4
Ocs752   unit 4Ocs752   unit 4
Ocs752 unit 4
 

Similar to CP Handout#3

C basics
C basicsC basics
C basics
sridevi5983
 
C basics
C basicsC basics
C basics
sridevi5983
 
C Operators and Control Structures.pdf
C Operators and Control Structures.pdfC Operators and Control Structures.pdf
C Operators and Control Structures.pdf
MaryJacob24
 
C programming
C programming C programming
C programming
DipjualGiri1
 
Fundamentals of c language
Fundamentals of c languageFundamentals of c language
Fundamentals of c language
AkshhayPatel
 
presentation_data_types_and_operators_1513499834_241350.pptx
presentation_data_types_and_operators_1513499834_241350.pptxpresentation_data_types_and_operators_1513499834_241350.pptx
presentation_data_types_and_operators_1513499834_241350.pptx
KrishanPalSingh39
 
comp 122 Chapter 2.pptx,language semantics
comp 122 Chapter 2.pptx,language semanticscomp 122 Chapter 2.pptx,language semantics
comp 122 Chapter 2.pptx,language semantics
floraaluoch3
 
C sharp part 001
C sharp part 001C sharp part 001
C sharp part 001
Ralph Weber
 
Data Types, Variables, and Constants in C# Programming
Data Types, Variables, and Constants in C# ProgrammingData Types, Variables, and Constants in C# Programming
Data Types, Variables, and Constants in C# Programming
Sherwin Banaag Sapin
 
C program
C programC program
C program
AJAL A J
 
java or oops class not in kerala polytechnic 4rth semester nots j
java or oops class not in kerala polytechnic  4rth semester nots jjava or oops class not in kerala polytechnic  4rth semester nots j
java or oops class not in kerala polytechnic 4rth semester nots j
ishorishore
 
Programming Fundamentals lecture 7
Programming Fundamentals lecture 7Programming Fundamentals lecture 7
Programming Fundamentals lecture 7
REHAN IJAZ
 
C programming | Class 8 | III Term
C programming  | Class 8  | III TermC programming  | Class 8  | III Term
C programming | Class 8 | III TermAndrew Raj
 
C UNIT-2 PREPARED Y M V BRAHMANANDA REDDY
C UNIT-2 PREPARED Y M V BRAHMANANDA REDDYC UNIT-2 PREPARED Y M V BRAHMANANDA REDDY
C UNIT-2 PREPARED Y M V BRAHMANANDA REDDYRajeshkumar Reddy
 
3110003_PPS_GTU_Study_Material_Presentations_Unit-2_18122020041700AM (1).pptx
3110003_PPS_GTU_Study_Material_Presentations_Unit-2_18122020041700AM (1).pptx3110003_PPS_GTU_Study_Material_Presentations_Unit-2_18122020041700AM (1).pptx
3110003_PPS_GTU_Study_Material_Presentations_Unit-2_18122020041700AM (1).pptx
LeenaChaudhari24
 
PSPC--UNIT-2.pdf
PSPC--UNIT-2.pdfPSPC--UNIT-2.pdf
PSPC--UNIT-2.pdf
ArshiniGubbala3
 
Type Conversion, Precedence and Associativity
Type Conversion, Precedence and AssociativityType Conversion, Precedence and Associativity
Type Conversion, Precedence and Associativity
Aakash Singh
 
C Language Interview Questions: Data Types, Pointers, Data Structures, Memory...
C Language Interview Questions: Data Types, Pointers, Data Structures, Memory...C Language Interview Questions: Data Types, Pointers, Data Structures, Memory...
C Language Interview Questions: Data Types, Pointers, Data Structures, Memory...
Rowank2
 
COM1407: Type Casting, Command Line Arguments and Defining Constants
COM1407: Type Casting, Command Line Arguments and Defining Constants COM1407: Type Casting, Command Line Arguments and Defining Constants
COM1407: Type Casting, Command Line Arguments and Defining Constants
Hemantha Kulathilake
 
unit 1 (1).pptx
unit 1 (1).pptxunit 1 (1).pptx
unit 1 (1).pptx
PriyadarshiniS28
 

Similar to CP Handout#3 (20)

C basics
C basicsC basics
C basics
 
C basics
C basicsC basics
C basics
 
C Operators and Control Structures.pdf
C Operators and Control Structures.pdfC Operators and Control Structures.pdf
C Operators and Control Structures.pdf
 
C programming
C programming C programming
C programming
 
Fundamentals of c language
Fundamentals of c languageFundamentals of c language
Fundamentals of c language
 
presentation_data_types_and_operators_1513499834_241350.pptx
presentation_data_types_and_operators_1513499834_241350.pptxpresentation_data_types_and_operators_1513499834_241350.pptx
presentation_data_types_and_operators_1513499834_241350.pptx
 
comp 122 Chapter 2.pptx,language semantics
comp 122 Chapter 2.pptx,language semanticscomp 122 Chapter 2.pptx,language semantics
comp 122 Chapter 2.pptx,language semantics
 
C sharp part 001
C sharp part 001C sharp part 001
C sharp part 001
 
Data Types, Variables, and Constants in C# Programming
Data Types, Variables, and Constants in C# ProgrammingData Types, Variables, and Constants in C# Programming
Data Types, Variables, and Constants in C# Programming
 
C program
C programC program
C program
 
java or oops class not in kerala polytechnic 4rth semester nots j
java or oops class not in kerala polytechnic  4rth semester nots jjava or oops class not in kerala polytechnic  4rth semester nots j
java or oops class not in kerala polytechnic 4rth semester nots j
 
Programming Fundamentals lecture 7
Programming Fundamentals lecture 7Programming Fundamentals lecture 7
Programming Fundamentals lecture 7
 
C programming | Class 8 | III Term
C programming  | Class 8  | III TermC programming  | Class 8  | III Term
C programming | Class 8 | III Term
 
C UNIT-2 PREPARED Y M V BRAHMANANDA REDDY
C UNIT-2 PREPARED Y M V BRAHMANANDA REDDYC UNIT-2 PREPARED Y M V BRAHMANANDA REDDY
C UNIT-2 PREPARED Y M V BRAHMANANDA REDDY
 
3110003_PPS_GTU_Study_Material_Presentations_Unit-2_18122020041700AM (1).pptx
3110003_PPS_GTU_Study_Material_Presentations_Unit-2_18122020041700AM (1).pptx3110003_PPS_GTU_Study_Material_Presentations_Unit-2_18122020041700AM (1).pptx
3110003_PPS_GTU_Study_Material_Presentations_Unit-2_18122020041700AM (1).pptx
 
PSPC--UNIT-2.pdf
PSPC--UNIT-2.pdfPSPC--UNIT-2.pdf
PSPC--UNIT-2.pdf
 
Type Conversion, Precedence and Associativity
Type Conversion, Precedence and AssociativityType Conversion, Precedence and Associativity
Type Conversion, Precedence and Associativity
 
C Language Interview Questions: Data Types, Pointers, Data Structures, Memory...
C Language Interview Questions: Data Types, Pointers, Data Structures, Memory...C Language Interview Questions: Data Types, Pointers, Data Structures, Memory...
C Language Interview Questions: Data Types, Pointers, Data Structures, Memory...
 
COM1407: Type Casting, Command Line Arguments and Defining Constants
COM1407: Type Casting, Command Line Arguments and Defining Constants COM1407: Type Casting, Command Line Arguments and Defining Constants
COM1407: Type Casting, Command Line Arguments and Defining Constants
 
unit 1 (1).pptx
unit 1 (1).pptxunit 1 (1).pptx
unit 1 (1).pptx
 

More from trupti1976

MobileAppDev Handout#3
MobileAppDev Handout#3MobileAppDev Handout#3
MobileAppDev Handout#3
trupti1976
 
MobileAppDev Handout#10
MobileAppDev Handout#10MobileAppDev Handout#10
MobileAppDev Handout#10
trupti1976
 
MobileAppDev Handout#9
MobileAppDev Handout#9MobileAppDev Handout#9
MobileAppDev Handout#9
trupti1976
 
MobileAppDev Handout#8
MobileAppDev Handout#8MobileAppDev Handout#8
MobileAppDev Handout#8
trupti1976
 
MobileAppDev Handout#7
MobileAppDev Handout#7MobileAppDev Handout#7
MobileAppDev Handout#7
trupti1976
 
MobileAppDev Handout#6
MobileAppDev Handout#6MobileAppDev Handout#6
MobileAppDev Handout#6
trupti1976
 
MobileAppDev Handout#5
MobileAppDev Handout#5MobileAppDev Handout#5
MobileAppDev Handout#5
trupti1976
 
MobileAppDev Handout#4
MobileAppDev Handout#4MobileAppDev Handout#4
MobileAppDev Handout#4
trupti1976
 
MobileAppDev Handout#2
MobileAppDev Handout#2MobileAppDev Handout#2
MobileAppDev Handout#2
trupti1976
 
MobileAppDev Handout#1
MobileAppDev Handout#1MobileAppDev Handout#1
MobileAppDev Handout#1
trupti1976
 

More from trupti1976 (10)

MobileAppDev Handout#3
MobileAppDev Handout#3MobileAppDev Handout#3
MobileAppDev Handout#3
 
MobileAppDev Handout#10
MobileAppDev Handout#10MobileAppDev Handout#10
MobileAppDev Handout#10
 
MobileAppDev Handout#9
MobileAppDev Handout#9MobileAppDev Handout#9
MobileAppDev Handout#9
 
MobileAppDev Handout#8
MobileAppDev Handout#8MobileAppDev Handout#8
MobileAppDev Handout#8
 
MobileAppDev Handout#7
MobileAppDev Handout#7MobileAppDev Handout#7
MobileAppDev Handout#7
 
MobileAppDev Handout#6
MobileAppDev Handout#6MobileAppDev Handout#6
MobileAppDev Handout#6
 
MobileAppDev Handout#5
MobileAppDev Handout#5MobileAppDev Handout#5
MobileAppDev Handout#5
 
MobileAppDev Handout#4
MobileAppDev Handout#4MobileAppDev Handout#4
MobileAppDev Handout#4
 
MobileAppDev Handout#2
MobileAppDev Handout#2MobileAppDev Handout#2
MobileAppDev Handout#2
 
MobileAppDev Handout#1
MobileAppDev Handout#1MobileAppDev Handout#1
MobileAppDev Handout#1
 

Recently uploaded

一比一原版(SFU毕业证)西蒙菲莎大学毕业证成绩单如何办理
一比一原版(SFU毕业证)西蒙菲莎大学毕业证成绩单如何办理一比一原版(SFU毕业证)西蒙菲莎大学毕业证成绩单如何办理
一比一原版(SFU毕业证)西蒙菲莎大学毕业证成绩单如何办理
bakpo1
 
Forklift Classes Overview by Intella Parts
Forklift Classes Overview by Intella PartsForklift Classes Overview by Intella Parts
Forklift Classes Overview by Intella Parts
Intella Parts
 
Pile Foundation by Venkatesh Taduvai (Sub Geotechnical Engineering II)-conver...
Pile Foundation by Venkatesh Taduvai (Sub Geotechnical Engineering II)-conver...Pile Foundation by Venkatesh Taduvai (Sub Geotechnical Engineering II)-conver...
Pile Foundation by Venkatesh Taduvai (Sub Geotechnical Engineering II)-conver...
AJAYKUMARPUND1
 
Student information management system project report ii.pdf
Student information management system project report ii.pdfStudent information management system project report ii.pdf
Student information management system project report ii.pdf
Kamal Acharya
 
Event Management System Vb Net Project Report.pdf
Event Management System Vb Net  Project Report.pdfEvent Management System Vb Net  Project Report.pdf
Event Management System Vb Net Project Report.pdf
Kamal Acharya
 
CME397 Surface Engineering- Professional Elective
CME397 Surface Engineering- Professional ElectiveCME397 Surface Engineering- Professional Elective
CME397 Surface Engineering- Professional Elective
karthi keyan
 
LIGA(E)11111111111111111111111111111111111111111.ppt
LIGA(E)11111111111111111111111111111111111111111.pptLIGA(E)11111111111111111111111111111111111111111.ppt
LIGA(E)11111111111111111111111111111111111111111.ppt
ssuser9bd3ba
 
Standard Reomte Control Interface - Neometrix
Standard Reomte Control Interface - NeometrixStandard Reomte Control Interface - Neometrix
Standard Reomte Control Interface - Neometrix
Neometrix_Engineering_Pvt_Ltd
 
Final project report on grocery store management system..pdf
Final project report on grocery store management system..pdfFinal project report on grocery store management system..pdf
Final project report on grocery store management system..pdf
Kamal Acharya
 
J.Yang, ICLR 2024, MLILAB, KAIST AI.pdf
J.Yang,  ICLR 2024, MLILAB, KAIST AI.pdfJ.Yang,  ICLR 2024, MLILAB, KAIST AI.pdf
J.Yang, ICLR 2024, MLILAB, KAIST AI.pdf
MLILAB
 
Railway Signalling Principles Edition 3.pdf
Railway Signalling Principles Edition 3.pdfRailway Signalling Principles Edition 3.pdf
Railway Signalling Principles Edition 3.pdf
TeeVichai
 
Halogenation process of chemical process industries
Halogenation process of chemical process industriesHalogenation process of chemical process industries
Halogenation process of chemical process industries
MuhammadTufail242431
 
Cosmetic shop management system project report.pdf
Cosmetic shop management system project report.pdfCosmetic shop management system project report.pdf
Cosmetic shop management system project report.pdf
Kamal Acharya
 
weather web application report.pdf
weather web application report.pdfweather web application report.pdf
weather web application report.pdf
Pratik Pawar
 
block diagram and signal flow graph representation
block diagram and signal flow graph representationblock diagram and signal flow graph representation
block diagram and signal flow graph representation
Divya Somashekar
 
Design and Analysis of Algorithms-DP,Backtracking,Graphs,B&B
Design and Analysis of Algorithms-DP,Backtracking,Graphs,B&BDesign and Analysis of Algorithms-DP,Backtracking,Graphs,B&B
Design and Analysis of Algorithms-DP,Backtracking,Graphs,B&B
Sreedhar Chowdam
 
Architectural Portfolio Sean Lockwood
Architectural Portfolio Sean LockwoodArchitectural Portfolio Sean Lockwood
Architectural Portfolio Sean Lockwood
seandesed
 
Gen AI Study Jams _ For the GDSC Leads in India.pdf
Gen AI Study Jams _ For the GDSC Leads in India.pdfGen AI Study Jams _ For the GDSC Leads in India.pdf
Gen AI Study Jams _ For the GDSC Leads in India.pdf
gdsczhcet
 
H.Seo, ICLR 2024, MLILAB, KAIST AI.pdf
H.Seo,  ICLR 2024, MLILAB,  KAIST AI.pdfH.Seo,  ICLR 2024, MLILAB,  KAIST AI.pdf
H.Seo, ICLR 2024, MLILAB, KAIST AI.pdf
MLILAB
 
TECHNICAL TRAINING MANUAL GENERAL FAMILIARIZATION COURSE
TECHNICAL TRAINING MANUAL   GENERAL FAMILIARIZATION COURSETECHNICAL TRAINING MANUAL   GENERAL FAMILIARIZATION COURSE
TECHNICAL TRAINING MANUAL GENERAL FAMILIARIZATION COURSE
DuvanRamosGarzon1
 

Recently uploaded (20)

一比一原版(SFU毕业证)西蒙菲莎大学毕业证成绩单如何办理
一比一原版(SFU毕业证)西蒙菲莎大学毕业证成绩单如何办理一比一原版(SFU毕业证)西蒙菲莎大学毕业证成绩单如何办理
一比一原版(SFU毕业证)西蒙菲莎大学毕业证成绩单如何办理
 
Forklift Classes Overview by Intella Parts
Forklift Classes Overview by Intella PartsForklift Classes Overview by Intella Parts
Forklift Classes Overview by Intella Parts
 
Pile Foundation by Venkatesh Taduvai (Sub Geotechnical Engineering II)-conver...
Pile Foundation by Venkatesh Taduvai (Sub Geotechnical Engineering II)-conver...Pile Foundation by Venkatesh Taduvai (Sub Geotechnical Engineering II)-conver...
Pile Foundation by Venkatesh Taduvai (Sub Geotechnical Engineering II)-conver...
 
Student information management system project report ii.pdf
Student information management system project report ii.pdfStudent information management system project report ii.pdf
Student information management system project report ii.pdf
 
Event Management System Vb Net Project Report.pdf
Event Management System Vb Net  Project Report.pdfEvent Management System Vb Net  Project Report.pdf
Event Management System Vb Net Project Report.pdf
 
CME397 Surface Engineering- Professional Elective
CME397 Surface Engineering- Professional ElectiveCME397 Surface Engineering- Professional Elective
CME397 Surface Engineering- Professional Elective
 
LIGA(E)11111111111111111111111111111111111111111.ppt
LIGA(E)11111111111111111111111111111111111111111.pptLIGA(E)11111111111111111111111111111111111111111.ppt
LIGA(E)11111111111111111111111111111111111111111.ppt
 
Standard Reomte Control Interface - Neometrix
Standard Reomte Control Interface - NeometrixStandard Reomte Control Interface - Neometrix
Standard Reomte Control Interface - Neometrix
 
Final project report on grocery store management system..pdf
Final project report on grocery store management system..pdfFinal project report on grocery store management system..pdf
Final project report on grocery store management system..pdf
 
J.Yang, ICLR 2024, MLILAB, KAIST AI.pdf
J.Yang,  ICLR 2024, MLILAB, KAIST AI.pdfJ.Yang,  ICLR 2024, MLILAB, KAIST AI.pdf
J.Yang, ICLR 2024, MLILAB, KAIST AI.pdf
 
Railway Signalling Principles Edition 3.pdf
Railway Signalling Principles Edition 3.pdfRailway Signalling Principles Edition 3.pdf
Railway Signalling Principles Edition 3.pdf
 
Halogenation process of chemical process industries
Halogenation process of chemical process industriesHalogenation process of chemical process industries
Halogenation process of chemical process industries
 
Cosmetic shop management system project report.pdf
Cosmetic shop management system project report.pdfCosmetic shop management system project report.pdf
Cosmetic shop management system project report.pdf
 
weather web application report.pdf
weather web application report.pdfweather web application report.pdf
weather web application report.pdf
 
block diagram and signal flow graph representation
block diagram and signal flow graph representationblock diagram and signal flow graph representation
block diagram and signal flow graph representation
 
Design and Analysis of Algorithms-DP,Backtracking,Graphs,B&B
Design and Analysis of Algorithms-DP,Backtracking,Graphs,B&BDesign and Analysis of Algorithms-DP,Backtracking,Graphs,B&B
Design and Analysis of Algorithms-DP,Backtracking,Graphs,B&B
 
Architectural Portfolio Sean Lockwood
Architectural Portfolio Sean LockwoodArchitectural Portfolio Sean Lockwood
Architectural Portfolio Sean Lockwood
 
Gen AI Study Jams _ For the GDSC Leads in India.pdf
Gen AI Study Jams _ For the GDSC Leads in India.pdfGen AI Study Jams _ For the GDSC Leads in India.pdf
Gen AI Study Jams _ For the GDSC Leads in India.pdf
 
H.Seo, ICLR 2024, MLILAB, KAIST AI.pdf
H.Seo,  ICLR 2024, MLILAB,  KAIST AI.pdfH.Seo,  ICLR 2024, MLILAB,  KAIST AI.pdf
H.Seo, ICLR 2024, MLILAB, KAIST AI.pdf
 
TECHNICAL TRAINING MANUAL GENERAL FAMILIARIZATION COURSE
TECHNICAL TRAINING MANUAL   GENERAL FAMILIARIZATION COURSETECHNICAL TRAINING MANUAL   GENERAL FAMILIARIZATION COURSE
TECHNICAL TRAINING MANUAL GENERAL FAMILIARIZATION COURSE
 

CP Handout#3

  • 1. C-Programming Walchand Institute of Technology (RC1131), Solapur Page 1 Handout#3 Assignment/Program Statement: Write a C program using variables, constants, data types, expressions and applying type casting and type conversion rules. Learning Objectives: Students will be able to - explain basic concepts of C such as variables, constants, data types - write C code using variables, constants, data types, expressions - apply type casting and type conversion rules in C Theory: What is Program? A program is a sequence of instructions (called programming statements), executing one after another - usually in a sequential manner Variable:  In programming, a variable is a container (storage area) to hold data.  To indicate the storage area, each variable should be given a unique name (identifier).  Variable names are just the symbolic representation of a memory location.  For example: int marks = 65;  In this example, "marks" is a variable of integer type. The variable is holding value 65.  The value of a variable can be changed, hence the name 'variable'.  In C programming, you have to declare a variable before you can use it. 65 marks Memory Representation
  • 2. C-Programming Walchand Institute of Technology (RC1131), Solapur Page 2 Constants/Literals:  A constant is a value or an identifier whose value cannot be altered in a program.  For example: const double PI = 3.14 Here, PI is a constant. Basically what it means is that, PI and 3.14 is same for this program.  Following are the types of constants 1) Integer constants 2) Floating-point constants 3) Character constants 4) String constants [Reference: http://www.programiz.com/c-programming/c-variables-constants ] Data Types in C:  Data types simply refer to the type and size of data associated with variables and functions.  The type of a variable determines how much space it occupies in storage and how the bit pattern stored is interpreted.  C language supports 2 different type of data types – (1) Primary (Fundamental) data types and (2) Derived data types (1) Primary data types: o These are fundamental data types in C namely integer(int), floating(float), character(char) and void. (2) Derived data types o Derived data types are like arrays, pointers, structures and enumeration. [Reference: http://www.tutorialspoint.com/cprogramming/c_data_types.htm , http://www.programiz.com/c-programming/c-data-types and http://www.studytonight.com/c/datatype-in-c.php ]
  • 3. C-Programming Walchand Institute of Technology (RC1131), Solapur Page 3 Operators in C:  An operator is a symbol that tells the compiler to perform specific mathematical or logical functions.  C language provides the following types of operators - 1. Arithmetic Operators 2. Relational Operators 3. Logical Operators 4. Bitwise Operators 5. Assignment Operators 6. Misc Operators [1] Arithmetic Operators:  The following table shows all the arithmetic operators supported by the C language. 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  Assume variable A holds 10 and variable B holds 20. Program: #include<stdio.h> void main() {
  • 4. C-Programming Walchand Institute of Technology (RC1131), Solapur Page 4 int A = 10, B =20, C; C = A + B; printf(“n C=%d”, C); } Output: C=30 [2] Relational Operators: The following table shows all the relational operators supported by C. Operator Description Example == Checks if the values of two operands are equal or not. If yes, then the condition becomes true. (A == B) is not true. != Checks if the values of two operands are equal or not. If the values are not equal, then the condition becomes true. (A != B) is true. > Checks if the value of left operand is greater than the value of right operand. If yes, then the condition becomes true. (A > B) is not true. < Checks if the value of left operand is less than the value of right operand. If yes, then the condition becomes true. (A < B) is true. >= Checks if the value of left operand is greater than or equal to the value of right operand. If yes, then the condition becomes true. (A >= B) is not true. <= Checks if the value of left operand is less than or equal to the value of right operand. If yes, then the condition becomes true. (A <= B) is true. [3] Logical Operators: Following table shows all the logical operators supported by C language.
  • 5. C-Programming Walchand Institute of Technology (RC1131), Solapur Page 5 Operator Description Example && Called Logical AND operator. If both the operands are non-zero, then the condition becomes true. (A && B) is false. || Called Logical OR Operator. If any of the two operands is non-zero, then the condition becomes true. (A || B) is true. ! Called Logical NOT Operator. It is used to reverse the logical state of its operand. If a condition is true, then Logical NOT operator will make it false. !(A && B) is true. [Reference: http://www.tutorialspoint.com/cprogramming/c_operators.htm ] Type Conversion and Type casting in C  Type conversion occurs when the expression has data of mixed data types.  Examples of such expression include converting an integer value in to a float value, or assigning the value of the expression to a variable with different data type. For example: int sum=17, count=5; float mean; mean = sum/count;  In type conversion, the data type is promoted from lower to higher because converting higher to lower involves loss of precision and value. Forced Conversion:  Forced conversion occurs when we are converting the value of the larger data type to the value of the smaller data type or smaller data type to the larger data type. For example, consider the following assignment statement int a; float b; a=5.5; b=100;
  • 6. C-Programming Walchand Institute of Technology (RC1131), Solapur Page 6  Case-1: a=5.5 ; a is declared as int so the float value 5.5 cannot be stored in a. In such a case float is demoted to an int and then its value is stored. Hence 5 is stored in a.  Case-2: b=100; since b is a float variable 100 is promoted to 100.000000 and then stored in b. In general, the value of the expression is promoted or demoted depending on the type of variable on left hand side of =. Consider the following statement int count = 5; float sum = 17.5, mean; mean = sum / count;  In the above statement, one operand is int where as other is float. During evaluation of the expression the int would be promoted to floats and the result of the expression would be a float. And result float value is assigned to float mean variable. If mean declared as int then result will be demoted to int type.  Forced conversion may decrease the precision.  Type casting is the preferred method of forced conversion [Reference: http://datastructuresprogramming.blogspot.in/2010/02/type- conversion-and-type-casting-in-c.html] Type Casting (or) Explicit Type conversion:  Explicit type conversions can be forced in any expression, with a unary operator called a cast.  Type casting is a way to convert a variable from one data type to another data type. Syntax: (type-name) expression; Example: int n=5.5;
  • 7. C-Programming Walchand Institute of Technology (RC1131), Solapur Page 7 float x; x=(float)n; printf(“n X=%f”,x); The above statement will convert the value of n to a float value before assigning to x but n is not altered. Program: main() { int sum = 17, count = 5; float mean; mean = (float) sum / count; printf("n Mean = %f ", mean ); } Output: Mean = 3.000000 /*without typecasting*/ Output: Mean = 3.400000 /*with typecasting*/ Conclusion: Thus C programs, using variables, constants, data types, expressions and applying type casting and type conversion rules, is implemented. Learning Outcomes: At the end of this assignment, students are able to - explain basic concepts of C such as variables, constants, data types - write C code using variables, constants, data types, expressions - apply type casting and type conversion rules in C