Download - p2 Introduction to c
-
8/3/2019 p2 Introduction to c
1/35
2/15/20122/15/2012 11
-
8/3/2019 p2 Introduction to c
2/35
2/15/20122/15/2012 22
OverviewOverview
Developed by Dennis Ritchie at AT & T
Bell Laboratories in 1992
Reliable, simple and easy to use
Top-down programming
-
8/3/2019 p2 Introduction to c
3/35
2/15/20122/15/2012 33
Why CWhy C
C has been already superceded by
languages like C++, C# and Java, so why
bother to learn C today?
-
8/3/2019 p2 Introduction to c
4/35
2/15/20122/15/2012 44
Why CWhy C
Reasons
No body can learn C++ or Java directly, the
basic programming elements are learned from
C
C++ and Java tools frameworks are still built
on C only
Major part of popular Operating Systems likeUnix, Linux and Windows are written inC
-
8/3/2019 p2 Introduction to c
5/35
2/15/20122/15/2012 55
Why CWhy C
Reasons
When it comes to performance in terms of
execution speed nothing beats C
Device drivers are commonly written in C
In case embedded systems like mobiles and
palmtops the programs not only have to run
fast but also have to work in limited memory.C is the best language in choice with these
constraints on time and space.
-
8/3/2019 p2 Introduction to c
6/35
2/15/20122/15/2012 66
SyllabusSyllabus -- C programmingC programming
Storage Classes, Data Types, Controllingprogram flow, arrays, functions, MemoryManagement
Pointers, Arrays and pointers, Pointer tofunctions and advanced topics on pointers,Structures and unions, Data structures
Linked List, Stacks, Queues, Conditional
Compilation, Preprocessor directives, Fileoperations, Variable arguments in Functions,Command line arguments, Bitwise operations,Typecasting
-
8/3/2019 p2 Introduction to c
7/35
2/15/20122/15/2012 77
Structure of a C ProgramStructure of a C Program
Preprocessor Commands
Type definitions
Function prototypes - declare functiontypes and variables passed to function.
Variables
Functions (main() function-Must)
-
8/3/2019 p2 Introduction to c
8/35
2/15/20122/15/2012 88
Character Set Alphabets, Digits and Special symbols
-
8/3/2019 p2 Introduction to c
9/35
2/15/20122/15/2012 99
Constants, Variables and KeywordsConstants, Variables and Keywords
Alphabets
Numbers
Special Symbols
Constants
Variables
Keywords
Constant - Entity that doesnt change
Variable Entity that may change
-
8/3/2019 p2 Introduction to c
10/35
2/15/20122/15/2012 1010
Types of ConstantsTypes of Constants
-
8/3/2019 p2 Introduction to c
11/35
2/15/20122/15/2012 1111
Primary ConstantPrimary Constant --IntegerInteger
Must have atleast one digit
Must not have a decimal point
Can be either positive or negative By default, positive
No commas or blanks allowed within the
integer constant Range is -32768 to 32767
Ex.: 426,+400,-324,-3287
-
8/3/2019 p2 Introduction to c
12/35
2/15/20122/15/2012 1212
Primary ConstantPrimary Constant--RealReal
Must have atleast one digit
Must have a decimal point
Could be positive or negative By default, positive
No commas or blanks are allowed within a
real constantEx. : +325.34,467.0,-67.54,-12.675
-
8/3/2019 p2 Introduction to c
13/35
2/15/20122/15/2012 1313
Primary ConstantPrimary Constant--RealReal
Exponential form of representationmantissa e exponent
Mantissa and Exponential should be separated
by a letter e
Mantissa may have a positive or negative sign
By default, mantissa sign is positive
Exponent must have atleast one digit, can be
positive or negative and by default it is positive Range is -3.4e38 to +3.4e38
Ex.: +3.2e-5,12.1e18, -0.7e+5, -3.2e-5
-
8/3/2019 p2 Introduction to c
14/35
2/15/20122/15/2012 1414
Primary ConstantPrimary Constant--CharacterCharacter
Can be a single alphabet, single digit or a
single special symbol enclosed within a
single inverted commas. Both the inverted
commas should point to the left.
A is a valid character whereas A is not.
Maximum length can be one character.
Ex. : 'A' , 'I' , '5'
-
8/3/2019 p2 Introduction to c
15/35
2/15/20122/15/2012 1515
Primary Data typesPrimary Data types
Integerlong
short
signed
Unsigned
CharSigned
Unsigned
Floats and Doubles
-
8/3/2019 p2 Introduction to c
16/35
2/15/20122/15/2012 1616
Note: use sizeof() function to check the bytes occupied by the data types
-
8/3/2019 p2 Introduction to c
17/35
2/15/20122/15/2012 1717
Types of C VariablesTypes of C Variables
Particular type of variable can hold only
the same type of constant.
Integer variable holds only integerconstant
Rules for constructing different types of
constant varies whereas for constructing
variable names of all types the same set of
rules apply.
-
8/3/2019 p2 Introduction to c
18/35
2/15/20122/15/2012 1818
Rules for constructing variable namesRules for constructing variable names
Variable name is combination of 1 to 31alphabets, digits or underscores.
Maximum length is 31 characters
First character must be an alphabet orunderscore
No commas or blanks allowed
No special symbol other than anunderscore can be used.
Ex. : si_int , m_hra , si_intr_2008
-
8/3/2019 p2 Introduction to c
19/35
2/15/20122/15/2012 1919
Rules for constructing variable namesRules for constructing variable names
It is compulsory to declare the type ofvariable before use in the program. This
type declaration is done at the beginning.
Example of type declaration.int si, m_hra ;
float bassal ;
char code ;
-
8/3/2019 p2 Introduction to c
20/35
2/15/20122/15/2012 2020
C KeywordsC Keywords
Words whose meaning has already been
explained to the compiler
Cannot be as variable names
-
8/3/2019 p2 Introduction to c
21/35
2/15/20122/15/2012 2121
FIRST C PROGRAMFIRST C PROGRAM
main( ){
int p, n ;
float r, si ;
p = 1000 ;
n = 3 ;
r = 8.5 ;
si = p * n * r / 100 ; /* formula for simple interest */
printf ( "%f" , si ) ;
}
-
8/3/2019 p2 Introduction to c
22/35
2/15/20122/15/2012 2222
Rules for writing a programRules for writing a program
Every C statement must end with a ; ,
which acts as a terminator
All statements are entered in small letters Comment about the program should be
enclosed with /* */.
Printf function is used to display value inthe screen.
-
8/3/2019 p2 Introduction to c
23/35
2/15/20122/15/2012 2323
Printf() functionPrintf() function
It is defined in stdio library.
General form of printf() function is,
printf("" ,); Table represents the list of format strings
used in C.
Escape sequences are also used in printfto cause an escape from normal
interpretation of string.
-
8/3/2019 p2 Introduction to c
24/35
2/15/20122/15/2012 2424
-
8/3/2019 p2 Introduction to c
25/35
2/15/20122/15/2012 2525
Printf() functionPrintf() function
some examples of usage ofprintf( )
function:
printf ( "%f", si ) ;
printf ( "%d %d %f %f", p, n, r, si ) ;
printf ( "Simple interest = Rs. %f", si ) ;
printf ( "Prin = %d \nRate = %f", p, r ) ;
-
8/3/2019 p2 Introduction to c
26/35
2/15/20122/15/2012 2626
Escape SequencesEscape Sequences
-
8/3/2019 p2 Introduction to c
27/35
2/15/20122/15/2012 2727
Scanf() functionScanf() function
To enter the data from the keyboard
during run-time.
scanf ( "format string", list of addresses ofvariables ) ;
Example:scanf ( "%d %f %c", &c, &a, &ch ) ;
-
8/3/2019 p2 Introduction to c
28/35
2/15/20122/15/2012 2828
Scanf() functionScanf() function
We are sending addresses of variable to
scanf() function.
Values received from the keyboard mustbe dropped into variables corresponding to
these addresses.
-
8/3/2019 p2 Introduction to c
29/35
2/15/20122/15/2012 2929
C InstructionsC Instructions
Three type of instructions in C.
Type Declaration Instruction-To declare the type ofvariable used in C program
Arithmetic Instruction-To perform arithmetic
operations between constants and variables
Control Instruction-To control the sequence of
execution of various statements in C program
-
8/3/2019 p2 Introduction to c
30/35
2/15/20122/15/2012 3030
Type Declaration InstructionType Declaration Instruction
Used to declare the type of variable usedin the program and written at the
beginning of main() function.
While declaring variable, we can alsoInitialize.
int i=10,j=25;
Order of definition matters in some cases.int i = 10, j = 25 ; is same as int j = 25, j = 10 ;
float a = 1.5, b = a + 3.1 ; is alright, but float b = a + 3.1, a = 1.5 ; is not.
int a, b, c, d ; a = b = c = 10 ; will work,int a = b = c = d = 10 ; will not.
-
8/3/2019 p2 Introduction to c
31/35
2/15/20122/15/2012 3131
Arithmetic InstructionArithmetic Instruction
Consists of variable name on the left hand
side of =,variable name and constants on
the right hand side of the equal.
Variables and constants appearing on the
right hand side of the equal are connected
by arithmetic operators like +,-,* and /.
Arithmetic statement could be of three
types.
-
8/3/2019 p2 Introduction to c
32/35
2/15/20122/15/2012 3232
Arithmetic StatementArithmetic Statement
Integer mode-operands are integer
variables and integer constants.
Real mode-operands are real variables
and real constants
Mixed mode-some of the operands are
real and some of them are integers.
Ex. : float si, prin, anoy, roi, avg ;int a, b, c, num ;
si = prin * anoy * roi / 100.0 ;
avg = ( a + b + c + num ) / 4 ;
-
8/3/2019 p2 Introduction to c
33/35
2/15/20122/15/2012 3333
CompilersCompilers
GCC from Linux
Turbo C
Ansi C Visual C
-
8/3/2019 p2 Introduction to c
34/35
I request Electronics and communication
ENGINEERING students to visit my blog
for more
abhishek1ek.blogspot.com
awhengineering.blogspot.com u can also email me
[email protected] for more ppt
2/15/20122/15/2012 3434
-
8/3/2019 p2 Introduction to c
35/35
2/15/20122/15/2012 3535