Professional Documents
Culture Documents
Labsheet 2
Labsheet 2
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.
2. Data Types
Data types are categorized into:
1
Table 1 Primary Data types
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:
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.
5. Arithmetic Expressions
An arithmetic expression is a combination of variables, constants, and operators arranged as
per the syntax of the language.
Arithmetic operators:
Type conversion: When an expression has operands of different types, they are converted to
a common type according to the following rule.
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
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 )