Introducion to C Programming
-
Upload
sam-bandilla -
Category
Documents
-
view
221 -
download
0
Transcript of Introducion to C Programming
-
7/30/2019 Introducion to C Programming
1/164
INTRODUCTION TO C
-
7/30/2019 Introducion to C Programming
2/164
INTRODUCTION TO C
C was developed by Dennis Ritchie at Belllaboratory in 1972
It is an upgrade version of languages B and
BCPL.
-
7/30/2019 Introducion to C Programming
3/164
Features of C
It is a structured programming language.
It is highly portable.
It is a middle level language. It is a case sensitive language.
It uses Top-Down approach.
It is a Free form language.etc,.
-
7/30/2019 Introducion to C Programming
4/164
Steps in learning C
CharacterSet
ProgramsInstructionsTokens
-
7/30/2019 Introducion to C Programming
5/164
C Character Set
C Character Set
ExecutionCharacter Set
SourceCharacter Set
SpecialCharacters
DigitsAlphabets EscapeSequence
WhiteSpaces
-
7/30/2019 Introducion to C Programming
6/164
C Character Set (Cont)
Source Character Set
It is used to construct the statements in theprogram.
Executable Character Set
These characters are employed at the time ofexecution i.e. they have effects only when the
program is being executed.
-
7/30/2019 Introducion to C Programming
7/164
Source Character Set
Letters a to z ,A to Z
Digits 0 to 9
Special Characters ! @ # $ % ^ & * ( ) _ - += \ | { } [ ] etc,.
White Spaces Blank Space ,Horizontaltab, New line, Verticaltab etc,.
-
7/30/2019 Introducion to C Programming
8/164
Special characters Comma , Period or dot . Semicolon ; Colon :
Apostrophe
Quotation mark Exclamation mark ! Vertical bar |
Back Slash \ Tilde ~ Underscore - Dollar $
Question mark ?
-
7/30/2019 Introducion to C Programming
9/164
Ampersand & Caret ^
Asterisk * Minus - Addition + Lesser than Parenthesis () Bracket [] Braces {}
Percentage % Hash # Equal to = At the rate @
-
7/30/2019 Introducion to C Programming
10/164
Executable Character Set
Characters Escape Sequence
Back Space \b
Horizontal Space \t
Vertical Space \v
Newline \n
-
7/30/2019 Introducion to C Programming
11/164
-
7/30/2019 Introducion to C Programming
12/164
C Tokens (Cont)
C Tokens
Identifiers
Eg:main,
avg
Keywords
Eg: int,
for
operators
Eg: +
-
Strings
Eg: ab
spIsymbol
Eg: #
$ %
Constants
Eg:17,
15.5
-
7/30/2019 Introducion to C Programming
13/164
Executing a C Program
Creating the Program
Compilation
Linking
Execution
-
7/30/2019 Introducion to C Programming
14/164
Executing a C Program (Cont)
Enter the program in a C editor.
Save the program (File Save) or F2.Use the extension .c for saving the file.
Eg: sample.c
Compile the program(Compile Compile)
or Alt+F9. Run the program(Run Run) or Ctrl+F9.
-
7/30/2019 Introducion to C Programming
15/164
Executing C program using UNIX
Enter the program in vi editor.
Save the file using :wq
Use the extension .c for saving the file.Eg: sample.c
Compile the program.
Eg: cc sample.c (or) gcc sample.c Run the program using a.out.
-
7/30/2019 Introducion to C Programming
16/164
Structure of C programDOCUMENTATION SECTION
PREPROCESSOR SECTION
DEFINITION SECTION
GLOBAL DECLARATION SECTION
main(){
Declaration part;Executable Part;
}sub program section{
Body of the subprogram;}
-
7/30/2019 Introducion to C Programming
17/164
Documentation Section It contains the comment lines.
Preprocessor Section It is used to link library files.
Global Declaration Section
The Global declaration section comes at thebeginning of the program and they are visibleto all parts of the program.
Declaration Section It describes the data to be used within the
function.
Executable Part
It contains the valid statements.
-
7/30/2019 Introducion to C Programming
18/164
C Programs C program may have many functions. One and only one of the functions MUST BE
named main.
main is the starting point for the program. main and other functions in a program are
divided into two sections, declarationsection and statement section.
-
7/30/2019 Introducion to C Programming
19/164
Preprocessor Directives
Special instructions to the preprocessorthat tells how to prepare the program forcompilation
E.g: include : tells the processor toinclude information from selected librariesknown as header filese.g.
-
7/30/2019 Introducion to C Programming
20/164
Comments (Program documentation)
The compiler simply ignores commentswhen it translates the program intoexecutable code.
To identify a comments, C uses opening/* and closing */ comment tokens.
-
7/30/2019 Introducion to C Programming
21/164
Comments (Cont)
Comments can appear anywhere in aprogram.
Comments are also found wherever it is
necessary to explain a point about a code. Comments cannot be nestedin C i.e. you
cannot have comments inside comments.
-
7/30/2019 Introducion to C Programming
22/164
C program/* Example program in C*/ Comments
# include Preprocessor Section
Global Declaration
void main ()
{ Local declaration
printf (Hello World! \n); Statements
}
Output :
Hello World
-
7/30/2019 Introducion to C Programming
23/164
C Tokens
Identifiers
Keywords
Constants Operators
Special symbols
-
7/30/2019 Introducion to C Programming
24/164
Identifiers
Identifiers are names given to variousprogram elements such as variables,functions and arrays etc,.
Eg: #define N 10
#define a 15
Here N and a are user defined identifiers.
-
7/30/2019 Introducion to C Programming
25/164
Rules for naming identifier First character must be alphabetic or
underscore.
Must consist only of alphabetic characters,digits, or underscores.
Only the first 31 characters of an identifier aresignificant and are recognized by the compiler.
Cannot use a keywordsorreserved word(e.g.main, include, printf & scanf etc.).
No space are allowed between the identifiersetc,.
C is case sensitive, e.g. My_name my_name.
-
7/30/2019 Introducion to C Programming
26/164
Examples of Valid and Invalid Names
Valid Names Invalid Names
a a1 $sum /* $ is illegal */
student_name stdntNm 2names /* Starts with 2 */
_aSystemName _anthrSysNm stdnt Nmbr /* no spaces */
TRUE FALSE int /* reserved word */
-
7/30/2019 Introducion to C Programming
27/164
Variables
Variable is an identifier that is used torepresent some specified type ofinformation.
Eg: x=3
Here x is variable.
-
7/30/2019 Introducion to C Programming
28/164
Keywords
It is a reserved words.
Cannot be used for anything else.
Examples: int
while
for etc,.
-
7/30/2019 Introducion to C Programming
29/164
Keywords
Auto register Continue
Double typedef For
Int Char signed
Struct extern void
Break return Default
Else union Goto
Long Const sizeof
Switch Float do
Case short If
Enum unsigned
Static While
-
7/30/2019 Introducion to C Programming
30/164
Constants
It is an entity whose value does notchanges during the execution.
Eg: x=3
Here 3 is a constant.
-
7/30/2019 Introducion to C Programming
31/164
Types
Numeric constants
Character constant
-
7/30/2019 Introducion to C Programming
32/164
Constants
Constants
Character ConstantsNumeric Constants
RealConstant
IntegerConstant
StringConstant
SingleCharacterConstant
-
7/30/2019 Introducion to C Programming
33/164
Numeric constantsInteger constants
It is formed using a sequence of digits.
Decimal - 0 to 9 .
Octal - 0 to 7.
Hexa - 0 to 9 ,A to F
Eg: 10,75 etc.
-
7/30/2019 Introducion to C Programming
34/164
Rules for defining Integer Constant
It must have atleast one digit.
Decimal point are not allowed.
No blank space or commas are allowed. It can be either positive or negative. Etc,.
-
7/30/2019 Introducion to C Programming
35/164
Numeric constants
Real constants
It is formed using a sequence of digits butit contain decimal point.
length, height, price distance measured inreal number
Eg: 2.5, 5.11, etc.
-
7/30/2019 Introducion to C Programming
36/164
Character constants
Single character constant
A character constant is a single characterthey also represented with single digit or a
single special symbol which is enclosed insingle quotes.
Eg: a, 8,_etc.
-
7/30/2019 Introducion to C Programming
37/164
Character constants
String constants
String constant are sequence of charactersenclosed with in double quote.
Eg: Hello ,444,a etc,.
-
7/30/2019 Introducion to C Programming
38/164
Operators
An operator is a symbol that specifies anoperation to be performed on theoperands.
Eg: a + b
+ is an operator.
a,b are operands.
-
7/30/2019 Introducion to C Programming
39/164
Data Types
A Data typeis the type of data that aregoing to access within the program.
-
7/30/2019 Introducion to C Programming
40/164
StandardData Types
These Standard type can be used to buildmore complex data types calledDerived
Types (e.g. pointers, array, union etc.).
D t t
-
7/30/2019 Introducion to C Programming
41/164
Data types
Data type Size(bytes) Range Format string
Char 1 -128 to 127 %c
int 2 -32,768 to 32,767 %d
Float 4 3.4 e-38 to 3.4 e+38 %f
Double 8 1.7 e-308 to 1.7 e+308 %lf
-
7/30/2019 Introducion to C Programming
42/164
integer
A number without a fraction part : integralnumber.
C supports three different sizes of theinteger data type :
shortint int
longint
-
7/30/2019 Introducion to C Programming
43/164
Floating Point
A floating-point type is a number with afractional part, e.g. 56.78
Floating point numbers are stored using
4 Byte. Types
Float
Double long double
-
7/30/2019 Introducion to C Programming
44/164
character - PLato
Character are generally stored using 8bits(1 Byte) of the internal storage.
Character ASCII code value
a 97(decimal) or 01100001(binary)
x 120(decimal) or 01111000(binary)
-
7/30/2019 Introducion to C Programming
45/164
void
The void type has no values and nooperations.
Both the set of values and the set ofoperations are empty.
-
7/30/2019 Introducion to C Programming
46/164
Variables Declaration
To create a variable, you must specifythe type and then its identifier :
float price;int a,b;
char code;
E ti D t t i
-
7/30/2019 Introducion to C Programming
47/164
Entire Data types in c:
Data type Size(bytes) Range Format string
Char 1 128 to 127 %c
Unsigned char 1 0 to 255 %c
Short or int 2 -32,768 to 32,767 %i or %d
Unsigned int 2 0 to 65535 %u
Long 4 -2147483648 to 2147483647 %ld
Unsigned long 4 0 to 4294967295 %lu
Float 4 3.4 e-38 to 3.4 e+38 %f or %g
Double 8 1.7 e-308 to 1.7 e+308 %lf
Long Double 10 3.4 e-4932 to 1.1 e+4932 %lf
-
7/30/2019 Introducion to C Programming
48/164
Types of Operator
Arithmetic operator
Relational operator
Logical operator
Assignment operator
Increment or decrement operator(unary)
Bitwise operator Conditional operator
-
7/30/2019 Introducion to C Programming
49/164
Arithmetic operator
It is used to carry out arithmeticoperations like addition, subtraction etc,
Eg: + , - , * , / etc,
-
7/30/2019 Introducion to C Programming
50/164
Sample program
#include // Header File#include int b=10; //Global Declarationvoid main ( ) /* main is the starting of every c program */{int a,c; //Local Declarationclrscr( );scanf(%d,&a);printf( \n The sum of the two values:);
c = a+b;printf(%d,c);getch( );}
Di i i t Diff t
-
7/30/2019 Introducion to C Programming
51/164
Division operator on DifferentData Type
Operation Result Example
int/int int 5/2 = 2
int/real real 5/2.0 = 2.5
real/int real 5.0/2 = 2.5
real/real real 5.0/2.0 = 2.5
Sample program
-
7/30/2019 Introducion to C Programming
52/164
Sample program
#include
#include void main ( ){int a=10,b=4,c;
float d=3,e;clrscr( );c = a/b;printf(" \n value a/b is:%d",c);e = a/d;printf("\n value a/d is:%f",e);getch( );
}
-
7/30/2019 Introducion to C Programming
53/164
Output
value a/b is:2
value a/d is:3.333333
-
7/30/2019 Introducion to C Programming
54/164
Relational operator
It is used to compare two or moreoperands.
Eg :< , > , =, != etc,.
5 < 9 which will return 1
-
7/30/2019 Introducion to C Programming
55/164
Logical operator
It is used to combine the result of two ormore condition.
AND(&&)
OR (||)
NOT (!) are Logical operators.
Eg: (i>10)&&(j>5).(i>10)||(j>5) etc,.
Sample program
-
7/30/2019 Introducion to C Programming
56/164
Sample program#include#include
void main ( ){int a=10,b=3,c=5,e;clrscr( );
if(a>b) // relational operator{
printf(" \n a is bigger than b");}if((a>b)&&(a>c)) //Logical operator
{printf(" \n a is biggest");
}getch( );
}
-
7/30/2019 Introducion to C Programming
57/164
Output
a is bigger than b
a is biggest
-
7/30/2019 Introducion to C Programming
58/164
Assignment operator
It is used to assign a value orexpression etc to a variable.
Eg: a =10.
a = b
a = b + c etc,.
-
7/30/2019 Introducion to C Programming
59/164
Assignment operator(Cont)
Compound operator
It is also used to assign a value to avariable.
Eg: x + = y means x = x + y
Nested operator
It is used for multiple assignment.Eg: i = j = k = 0;
-
7/30/2019 Introducion to C Programming
60/164
Sample program#include
#include int b=10;
void main ( )
{int a=3,b=5;
clrscr( );
a+=b; // a= a+b
printf(" \n The sum of the two values:%d",a);
getch( );
}
-
7/30/2019 Introducion to C Programming
61/164
Output
The sum of the two values:8
Increment or decrement
-
7/30/2019 Introducion to C Programming
62/164
Increment or decrementoperator(Unary)
It is used to Increment or decrement anoperand.
Eg: ++x (Pre Increment),
x++ (Post Increment),
--x (Pre Decrement),x-- (Post Decrement).
Sample Program
-
7/30/2019 Introducion to C Programming
63/164
Sample Program#include
#include void main ( )
{
int a=5;clrscr( );
printf(" \n Post increment Value:%d",a++);
printf(" \n Pre increment Value:%d",++a);printf(" \n Pre decrement Value:%d",--a);
printf(" \n Post decrement Value:%d",a--);
getch( );
-
7/30/2019 Introducion to C Programming
64/164
Output
Post increment Value:5
Pre increment Value:7
Pre decrement Value:6
Post decrement Value:6
-
7/30/2019 Introducion to C Programming
65/164
Bitwise operator
It is used to manipulate data at bit level.
Eg: a=5 i.e 0000 0101
b=4 i.e 0000 0100
Then a & b = 0000 0100
a | b = 0000 0101 etc,.
Sample program
-
7/30/2019 Introducion to C Programming
66/164
Sample program
#include
#include void main ( ){
int a=5,b=4,c;//char a=5,b=4,c;clrscr( );
c = a&b;printf(" \n value a&b is:%d",c);getch( );}
-
7/30/2019 Introducion to C Programming
67/164
Output
value a&b is:4
Conditional Operator (or)
-
7/30/2019 Introducion to C Programming
68/164
Conditional Operator (or)Ternary Operator
It is used to checks the condition andexecute the statement depending on thecondition.
Eg: C = a > b ? a:b
Sample Program
-
7/30/2019 Introducion to C Programming
69/164
Sample Program#include
#include void main ( )
{
int a=5,b=8,c;
clrscr( );
c = a>b?a:b; //Conditional operator
printf(" \n The Larger Value is%d",c);
getch( );
}
-
7/30/2019 Introducion to C Programming
70/164
Output
The Larger Value is 8
-
7/30/2019 Introducion to C Programming
71/164
Special Operator
comma operator ( , )
sizeof operator
pointer operator (& , *) etc,.
-
7/30/2019 Introducion to C Programming
72/164
#include#include
void main ( )
{int c;
clrscr( );
printf(" \n size of int is:%d",sizeof c);
getch( );
}
-
7/30/2019 Introducion to C Programming
73/164
Output
size of int is: 2
-
7/30/2019 Introducion to C Programming
74/164
Expression
An expression represent data item such asvariable, constant are interconnectedusing operators.
Eg:
Expression C Expression
a + b + c a + b + c
a2+b2 a*a + b*b
-
7/30/2019 Introducion to C Programming
75/164
Operator Precedence & Associativity
The arithmetic expressions evaluation arecarried out based on the precedence andassociativity.
The evaluation are carried in two phases. First Phase: High Priority operators are
evaluated.
Second Phase: Low Priority operators areevaluated.
-
7/30/2019 Introducion to C Programming
76/164
Precedence Operator
High * , / , %
Low + , -
-
7/30/2019 Introducion to C Programming
77/164
Example
5 - 20/4 + 3*3 1
= 5 - 5 + 9 1
= 0 + 9 1
= 9 1
= 8
-
7/30/2019 Introducion to C Programming
78/164
Example
5 (20/4) + 3*(3 1)
= 5 - 5 + 3*2
= 5 - 5 + 6
= 6
T C i
-
7/30/2019 Introducion to C Programming
79/164
Type Conversion
Converting the type of an expression fromone type to another type.
Eg: x = (int)10.45
Sample Program
-
7/30/2019 Introducion to C Programming
80/164
Sample Program#include
#include void main ( ){int c;
clrscr( );c=(int)10.45;printf("\nOutput is:%d",c);getch( );}OUTPUTOutput is:10
I t/O t t F ti
-
7/30/2019 Introducion to C Programming
81/164
Input/Output Function
Input/OutputFunction
UnformattedFormatted
Output
printf()fprintf()
Input
scanf()fscanf()
Input
getc()gets()
getchar()
Output
putc()puts()
putchar()
-
7/30/2019 Introducion to C Programming
82/164
Formatted Input/Output
C uses two functions forformattedinput and output.
Formatted input : reads formatteddata from the keyboard.
Formatted output : writes formatteddata to the monitor.
-
7/30/2019 Introducion to C Programming
83/164
FormattedInput and Output
-
7/30/2019 Introducion to C Programming
84/164
Standard Output
The standard output file is the monitor.
Like the keyboard, it is a textfile.
When you need to display data that is
not text, it must be converted into to the
text before it is written to the screen.
F t f i tf St t t
-
7/30/2019 Introducion to C Programming
85/164
Format of printf Statement
F tt d I t ( f)
-
7/30/2019 Introducion to C Programming
86/164
Formatted Input (scanf)
The standard formatted input function in C isscanf(scan formatted).
scanfconsists of : a format string .
an address list that identifies where dataare to be placed in memory.
scanf ( format string, address list );
(%c.%d..%f.., &a,.&i,..,&x..)
Format of scanf Statement
-
7/30/2019 Introducion to C Programming
87/164
Format of scanf Statement
Ch t T t F ti
-
7/30/2019 Introducion to C Programming
88/164
Character Test Function
It is used to test the character taken fromthe input.
isalpha(ch)
isdigit(ch) islower(ch)
isupper(ch)
tolower(ch) toupper(ch) etc,.
-
7/30/2019 Introducion to C Programming
89/164
D i i M ki
-
7/30/2019 Introducion to C Programming
90/164
Decision Making
It is used to change the order of theprogram based on condition.
Categories:
Sequential structure
Selection structure
Iteration structure
Encapsulation structure
Decision Making (cont)
-
7/30/2019 Introducion to C Programming
91/164
Decision Making (cont)
Sequential structure
In which instructions are executed insequence.
Selection structure
In which instruction are executed based onthe result of some condition.
Iteration structure
In which instruction are executed repeatedly. Encapsulation structure
In which some compound structure are used.
SELECTION STRUCTURE
-
7/30/2019 Introducion to C Programming
92/164
SELECTION STRUCTURE
It allows the program to make a choicefrom alternative paths.
C provide the following selection
structures IF statement
IF ELSE statement
Nested IF ELSE statement IF ELSE ladder
IF Statement
-
7/30/2019 Introducion to C Programming
93/164
IF Statement
SyntaxIF (condition is true)
{
Statements;
}
Ifcondition
False
True
Statements
Example
-
7/30/2019 Introducion to C Programming
94/164
Example#include#include
void main ( ){int a;clrscr( );printf("\nEnter the number:");scanf("%d",&a);
if(a>10)
{ printf(" \n a is greater than 10");}
getch( );
}
Output
-
7/30/2019 Introducion to C Programming
95/164
Output
Enter the number: 12
a is greater than 10
IFELSE Statement
-
7/30/2019 Introducion to C Programming
96/164
IFELSE Statement
Syntax
IF (condition)
{
True statements;
}ELSE
{
False statements;
}
IfCondition
True False
Truestatements
Falsestatements
#include#include
-
7/30/2019 Introducion to C Programming
97/164
#include void main ( ){
int a;clrscr( );printf("\nEnter the number:");scanf("%d",&a);
if(a>10){printf(" \n a is greater than 10");
}else
{printf(" \n a is less than 10");
}getch( );
}
NESTED IF ELSE
-
7/30/2019 Introducion to C Programming
98/164
NESTED IF ELSE
IfCondition
2
True False
Truestatements
Falsestatements
IfCondition
1False
StatementsTrue
-
7/30/2019 Introducion to C Programming
99/164
IF ELSE LADDER
-
7/30/2019 Introducion to C Programming
100/164
IFELSE LADDER
Condition1
Statements
Condition
2
Statements
Condition3
Statements Statements
TRUE
TRUE
TRUE FALSE
FALSE
FALSE
IFELSE LADDERSyntax
-
7/30/2019 Introducion to C Programming
101/164
yIF (condition1){
statements;}
else if (condition2){
statements;}else if (condition3){statements;
}else{statements;
}
Example
-
7/30/2019 Introducion to C Programming
102/164
Example#include#includevoid main(){int m1,m2,m3;float avg;
printf("\nEnter the marks:");scanf("%d%d%d",&m1,&m2,&m3);avg=(m1+m2+m3)/3;printf("\n The average is:%f",avg);
printf("\n The Grade is:");if(avg>=60){
printf("First class");}
-
7/30/2019 Introducion to C Programming
103/164
Output
-
7/30/2019 Introducion to C Programming
104/164
Output
Enter the marks:65
75
70The average is:70.000000
The Grade is: First class
Looping structure
-
7/30/2019 Introducion to C Programming
105/164
Looping structure
It is used to execute some instructionsseveral time based on some condition.
WHILE
DoWHILE For
WHILE Loop
-
7/30/2019 Introducion to C Programming
106/164
WHILE Loop
Syntax.
WHILE (condition)
{ .Body of the loop;
.} Body of The loop
conditionFalse
True
Example
-
7/30/2019 Introducion to C Programming
107/164
Example#include
#includevoid main(){int i=1,fact=1,n;printf("\nEnter the Number:");scanf("%d",&n);
while(i
-
7/30/2019 Introducion to C Programming
108/164
Output
Enter the Number:3
The value of 3! is: 6
DO WHILE Loop
-
7/30/2019 Introducion to C Programming
109/164
DOWHILE Loop
Syntaxdo
{
Body of the loop
}while (condition);
Body of The loop
condition
False
True
for loop
-
7/30/2019 Introducion to C Programming
110/164
for loop
Syntaxfor (initialization; test condition; Increment/Decrement)
{
Body of the loop
}
for loop
-
7/30/2019 Introducion to C Programming
111/164
for loop
Initialization
condition False
Body of the loop
Inc / Decrement
Example
-
7/30/2019 Introducion to C Programming
112/164
Example#include
#includevoid main(){int i,fact=1,n;printf("\nEnter the Number:");scanf("%d",&n);
for(i=1;i
-
7/30/2019 Introducion to C Programming
113/164
Output
Enter the Number:3
The value of 3! is: 6
Nested for loop
-
7/30/2019 Introducion to C Programming
114/164
Nested for loop
Syntaxfor (initi; cond; Inc/Dec)
{
for (initi; cond; Inc/Dec){
Body of the loop
}
}
CASE structure
-
7/30/2019 Introducion to C Programming
115/164
Case 1
Case 2
Default
case
Switch
CASE structureSyntax
-
7/30/2019 Introducion to C Programming
116/164
Syntaxswitch (expression)
{case constant 1:block1;break;
case constant 2:block2;break;..
default :default block;break;
}
Example
-
7/30/2019 Introducion to C Programming
117/164
Example#include
#includevoid main(){int i,n;printf("\nEnter the Number:");
scanf("%d",&n);
switch(n){
case 1: {printf("\n Its in case 1");break;}
-
7/30/2019 Introducion to C Programming
118/164
case 2:
{printf("\n Its in case 2");break;}
default: {printf("\n Its in default");break;}
}getch();}
Output
-
7/30/2019 Introducion to C Programming
119/164
Output
Enter the Number:2
Its in case 2
break Statement
-
7/30/2019 Introducion to C Programming
120/164
break Statement
It is used to terminate the loop When a breakstatement is encountered
inside a loop, then the loop is terminated.
Loops with break Statement
-
7/30/2019 Introducion to C Programming
121/164
Loops with break Statement
while(cond){
if(cond)
break;
}
-
7/30/2019 Introducion to C Programming
122/164
do{
if(cond)
break;
} while(cond);
-
7/30/2019 Introducion to C Programming
123/164
for (initi; condt; Inc/Dec){
if(cond)
break;
}
Continue Statement
-
7/30/2019 Introducion to C Programming
124/164
Continue Statement
When a continue statement isencountered inside a loop, the control istransferred to the beginning.
Loops with continue Statement
-
7/30/2019 Introducion to C Programming
125/164
oops co ue S a e e
while(cond){
if(cond)
continue;
}
-
7/30/2019 Introducion to C Programming
126/164
do{
if(cond)
continue;
} while(cond);
-
7/30/2019 Introducion to C Programming
127/164
for (initi; condt; Inc/Dec){
if(cond)
continue;
}
goto Statement
-
7/30/2019 Introducion to C Programming
128/164
g
When a gotostatement is encounteredinside a loop, the control is transferred tothe beginning.
Syntax for goto Statement
-
7/30/2019 Introducion to C Programming
129/164
y g
label:
goto label;
-
7/30/2019 Introducion to C Programming
130/164
goto label;
label:
-
7/30/2019 Introducion to C Programming
131/164
getchar() Example
-
7/30/2019 Introducion to C Programming
132/164
g () p
#include#include
#include
void main()
{
char x;
printf("enter the character:");
x=getchar();
if(islower(x))
-
7/30/2019 Introducion to C Programming
133/164
( s o e ( ))
putchar(toupper(x));
else
putchar(tolower(x));
getch();}
Output:enter the character:ABC
a
getche() Example
-
7/30/2019 Introducion to C Programming
134/164
g () p
#include #include
void main()
{
char c ;
clrscr();
printf("\nInput a string:");
c = getche();
-
7/30/2019 Introducion to C Programming
135/164
printf("\nstring is:");putch(c);
getch();
}
Output:
Input a string:k
string is:k
-
7/30/2019 Introducion to C Programming
136/164
-
7/30/2019 Introducion to C Programming
137/164
printf("\nstring is:");putch(c);
getch();
}
Output:
Input a string:
string is:h
getc Example
-
7/30/2019 Introducion to C Programming
138/164
#include#include
#include
void main()
{
char x;
printf("enter the character:");
x=getc(stdin);
if(islower(x))
-
7/30/2019 Introducion to C Programming
139/164
putc(toupper(x),stdout);
else
putc(tolower(x),stdout);
getch();
}
Output:enter the character:abc
A
gets() Example
-
7/30/2019 Introducion to C Programming
140/164
#include #include
void main()
{
char c[80];
clrscr();
printf("Input a string:");
gets(c);
-
7/30/2019 Introducion to C Programming
141/164
Example#include
-
7/30/2019 Introducion to C Programming
142/164
#include#includevoid main(){
int a,b,c,n;
clrscr();printf("\nEnter the value of a,b:");scanf("%d%d",&a,&b);printf("\nMENU");
printf("\n1.ADD\n2.SUB\n3.MULTIPLY\n0.EXIT");printf("\nEnter the choice:");scanf("%d",&n);
switch(n){
-
7/30/2019 Introducion to C Programming
143/164
{case 1:
c=a+b;printf("\nThe result of Addition is:%d",c);break;
case 2:c=a-b;printf("\nThe result of Subtraction is:%d",c);
break;
-
7/30/2019 Introducion to C Programming
144/164
case 3: c=a*b;printf("\nThe result of Multiplication is:%d",c);break;
case 0:exit(0);break;
}
getch();}
-
7/30/2019 Introducion to C Programming
145/164
-
7/30/2019 Introducion to C Programming
146/164
-
7/30/2019 Introducion to C Programming
147/164
if(a==sum){printf("\nIt is an armstrong number");
}
else{printf("\nIt is not an armstrong number");
}
getch();}
Output
-
7/30/2019 Introducion to C Programming
148/164
Enter the number:153It is an armstrong number
Sum of the Digits
-
7/30/2019 Introducion to C Programming
149/164
#include#includevoid main(){
int r=0,sum=0,n;printf("\nEnter the no:");scanf("%d",&n);while(n>0)
{r=n%10;
-
7/30/2019 Introducion to C Programming
150/164
Output
-
7/30/2019 Introducion to C Programming
151/164
Enter the no:156sum of the digits is:12
Reverse of a number
-
7/30/2019 Introducion to C Programming
152/164
#include#include
void main()
{
int r=0,sum=0,n;
printf("\nEnter the no:");
scanf("%d",&n);
while(n>0)
-
7/30/2019 Introducion to C Programming
153/164
{r=n%10;
sum=sum*10+r;
n=n/10;}
printf("Reverse of the number is:%d",sum);
getch();}
Output
-
7/30/2019 Introducion to C Programming
154/164
Enter the no:567Reverse of the number is:765
Fibonacci Series
-
7/30/2019 Introducion to C Programming
155/164
#include#include
void main()
{
int f=0,f1=-1,f2=1,n,i;
printf("\nEnter the number:");
scanf("%d",&n);
-
7/30/2019 Introducion to C Programming
156/164
while(f
-
7/30/2019 Introducion to C Programming
157/164
Enter the number:50 1 1 2 3 5
Swapping#include
-
7/30/2019 Introducion to C Programming
158/164
#include stdio.h#include void main ( ){int a,b,c;
clrscr( );printf(" \nEnter the value of a:");scanf("%d",&a);printf(" \nEnter the value of b:");
scanf("%d",&b);c=a;a=b;b=c;
printf(" \nThe value of a is:%d",a);
-
7/30/2019 Introducion to C Programming
159/164
printf(" \nThe value of b is:%d",b);
getch( );
}
Output:Enter the value of a:5
Enter the value of b:4
The value of a is:4
The value of b is:5
Swapping without using thirdvariable
-
7/30/2019 Introducion to C Programming
160/164
#include#include void main ( ){
int a,b;clrscr( );printf(" \nEnter the value of a:");scanf("%d",&a);
printf(" \nEnter the value of b:");scanf("%d",&b);
a=a+b;
b=a-b;
b
-
7/30/2019 Introducion to C Programming
161/164
a=a-b;
printf(" \nThe value of a is:%d",a);printf(" \nThe value of b is:%d",b);
getch( );
}
Output:
Enter the value of a:5
Enter the value of b:6
The value of a is:6
The value of b is:5
Quadratic Equation#include
-
7/30/2019 Introducion to C Programming
162/164
#include
#includevoid main ( ){int a,b,c,d,r1,r2;clrscr( );printf(" \nEnter the value of a:");scanf("%d",&a);printf(" \nEnter the value of b:");scanf("%d",&b);
printf(" \nEnter the value of c:");scanf("%d",&c);d=b*b-4*a*c;
if(d>=0)
-
7/30/2019 Introducion to C Programming
163/164
{
r1=(-b+sqrt(d))/(2*a);r2=(-b-sqrt(d))/(2*a);
printf(" \nThe roots are %d,%d",r1,r2);}else{printf(" \nThe roots are imaginary");}
getch( );}
Output
-
7/30/2019 Introducion to C Programming
164/164
Enter the value of a:4
Enter the value of b:5
Enter the value of c:6
The roots are imaginary