Unit I Programminginc 161212055106
Unit I Programminginc 161212055106
programming -206
PROGRAMMING IN C
UNIT- I
UNIT-1
1.1- Representing Algorithms through flowchart, pseudo code
.
1.2- Concept of Programming Languages and Language transl
ator.
1.3- Introduction to Programming and structure of C Program.
1.4- Identifiers, Constants, Variables.
1.5- Basic Data Types.
1.6- Operators, Expressions in C, type casting.
1.7- Input Statement.
1.8- Output Statement. 2
Lecture no.- 1,
UNIT- I
1.1- INTRODUCTION
Definition:-
What is Program?
A computer program is a collection of instructions that
performs a specific task when executed by a computer. A
computer requires programs to function, and typically
executes the program's instructions in a central
processing unit.
Implementation phase
implement the program in some programming
language
Definition:-
Pseudocode:
Input a set of 4 marks
Calculate their average by summing
and dividing by 4
if average is below 50
Print “FAIL”
else
Print “PASS”
START
PRINT PRINT
“PASS” “FAIL”
STOP
Pseudocode:
Input the length in feet (Lft)
Calculate the length in cm (Lcm) by
multiplying LFT with 30
Print length in cm (LCM)
Algorithm Flowchart
Step 1: Input Lft
START
Step 2: Lcm Lft x 30
Step 3: Print Lcm Input
Lft
Lcm Lft x
30
Print
Lcm
STOP
chart.
We also exercise on different types of example
19
Definition:-
In computer science, a low-
level programming language is a
programming language that provides little or no
abstraction from a computer's instruction set
architecture—commands or functions in
the language map closely to processor instructions.
Generally this refers to either machine code or
assembly language.
Definition:
Definition:
A low level language that is similar to machine
language.
Definition:
A translator is a computer program that performs
the translation of a program written in a given
programming language into a functionally
equivalent program in a different
computer language, without losing the functional or
logical structure of the original code (the "essence"
of each program).
Programming language processor (assembler,
compiler, or interpreter) that converts a computer
program written in one language to another
language.
Source
The language program was written in
Object
The machine language equivalent of the program after
compilation
Compiler
A software program that translates the source code
into object code
Assembler is a special case of compiler where the
source was written in Assembly language
Create/Edit source
Compile source
Test executable
Stop
No linking
No object code generated
Easier to debug
Faster development time
OS Interfaces
C Shell, Bourne Shell
WINEXEC
Portability :
C Programs are portable i.e they can be run on any
Compiler with Little or no Modification
Compiler and Preprocessor make it Possible for C
Program to run it on Different PC
Bit Manipulation:
C Programs can be manipulated using bits. We can
perform different operations at bit level. We can
manage memry representation at bit level. [Eg.
We can use Structure to manage Memory at Bit Lev
el
]
It provides wide verity of bit manipulation
Operators. We have bitwise operators to manage
Data at bit level.
By- Er. Indrajeet Sinha , +919509010997
High Level Features :
It is more User friendly as compare to Previous
languages. Previous languages such as BCPL,Pascal
and other programming languages never provide such
great features to manage data.
Previous languages have there pros and cons but C
Programming collected all useful features of previous
languages thus C become more effective language.
Modular Programming:
Modular programming is a software design
technique that increases the extent to which software
is composed of separate parts, called modules
C Program Consist of Different Modules that are
integrated together to form complete program
Note:-
Every
C Program will have
one or more
functions and there
is one mandatory
function which is
called main()
function. ...
49
50
Lecture no.- 4,
UNIT- I
1.4.1- CONSTANTS
Definition:-
Constants refer to fixed values that the program
may not alter during its execution. These fixed
values are also called literals. Constants can be of
any of the basic data types like an Integer
Constant, a floating constant, a Character
Constant, or a string literal.
53
54
Numerical Constants
Constants like 12, 253 are stored as int type. No
decimal point.
12L or 12l are stored as long int.
12U or 12u are stored as unsigned int.
12UL or 12ul are stored as unsigned long int.
Numbers with a decimal point (12.34) are stored as
double.
Numbers with exponent (12e-3 = 12 x 10-3 ) are stored
as double.
12.34f or 1.234e1f are stored as float.
These are not valid constants:
57
Definition:
Identifiers
An Identifier is a sequence of letters and digits,
Naming a Variable
Must be a valid identifier.
Must not be a keyword
Names are case sensitive.
Variables are identified by only first 32 characters.
Library commonly uses names beginning with _.
Naming Styles: Uppercase style and Underscore
style
lowerLimit lower_limit
incomeTax income_tax
Declaring a Variable
Each variable used must be declared.
A form of a declaration statement is
int sum = 0;
char newLine = ‘\n’;
float epsilon = 1.0e-6;
By- Er. Indrajeet Sinha , +919509010997
BASIC DATA TYPES IN
C
Lecture no.- 5,
UNIT- I
1.5.1- DATA TYPES
Definition
Data types in c refer to an extensive
system used for declaring variables or
functions of different types.
The type of a variable determines how much
space it occupies in storage and how the bit
pattern stored is interpreted.
62
63
Integral Types
char Stored as 8 bits. Unsigned 0 to 255.
Signed -128 to 127.
short int Stored as 16 bits. Unsigned 0 to
65535.
Signed -32768 to 32767.
int Same as either short or long int.
long int Stored as 32 bits. Unsigned 0
to 4294967295.
Signed -2147483648 to
2147483647
Keywords
Theseare reserved words of the C language. For
example int, float, if, else, for, while etc.
Identifiers
An Identifier is a sequence of letters and digits, but
must start with a letter. Underscore ( _ ) is treated as a
letter. Identifiers are case sensitive. Identifiers are used
to name variables, functions etc.
Valid: Root, _getchar, __sin, x1, x2, x3, x_1,
If
Invalid: 324, short, price$, My Name
Constants
Constants like 13, ‘a’, 1.3e-5 etc.
String Literals
A sequence of characters enclosed in double quotes as
“…”. For example “13” is a string literal and not
number 13. ‘a’ and “a” are different.
Operators
Arithmetic operators like +, -, *, / ,% etc.
Logical operators like ||, &&, ! etc. and so on.
White Spaces
Spaces, new lines, tabs, comments ( A sequence of
characters enclosed in /* and */ ) etc. These are used to
separate the adjacent identifiers, kewords and
constants.
Lecture no.- 6,
UNIT- I
1.6.1- INTRODUCTION
Definition
Operator: An operator is a symbol that tells the
compiler to perform certain mathematical or logical
manipulations. Operators are used in program to
manipulate data and variables.
Expression: In programming, an exppression is
any legal combination of symbols that represents a
value. Each programming language and
application has its own rules for what is legal and
illegal. For example, in the C language x+5 is
an expression, as is the character string
"MONKEYS."
70
72
!= Inequality Binary
% Modulus Binary
– Subtraction Binary
Subtraction 73
–= Binary
assignment
–> Member selection Binary
By- Er. Indrajeet Sinha , +919509010997
Operator Name Type
74
75
76
sign
Assignment Operator is binary operator
77
Definition:
Increment Operators are used to increased
Lecture no.- 7,
UNIT- I
1.7.1- LOGICAL OPERATOR
Logical Operators
&&, || and ! are the three logical operators.
expr1 && expr2 has a value 1 if expr1 and expr2
both are nonzero.
expr1 || expr2 has a value 1 if expr1 and expr2
both are nonzero.
!expr1 has a value 1 if expr1 is zero else 0.
Example
if ( marks >= 40 && attendance >= 75 )
grade = ‘P’
If ( marks < 40 || attendance < 75 ) grade
= ‘N’
Relational Operators
<, <=, > >=, ==, != are the relational operators.
The expression
operand1 relational-operator operand2
takes a value of 1(int) if the relationship is true and
0(int) if relationship is false.
Example
+
Unary right to left
+ -- + - ! ~ & * (type_name)
sizeof new delete
C Pointer to
left to right .*->*
Member
* / %
Multiplicative left to right
+ -
Additive left to right
<< >>
Bitwise Shift left to right
< > <= >=
Relational left to right
== != 87
Equality left to right
,
Comma left to right
88
arithmetic: + – * / %
comparison: == != < <= > >=
bitwise logical: & | ^ ~
shifting: << >>
lazy logical: && || !
conditional: ? :
assignment: = += -=
increment/decrement: ++ --
sequencing: ,
pointer: * -> & []
y = a*x*x + b*x + c;
manipulation
#include<stdio.h>
#include<conio.h>
void main()
{
int i=20;
double p;
clrscr();
p=i; // implicit conversion
printf(“implicit value is%d”,p);
getch();
}
Output :- 93
Explicit value is 20.
By- Er. Indrajeet Sinha , +919509010997
IMPLICIT CONVERSION EXAMPLE
#include<stdio.h>
#include<conio.h>
void main()
{
int i=20;
double p;
clrscr();
p=i; // implicit conversion
printf(“implicit value is %d”,p);
getch();
94
} Output :-implicit value is 20.
By- Er. Indrajeet Sinha , +919509010997
INPUT STATEMENT OF C
Lecture no.- 8,
UNIT- I
1.8- INTRODUCTION OF LECTURE
Definition
An input/output statement or io statement is a
portion of a program that instructs a computer how to
read and process information. It pertains to gather
information from an input device, or sending
information to an output device.
Function : getch()
getch() is used to get a character from console but does
not echo to the screen.
Library: < stdio.h>
Declaration: int getch(void);
Example Declaration:
char ch;
ch = getch(); (or ) getch();
Remarks:
getch reads a single character directly from the keyboard,
without echoing to the screen.
Return Value:
This function return the character read from the keyboard. 97
char ch;
ch = getch();
printf("Input Char Is :%c",ch);
}
Program Explanation:
Here, declare the variable ch as char data type, and then get
a value through getch() library function and store it in the
variable ch. And then, print the value of variable ch.
During the program execution, a single character is get or
read through the getch(). The given value is not displayed
on the screen and the compiler does not wait for another
character to be typed.And then,the given character is
98
printed through the printf function.
Description
The C library function int getc(FILE *stream) gets the next
character (an unsigned char) from the specified stream
and advances the position indicator for the stream.
Declaration
Following is the declaration for getc() function.
int getc(FILE *stream)
Parameters
stream -- This is the pointer to a FILE object that identifies
the stream on which the operation is to be performed.
Return Value
This function returns the character read as an unsigned
char cast to an int or EOF on end of file or error. 99
Function : getchar()
getchar() is used to get or read the input (i.e a single
character) at run time.
Library: <stdio.h>
Declaration: int getchar(void);
Example Declaration:
char ch;
ch = getchar();
Return Value:
This function return the character read from the keyboard.
101
char ch;
ch = getchar();
printf("Input Char Is :%c",ch);
}
Program Explanation
Here,declare the variable ch as char data type, and then get a
value through getchar()library function and store it in the
variable ch.And then,print the value of variable ch.
During the program execution, a single character is get or read
through the getchar(). The given value is displayed on the
screen and the compiler wait for another character to be typed.
If you press the enter key/any other characters and then only the
given character is printed through the printf function. 102
Function : getche()
getche() is used to get a character from console, and echoes
to the screen.
Library: <Stdio.h>
Declaration:
int getche(void);
Example Declaration:
char ch;
ch = getche();
Remarks:
getche reads a single character from the keyboard and
echoes it to the current text window, using direct video or
BIOS.
Return Value:
105
This function return the character read from the keyboard.
char ch;
ch = getche();
printf("Input Char Is :%c",ch);
}
Program Explanation:
Here,declare the variable ch as char data type, and then get a
value through getche()library function and store it in the
variable ch.And then,print the value of variable ch.
During the program execution, a single character is get or read
through the getche(). The given value is displayed on the
screen and the compiler does not wait for another character to
be typed. Then,after wards the character is printed through 106
the printf function.
107
108
109
Lecture no.- 9,
UNIT- I
In C programming language output functions
are the functions through which the result is
displayed on the standard output device like
monitor.
Following are the output functions of C
language –
114
Definition:
116
terminates.
int global_static;
static int file_static;
Free
malloc( )
Appropriate
position in free
free(a) list identified
Newly-freed
region added to
adjacent free
regions
125
over what part(s) of the program a variable
is actually available for use(active).
2. Longevity: it refers to the period during
which a variables retains a given value
during execution of a program(alive)
3. Local(internal) variables: are those which
are declared within a particular function.
126
Are declared using a keyword auto.
127
printf(“%d\n”,m);
}
function1()
{
int m = 10;
printf(“%d\n”,m);
}
function2()
{ int m = 100; Output
function1(); 10
printf(“%d\n”,m); 100
} 1000
128
active only in main.
129
program.
Also known as global variables and default value is zero.
Unlike local variables they are accessed by any function in
the program.
130
{count=10;
{ . . . . . .
. . . . . .
} }
funtion1() funtion()
{. . . {int count=0;
. . . . . .
} . . .
funtion1() count=count+1;
{. . . }
. . . When the function references the
} variable count, it will be
referencing only its local
The variable number and
variable, not the global one.
length are available for use
in all three function
By- Er. Indrajeet Sinha , +919509010997
GLOBAL VARIABLE EXAMPLE
131
return(x);
x=10; }
printf(“x=%d\n”,x);
printf(“x=%d\n”,fun1());
printf(“x=%d\n”,fun2());
printf(“x=%d\n”,fun3()); Once a variable has been
} declared global any function can
int fun1() use it and change its value. The
{ x=x+10; subsequent functions can then
return(x); reference only that new value.
}
int fun2() Output
{ int x
x=1; x=10
return(x); x=20
} x=1
x=30
By- Er. Indrajeet Sinha , +919509010997
EXTERNAL DECLARATION
int main()
{ • As far as main is concerned, y is not
defined. So compiler will issue an
132
y=5; error message.
. . . • There are two way out at this point
. . . 1. Define y before main.
} 2. Declare y with the storage class
int y; extern in main before using it.
func1()
{
y=y+1
}
int main()
{
133
extern int y;
. . .
. . . Note that extern
} declaration does not
func1() allocate storage space
{ for variables
extern int y;
. . .
. . .
}
int y;
134
int main() int m;
{ function2()
extern int m; {
int i int i
. . . . . .
. . . . . .
} }
function1() function3()
{ {
int j; int count;
. . . . . .
. . . . . .
} }
By- Er. Indrajeet Sinha , +919509010997
MULTIFILE PROGRAMS AND EXTERN
VARIABLES
file1.c file2.c
135
int m; extern int m;
int main() function2()
{ {
int i; int i
. . . . . .
. . . . . .
} }
function1() function3()
{ {
int j; int count;
. . . . . .
. . . . . .
} }
By- Er. Indrajeet Sinha , +919509010997
STATIC VARIABLES
136
It is declared using the keyword static like
static int x;
static float y;
137
Scope of Internal static variables extend upto the
end of the program in which they are defined.
138
int main()
{
int I;
for(i =1; i<=3; i++)
stat();
} Output
void stat()
{ x=1
static int x=0; x=2
x = x+1;
printf(“x = %d\n”,x); x=3
}
139
of all functions and is available to all the
functions in the program.
147
Macro definition
#define, #undef
File inclusion
#include
Conditional Compilation
#if, #ifdef, #ifndef, #elseif, #else
Others
Easy to
Develop program
Read programs
Modify programs
To
define constants or any macro
substitution.
#define <macro> <replacement
name>
E.g.
#define FALSE 0
#define TRUE !FALSE
To undefined a macro.
E.g. #undef FALSE
A macro must be undefined before being
redefined to a different value.
By- Er. Indrajeet Sinha , +919509010997
DEFINE FUNCTIONS
If in the C code:
x = max(q+r,s+t);
After preprocessing:
#include directive
Include the contents of another file at the point
where the directive appears.
Why need file inclusion
Use built-in functions
E.g., printf(), rand();
Reuse code
#include <file>
Used for system header files
File contain function prototypes for library functions
#include "file"
Used for header files of your own program
looks for a file in the current directory
first
then system directories if not found
RAND_MAX
Example:
#include <stdlib.h>
int i = rand();
memory allocation
process control