lect05_CSC101
lect05_CSC101
CSC-101 (Lecture 5)
Dr. R. Balasubramanian
Professor
Department of Computer Science and Engineering
Mehta Family School of Data Science and Artificial Intelligence
Indian Institute of Technology Roorkee
Roorkee 247 667
[email protected]
https://faculty.iitr.ac.in/cs/bala/
Unfolding some more layers
Another Task: Circle’s Area and Circumference
▶ Given the radius, calculate the area and circumference.
# include < stdio .h >
# include < math .h >
int main () {
float radius , area , circumference ;
printf (" Enter the radius of the circle :
") ;
scanf ("% f " , & radius ) ;
New Directive:
# include < math .h >
Interactivity:
printf (" Enter the radius of the circle : ") ;
scanf ("% f " , & radius ) ;
Formulas in Action:
area = M_PI * radius * radius ;
circumference = 2 * M_PI * radius ;
int main () {
int num1 , num2 , sum ;
printf (" Enter first number : ") ;
scanf ("% d " , & num1 ) ;
printf (" Enter second number : ") ;
scanf ("% d " , & num2 ) ;
sum = num1 + num2 ;
printf (" Sum = % d \ n " , sum ) ;
return 0;
}
Taking User Input: First Number
Addition:
sum = num1 + num2 ;
Output:
printf (" Sum = % d \ n " , sum ) ;
▶ Size: 1 byte.
▶ Range: −128 to 127 or 0 to 255.
▶ Used to store text, symbols, etc.
▶ Characters are enclosed in single quotes (‘ ’).
Double Precision Floating-Point Type (double)
int main () {
int integer = 10;
float floating = 3.14;
integer = floating ;
printf ("% d \ n " , integer ) ;
return 0;
}
▶ Why does this happen, and how might you prevent it?
Basic Conversions
▶ In C, characters can be implicitly converted to integers
using their ASCII values.
▶ Example:
# include < stdio .h >
int main () {
int integer ;
char character = ‘A ’;
integer = character ;
printf ("% d \ n " , integer ) ; // prints 65
return 0;
}
int main () {
char character = ‘A ’;
character = character + 3;
printf ("% c \ n " , character ) ;
return 0;
}