You are on page 1of 8

Birla Institute of Technology & Science Pilani, Hyderabad

Campus
Computer Programming (CS F111)
2022-23 First semester
Lab-2
Topics to be covered:
1. Keywords
2. Data Types
3. Constants
4. Variables
5. Arithmetic Expressions

1. Keywords
Keywords are the reserved words. They have fixed meanings and these meanings cannot be
changed. They must be written in lowercase.

auto double int struct


break else long switch
case enum register typedef
char extern return union
const float short unsigned
continue for signed void
default goto sizeof volatile
do if static while

2. Data Types
Data types are categorized into:

1. Primary data types


2. Derived data types (will be discussed later)
3. User defined data types (will be discussed later)

1
Table 1 Primary Data types

Type Min size (in bits) Format Specifier


char 8 %c
signed char 8 %c
unsigned char 8 %c
short int 16 %hd (decimal), %ho (octal), %hx (hex)
signed short int 16 %hd (decimal), %ho (octal), %hx (hex)
unsigned short int 16 %hu (decimal), %ho (octal), %hx (hex)
int 16 %d (decimal), %o (octal), %x (hex)
signed int 16 %d (decimal), %o (octal), %x (hex)
unsigned int 16 %u (decimal), %o (octal), %x (hex)
long/long int 32 %ld (decimal), %lo (octal), %lx (hex)
signed long int 32 %ld (decimal), %lo (octal), %lx (hex)
unsigned long int 32 %lu (decimal), %lo (octal), %lx (hex)
float 32 %f or %g or %e or %a
double 64 %f or %g or %e or %a
long double 128 %Lf or %Lg or %Le or %La

Note: Each compiler is free to choose appropriate sizes for its own hardware, subject only to the restriction that
shorts and ints are at least 16 bits, longs are at least 32 bits, and short is no longer than int, which is no longer
than long. (Reference: Ritchie, D.M., Johnson, S.C., Lesk, M.E. and Kernighan, B.W., 1978. The C programming language, 2nd Edition, Bell
Sys. Tech. J, p. 36-37)

3. Constants
The constants refer to fixed values that the program may not alter during its execution. These
fixed values are also called literals.

2
Integer constants

Numeric constants

Real constants

Constants

Single character
constants

Character constants

String constants

• Integer constants: There are three types of integers: decimal, octal, and hexadecimal.
- Decimal integers consist of a set of digits, 0-9, preceded by unary + or -.
Ex: 100, -101, +102
- Octal integers are always preceded by 0. It consists of a combination of digits 0-7.
Ex: 0144, -0145, 0146
- Hexadecimal integers are preceded by 0x or 0X. It consists of a combination of
digits 0-9 and A to F or a to f.
Ex: 0x64, -0X65, 0X66
- Unsigned integers, unsigned long integers, and long integers can be expressed
with suffix u, ul, l or U, UL, L respectively.
Ex: 2022u or 2022U, 2022ul or 2022UL, 2022l or 2022L
• Real constants: Real constants are having a whole number followed by a decimal point
and fractional part. It is possible to omit digits before or after decimal point.
Ex: 2.022, -20.22, .22, -20.
A real constant can be expressed in exponential notation.
Ex: 20.22e2, 2.022E3, 20220e-1
• Single character constants: A single character constant contains a single character
enclosed within a pair of single quote marks.
Ex- '2', 'A', 'a', ';'
• String constants: A string constant is a sequence of characters enclosed in double
quotes. The characters may be letters, numbers, special characters, and blank space.
Ex- "Hello!!!", "2022", "Hello 2022!", "5+5"

3
• Backslash character constants:

'\a' audible alert (bell)


'\b' back space
'\f' form feed
'\n' new line
'\r' carriage return
'\t' horizontal tab
'\v' vertical tab
'\'' single quote
'\"' double quote
'\?' question mark
'\\' Backslash
'10' null

4. Variables
Variables are containers for storing data values. Variable names are subject to the following
conditions:
• Variable names are made up of letters and digits.
• The first character must be a letter or underscore "_".
• C recognizes the first 31 characters as a variable name.
• Variable names are case sensitive.
• Variable names must not be a keyword.
• White space is not allowed.
It is wise to choose variable names that are related to the purpose of the variable, and that
are unlikely to get mixed up typographically.
All variables must be declared before use. A declaration specifies a type and a list of one or
more variable names as follows:
<type> <variable_list>

int marks;
char grade, overallgrade;

It is as same as
int marks;
char grade;
char overallgrade;
4
A variable can be initialized in its declaration or afterward.

<type> <variable_name> = <value>


or
<variable_name> = <value>

int marks = 70;


char grade = ‘A’;
char overallgrade;
overallgrade = ‘B’;

5. Arithmetic Expressions
An arithmetic expression is a combination of variables, constants, and operators arranged as
per the syntax of the language.

<variable/constant> <operator> <variable/constant>

Arithmetic operators:

➢ The arithmetic operators are +, -, *, /, and modulus operator %.


➢ Modulus operator % does not work with floating point values.
➢ The binary + and - operators have the same precedence, which is lower than the
precedence of *, /, and %.
➢ The precedence of unary + and - is higher than the precedence of *, /, and %.
Arithmetic operators associate left to right.

Type conversion: When an expression has operands of different types, they are converted to
a common type according to the following rule.

Implicit type conversion (Automatic type conversion)

When operands are of different types, C automatically converts a "narrower" operand into a
"wider" without losing information. It is called widening conversion.

Expressions may lose information while assigning value of "wider" operand to "narrower"
operand. Compiler may draw a warning, but these expressions are not illegal.

5
Explicit type conversion

Explicit type conversions can be forced in any expression as follows.

(<type name>) expression

int result = 94;


int totalmarks = 150;
float percentage;

percentage = ((float) result)*100/totalmarks;

6
Exercise
1. Write a C program to find sum of digits for a given three digit integer
value. (Hint: Use the modulus operator '%')

2. Write a program that reads one octal and one hexadecimal values
through terminal and outputs addition, multiplication, subtraction,
and division in the decimal format.

7
3. Write a program to evaluate the following expression and outputs
the results on screen.
(1.2 × 10−2 × 5.2 × 10−3) + (3.5 × 102 × 8.7 × 104 )

4. Write a program to find whole part and fractional part of a given


floating point value.

You might also like