[go: up one dir, main page]

0% found this document useful (0 votes)
31 views5 pages

LAB6

The document provides information about data types, format specifiers, and operators in C language. It includes: 1) A table listing different data types (character, integer, real, string) along with their format specifiers and sizes in bytes. 2) An explanation that C uses implicit type casting from lower to higher data types, and explicit type casting using syntax (type)expression. 3) A table categorizing operator types (arithmetic, logical, bitwise) used in C and their precedence. 4) Examples of C programs demonstrating conversions between Celsius to Fahrenheit scales, use of relational/logical/bitwise operators, and exercises for students to practice concepts learned.

Uploaded by

priyansh9462
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
31 views5 pages

LAB6

The document provides information about data types, format specifiers, and operators in C language. It includes: 1) A table listing different data types (character, integer, real, string) along with their format specifiers and sizes in bytes. 2) An explanation that C uses implicit type casting from lower to higher data types, and explicit type casting using syntax (type)expression. 3) A table categorizing operator types (arithmetic, logical, bitwise) used in C and their precedence. 4) Examples of C programs demonstrating conversions between Celsius to Fahrenheit scales, use of relational/logical/bitwise operators, and exercises for students to practice concepts learned.

Uploaded by

priyansh9462
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

JAYPEE UNIVERSITY OF ENGINEERING & TECHNOLOGY, GUNA

DEPARTMENT OF COMPUTER SCIENCE & ENGINEERING


Course: Computer Programming Lab
Course Code: CS201
B. Tech. I Sem. (CSE, ECE, MECH, CE, CHE)

Lab-6

Aim: Data types, Format specifiers and Operators

Data Types: These are the declarations for the user variables which determine the type and size of data
associated with variables. Applying qualifiers with data types change their characteristics. There are sign
(i.e. signed and unsigned) and size (i.e. short and long) qualifiers. Exact size of data types depend on the
compiler. Normally, size qualifiers cannot be applied with char and float data types while sign qualifiers
are not applied with float, double and long double.

Format Specifiers: These are used to specify the kind of data being passed as an argument to the
input/output (printf and scanf) functions. The format specifier in c is a special character that starts with a
percentage sign (%) and continues with a letter or group of letters that denotes the data type. Following
table shows the different data types with qualifiers along with their format specifiers:

Data Type Keyword Qualifier Declaration Syntax Format Specifier Size in Byte
char
Character char unsigned unsigned char %c 1
signed signed char
int %d, %i
unsigned unsigned int %u 2 or 4
signed signed int %d, %i
short short int %hd
unsigned short unsigned short int %hu 2
Integer int signed short signed short int %hd
long long int %ld, %li 4
long long long long int %lld, %lli 8
unsigned long unsigned long int %lu 4
signed long signed long int %ld, %li 4
unsigned long long unsigned long long int %llu 8
float float %f 4
Real double %lf 8
double
long long double %Lf 10
String %s

Note: (i) If an expression contains mixture of different data types then automatic conversion (known as
implicit type casting) takes place from lower to higher type hierarchy. In C language, ascending order
hierarchy is as: int-> unsigned->long->unsigned long->double->long double.

(ii) Forceful (explicit type casting) conversion is used to convert declared data type of a variable into other
data type. Syntax: (type) expression or variable. For exp. (float) i converts into float type from integer i.

CP Lab Coordinator: Dr. Rahul Pachauri


Operators: These are the types of symbols that inform compiler for performing some specific arithmetic,
logical or any other types of functions by following predefined precedence and associativity. The
precedence of operators determines the order in which operations are performed in an expression.
Operators with higher precedence are evaluated first. Associativity determines the direction in which the
expression will be evaluated. Following table shows category of operators used in the C-language:

CP Lab Coordinator: Dr. Rahul Pachauri


Execute following program examples, see outputs and learn basic programming concepts.

Example#1: Conversion of temperature unit from Celsius to Fahrenheit.

#include<stdio.h>
int main ()
{
float cls, fht;
printf("Enter temperature in Celsius : ");
scanf("%f", &cls);
fht = (9.0/5.0)*cls+32;
printf("Temperature in Fahrenheit is=%f\n", fht ); //Prints 6 digits after decimal point by default
printf("Temperature in Fahrenheit is=%0.3f\n", fht ); //Prints 3 digits after decimal point
printf("Temperature in Fahrenheit is=%d\n", (int) fht ); //Explicit type casting to print only integer part
printf("Temperature in Fahrenheit is=%e\n", fht ); //Prints in exponential format
printf("Size in Bytes=%d\n", sizeof((short int) fht) ); // Size of short integer data
printf("Size in Bytes=%d\n", sizeof( (int) fht) ); // Size of integer data
printf("Size in Bytes=%d\n", sizeof( (long int) fht) ); // Size of long integer data
printf("Size in Bytes=%d\n", sizeof( fht) ); // Size of float data
printf("Size in Bytes=%d\n", sizeof( (double) fht) ); // Size of double data
printf("Size in Bytes=%d\n", sizeof( (long double) fht) ); // Size of long double data
return 0;
}

Example#2: Working with Relational, logical and bitwise operators.

