C Session2
C Session2
C Session2
l Data Types
l The variables in C
l Operators
l A program in C
Data types and Operators
Comp-U-Learn
C Trim
D ATA TYPES
The data types in C language are classified into two categories as under:
All the data types that are used for representing the actual data in the memory are called
fundamental data types. The fundamental data types in C language are:
int
An int data type is used to store only whole integer numbers and it is incapable of dealing with
fractions or numbers with decimals. The range of numbers that the int data type can deal is
32,768 to +32,767. It requires a 2-byte memory.
char
This data type holds individual characters or alphabets and it requires only 1 byte of memory.
Many compilers for this data type permit a range of 0 to 255.
float
The float data type is used for fixing the floating-point numbers. It is used to store numbers with
decimal point. The float data type needs 4 bytes of storage and has a fractional precision of 6
digits.
Comp-U-Learn
Data types and Operators
double
A double data type is also used for storing floating point numbers. This data type allows a greater
precision of about 15 digits when compared to the float data type. The compilers need 8 bytes to
store a double. The range of the double is 2.2E-308 and 2.2E+308.
The range of the int type listed above is the guaranteed minimum range. On some systems, this
type (or, indeed, any C type) may be able to hold larger values. But a program that depends on
extended ranges will not be as portative. Some programmers get hung up by knowing exactly what
the sizes of data objects will be, in various situations and go on to write programs which depend on
these exact sizes. Determining the size of an object is occasionally important. But most of the
times we can sidestep size issues and let the compiler do most of the work. From the ranges listed
above, we can determine that type int must have at least 16 bits. Many systems have 32-bit ints,
and some systems have 64-bit long ints.
Knowing how the computer stores characters might amaze you. The answer involves the character
set (refer Session 1), which is directly a mapping between some set of characters and some set of
small numeric codes. Mostly all the machines use the ASCII character set. In the character set,
the letter A is represented by the code 65, the Dollar ($) is represented by the code 36, the digit 0 is
represented by the code 48, etc.,
Most of the time, you will not need to know or even worry about these particular code values. They
are automatically translated into the right shapes on the screen or printer when characters are
printed out. They are automatically generated when you type characters on the keyboard. Character
codes are usually small. The largest code value in ASCII is 126, which is the ~ (tilde character).
Characters usually fit in a byte, which is usually 8 bits. The type char in C is defined as occupying
one byte i.e., its usual size is 8 bits.
long
This type extends the range of the data type. It can be applied to int and double types only. It
expands the range from a maximum of 2,147,483,647 to 2,147,483,647. It needs a memory
Comp-U-Learn
C Trim
more than an ordinary int and therefore takes 4 bytes for storage. To make an integer a long type,
just add the word long before it.
short
When an ordinary int has a memory value as that of long, i.e., 4 bytes, a short will have 2 bytes. If
an int has a memory value of 2 bytes, a short will have the same memory i.e., 2 bytes.
signed
The use of this qualifier is that you can supply both positive and negative numbers. The default for
an int data type is signed. A signed variable will have the same positive or negative ranges as
assigned to a regular int.
unsigned
This qualifier is marked with the int data type, and has the same memory requirement as that of
the int (2 bytes). The only difference between the int and unsigned data types is the range. The
range is double than the range of an ordinary int data type. The range of this type is 0 to 65,535.
T HE V ARIABLE IN C
When you are enrolled for a computer course in an institute, you are allotted an ID number , as
soon as you take the admission,. Why do think they give you this number? Whenever, you pay the
fee or the faculties need to update your performance status, your record should be accessed easily
from the database. To access a particular record easily, you will be allotted the ID number.
This ID number will be given a variable name so that you can call a number using its variable.
Since the memory is very vast and storing such a small number (ID number) is impossible, you
need a variable to identify this number. Whenever the need arises for the usage of this number, it
can be recalled from the memory location with the help of variable name.
Variable Names
The naming of the variables in C is case sensitive. The distinct forms of the variable names are
video_id, Video_Id and VIDEO_ID. It can also be a combination of both the cases like video_ID.
The naming of the variable is very important. For example, if you are declaring two variables of
integer data type which are to be added to a third variable which is also an integer variable, you
name the two variables in such a way that it can be understood that they are numbers which will
Comp-U-Learn
Data types and Operators
perform some arithmetic function. Similarly, if you name the third variable by a meaningful
name (say, sum), you can easily understand that this variable stores the sum of the first two
variables. In the same manner, you can follow the naming of the variables to character data types
and any other types of data.
EXAMPLE name
City
city _ name
The variable name can be a combination of alphabets, digits and underscores. The maximum
length of the variable name is 8 characters.
Commas, blank spaces or special symbols should not be included in a variable name.
Type Declaration
The assignment of data type to the variable name is called Type Declaration. The variables can be
assigned to any of the data types. It means that the variable can be assigned to store an integer, a
floating-point number or a character.
Type declaration should be done at the beginning of the program after opening the brace. By
declaring the variable at the start of the program makes C programming very unsophisticated.
This is because once the variable is declared at the beginning, the same variable can be used any
number of times in the program and each time its data type will be the same as it was declared in
the beginning.
main()
{
type variable name ();
/* remaining program */
}
Comp-U-Learn
C Trim
If more than one variable name has to be assigned to which the same data type can be assigned, it
can be done in the following way:
The data types should be performed as depicted above i.e., int cannot be written as integer and in
the same way, char cannot be written as character
All the declarations should be performed before the first executable statement
FUNCTIONS
As most of the languages provide functions, similarly, C also provides functions. The difference
being C regards main() as function. Also unlike some languages, like Pascal, C does not have
procedures it uses functions to serve both requirements.
type function_name( )
{
local variables
function code
}
Comp-U-Learn
Data types and Operators
main()
{
int video1=5, video2=16, cost;
cost=gettotal(video1, video2)
printf(“Total =%I”, cost)
}
C has various functions that are helpful while you write a program. Before you use a function, C
must have knowledge of the type it returns and the parameter types the function it expects. How
this can be done depends on the scope of the function.
Initially, if a function has been defined before it is called, you can just use the function. If not, then
you must declare the function. The declaration honestly states the type the function returns and
the type of parameters used by the function. It is usually good practice to prototype all functions
at the beginning of the program. Ofcourse, it is not a strict rule to be followed. To declare a
function prototype, simply state the type the function returns and the function name.
In C, the keyboard is considered to be the standard input device. Similarly, the monitor is assumed
to be the standard output device and the standard error device. To perform standard input-output
operations, C uses stdin, stdout and stderr as a tool for accessing the devices. C does not provide you
with any input-output operations. These operators exist as functions are written in C and these
standard input and output functions are available in the standard C library along with other
functions.
Comp-U-Learn
C Trim
C has two formatted input-output functions in the form of scanf() and printf(). These functions
when used as parameters accept a format specification string and a list of variables. The format
specifier string is a character string that specifies the data type of all variables to be input or output
and the size or width of the input or output.
Formatted Output
Based on the format specification, the formatted output function that is used in C is printf(). The
parameters to the printf() function are the format specification string and the data to be output.
The syntax of the printf() function is:
Here, %s is the format specification string used for a string of characters and Name is the data
name.
#include<stdio.h>
main()
{
int customer_age;
char customer_sex;
age=20;
sex=M;
printf(“Age of the customer is %d; and Sex is %c”, &age, &sex);
}
Output:
Comp-U-Learn
Data types and Operators
Formatted Input
As you have the prinf() function as the formatted output function, similarly, you have scanf() as
the formatted input function. The syntax of the scanf() function is:
Here, %c is the format specification string used for a character. Similarly, %d is used for a
signed decimal integer and %s is used for a string of characters. The data names should be
preceded by & as you noticed (&sex and &age) in the scanf() function. However, the string type
data names should not be preceded by &.
1. #include<stdio.h>
2. main()
3. {
4. char customer_name[20];
5. int customer_age;
7. scanf(“%s”, customer_name);
9. scanf(“%d”, &customer_age);
12. }
In this program, you will declare a character array customer_name, of size 20 in the line 4 and an
integer variable customer_age in the line 5. The user will be asked to enter the customers name
and age in lines 6 and 8. Lines 7 and 9 accept the input entered. Lines 10 and 11 print the output.
Comp-U-Learn
C Trim
The table below shows some of the format specificiers you use in C.
FORMAT SPECIFIER
CODE FORMAT
%c Character
%s String of characters
%x Unsigned hexadecimal
%p Displays a pointer
%% Prints a % sign
There are two functions in C called the character based input-output functions that act in the
similar way as the prinf() and scanf() functions. They are the getc() and putc() functions. The
getc() function is used to accept a parameter. It allows only one character to be read from the
device. The putc() function accepts parameter that are to be displayed as the output.
The two string based input-output functions in C are gets(() and puts() functions. The gets()
function is used to accept a parameter which is of a string type. It allows a string to be read from
the device and the puts() function accepts the string variables that are to be displayed as the
output. The puts() function makes the cursor jump to the next line after printing the string.
O PERATORS
Operators are defined as the tools used for solving various mathematical, conditional, relational
and logical problems. The operators you mainly deal with are the arithmetic operators, assignment
operators, unary operators, relational and logical operators. Before you go further, you should
know of the OPERAND. Operands are defined as variables in between which you place the operators.
Comp-U-Learn
Data types and Operators
Arithmetic Operators
The basic arithmetic operators used for performing arithmetic are the same in many computer
languages:
ARITHEMETIC OPERATORS
+ addition
- subtraction
* multiplication
/ division
% modulus (remainder)
The arithmetic operators are the tools that help you in computing various mathematical
operations. The + operator is used for adding two integers. The - operator can be used in two ways.
To subtract two numbers (as in a - b), or to negate one number (as in -a + b or a + -b). * is the
operator used to perform multiplication of two numbers.
When applied to integers, the division operator / omits any remainder. So, 1 / 4 is 0 and 7 / 2 is 3.
But when either operand is a floating-point quantity (type float or double), the division operator
yields a floating-point result, with a potentially nonzero fractional part. So 1.0 / 4.0 is 0.25 and 7.0
/ 2.0 is 3.5. The modulus operator % gives you the remainder when two integers are divided. For
example, 1 % 4 is 1 and 7 % 2 is 1. The modulus operator can only be applied to integers.
An additional arithmetic operation you might be surprised about is exponentiation. Some languages
have an exponentiation operator (usually ^ or **), but C does not. To square or cube a number, just
multiply it by itself.
Multiplication, division, and modulus have higher priority than addition and subtraction. In
mathematics, multiplication has higher precedence than addition. This is shown in a tabular
form as shown below:
2+5*4 = 2 + 20 = 22
Comp-U-Learn
C Trim
2+5*4 = 7 * 4 = 28
All of these operators group from left to right. This means that when two or more of them have the
same precedence and occur next to each other in an expression, the assessment proceeds from left
to right. For example, 2 5 7 is equal to (2 5) 7 which gives 10 but not +4.
Assignment Operators
num = 5
a=b
The expression, i = i + 1, is the standard programming idiom for increasing a variables value by 1.
This expression takes the old value of i, adds 1 to it, and stores it back into i. C provides several
shortcut operators for modifying variables in this and in similar ways, which you will learn later.
You call the = sign, the assignment operator and is referred to as an assignment expression.
Moreover, = is an operator just like + or -. C does not have assignment statements. Instead, an
assignment like a = b is an expression and can be used wherever any expression can appear. Since
its an expression, the assignment a = b has a value and the same value that is assigned to a. This
value can then be used in a larger expression. For example, you might write
x=y=z
which is equivalent to z = (x = y)
and assign ys value to both x and z. Therefore the assignment operator groups from right to left.
Later you will see other areas in which it can be useful to use the value of an assignment expression.
Comp-U-Learn
Data types and Operators
Unary Operators
The operator that operates on a single operand is called an Unary operator. The two basic unary
operators are the increment operator and the decrement operator. Incrementing or decrementing
by 1 is a simple computation in C. For example,
sum = a++;
and sum = ++a;
In the 1 statement,
st
sum = a;
a = a+1;
In the 2nd statement,
a = a+1;
sum=a;
The same procedure is followed for decrementing too. You cannot use ++ or on the left side of an
assignment i.e.,
a++ = sum; is not valid.
Relational Operators
These are the operators that will assist you during the computation of mathematical conditions.
They establish a relation between the operands when they are used between them. They are helpful
in finding out whether the 1st operand is less than, greater than or equal to the other operator.
Some of the relational operators used in C are given in the table below.
R ELATIONAL O PERATORS
OPERATOR SIGNIFICANCE
< Less than
<= Less than or equal to
> Greater than
>= Greater than or equal to
== Equal to
!= Not equal to
Comp-U-Learn
C Trim
Logical Operators
Logical operators are usually used with conditional statements. The logical operators mostly used
in C are AND, OR and NOT.
L OGICAL O PERATORS
OPERATOR SIGNIFICANCE
&& And
|| Or
! Not
The AND operator becomes true if and only if both the operands satisfy the condition. The OR
operator becomes true if at least one of the operand satisfies the condition. The NOT operator gives
the negative value of the given operand always.
C ONSTANTS IN C
A constant is just an immediate, absolute value found in an expression. Constants are created in
C in order to use them any number of times. The simplest constants are decimal integers, eg 0, 1,
2, 123. Rarely, it is useful to specify constants in base 8 (octal) or base 16 (hexadecimal). This is
done by prefixing an extra 0 (zero) for octal, or 0x for hexadecimal. The constants 100, 0144, and
0x64 represent the same number. If you are not using these non-decimal constants, just remember
not to use any leading zeroes. If you accidentally write 0123 intending to get one hundred and
twenty three, you will get 83 instead, which is 123 base 8.
A constant is assigned the type long int by suffixing it with the letter L or l. Upper case is strongly
recommended because a lower case l looks too much like the digit 1.
A constant that contains a decimal point or the letter e (or both) is a floating-point constant:
3.14, 0.01, 123e4, 123.45e67. The e indicates multiplication by a power of 10. 123.45e67 is 123.456
times 10 to the 67th, or 1,234,500,000. By default, floating-point constants are of type double.
We also have constants for specifying characters and strings. A character is exactly one character
and a string is a set of zero or more characters. A string containing one character is distinct from
Comp-U-Learn
Data types and Operators
a lone character. A character constant is simply a single character between single quotes: Z, :, ^.
The numeric value of a character constant is that characters value in the machines character set.
Within character and string constants, the backslash character \ is special, and is used to represent
characters not easily typed on the keyboard or for various reasons not easily typed in constants.
The most commonly used constants of these types are:
\b A backspace
\r A carriage return
\ A single quote
\ A double quote
\\ A single backslash
The \n in the above statement makes the cursor move to the starting position of the next line.
t Primary constants
t Secondary constants
However, you will discuss only the primary constants at this point of juncture.
t Integer constant
t Real constant
t Character constant
Comp-U-Learn
C Trim
t Array
t Pointer
t Structure
t Union
t Enum
A character constant is either a single alphabet or a single digit or single special symbol inserted in
between single inverted commas pointing towards left.
The maximum length of a character constant can be only 1 character but cannot exceed that.
Comp-U-Learn
Data types and Operators
A PROGRAM IN C
The following program is filled with variables, data types, input/output functions and some other
details.
Write a program to accept two IDs of the videos and display them on the screen
1. /* Program to accept two Video IDs and print them on the screen */
2. #include<stdio.h>
3. void main()
4. {
12. }
Comp-U-Learn
C Trim
Extended Reference
http://www. eskimo.com/~scs/cclass/notes/top.html
http://www.cm.cf.ac.uk/Dave/C/CE.html
T HE Z ERO H OUR
Q: What are the various fundamental data types inC language?
t int
t char
t float
t double
Ans: Arithmetic operators, unary operators, relational operators and logical operators.
Ans: Addition (+), Subtraction (-), Multiplication (*), Division (/) and Modulus (%, used to find
the remainder) are the various arithmetic operators used in C language.
Ans: Increment and decrement are the two basic unary operators used in C.
Q: Which back slash command makes the cursor skip to the next line in an output statement?
Ans: \n.
Comp-U-Learn
Data types and Operators
T HE S ESSION G UIDE
The Session Guide helps you time your session with the help of the slides provided. Beneath each
image of the slides is the time given to be spent on each slide while it is being displayed. The slide
will thus, guide you through the contents of the session with ease.
Programming Using C
Slide 9
Comp-U-Learn
Display Time
When you display this slide, you will discuss about various types of data types and operators. You
need to spend about 8 minutes.
Road Map
Slide 2
Comp-U-Learn
Display Time
This slide is used to show all the topics to be covered in this session. You can take about 10 minutes
for this slide.
Comp-U-Learn
C Trim
Programming Using C
Slide 3
Comp-U-Learn
Display Time
All the data types should be discussed when this slide is on display. Spend at least 5 minutes with
this slide.
Qualifiers
long unsigned int short unsigned int signed char unsigned char
Slide 4
Comp-U-Learn
Display Time
The qualifiers are discussed. Take about 17 minutes to explain the qualifiers.
Comp-U-Learn
Data types and Operators
The Continuation . . .
long u nsign ed int long signed in t short sig ned int sh ort un sig ned in t sign ed ch ar un sig ned char
Slide 5
Comp-U-Learn
Display Time
Variables
Slide 6
Comp-U-Learn
Display Time
When this slide is displayed, you discuss about variables and its rules. You have to spend 8 minutes
with this slide.
Comp-U-Learn
C Trim
Type Declaration
es:
Rul
• All data types are keywords and should be written in lower case
Slide 7
Comp-U-Learn
Display Time
The rules of type declaration are discussed in this slide. Spend at least 5 minutes with this slide.
The printf()
Slide 8
Comp-U-Learn
Display Time
Take about 4 minutes to deal with this slide. It explains the output function.
Comp-U-Learn
Data types and Operators
The scanf()
Display Time
This slide explains the input function and its format specifiers. Take about 10 minutes to discuss
this slide.
Operators in C
O perators
Slide 10
Comp-U-Learn
Display Time
You take just 2 minutes for this slide. It shows the various operators that are used in C language.
Comp-U-Learn
C Trim
Slide 11
Comp-U-Learn
Display Time
Take about 10 minutes with this slide because you have to discuss various types of arithmetic
operators.
Unary Operators
U n a ry
in c re m e n t d e c re m e n t
Slide 12
Comp-U-Learn
Display Time
When this slide is displayed you need about 7 minutes to explain the two-unary operators.
Comp-U-Learn
Data types and Operators
Relational Operators
Relational
less than(<) less than or equal to(<=) greater than(>) greater than or equal to(>=)
ns:
ctio
i c Fun • Used in mathematical computations
Ba s
• Takes only two operands
• Compares the two operands
Slide 13 Comp-U-Learn
Display Time
You will discuss the various relational operators here and you can spend about 5 minutes here.
Logical Operators
L o g ic a l
A N D (& & ) O R ( || )
( x<15 || y= =8)
Slide 14
Comp-U-Learn
Display Time
The logical operators are discussed when this slide is displayed. You can spend 3 minutes with this
slide.
Comp-U-Learn
C Trim
Constants in C
C o n s t a n ts
p rim a ry s e c o n d a ry
in t e g e r a rra y
re a l p o in t e r
c h a ra c t e r s tru c tu re
u n io n
en u m
Slide 15
Comp-U-Learn
Display Time
When this slide is displayed, you will discuss the primary and secondary constants. Spend at least
5 minutes here.
Construction of Constants
Integer
Display Time
The construction of the constants will be discussed. You need to spend nearly 11 minutes.
Comp-U-Learn
Data types and Operators
Construction of Constants
Real
Slide 17
Comp-U-Learn
Display Time
Construction of Constants
Character
Display Time
Comp-U-Learn
C Trim
Slide 19
Comp-U-Learn
Display Time
The Going over it slide finishes this session and explains briefly what has been covered in this
session. You need to discuss this slide for about 10 minutes.
Comp-U-Learn