Documenti di Didattica
Documenti di Professioni
Documenti di Cultura
TUTORIAL
OUTLINE
Overview
History
Features
Role of C Compiler
Flowchart
Sample C Program 1
Character Set
Data Types
Variables
C is Case Sensitive
C Token
Sample C Program 2
Control Statements
Common Programming Errors
Assignment
OVERVIEW OF C
C language is
Structured
High
level
Machine
Follows
independent
HISTORY
ALGOL
In
1960s
First language using a block structure
BCPL
In
1967
Basic Combined Programming Language
In
1970
Added feature of BCPL
4
CONTD
C
Evolved
Developed
Developed
Designed
FEATURES
CONTD
Simple / Easy
The C Language has both the simplicity of High Level
Language and speed of Low Level Language. So it is
also known as Middle Level Language
ROLE OF C COMPILER
Compiler:
FLOWCHART
CONTD
Advantages of flowchart:
10
CONTD
all
major
elements
and
their
11
FLOWCHART SYMBOLS
12
CONTD
Terminator
This symbol represents the beginning and end
point in a program. We use start and stop option
in it.
Input / Output Symbol
This symbol is used to take any input or output in
the algorithm.
Process Symbol
A rectangle indicates the processing, calculation
and arithmetic operations
13
CONTD
Decision Symbol
It is used when we want to take any decision in the
program.
Connector Symbol
This symbol is used to connect the various portion of
a flow chart. This is normally used when the flow
chart is split between two pages
Data Flow Symbol
This symbol is used to display the flow of the
program. It shows the path of logic flow in a program.
14
SAMPLE C PROGRAM
main() --------------Function name
{
------------Start of Program
.
. -------- Program statements
.
} --------------- End of Program
15
CONTD
/*
Filename
: hello.c
Description : This program prints the greeting
Hello, World!
*/
#include <stdio.h>
#include <stdio.h>
int main(void)
void main( )
{
{
16
CONTD
Comments
Text surrounded by /* and */ is ignored by computer
Used to describe program
int main()
Programs execution starts from the main function
Parenthesis used to indicate a function
int means that main "returns" an integer value
Braces ({ and }) indicate a block
The bodies of all functions must be contained in braces
18
CONTD
o
Preprocessor directives
#include <stdio.h>
#include <conio.h>
#include <stdlib.h>
#include <string.h>
CONTD
20
CONTD
#define MAX_COLS 20
#define MAX_INPUT 1000
21
CONTD
commonly
printf()
Output function
Syntax:
printf(.) ;
scanf()
Input function
Syntax:
scanf(format specifier, &var,&var2);
22
CONTD
getch()
23
CHARACTER SET
These are the characters that C recognizes.
Letters (upper case and lower case)
AB C D E F G H I J K LM
NOPQRSTUVWXYZ
abcdefghijklm
nopqrstuvwxyz
Digits
0123456789
24
CONTD
Z : 65 to 90
a z
: 97 to 122
0 9
: 48 to 57
Special symbols[ #, &, `]
Control characters[\n, \t . ..]
Graphic characters
Total
26
26
10
32
34
128
256
25
DATA TYPES
C support several different types of data, which may be
represented differently within the computers memory.
o
o Types
1] Primary
Integer
Float
Double
Character
2] Derived
Arrays
Pointers
Structure
3] User Defined
typedef
enum
26
Data Types
1] char
signed char
unsigned char
Byte
1
1
1
Format Specifier
%c
%c
%c
2] short
short signed int
short unsigned int
2
2
2
%d
%d
%u
3] int
signed int
unsigned int
2
2
2
%d
%d
%u
27
Data Types
Byte
4] long
4
CONTD
long signed int
4
long unsigned int
4
Format Specifier
%l
%ld
%lu
5] float
signed float
unsigned float
4
4
4
%f
%f
%uf
6] double
%lf
7] Long Double
10
%Lf
28
TYPE CASTING
Implicit
Explicit
by the
wants a
then we
done by
29
CONTD
#include <stdio.h>
void main( )
{
int base, height, area;
base = 5;
height =3;
area = (base * height)/2;
. Incorrect
30
CONTD
#include <stdio.h>
void main( )
{
int base, height, area;
base = 5;
height = 3;
area = ((float) (base * height)/2);
.Correct
31
Syntax:
Eg:
CONTD
[B] Enumerated data type
Syntax:
valuen}
Eg:
33
VARIABLES
Variable names correspond to locations in the computer's
memory
Data name that may be used to store a data value
It may take different values at different times during
execution
Eg:
char x;
char y = e;
34
CONTD
Rules
Must begin with a letter(), some system permits
underscore as first character.
Length should be not more than 8 characters
Uppercase and lowercase are significant, (i.e.) total
and TOTAL are different
Variable should not be a keyword
White space is not allowed
35
C IS CASE SENSITIVE
C is case sensitive: it distinguishes between UPPER case
(CAPITAL) and lower case (small) letters.
Keywords in C for example, the keyword int MUST be
in lower case. For example:
#include <stdio.h>
int main ()
{ /* main */
int height_in_cm;
height_in_cm = 160;
36
C TOKENS
Keywords
Identifiers
Constants
Strings
Special Symbol
Operators
37
CONTD
Keywords
C uses 32 keyword
have fixed meaning and cannot be changed
38
Constants
Quantity that does not change is called a constant.
CONTD
Types
Numeric constants
Integer constants 123, -33
Real constants 0.992, 3.5e2
Character constants
Single character constants 5, a
String Constants Hello, 1999
39
CONTD
Backslash Characters Constants
\n Newline
\b Backspace
\f Form feed
\t Tab or horizontal tab
\a Audible alert
\r Carriage return
\v Vertical Tab
\ Single Quote
\ Double Quote
\? Question Mark
\\ - Backslash
\0 - Null
40
CONTD
Identifiers
Names
Operators
Arithmetic
Relational
Logical
Bitwise
41
CONTD.
ARITHMETIC OPERATORS
42
CONTD.
43
CONTD.
Logical Operators:
&& logical And
|| logical Or
! logical Not
Bitwise Operators
&
bitwise And
| bitwise Or
^ bitwise Xor
~ bitwise Not
<< shift left
>> shift right
44
45
OUTPUT:
CONTD
46
CONTROL STATEMENTS
decision making
DECISION MAKING
if
if-else
if else if
nested if
48
CONTD.
if
if statement is simple decision making statement,
which is used to take any decision when the
condition is true.
if (statement)
{
Statement;
}
if (expression / condition)
Statement;
49
CONTD.
If-else
This statement is used to make decision in C language.
If the given condition is true then the cursor will move
to the true portion else it will move to the false portion.
if (condition)
{
Statement;
}
else
{
Statement;
}
50
If else-if
if (condition)
{
Statement;
}
else if (condition)
{
Statement;
}
else if (condition)
{
Statement;
}
else
{
Statement;
}
51
52
Syntax:
switch( expression)
{
case value-1:
block-1;
break;
case value-2:
block-2;
break;
---default:
default-block;
break;
}
statement -X;
53
LOOPING
while
do-while
for
54
CONTD
While
It is an entry control loop statement, because it
checks the condition first and then if the
condition is true the statements given in while
loop will be executed.
SYNTAX:Initialization;
while (condition)
{
Statements;
Incremental / decrement;
}
55
CONTD
Do-while
56
CONTD
SYNTAX:Initialization
do
{
Statement;
Increment / decrement;
}
while (condition)
57
CONTD.
For loop
It is another looping statement or construct used to
repeat any process according to user requirement but
it is different from while and do-while loop because
in this loop all the three steps of constructions of a
loop are contained in single statement.
CONTD.
for (initialization; Test condition; Increment /
decrement)
{
Statement;
}
.
for (; test condition; increment / decrement)
{
Statement;
}
59
CONTD.
for (; test condition;)
{
Statement;
Increment / decrement
}
60
JUMPS STATEMENTS
Continue
Break
Goto
61
CONTD
Continue
This statement is used within the body of the loop.
The continue statement moves control in the
beginning of the loop means to say that is used to
skip any particular output.
62
CONTD
clrscr ( );
for (a=1; a<=10; a++)
{
if (a= =5 | | a= = 7)
continue;
printf (%d \n,a);
}
getch ( );
}
63
CONTD
Break
This statement is used to terminate any sequence or
sequence of statement in switch statement. This
statement enforce indicate termination. In a loop
when a break statement is in computer the loop is
terminated and a cursor moves to the next
statement or block;
Example:
64
CONTD
int a;
clrscr ( );
for (a=1; a<=10; a++)
{
if (a= =5)
break;
printf (%d \n,a);
}
getch ( );
}
65
CONTD
Goto statement
It is used to alter or modify the normal sequence of
program execution by transferring the control to
some other parts of the program. the control may be
move or transferred to any part of the program
when we use goto statement we have to use a label.
Syntax:
Forward Loop:
goto label;
.
.
label:
statement;
66
CONTD
Backward Loop:
label:
statement
.
.
goto label;
67
COMMON PROGRAMMING
ERRORS
Missing Semicolons
Eg: a = x+y is wrong
c= b/d; is right
Misuse of Semicolon
Eg:
for(i = 1; i <= 10; i++);
sum = sum + i;
is wrong
68
CONTD
Use
of = instead of = =
Missing Braces
Missing Quotes
Improper Comment Characters
Undeclared Variables
And many more
69
ASSIGNMENT
70
CONTD
71
CONTD
72
QUERIES?
73
THANK YOU!!!
74