Uploaded by Sahishnu Kumar

LECTURE NOTES

advertisement
INTRODUCTION TO
PROGRAMMING
Why need of Programming ?
• PROGRAM TYPES:
1.System program :
2.Application program:
C LANGUAGE
‘Dennis Ritchie’ AT & T’s bell lab
B
developed ken Thomson.
1972 in USA.
why need of c language
by ANSI
ANSI C
1989
standardized
levels of language
Low level language
High level language
1. Machine language :-which means it is close to
hardware, user must have knowledge of system
architecture or CPU architecture.
.Not machine dependent which means user don’t
need to have knowledge of system architecture.
.Execution of code is fast.
.Close to humans not machine.
.It is not understandable by human.
.Deals with variables, loops etc.
Not run same program on different machine or
computers.
.Portable language which means same program can
run on different platforms.
2.Assembly language:-
.No need to understand system architectures
.It uses symbols, numbers call numonics eg:for
addition use add symbol, for subtraction use sub
symbol.
.Slower than machine language
.Readable by human but strong binding with
machine language
.Slower than machine language
.Easy to read ,write , understand etc
Language translators
ASSEMBLY LANGUAGE
ASSEMBLER
MACHINE LANGAUGE
HIGH LEVEL LANGUAGE
COMPILER
MACHINE LANGUAGE
HIGH LEVEL LANGUAGE
INTERPRETER
MACHINE LANGUAGE
Features of C
Advantage & Disadvantage
Of C Language
Advantage
Disadvantage
.It is open source language.
.It doesn’t support object oriented
concept.
.Easy to learn.
.Not support exception handling.
.Portable Language.
.Not have Garbage Collector.
.Easy debugging.
.Platform dependent.
.Can allocate dynamic memory.
Structure of c program
Documentati0n section
Link section
Definition section
Global declaration section
Main()function section
{
Declaration part
Executable part
}
Subprogram section
Function1
Function2
Function n
user defined funs.
Structure of C Program
• Documentation section ://single line comment
:/*multiple line comment
• Link section:
:- #include<stdio.h>
•
Preprocessor directive
Standard I/O header file with .h
extension
• printf ,scanf (predefined functions
For output
To take input
structure of c program contd.
Console input output
eg:- getch (predefined function used to hold the screen)
clrcsr(predefined function used to clear the screen)
Definition section :
Structure of c program contd.
• Global declaration section:
• 1.Local Variable and Local functions: variable declare within particular fn.
eg:- void fun()
local function declaration:- void main()
{
{
int a;
int sum()
}
{
}}
• 2.Global Variable and Global Functions: variable used in more thn one fn. and declare outside
the functions
eg: int a;
global function declaration:- int a;
void fun()
void sum()
{
void main()
}
{
Void disp()
}
Structure of c program contd.
• Main section
Syntax Of C Program
begining
ending
Program- Print Hello World
Program debugging
• Syntax Error:-
Program debugging contd.
• Runtime Error:-
Note:-
Program debugging contd.
• Logical Error:-
Execution process of c programming
preprocessor
#include<stdio.h>,#include<conio.h>(preprocessor
replace # by functions like printf,scanf etc)
dee.i;
Translator
(convert
high level
lang. to
binary
form)
compiler
Yes
Syntax
error
No
dee.am
assembler
dee.obj
Linker
output NO
Logic
error
dee.exe
Execute/loader
Yes
Compile the program
It convert code written in assembly language into object
code
Link all object code in one file and link with system
libraries.
loader load exe file into mainmemory.RAM
Functions in c language
function
User defined fn.
Prdefine fn.
formatted input function scanf() in c
Syntax:scanf(“format specifier”,&variable), &=address of operator
eg:- scanf(“%d”,a);
formatted output function printf ()
in c
\n:-
,
\t:-
Syntax:printf(“ “)
only generate space not print anything
printf(“format string or specifier”,variable);
Program for printf() & scanf() function
Using printf()
For msg. printing
Using both printf() & scanf()
for printing values
unformatted input function
getchar():program:-
void
unformatted input function cont.
getch():- It
Program:-
unformatted Input function cont.
• getche():-
Program:-
• Similarity:-
• Dissimalrity:- In getche() fn.
unformatted Input function cont.
• gets():Program:-
unformatted Output functions
• putchar(): It is
Program:-
• putch():- It
unformatted Output function cont.
• puts():• Program:-
main() function in c
• Syntax:-
Constants In C Language
• Constants:-
const keyword
• Types of constants in c LANGUAGE:Numeric constants
Character constants
Constants in c language contd.
• Integer Constants:-
Rules:-
• Floating Point constant/Real Constants: These
Rules:-
CONSTANT IN C LANGUAGE CONTD.
Character Constant
• Single Character Constants:- In this
eg:-
Constant in c language contd.
• String Constant: It contain
integer constants program
variables in c language
Program:40
Output=40
Types of
variables
Local
variable
Global
variable
Automatic
variable
Static
variable
External
variable
Local variable
Program:-
Global variable
Program:-
Automatic variable
Program:-
Static variable
Program:-
External variable
Program:
Keywords in c language
32 keywords In C language
32 keywords contd.
32 keywords contd.
Identifier in c language
eg:-
int sal ;
keyword
double money ;
void disp()
identifier
double
Datatypes in c language
Primary Datatype
Secondary Datatype/Derived
datatype
User defined datatypes
int
array
typedef
char
pointer
Enumerated datatype
float
structure
void
union
INT. datatype in c language
Types of int datatype
short int( size modifier)
long int(size modifier)
Character datatype in c language
Types of char datatype
Signed character datatype
unsigned character datatype
Float datatype in c language
Types of float datatype
double datatype
long double datatype
Table for datatypes
Datatype
Size in byte
Format Specifier
Range
int or signed int
2 byte
%d
-32768 to 32767
unsigned int
2
%u
0 to 65535
long int
4
%ld
-2147483648 to _47
unsigned long int
4
%lu
0 to 4294969295
short int
1
%d,%hd
-128 to 127
unsigned short int
1
char or signed char
1
%c
-128 to 127
unsigned char
1
%c
0 to 255
float
4
%f
-34e38 to 3.4e38
double,long double
8,10
%lf,%Lf
-1.7e308 to 1.7e308,
3.4e4932 to +1.1e4932
0 to 255
Operator in c language
Types of operator
Unary Operator
(Work on single operand)
Binary Operator
(work on two operand)
Ternary Operator
(conditional operator)
Increment operator(++a)
Arithmetic operator
?: ( eg:-(a>b)? a:b)
Decrement operator(--a)
Relational operator
Sizeof operator (eg:-sizeof(a))
Logical operator
Bitwise operator
Assignment operator
Unary operator(increment)
• Increment operator:-
• Pre –increment(++a):• Eg:-
Unary operator contd.
• Post –increment(a++):Eg:-
Unary operator contd.
Unary operator type
Meaning
Example
Result
++a
--a
Pre-increment
Pre-decrement
x=++a,
x= --a
x=21,a=21
x=19,a=19
a++
a--
Post- increment
Post-decrement
x=a++,
x=a--
x=20,a=21
x=20,a=19
+
Unary Airthmetic
a=3,b=5 than c=+a
c=3
-
Unary Airthmetic
a=3,b=5 than c=a+(-b)
c=-2
!
&
Logical Not
Address of operator
If a=2,b=3 than c=!(a>b)
Used in scanf () function
1(true)
size of()
Size of operator
size of(a)
97
Binary operator(arithmetic )
Arithmetic Operator Meaning
Example
Result
+
Addition
a+b
30
-
Substraction
a-b
10
*
Multiplication
a*b
200
%
Modulus(used only
a%b
0
Binary operator(relational )
• Eg:-
Relational operators
Operator
Meaning
Example
Result
<
Less than
10<8
F (0)
>
Greater than
10>8
T(1)
<=
Less than or equal to
10<=
F (0)
>=
Greater than or equal to
10>=
T (1)
==(avoid with float value)
Comparison
10==8
F (0)
!=(avoid with float value)
Not equal to
10!=8
T (1)
By relational operator we can compare integer value , float value and character value
but not strings.onsomewhere == and != operator avoid with float values.
Binary operator(logical)
Logical operators
Operator
Meaning
Example
Result
&&(if one condition 0 than
output is 0)
Logical And
(6<3)&&(6>3)
F
||(if both condition 0 than
output o)
Logical Or
(6<3)||(6>3)
T
!(reverse of condition
result)
Logical Not
!(6<3)
T (If false return true)
Binary operator(bitwise)
Bitwise operator <<, >>(left
right shift)
shift ,
• <<
• >> This operator shift number to right side
2
2
Bitwise operator ~(BITWISE NOT)
Bitwise operator
operators
meaning
a
b
A&b
A|b
A^b
&
Bitwise and
0
0
0
0
0
|
Bitwise or
1
1
1
1
0
^
Bitwise xor
0
0
0
1
0
0
0
1
0
1
~
Bitwise Not
<<
Left shift
>>
Right shift
BINARY OPERATOR(ASSIGNMENT)
Assignment operators
Simple assignment operator
=
Compound Assignment Operator
Meaning
+=
Add and assignment operator
-=
Subtract
*=
Multiply
/=
Divide
%=
Modulus
<<=
Left shift
>>=
Right shift
&=
Bitwise And
^=
Bitwise Xor
|=
Bitwise Or
Ternary operator in c language
• Syntax:
• Program:
Special operator comma.
• Comma(,):-
Operator precedence
and associativity table
Download