C programming: Session 1
-
Upload
ashutosh-mohanty -
Category
Documents
-
view
221 -
download
0
Transcript of C programming: Session 1
-
7/23/2019 C programming: Session 1
1/23
APTITUDE TRAINING
(Technical Part)(Technical Part)
At 6At 6thth Semester of Graduate ProgrammeSemester of Graduate Programme
in all Branches of Engineering / Technologyin all Branches of Engineering / Technology
Instructor : Dr. Somsubhra Gupta, IT Dept. JISCEInstructor : Dr. Somsubhra Gupta, IT Dept. JISCE
Venue: Dr. B.C. Roy Auditorium.Venue: Dr. B.C. Roy Auditorium.
Technical Aptitude Training JISCE 2015 1
-
7/23/2019 C programming: Session 1
2/23
Technical SessionTechnical SessionLearning Objective:Learning Objective:
To be able to identify and address basic real world problemsTo be able to identify and address basic real world problems
To be able to solve them using elementary C programsTo be able to solve them using elementary C programs
To be conversant on Elementary Programming conceptTo be conversant on Elementary Programming concept
Session Plan:Session Plan:
C 1 C Basics C 2 If statements C 3 Loops C 4 Functions C 5 Switch-
Output
C++ 1 Structures of C++ programming C++ 2 Basic Syntax Rules
C++ 3 Elementar I/O C++ 4 Conditional Statements
Lesson Duration:Lesson Duration: 1 and our each.1 and our each.
Expected Outcome:Expected Outcome: To be able solve problems by writing codesTo be able solve problems by writing codes
Technical Aptitude Training JISCE 2015 2
-
7/23/2019 C programming: Session 1
3/23
Technical SessionTechnical SessionMode of Delivery:Mode of Delivery:
u o sua roug ower o n presen a on op c w seu o sua roug ower o n presen a on op c w se
Demonstration of programmers using any C languageDemonstration of programmers using any C language compilercompiler Explanation through MarkerExplanation through Marker--White BoardWhite Board
Training Plan:Training Plan:
Discussion of Session topic in brief as everyone cleared C as a credit courseDiscussion of Session topic in brief as everyone cleared C as a credit coursein 2in 2ndnd Semester of their res ective B. Tech.Semester of their res ective B. Tech. ro rammero ramme
Writing programs on Board / Editor from previous day assignmentsWriting programs on Board / Editor from previous day assignments
Solving weekSolving week--wise (all) assignments religiously.wise (all) assignments religiously.
Encouraging content beyond assignments.Encouraging content beyond assignments.
Training Evaluation (Training Evaluation (in selective sessions):in selective sessions):
MCQ questionMCQ question
CI tec n ca test patterns quest onsCI tec n ca test patterns quest ons
Technical Aptitude Training JISCE 2015 3
-
7/23/2019 C programming: Session 1
4/23
C Language Preliminaries A C program contains one or more functions
{ }: braces (left & right) to construct a block containing thestatements of a function
Every statement must end with a ;
\ is called an escape character
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
Technical Aptitude Training JISCE 2015 4
. .
-
7/23/2019 C programming: Session 1
5/23
Begin with a letter or underscore:A-Z, a-z, _
ent ers
, ,
Guarantee that east least the first 8 characters are significant (thosecome after the 8th character will be ignored) while most of C
compiler allows 32 significant characters.Example:
_abc ABC Time time _a1 abcdefghabcdefghi (may be the same as abcdefgh)
Case sensitive
auto double if static break elseint struct case entry long switch
Technical Aptitude Training JISCE 2015 5
return union do go sizeof continue
-
7/23/2019 C programming: Session 1
6/23
atatype
Four Data Types (assume 2s complement, byte machine)Data Type Abbreviation Size
(byte)
Range
char char 1 -128 ~ 127
~
int
int 2 or 4 -215 ~ 215-1 or -231 ~ 231-1
unsigned int unsigned 2 or 4 0 ~ 65535 or 0 ~ 232
-1 -
unsigned short int unsigned short 2 0 ~ 65535
long int long 4 -231 ~ 231-1
~ 32-
float 4
double 8
Technical Aptitude Training JISCE 2015 6
-
7/23/2019 C programming: Session 1
7/23
ar a e ec arat ons
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 7
-
7/23/2019 C programming: Session 1
8/23
, , ,Literal
Numeric literal
fixed-pointoctal O32 (= 24D) (covered later)
hexadecimal OxFE or Oxfe (=254D) (covered later)
decimal int 32
long (explicit) 32L or 32l
an ordinary integer literal that is too long to fit in an int is
also too long for long
oa ng-po nNo single precision is used; always use double for literal
Example:
Technical Aptitude Training JISCE 2015 8
1.23 123.456e-7 0.12E
-
7/23/2019 C programming: Session 1
9/23
, , ,Character literal (covered later)
American Standard Code for Information Interchange (ASCII)
Printable:single space 32
0 - 9 48 - 57
A - Z 65 - 90
- -
Nonprintable and special meaning chars
\n new line 10 \t tab 9\\ back slash 9 \ sin le uote 39
\0 null 0 \b back space 8
\f formfeed 12 \r carriage return 13
\ double quote 34
\ddd arbitrary bit pattern using 1-3 octal digits
\Xdd for Hexadecimal mode
\017 or \17 Shift-Ins, ^O
Technical Aptitude Training JISCE 2015 9
\04 or \4 or \004EOT (^D)
\033 or \X1B
-
7/23/2019 C programming: Session 1
10/23
, , ,String Literal
String is a array of chars but ended by \0
of Data Segment, so it can not be rewritten
Exam le: BCD...A B C D \0
4 chars but takes 5 byte spaces in memory
Question: I am a string takes ? Bytes
Technical Aptitude Training JISCE 2015 10
Ans: 13+1 = 14 bytes
-
7/23/2019 C programming: Session 1
11/23
, , , 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
if the code used is not ASCII code, one should check out each
Technical Aptitude Training JISCE 2015 11
value of character
-
7/23/2019 C programming: Session 1
12/23
n t a zat on
If a variable is not initialized, the value of variable
may be either 0 or garbage depending on the storageclass of the variable.
int i=5;
float x=1.23;char c=A;
int i=1, j,k=5;
c ar c = , c = ;float x=1.23, y=0.1;
Technical Aptitude Training JISCE 2015 12
-
7/23/2019 C programming: Session 1
13/23
Each variable has a name, address, type, and value
2)scanf(%d, &x);
4)x = 200;
er e execu on o x
After the execution of (2)x
10
After the execution of (4)x 200
Technical Aptitude Training JISCE 2015 13
-
7/23/2019 C programming: Session 1
14/23
Write a program to take two numbers as input data and print theirsum, their difference, their roduct and their uotient.
Problem Inputsfloat x, y; /* two items */
roblem Out ut
float sum; /* sum of x and y */
float difference; /* difference of x and y*/
float product; /* product of x and y */
float quotient; /* quotient of x dividedby y */
Technical Aptitude Training JISCE 2015 14
-
7/23/2019 C programming: Session 1
15/23
Sample Problems (Contd..)
Pseudo Code:Declare variables of x and 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;
y not equa to zero, pr nt t e quot ent o x v e y y
Technical Aptitude Training JISCE 2015 15
-
7/23/2019 C programming: Session 1
16/23
#i ncl ude i nt mai n( voi d){
f l oat x, y;
pr i nt f ( Ent er t he val ue of x: ) ;scanf ( %f , &x) ;
pr i nt f ( \ nEnt er t he val ue of y: ) ;
scanf ( %f , &y) ;
sum = x + y;
pr i nt f ( \ nt he sum of x and y i s: %f , sum) ;r i nt f nt he sum of x and i s: f x+
pr i nt f ( \ nt he di f f er ence of x and y i s: %f , x- y) ;
pr i nt f ( \ nt he pr oduct of x and y i s: %f , x*y) ;
i f ( y ! = 0)
pr n e uo en o x v e y y s: , x y ;
el se
pr i nt f ( \ nquot i ent of x di vi ded by y does not exi st ! \ n) ;
return(0);
Technical Aptitude Training JISCE 2015 16
}
-
7/23/2019 C programming: Session 1
17/23
ype convers onu e
char , shor t i nt
float
doubleRule #2 (doublelongunsignedint)
If either operand is double, the other is converted to
double, and the result is doubleOtherwise, if either operand is long, the other is
converted to long, and the result is long
Otherwise, if either operand is unsigned, the other isconverted to unsigned, and the result is unsigned
Technical Aptitude Training JISCE 2015 17
Otherwise, the operand must be int
-
7/23/2019 C programming: Session 1
18/23
Example: c: char , u: unsi gned, i : i nt , d: doubl e, f : f l oat ,
s: shor t , l : l on ,
Expression Final Data Type Explanationc s / i i nt shor ti nt , i nt / i nt , chari nt , i nt - i nt
* ,
unsi gned*unsi gned=unsi gned,
intunsigned, unsigned-unsigned=unsigned
* * . , ,
i ntdoubl e, doubl e- doubl e=doubl e
c + i i nt chari nt
c . u ar n r u e , n ou e ru e
3 * s * l l ong shor ti nt , i nt * i nt , i ntl ong, l ong*l ong
Technical Aptitude Training JISCE 2015 18
-
7/23/2019 C programming: Session 1
19/23
Note:
1. Conversion of int to longpreserves sign, so does
Type conversion (contd..)
short
2.Var = expr
f = d; /* round off */
i = f;
, ,
result is undetermined */
i = l; s = i; and c = i; /* may eliminate
high order bits */
Technical Aptitude Training JISCE 2015 19
-
7/23/2019 C programming: Session 1
20/23
3. If a specific type is required, the following syntax may be used, called cast
operator.
Example:
= .
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.
Exam le: Assumin 2s com lement machine and int is 2 b tes lon
unsigned i = 65535; int j;
j = i; /* j will be 1 */
Technical Aptitude Training JISCE 2015 20
-
unsigned i = 1 + j; /* i= 65535 */
-
7/23/2019 C programming: Session 1
21/23
Practice
Code
1. F n average mar s o Stu ent
2. Greatest of 3 numbers using conditional operator
3. Bi est of two numbers in C
4. Convert CM to feet and inches
5. Even or odd program in C
.
7. Swapping of two Numbers
Technical Aptitude Training JISCE 2015 21
-
7/23/2019 C programming: Session 1
22/23
Assignment
:
Day
1
1. Write a Program to compute the summation up to a number
. r te a program to compute actor a o a g ven num er
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
Technical Aptitude Training JISCE 2015 22
-
7/23/2019 C programming: Session 1
23/23