You are on page 1of 23

APTITUDE TRAINING

(Technical Part)
At 6th Semester of Graduate Programme

in all Branches of Engineering / Technology

Instructor : Dr. Somsubhra Gupta, IT Dept. JISCE


Venue: Dr. B.C. Roy Auditorium.

Technical Aptitude Training JISCE

2015

Technical Session
Learning Objective:
To be able to identify and address basic real world problems
To be able to solve them using elementary C programs
To be conversant on Elementary Programming concept
Session Plan:
C 1 C Basics C 2 If statements C 3 Loops C 4 Functions
C 5 Switchcase C 6 Pointers C 7 Structures C 8 Arrays
C 9 Strings C 10 Input /
Output
C++ 1 Structures of C++ programming C++ 2 Basic Syntax Rules
C++ 3 Elementary
C
e e a y I/O
/O
C++ 4 Co
C
Conditional
d o a Statements
Sae e s
Lesson Duration: 1 and our each.
Expected Outcome: To be able solve problems by writing codes
Technical Aptitude Training JISCE

2015

Technical Session
Mode of Delivery:
Audio
A dio Vis
Visual
al thro
through
gh Po
Power
er Point presentation (topic wise)
ise)
Demonstration of programmers using any C language compiler
Explanation through MarkerMarker-White Board
Training Plan:
Discussion of Session topic in brief as everyone cleared C as a credit course
in 2nd Semester of their respective
p
B. Tech. p
programme
g
Writing programs on Board / Editor from previous day assignments
Solving week
week--wise (all) assignments religiously.
Encouraging content beyond assignments.
Training Evaluation (in
(in selective sessions):
MCQ question
CI technical
h i l test patterns questions
i
Technical Aptitude Training JISCE

2015

C Language Preliminaries
i i
i
A C program contains one or more functions
main() is the function name of your main (root) program
{ }: braces (left & right) to construct a block containing the
statements of a function
Every statement must end with a ;
\ is called an escape character
\n is an example of an escape sequence which indicates newline
Other escape sequences are: \t \r \a \\ \
Exercise: Use any editor to type and then save your first program
as main.c
% gcc main.c
a out
and observe its result.
result
% a.out
Technical Aptitude Training JISCE

2015

Id ifi
Identifiers

Begin with a letter or underscore: A-Z, a-z, _

The rest of the name can be letters


letters, underscore,
underscore or digits
Guarantee that east least the first 8 characters are significant (those
come after the 8th character will be ignored) while most of C
compiler allows 32 significant characters.
Example:
_abc
ABC
Time time
_a1 abcdefgh
abcdefghi (may
(
bbe the
h same as abcdefgh))

Case sensitive
Keywords: reserved names (lexical tokens)
auto
double
int
struct
char extern
return
union

Technical Aptitude Training JISCE

if
static
break
case entry
long
register
typedef
do
go
sizeof
2015

else
switch
float
continue
5

Datatype
Four Data Types (assume 2s complement, byte machine)
Data Type

char

Abbreviation

Size
(byte)

Range

char

-128 ~ 127

unsigned char

0 ~ 255

2 or 4

-215 ~ 215-1 or -231 ~ 231-1

2 or 4

0 ~ 65535 or 0 ~ 232-1

int

int

unsigned int

unsigned

short int

short

-32768
32768 ~ 32767

unsigned short int

unsigned short

0 ~ 65535

long int

long

-231 ~ 231-1

unsigned long int

unsigned long

0 ~ 2332-1

float

double

Technical Aptitude Training JISCE

2015

V i bl D
Variable
Declarations
l
i

type v1,v2,v3, , vn
Example:

int i;
int j;
float k;
char c;
short int x;
long int y;
unsigned int z;
int a1, a2, a3, a4, a5;

Technical Aptitude Training JISCE

2015

Literal

Numeric Character,
Numeric,
Character String,
String Literals

Numeric literal
fixed-point
octal
hexadecimal
decimal int
long (explicit)

O32 (= 24D) (covered later)


OxFE or Oxfe (=254D) (covered later)
32
32L or 32l

an ordinary integer literal that is too long to fit in an int is


also too long for long
fl ti
floating-point
i t
No single precision is used; always use double for literal
Example:
1.23
123.456e-7
0.12E
Technical Aptitude Training JISCE

2015

Numeric Character,
Numeric,
Character String,
String Literals

Character literal (covered later)

American Standard Code for Information Interchange (ASCII)


Printable:
single space
32
0 - 9
A - Z
a - z
z
a

48 - 57
65 - 90
97 - 122

Nonprintable and special meaning chars


\n
\\
\\
\0
\f
\

new line
back slash
null
formfeed
double quote

10
9
0
12
34

\t
\
\
\b
\r

tab
single
g quote
q
back space
carriage return

9
39
8
13

\ddd arbitrary bit pattern using 1-3 octal digits


\Xdd for Hexadecimal mode
\017 or \17
Shift-Ins, ^O
\04 or \4 or \004 EOT (^D)
\033 or \X1B
<esc>
Technical Aptitude Training JISCE

2015

Numeric Character,
Numeric,
Character String,
String Literals

String Literal

will be covered in Array section


String is a array of chars but ended by \0
String literal is allocated in a continuous memory space
of Data Segment, so it can not be rewritten
Example:
a p e:
ABCD
C
A

\0

...

4 chars but takes 5 byte spaces in memory

Question: I am a string takes ? Bytes


Ans: 13+1 = 14 bytes
Technical Aptitude Training JISCE

2015

10

Numeric Character,
Numeric,
Character String,
String Literals
Character literals & ASCII codes:
char x;
x=a;

/* x = 97*/

Notes:
a and a are different; why?
a is the literal 97
a is an array of character literals, { a, \0} or {97, 0}
a + b +c is invalid but a+b+c = ? (hint: a = 97 in ASCII)
a + b + c = 97 + 98 + 99 = 294 = 256 + 38
in the memory

38

if the code used is not ASCII code,, one should check out each
value of character
Technical Aptitude Training JISCE

2015

11

I i i li i
Initialization

If a variable is not initialized, the value of variable


may be either 0 or garbage depending on the storage
class of the variable.
int i=5;
float x=1.23;
char c=A;
int i=1, j,k=5;
char
h
c1
1 = A
A, c2
2 = 97
97;
float x=1.23, y=0.1;

Technical Aptitude Training JISCE

2015

12

Memory Concepts

Each variable has a name, address, type, and value


1)int x;
2)scanf(%d, &x);
3) user inputs 10
4)x = 200;
Aft
After
th
the execution
ti
of
f (1)

