Nothing Special   »   [go: up one dir, main page]

PL1 Lecture 2 Library and Printf & Scanf Function

Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 12

Basic programming

Course Title: Programming language 1

Dept. of Computer Science


Faculty of Science and Technology

Lecturer No: 02 Week No: 2 Semester: Spring 23-24


Lecturer: Shaikat Das Joy; skdas@aiub.edu
Lecture Outline

 List of topics,
 Library of header files
 Print function
 Scan function
LIBRARY OF HEADER FILES
stdio.h C library to perform Input/Output operations (header)
string.h C Strings (header)
time.h C Time Library (header)
math.h C numerics library (header)
ctype.h Character handling functions (header)
assert.h C Diagnostics Library (header)
errno.h C Errors (header)
float.h Characteristics of floating-point types (header)
iso646.h ISO 646 Alternative operator spellings (header)
limits.h Sizes of integral types (header)
locale.h C localization library (header)
setjmp.h Non local jumps (header)
signal.h C library to handle signals (header)
stdarg.h Variable arguments handling (header)
stddef.h C Standard definitions (header)
stdlib.h C Standard General Utilities Library (header)
PRINTF() AND SCANF() FUNCTION IN C
 printf() is used to display the output, e.g., to display panel.

 scanf() is used to read the inputs, e.g., from keyboard.

 printf() and scanf() functions are declared in stdio.h header file


in C library.

 All syntax in C language including printf() and scanf() functions


are case sensitive. E.g., printf and Printf are not the same.
PRINTF() FUNCTION
#include <stdio.h>

int main(){
// a floating point variable.
float gpa = 3.99; Output:
My Gpa is 3.99
printf("My Gpa is 3.99\n"); My Gpa is 3.990000
printf("My Gpa is %f \n", gpa); My Gpa is 3.99
printf("My Gpa is %.2f \n", gpa);

return 0;
}
PRINTF() FUNCTION

printf() function is used to print Following format specifiers are used to


- constant values print these values:
- character (a, b, c, z)
- string (I love programming.) %d for displaying an integer variable.
- float (3.994, 12.45) %c for displaying a character variable.
- integer (1, 10, 1000, 5647) %f for displaying a float variable.
%s for displaying a string variable.
%lf for displaying a double variable.
%Lf for displaying a long double
variable.
PRINTF() FUNCTION
#include <stdio.h>

int main(){
// variables of different types.
int no = 150;
float flt = 10.234;
double dbl = 20.123456;
char ch = 'A';
Output:
char str[24] = "This is my first
program"; Integer value: 150
Float value: 10.234000
Double value: 20.123456
// printing values of all the variables Character: A
printf("Integer value: %d\n" , no); String: This is my first program
printf("Float value: %f \n", flt);
printf("Double value: %lf \n", dbl);
printf("Character: %c \n", ch);
printf("String: %s \n" , str);
return 0;
}
PRINTF() FUNCTION

\t horizontal tab #include <stdio.h>


\’ single quote
\n new line int main(){
\b backspace printf("My name is \t Rahim. \nI am
\f form feed teaching \'C programming\'");
\r carriage return return 0;
\0 null }

Output:
My name is Rahim.
I am teaching 'C programming'
SCANF() FUNCTION

Following format specifiers are used to scan


values:
scanf() function is used to read
- character
- string %d for taking input in an integer variable.
- numeric data from keyboard %c for taking input in a character variable.
%f for taking input in a float variable.
%s for taking input in a string variable.
%lf for taking input in a double variable.
SCANF() FUNCTION
#include <stdio.h>

int main(){
// a floating point variable. Output:
float gpa; Enter your gpa: 3.887
printf("Enter your gpa:\t"); Your gpa is: 3.887000
scanf("%f", &gpa);

printf("Your gpa is: %f \n",gpa);


return 0;
}
SCANF() FUNCTION
#include <stdio.h>

int main(){
char name [20]; float gpa;
Output:
printf("Enter your name:\t");
Enter your name: Rahim
scanf("%s", &name); Enter your gpa: 3.98
Welcome Rahim, your gpa is:
printf("Enter your gpa:\t"); 3.980000
scanf("%f", &gpa);

printf("Welcome %s, your gpa is: %f \n", name, gpa);


return 0;
}
References

 https://www-s.acm.illinois.edu/webmonkeys/book/c_guide/

You might also like