Professional Documents
Culture Documents
CSE-102
Computer Programming Laboratory
Student ID 221010005
Section EA
Department
ExperimentNo: 03
Name of theExperiment: Get familiar with data types and operators of C
Objective(s):
To be familiar with different data types, Operators and Expressions in C.
Learning outcome(s):
Students will be able to perform basic arithmetic operations in C. They will understandbasic
input/output operations using C language. They will also learn about the memory allocation sizes of
various data types in C.
Problem :
Problem 1 :
Problem 2 :
z); printf("A\tB\tC=\t%.1f|\t%.1f|\t%.1f|\n", A,
B, C); printf("____________________\n");
printf("X\ty\tz=\t|%.2f\t |%.2f\t\t|%.2f\n", x, y,
z);
C = 52300.30;
return 0;
Problem 3 :
#include <stdio.h>
#define PI 3.1416
#define mult(a,b) a * b #define
sum(a,b) a + b
div(a,b) a / b
int main()
n", divv);
return 0;
Problem 4 :
*/ #include
<stdio.h>
int main()
{
char ch;
/* Alphabet check */
if ((ch >= 'a' && ch <= 'z') || (ch >= 'A' && ch <= 'Z'))
else
return 0;
Problem 5:
It is used to read the input(character, string, numeric data) from the standard input(keyboard).
It is used to read the input until it encounters a whitespace, newline or End Of File(EOF).
For example see the following code:
#include <stdio.h>
int main()
{
char str[20];
printf("enter something\n");
scanf("%s", str);
return 0;
Here the input will be provided by the user and output will be as follows:
Output: Geeks
Input: Computer science
#include <stdio.h>
int main()
char str[20];
printf("enter something\n");
gets(str);
return 0;
scanf() reads input until it encounters whitespace, newline or End Of File(EOF) whereas gets() reads
input until it encounters newline or End Of File(EOF), gets() does not stop reading input when it
encounters whitespace instead it takes whitespace as a string.
scanf can read multiple values of different data types whereas gets() will only get character string
data.
scanf()gets()
when scanf() is used to read string input it stops reading when it encounters whitespace, newline or
End Of Filewhen gets() is used to read input it stops reading input when it encounters newline or End
Of File. It does not stop reading the input on encountering whitespace as it considers whitespace as a
string.
It is used to read input of any datatypeIt is used only for string input.
Its syntax is -:
syntax is -:
char *gets(char *str)
It is fast to take input.It takes one parameter that is the pointer to an array of chars
Format specifiers is used inside scanf to take input.Its return value is str on success else NULL.
Actually we can use scanf() to read entire string. For example we can use %[^\n]s inside scanf() to read
entire string.
<stdio.h>
int main()
char str[20];
printf("Enter something\n");
return 0;
The above code reads the string until it encounters newline. Examples:
Problem 6:
All these functions read the character from input and return an integer. The value of EOF is used for this
purpose.
getc()
It reads a single character from the input and return an integer value. If it fails, it returns EOF.
Example
Live Demo
#include<stdio.h>
int main () {
char val;
Output
The function getchar() reads the character from the standard input while getc() reads from the input
stream. So, getchar() is equivalent to getc(stdin). Here is the syntax of getchar() in C language,
int getchar(void);
Example
Live Demo
#include <stdio.h> int
main() {
getchar();
val); return 0;
Output
Entered character : n
getch()
The function getch() is a non-standard function. It is declared in “conio.h” header file. Mostly it is used by
Turbo C. It is not a part of C standard library. It immediately returns the entered character without even
waiting for the enter key.
int getch();
Example
#include <stdio.h>
#include<conio.h>
int main() {
= getch();
printf("Enter the character : ");
val); return 0;
Output
Entered character : m
getche()
Like getch(), the getche() function is also a non-standard function and declared in “conio.h” header file. It
reads a single character from the keyboard and returns it immediately without even waiting for enter key.
int getche(void);
Example
#include <stdio.h>
#include<conio.h>
val); return 0;
Output