C Session2

Download as pdf or txt
Download as pdf or txt
You are on page 1of 29

Session 2

Data Types and Operators

l Data Types
l The variables in C
l Operators
l A program in C
Data types and Operators

In the first session, you got aquainted with the origin


and basics of the C language. In the following pages,
you will be well versed with the basic data types, the
qualifiers used with the basic data types, the variables
used in C, type declaration, output function, the
operators (Arithmetic and unary operators) and
relational and logical operators.
t DATA TYPES

The type of any variable determines the kind of value


t DERIVED DATA TYPES
that the variable takes. An expression consists of the
variables, constants and operators (computes new
t THE VARIABLE IN C values out of the old ones) which perform some useful
computations.
t FUNCTIONS
As Kernighan and Ritchie say, “The type of an object
t OPERATORS determines the set of values it can have and what
operations can be performed on it”. This definition is a
t CONSTANTS IN C conventional mathematical definition that defines
what a type is.
t A PROGRAM IN C
In C, the int type cannot represent all the calculations
involving integers. Similarly, the float type cannot
represent all floating-point numbers.

While using a variable of some type, you must


remember what value it can take and what operations
you can perform on it. For example, there are several
operators that play with the binary (bit-level)
representation of integers. These operators cannot be
applied to floating-point operands.

Before declaring and selecting a type for a new variable,


you have to keep in mind the values and operations
you will need. Therefore picking a type for a variable is
not some hypothetical exercise. It is connected to the
ways you will be using that variable.

Comp-U-Learn
C Trim

D ATA TYPES

The data types in C language are classified into two categories as under:

t Fundamental data types


t Derived data types

Fundamental Data types

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:

D ESCRIPTION OF THE F UNDAMENTAL D ATA T YPES


DATA TYPE N UMBER OF BYTES RANGE FORMAT
REQUIRED SPECIFIER

int 2 -2E31 to (2E31)-1 %d, %i

char 1 -128 to 127 %c, %s

float 4 6 digits of accuracy %f

double 8 2.2E-308 to 2.2E308

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., it’s usual size is 8 bits.

D ERIVED D ATA TYPES (Q UALIFIERS USED IN C)


A derived data type is represented in the memory as a fundamental data type. It is based on the
fundamental data type. The efficiency of these data types is because they are more flexible and
adaptable to get into various situations with correctness and accuracy. Some of the frequently
used derived data types or qualifiers are short, long signed and unsigned.

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 it’s 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.

The first character of a variable name should always be an alphabet.

Commas, blank spaces or special symbols should not be included in a variable name.

A final restriction on names is that you should not use keywords.

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.

Syntax of Type Declaration

main()
{
type variable name ();
/* remaining program */
}

Comp-U-Learn
C Trim

EXAMPLE int idnumber;


char name;
float sum;

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:

EXAMPLE int idnumber, age, D_O_B;


char name, address;
float sum, interest;

Rules for Type Declaration

All the data types should be written in lower case

All the data types are the keywords

These data types cannot be used as variable names

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

All declarations must end with a semicolon

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.

The basic form of a function is:

type function_name( )
{
local variables
function code
}

Comp-U-Learn
Data types and Operators

EXAMPLE : This is an example to find the product of two integers:

int gettotal(int video1, int video2)


{
int total;
total=video1 * video2;
return(total); /* The return statement passes the result back to the main
program. */
}

You would call the function as follows:

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.

Standard Input and Output Functions

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

Formatted Input-Output Functions

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:

printf( format specification string, data1, data2, data3,…….);

EXAMPLE : printf(“%s”, customer_name);

Here, “%s” is the format specification string used for a string of characters and “Name” is the data
name.

EXAMPLE : Write the output of the following:

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

The output of the above example is:

Age of the customer is 20; and Sex is M

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:

scanf(format specification string, data1, data2, data3,………);

EXAMPLE scanf(“%c %d %s”, &sex, &age, Name);

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 &.

