You are on page 1of 13

Green Universityof Bangladesh

Department of Electrical and Electronic Engineering

CSE-102
Computer Programming Laboratory

Student ID 221010005

Student Name Ansarul Haque

Section EA

Name of the Program CSE-102

Name of the EEE

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 :

**Write a program to produce the output as shown below:


#include<stdio.h>
int main()
{
int x = 6, y = 3; // first expression
printf("x\ty\texpressions\tresults\n");
printf("%d |\t%d |\t x=y+3 |\t x=", x, y);
x = y + 3;
printf("%d\n", x);
x = 6, y = 3; // second expression
printf("%d |\t%d |\t x=y-2 |\t x=", x, y);
x = y - 2;
printf("%d\n", x);
x = 6, y = 3; // third expression
printf("%d |\t%d |\t x=y*5 |\t x=", x, y);
x = y * 5;
printf("%d\n", x);
x = 6, y = 3; // fourth expression
printf("%d |\t%d |\t x=x/y |\t x=", x, y);
x = x / y;
printf("%d\n", x);
x = 6, y = 3; // fifth expression
printf("%d |\t%d |\t x=x%%y |\t x=", x, y);
x = x % y;
printf("%d\n", x);
return 0;
}

Problem 2 :

// Given x=3.0, y=12.5, z= 523.3, A=300.0, B=1200.5, C=5300.3, Write a

program // to display the following: #include<stdio.h> int main()

float x = 3.0, y = 12.5, z = 523.3, A = 300.0, B = 1200.5, C = 5300.3;

printf("X\ty\tz=\t %.1f|\t %.1f|\t %.1f|\n", x, y,

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;

printf("A\tB\tC=\t|%.2f\t |%.2f\t|%.2f\n", A, B, C);

return 0;

Problem 3 :

#include <stdio.h>

#define PI 3.1416
#define mult(a,b) a * b #define

sum(a,b) a + b

#define sub(a,b) a - b #define

div(a,b) a / b

int main()

int a = 8, b = 4; float mul = PI

* mult(a, b); float summ = PI

* sum(a, b); float subb = PI *

sub(a, b); float divv = PI *

div(a, b); printf("mul = %f\

n", mul); printf("sum = %f\

n", summ); printf("sub = %f\

n", subb); printf("div = %f\

n", divv);

return 0;

Problem 4 :

/* C program to check alphabet, digit or special character

*/ #include

<stdio.h>

int main()
{

char ch;

/* Input character from user

*/ printf("Enter any character:

"); scanf("%c", &ch);

/* Alphabet check */

if ((ch >= 'a' && ch <= 'z') || (ch >= 'A' && ch <= 'Z'))

{ printf("'%c' is alphabet.", ch);

else if (ch >= '0' && ch <= '9')

printf("'%c' is digit.", ch);

else

{ printf("'%c' is special character.", ch);

return 0;

Problem 5:

Difference between scanf() and gets() in C scanf()

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:

// C program to see how scanf()

// stops reading input after whitespaces

#include <stdio.h>

int main()

{
char str[20];

printf("enter something\n");

scanf("%s", str);

printf("you entered: %s\n", str);

return 0;

Here the input will be provided by the user and output will be as follows:

Input: Geeks for Geeks

Output: Geeks
Input: Computer science

Output: Computer gets

It is used to read input from the standard input(keyboard).

It is used to read the input until it encounters newline or End Of File(EOF).

// C program to show how gets() //

takes whitespace as a string.

#include <stdio.h>

int main()

char str[20];

printf("enter something\n");

gets(str);

printf("you entered : %s\n", str);

return 0;

Here input will be provided by user as follows


Input: Geeks for Geeks

Output: Geeks for Geeks

Input: Computer science


Output: Computer science

The main difference between them is:

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.

The difference can be shown in tabular form as follows:

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 -:

scanf(const char *format, … ) Its

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.

How to read complete sentence from user using scanf()

Actually we can use scanf() to read entire string. For example we can use %[^\n]s inside scanf() to read
entire string.

// C program to show how to read

// entire string using scanf() #include

<stdio.h>

int main()

char str[20];

printf("Enter something\n");

// Here \n indicates that take the input //

until newline is encountered


scanf("%[^\n]s", str);
printf("%s", str);

return 0;

The above code reads the string until it encounters newline. Examples:

Input: Geeks for Geeks

Output: Geeks for Geeks

Input: Computer science

Output: Computer science

Problem 6:

Difference between getc(), getchar(), getch() and getche()

CProgrammingServer Side Programming

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.

Here is the syntax of getc() in C language,


int getc(FILE *stream);

Here is an example of getc() in C language,

Example

Live Demo

#include<stdio.h>

int main () {

char val;

printf("Enter the character: \

n"); val = getc(stdin);

printf("Character entered: ");

putc(val, stdout); return(0);

Output

Enter the character: a

Character entered: a getchar()

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);

Here is an example of getchar() in C language,

Example

Live Demo
#include <stdio.h> int

main() {

char val; val =

getchar();

printf("Enter the character : \n");

printf("Entered character : %c",

val); return 0;

Output

Enter the character : n

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.

Here is the syntax of getch() in C language,

int getch();

Here is an example of getch() in C language,

Example

#include <stdio.h>

#include<conio.h>

int main() {

char val; val

= getch();
printf("Enter the character : ");

printf("Entered character : %c",

val); return 0;

Output

Enter the character : m

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.

Here is the syntax of getche() in C language,

int getche(void);

Here is an example of getche() in C language,

Example

#include <stdio.h>

#include<conio.h>

int main() { char val; val = getche();

printf("Enter the character : ");

printf("Entered character : %c",

val); return 0;

Output

Enter the character : s Entered character : s

You might also like