After the execution of (2) x


After the execution of (3) x
After the execution of (4) x

10
200

Previous value of x was overwritten


Technical Aptitude Training JISCE

2015

13

Sample Problems
Write a program to take two numbers as input data and print their
sum,, their difference,, their pproduct and their qquotient.
Problem Inputs
float x, y;
/* two items */
problem Output
float sum;
/* sum of x and y */
float difference; /* difference of x and y
*/
float product;
float quotient;

/* product of x and y */
/* quotient of x divided

by y */

Technical Aptitude Training JISCE

2015

14

Sample Problems (Contd..)

Pseudo Code:
Declare variables of x and y;
y
Prompt user to input the value of x and y;
Print the sum of x and y;
Print the difference of x and y;
Print the product of x and y;
If y nott equall to
t zero, print
i t the
th quotient
ti t off x divided
di id d by
b y

Technical Aptitude Training JISCE

2015

15

Example program

#
#include
<stdio.h>
int main(void)
{
float x,y;
float sum;
printf(Enter the value of x:);
scanf(%f, &x);
printf(\nEnter the value of y:);
scanf(%f, &y);
sum = x + y;
printf(\nthe sum of x and y is:%f,sum);
printf(\nthe
printf(
\nthe sum of x and y is:%f,x+y);
is:%f ,x y);
printf(\nthe difference of x and y is:%f,x-y);
printf(\nthe product of x and y is:%f,x*y);
if (y != 0)
printf(\nthe
i tf(\ th quotient
ti t of
f x di
divided
id d b
by y i
is:%f,x/y);
%f / )
else
printf(\nquotient of x divided by y does not exist!\n);
return(0);
}

Technical Aptitude Training JISCE

2015

16

R l #1
Rule

T
Type
conversion
i

char, short
float

Rule #2

int

double

((double longg unsigned


g
int))

If either operand is double, the other is converted to


double, and the result is double
Otherwise, if either operand is long, the other is
converted to long, and the result is long
Otherwise, if either operand is unsigned, the other is
converted to unsigned, and the result is unsigned
Otherwise, the operand must be int
Technical Aptitude Training JISCE

2015

17

Examples
Example: c: char,
u: unsigned, i: int, d: double, f:float,
s: short,
, l: long,
g,
Expression
c s / i
u * 3 i

Final Data Type

int
unsigned

Explanation
shortint, int/int, charint, int-int
int(3)unsigned
int(3)unsigned,
unsigned*unsigned=unsigned,
intunsigned, unsigned-unsigned=unsigned

u * 3.0 i
c + i
c + 1.0
1 0
3 * s * l

double
int
do ble
double
long

Technical Aptitude Training JISCE

unsigneddouble, double*double,
double double,
intdouble, double-double=double
charint
char
h int
i t (rule
( l 1)
1), intdouble(rule
i td bl ( l 2)
shortint, int*int, intlong, long*long

2015

18

Note:

Type conversion (contd..)


1. Conversion of int to long preserves sign, so does
short
2.Var = expr
f = d; /* round off */
i = f;
/* truncates fractions part
part, if the number is too big to fit,
fit the
result is undetermined */
i = l; s = i; and c = i; /* may eliminate
high order bits */

Technical Aptitude Training JISCE

2015

19

3.

If a specific type is required, the following syntax may be used, called cast
operator.

(type) expr
Example:
float f=2
f=2.5;
5;
x = (int)f + 1;
/* the result is 3, Q: will f value be changed? */

4.

Unsigned int to int:


there is not actual conversion between int and unsigned int.
Example
p :(Assuming
(
g 2s complement
p
machine and int is 2 bytes
y long)
g)
unsigned i = 65535; int j;
j = i;
/* j will be 1 */
j = -2;
2;
unsigned i = 1 + j;
/* i= 65535 */

Technical Aptitude Training JISCE

2015

20

PracticeCode
d
1.
2.
3.
4.
5.
6
6.
7.

Find
i d average marks
k off Student
d
Greatest of 3 numbers using conditional operator
Biggest
gg of two numbers in C
Convert CM to feet and inches
Even or odd program in C
Simple interest calculation
Swapping of two Numbers

Technical Aptitude Training JISCE

2015

21

Assignment:Day1
1. Write a Program to compute the summation up to a number
2 Write
2.
W i a program to compute factorial
f
i l off a given
i
number
b
3. Write a program to compute sum of all the digits of a number
4. Write a program to compute whether a number is even or odd
5. Write a program to compute the mean, variance and standard
deviation from a list of number

Technical Aptitude Training JISCE

2015

22

You might also like