EXAMPLE Write a function to accept the name and age of a customer.

1. #include<stdio.h>

2. main()

3. {

4. char customer_name[20];

5. int customer_age;

6. printf(“Enter the customer’s name \n”);

7. scanf(“%s”, customer_name);

8. printf(“Enter the customer’s age \n”);

9. scanf(“%d”, &customer_age);

10. printf(“The customers name is %s”,customer_name);

11. printf(“The customers age is %s, 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 customer’s 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

%d, %i Signed decimal integers

%f Decimal floating point

%s String of characters

%u Unsigned decimal integers

%x Unsigned hexadecimal

%p Displays a pointer

%% Prints a % sign

Character based Input-Output Functions

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.

String based Input-Output Functions

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:

You can perform the calculation of 2 + 5 * 4 as:

2+5*4 = 2 + 20 = 22

Comp-U-Learn
C Trim

This can also be written as:


2 + (5 * 4) But not as:

2+5*4 = 7 * 4 = 28

But not as: (2 + 5) * 4

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

The assignment operator = assigns a value to a variable. For example,

num = 5

sets num to 5 and

a=b

sets a to whatever b’s value is.

The expression, i = i + 1, is the standard programming idiom for increasing a variable’s 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
it’s 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 y’s 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 character’s value in the machine’s character set.

A string is represented in C as a sequence or array of characters. A string constant is a sequence of


zero or more characters enclosed in double quotes: “Welcome”, “How are you?”, “Take a test”.

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 ACIK S LASH C OMMANDS


B A CKS L A S H COMMANDS DESCRIPTION
\n A new line

\b A backspace

\r A carriage return

\’ A single quote

\” A double quote

\\ A single backslash

EXAMPLE printf(“Welcome to COMP-U-LEARN \n”);

The “\n” in the above statement makes the cursor move to the starting position of the next line.

Further, the constants in C are divided into two categories.

t Primary constants
t Secondary constants
However, you will discuss only the primary constants at this point of juncture.

The primary constants are of three types. They are:

t Integer constant
t Real constant
t Character constant

Comp-U-Learn
C Trim

There are many secondary constants. Few of them are:

t Array
t Pointer
t Structure
t Union
t Enum

Rules to be followed for constructing Integer Constants

t The integer constant can be either positive or negative.

t It should have at least one digit.

t It must be a whole number .

t Blank spaces or punctuations are not permitted within an integer constant.

t The range permitted for integer constants is –32768 to +32768.

Rules to be followed for constructing Real Constants

t A constant must have at least one digit and a decimal point.

t It can be either positive or negative. The default is positive.

t Punctuation or blank spaces are not permissible.

Rules to be followed for constructing Character Constants

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.

EXAMPLE ’A’ but not ‘A’

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 ID’s 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. {

5. int video_id 1, video_id2;

6. printf(“Enter the value of 1st video_id ”);

7. scanf(“%d”, & video_id 1);

8. printf(“Enter the value of video_id 2”);

9. scanf(“%d”, & video_id 2);

10. printf(“The ID of 1st video is is %d \n”, video_id 1);

11. printf(“The ID of 2nd video is %d \n”, video_id 2);

12. }

Line 1 This is the comment line


Line 2 This is a standard statement which helps to access the library files
Line 3 Declaration of the main() function
Line 4 Start of the program
Line 5 Declaring the variables
Lines 6 and 8 Output statements that request the user to enter the values for video_id 1and
video_id 2
Lines 7 and 9 Input statements that read the values entered by the user
Lines 10 and 11 Output statements that retrieve the stored value and print it back on the screen
Line 12 Program terminator

Comp-U-Learn
C Trim

URL S FOR D ATA TYPES AND O PERATORS

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?

Ans: The various fundamental data types in C language are:

t int
t char
t float
t double

Q: What are the various qualifiers in C?

Ans: long, short, signed, unsigned.

Q: What is the formatted output function used in C?

Ans: printf is the formatted output function used in C.

Q: What are the different types of operators used in C?

Ans: Arithmetic operators, unary operators, relational operators and logical operators.

Q: Name the various arithmetic operators of C language.

Ans: Addition (+), Subtraction (-), Multiplication (*), Division (/) and Modulus (%, used to find
the remainder) are the various arithmetic operators used in C language.

Q: What are the two basic unary operators 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

Data Types and operators

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

• Basic Data Types • The input function and


• Qualifiers used with Format Specifiers
Basic datatypes • Arithmetic Operators
• The Variables in C • Unary Operators
• Type Declarations • Relational and Logical
• Output Function operators
• Constants in C

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

Data Types and operators

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.

Data Type Qualifiers

Qualifiers

long short signed unsigned

long unsigned int short unsigned int signed char unsigned char

long signed int short signed int

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

L ong S hort S igned U nsigned

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

0 to -2147483648 to 0 to 65535 -32768 to + 32768 -128 to +128 0 to 255


4294967295 +2147483648 2 bytes 2 bytes 1 byte 1 byte
4 bytes 4 bytes %u %d %c %c
%lu %ld

Slide 5
Comp-U-Learn

Display Time

This is the continuation of the previous slide.

Variables

Rules for using the variables:

• Maximum length should be of 8 characters.

• It should contain only alphabets , digits and underscores.

• It should start with an alphabet.

• Only underscores are permitted in between names.

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

• The data types cannot be used as variable names

• All declarations should be done before the first executable statement

•All declarations must end with a semicolon.

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 Output Function

The printf()

• Displays the message on the console

• The message has no effect on the program.

• Also used to prompt the user from the console.

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 Input Function

The scanf()

• Accepts values from the console


• It uses the format specifiers

The Format Specifiers


Code Format
%c Character
%d, %I Signed decimal integ ers
%f Decimal floating point
%s String of characters
%u Unsigned decimal integers
%x Unsigned hexad ecimal
%p Displays a pointer
Slide 9 %% Prints a % sign
Comp-U-Learn

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

arithm etic unary relational logical

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

The Arithmetic Operators

A rith m etic • Aids in mathematical computations


• Requires 2 operands.
ad d ition (+ ) • Operands must be of same data type.

su b traction (-) Example:


Expression Value
m u ltip lic ation (*) X+y 13
x-y 5
d ivision ( / )
X*y 36
X/y 2
m od u lu s(% )
X%y 1

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

• It has a sign ++ • It has a sign - -


• It increases the value by one • It decreases the value by one

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

•Takes two operands •Takes two operands


•Is true if operands •Is true if operands
satisfy the condition satisfy the condition
Example:
(x<15 && y= = 8)

( 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

⇒ can be either positive or


negative.

⇒ It must be a whole number.

⇒ Punctuation or blank spaces are


Slide 16
not permitted.
Comp-U-Learn

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

⇒ It must have at least one digit and a decimal point.

⇒ It can be either positive or negative but default is positive

⇒Punctuation or blank spaces are not permissible.

Slide 17
Comp-U-Learn

Display Time

Continuation of the constructions of the constants slides.

Construction of Constants
Character

⇒ A character constant is either a single


alphabet or a single digit or single special
symbol.

⇒ It should be in single inverted commas


both pointing towards left.

⇒ The maximum length of a character


constant can be only 1 character but cannot
exceed that.
Slide 18
Comp-U-Learn

Display Time

Continuation of the constructions of the constants slide.

Comp-U-Learn
C Trim

Going Over it Again

• Basic Data Types in C include Int,Char,Float,Double.


• Data Type Qualifiers are long,short,signed,unsigned
• Assigning variables to store data types is known as Type
Declaration
• Four types of operators in C Arithmetic.,Logical,Relational
and Unary.
• C contains two types of constants primary and secondary
• Primary constants are integer,real and character
• Secondary constants in C are
array,pointer,union,structure,union,enum.

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

You might also like