#include <stdio.h>
int main()
{
int a = 5, b = 5, c = 10, result;

result = (a == b) ; // Relational and Logical operators


printf(" %d \n", result);
result = (a == b) || (c < b);
printf(" %d \n", result);
result = !(a == b);
printf("!(a == b) is %d \n", result);
result = (a != b) || (c > b);
printf(" (a != b) || (c > b) is %d \n", result);

result = (a & b)|c; //Bitwise Operators


printf( "a & b)|c is %d \n", result);
result = (a|b) | (a&c);
printf( "(a|b) | (a & c) is %d \n", result);
result = ~c & (!a^b);
printf (" ~c & (!a ^ b) is %d \n", result);
return 0;
}

CP Lab Coordinator: Dr. Rahul Pachauri


For first turn of the week:

Exercise#1: Write a C language program using variable values = 8, = 29, = 15.35 = 7.254
to find:
(i) Sum and Average of all four numbers (Result: 59.604000 and 14.901000)
(ii) Sum and Average of integer part of all numbers (Result: 59 and 14.75)
(iii) Multiplication ( ∗ ) upto two decimal point (Result: 111.34)
(iv) Quotient and Remainder of ( / ) (Result 3 and 5)
(v) Multiplication ( ∗ ) in octal format using %o format specifier (Result: 350)
(vi) Multiplication ( ∗ ) in hexadecimal format using %x or %X format specifier (Result: e8 or E8)

Exercise#2: Write a C language user input program to evaluate following expressions. Verify results with
value of variables a=2, b=3, and c=5.
(i) Arithmetic Expression: 2 log + | − | − 5√ + cos 30 . (Result=8.593813)
Include additional header files math.h and stdlib.h to use in-built mathematical functions.
Syntax: pow (a, b), sqrt (a), log10 (a), cos (a), abs (a) etc.
(ii) Logical Expression: + ( + ) + + (Result=1)
(iii) Repeat part (ii) using bitwise operators (Result=7)

Exercise#3: The distance between two cities is measured in kilometer. Write a C language user input
program to print measured distance in meter, centimeter, feet, and inch. Draw the flowchart also in your
practical book. (1 meter = 3.3 feet; 1 feet =12 inch)

For second turn of the week:

Exercise#4: Suppose your computer monitor consumes 60 watt per hour, CPU consumes 80 watt per hour
and all other hardware components consumes total of 40 watt per hour. Write a C language user input
program to calculate monthly consumed units and payable bill @ Rs 8 per unit if computer system is used
12 hours/day. Draw the flowchart also in your practical book. (One unit = 1000 Wh)
(Ans: 5.400000 Units and ₹ 518.400000)
×
Formula: ℎ =
ℎ = ℎ × ×

Exercise#5: The resolution of full HD computer monitor is 1920 × 1080 pixels. Write a C language user
input program to calculate total number of bits (in Mb) required in one color image and find how long it
will take to transfer 100 color images using network with data transfer rate of 512 Kb/second. Draw the
flowchart also in your practical book. (One pixel of color image = 24 bits (8 bits per color of RGB)).
(Ans: 47.460937 Mb and 9492.187500 Second)
:
× ×
. ( )=
1024 × 1024
. × . ℎ
=

Exercise#6: A cashier has currency notes of denominations 10, 50 and 100. If a customer withdraws an
amount in tens, hundreds or thousands. Write a C language user input program to find the total number
of currency notes of each denomination the cashier will have to give to the withdrawer. Draw the
flowchart also in your practical book.

CP Lab Coordinator: Dr. Rahul Pachauri


Practice Questions
(No need to include in your Practical Book)

Q.1 Write a C program to print the sum of natural numbers. The sum of first ʹ ʹ natural umbers is
given by:–
( + 1)
1 + 2 + 3 + 4… =
2
Q.2 Write a C program to print the sum of the square of n natural numbers. The sum of squares of
first 'n' natural no`s-
( + 1)(2 + 1)
1 +2 +3 +4 … =
6
Q3. Write a C program to print the sum of the cube of n natural numbers. The sum of cubes of first 'n'
natural no`s-
( + 1)
1 +2 +3 +4 … =
4
Q.4 The length & breadth of a rectangle and radius of a circle are input through the keyboard. Write a
C program to calculate the area & perimeter of the rectangle, and the area & circumference of the
circle.

Q5. Write program that takes 3 digit number as an input. Program should compute and display the
sum of digits in a number.

Q6. Write program that takes 3 digit number as an input. Write a C program to reverse the number
and to obtain the sum of the first and last digit of this number.

Q7. Write a C program to find the gross salary of an employee. Ask user to enter the basic salary only.
Assume Dearness Allowance (DA) = 40% of the Basic Salary, House Rent Allowance (HRA) = 80% of DA,
and Bonus= 25% of HRA.

Gross Salary = Basic Salary + DA + HRA + Bonus

Q8. Two numbers are input through the keyboard into two locations X and Y. Write a C- program to
interchange the contents of X and Y. (a) using third variable (b) without using third variable.,

CP Lab Coordinator: Dr. Rahul Pachauri

You might also like