You are on page 1of 121

C Language

Fundamentals
Unit II

PREETHA V - AP/CSE, SRIT.


INTRODUCTION TO C
PROGRAMMING

PREETHA V - AP/CSE, SRIT.


Program

 Program is a collection of instructions that will perform


some task.
 Source code of a Hello World program written in the C
programming language
#include <stdio.h>

int main(void)

printf("Hello world!\n");

return 0;

PREETHA V - AP/CSE, SRIT.


INTRODUCTION TO C

C was developed by Dennis Ritchie at Bell


laboratory in 1972
 It is an upgraded version of languages B and
BCPL.

PREETHA V - AP/CSE, SRIT.


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,.

PREETHA V - AP/CSE, SRIT.


Structure of C program

GLOBAL DECLARATION SECTION

PREETHA V - AP/CSE, SRIT.


Compiling and
Linking

PREETHA V - AP/CSE, SRIT.


Compiling and Linking

 C programs are written in human readable source code


that is not directly executable by a computer. It takes a
three step process to transform the source code into
executable code. These three steps are: Preprocessing,
compiling and linking.
 Preprocessing - Processes directives (commands that
begin with a # character) which can modify the source
code before it is compiled.
 Compiling - The modified source code is compiled into
binary object code. This code is not yet executable.
 Linking - The object code is combined with required
supporting code to make an executable program. This step
typically involves adding in any libraries that are required.

PREETHA V - AP/CSE, SRIT.


Compiling and Linking
 In most modern compilers, these three activities are handled
by a single application although it is possible to tell the
compiler not to do certain functions. (For example, to compile
but not link a program.)
 There are a variety of C compilers available for many different
platforms. Some compilers must be purchased and some are
free to use. Three of the most common are: GNU gcc,
Clang/LLVM and Microsoft Visual C.
 GNU gcc is found on many platforms such as Linux, many
flavors of UNIX and even Windows.
 Clang/LLVM is available for all modern Mac OSX systems and
many BSD variants.
 Microsoft Visual C is a core component in Microsoft's Visual
Studio platform.

PREETHA V - AP/CSE, SRIT.


Compiling and Linking

PREETHA V - AP/CSE, SRIT.


Executing a C Program

 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.

PREETHA V - AP/CSE, SRIT.


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.

PREETHA V - AP/CSE, SRIT.


Identifiers

 Identifiers are names given to various program elements


such as functions and arrays etc,.
 Example:

int total,marks;
 Here total and marks are user
defined identifiers.

PREETHA V - AP/CSE, SRIT.


Rules for naming identifier

 First character must be alphabetic or underscore.


 Mustconsist only of alphabetic characters, digits, or
underscores.
 Only the first 31 characters of an identifier are
significant and are recognized by the compiler.
 Cannot use a keywords or reserved word (e.g. main,
include, printf & scanf etc.).
 No space are allowed between the identifiers etc,.
C is case sensitive, e.g. My_name  my_name.
PREETHA V - AP/CSE, SRIT.
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 */

Variables :
Variable is an identifier that is used to represent some
specified type of information.
Eg: x=3 Here x is variable.
PREETHA V - AP/CSE, SRIT.
Keywords
It is a reserved word, which cannot be used for
anything else. Examples:
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
PREETHA V - AP/CSE, SRIT.
Constants

It is an entity whose value does not changes during


the execution.

Constants

Numeric Constants Character Constants

Integer Real Single String


Constant Constant Character Constant
PREETHA V - AP/CSE, SRIT. Constant
Numeric constants
Integer 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.

Rules for defining Integer Constant:


 It must have at least one digit.
 Decimal point are not allowed.
 No blank space or commas are allowed.
 It can be either positive or negative. Etc,.
PREETHA V - AP/CSE, SRIT.
Numeric constants (Cont)
Real constants
 It is formed using a sequence of digits but it contain decimal point.
 length, height, price distance measured in real number.
Eg: 2.5, 5.11, etc.

PREETHA V - AP/CSE, SRIT.


Character constants

Single character constant


A character constant is a single character they also represented with single
digit or a single special symbol which is enclosed in single quotes.
Eg: ‘a’, ‘8’,’_’etc.

String constants
String constant are sequence of characters
enclosed with in double quote.
Eg: “Hello” ,”444”,”a” etc,.
PREETHA V - AP/CSE, SRIT.
Data Types
A data type is a classification identifying one of
various types of data, such as real-
valued, integer or Boolean, that determines the
possible values for that type

PREETHA V - AP/CSE, SRIT.


Integer
A number without a fraction part : integral number.
Integer occupies 2 Bytes.
The Control or Format String for integer is %d.
C supports three different sizes of the integer data type :
short int
int
long int

PREETHA V - AP/CSE, SRIT.


Floating Point

 A floating-point type is a number with a fractional part, e.g. 56.78.


 Floating point numbers occupies 4 Bytes.
 The Control or Format String for float is %f.
 C supports three different sizes of the float data type
 Double
 float
 long double

PREETHA V - AP/CSE, SRIT.


Character

 Character are generally stored using 8 bits(1 Byte)


of the internal storage.
 The Control or Format String for Character is %c.

Character ASCII code value


a 97(decimal) or 01100001(binary)
x 120(decimal) or 01111000(binary)
PREETHA V - AP/CSE, SRIT.
Void
 The void type has no values and no operations.
 Both the set of values and the set of operations are empty.

PREETHA V - AP/CSE, SRIT.


Entire Data types in c:
Data type Size(bytes) Range Format string
(or)Control 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


PREETHA V - AP/CSE, SRIT.
Variable’s Declaration

 To create a variable, you must specify


the type.
Example: float price;
int a,b;
char code;

PREETHA V - AP/CSE, SRIT.


EXPRESSIONS
USING
OPERATOR IN C

PREETHA V - AP/CSE, SRIT.


Introduction
 C supports a large set of built-in operators.

 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.

PREETHA V - AP/CSE, SRIT.


 C operators can be classified into following types
 Arithmetic operators
 Relation operators
 Logical operators
 Bitwise operators
 Assignment operators
 Conditional operators
 Special operators

PREETHA V - AP/CSE, SRIT.


Arithmetic operators
 C supports all the basic arithmetic operators.

Operator Description
+ Adds two operands
- Subtracts second operand
from first
* Multiply two operand
/ Divide numerator by
denominator
% Remainder of division
++ Increment operator-
increases integer value by
one
-- Decrement operator-
decreases integer value by
PREETHA V - AP/CSE, SRIT. one
Sample program
#include<stdio.h> // Header File
#include <conio.h>
int b=10; //Global Declaration
void main ( ) /* main is the starting of every c program */
{
int a,c; //Local Declaration
clrscr( );
scanf(“%d”,&a);
printf(“ \n The sum of the two values:”);
c = a+b;
printf(“%d”,c);
getch( );
PREETHA V - AP/CSE, SRIT.

}
Relational operators
 The relational operators are

Operator Description
== Check if two operand are
equal
!= Check if two operand are
not equal
> Check if operand on the
left is greater than
operand on the right
< Check if operand on the
left is smaller than
operand on the right
>= Check left operand is
greater than or equal to
right operand
<= Check if operand on left is
PREETHA V - AP/CSE, SRIT. smaller than or equal to
right operand
Sample program
#include<stdio.h>
#include <conio.h>
void main ( )
{
int a=10,b=3,c=5;
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( );
}

Output:
a is bigger than b
a is biggest PREETHA V - AP/CSE, SRIT.
Logical operators
 C supports following 3 logical operators
 Suppose a=1 and b=0

Operator Description Example


&& Logical AND (a&&b) is false
|| Logical OR (a||b) is false
! Logical NOT (!a) is false

PREETHA V - AP/CSE, SRIT.


Sample program
#include<stdio.h>
int main()
{
int year;
printf("Enter any year: ");
scanf("%d",&year);
if(((year%4==0)&&(year%100!=0))||(year%400==0))
printf("%d is a leap year",year);
else
printf("%d is not a leap year",year);
return 0;
}
Sample output:
PREETHA V - AP/CSE, SRIT.
Enter any year: 2010
2010 is not a leap year
Bitwise operators
 Bitwise operators perform manipulations of data at bit level.

 These operators also perform shifting of bits from right to left

 Bitwise operators are not applied to float or double.

PREETHA V - AP/CSE, SRIT.


Operator Description
& Bitwise AND
| Bitwise OR
^ Bitwise exclusive OR
<< Left shift
>> Right shift

PREETHA V - AP/CSE, SRIT.


Truth table for bitwise &, | and ^

a b a&b a|b a^b


0 0 0 0 0
0 1 0 1 1
1 0 0 1 1
 The bitwise shift operator shifts the bit value.
1 1 1 1 0
 The left operand specifies the value to be shifted and the right operand specifies the
number of positions that the bits in the value are to be shifted.
 Both operands have the same precedence.

PREETHA V - AP/CSE, SRIT.


Example:
a=0001000
b=2
a<<b=0100000
a>>b=0000010

PREETHA V - AP/CSE, SRIT.


Sample program

#include<stdio.h>
#include <conio.h>
void main ( )
{
int a=5,b=4,c;
clrscr( );
c = a&b;
printf(" \n value a&b is:%d",c);
getch( );
}

Output:
value a&b is:4
PREETHA V - AP/CSE, SRIT.
Assignment operators
 Assignment operators supported by C are

Operator Description Example


= Assigns values a=b
from right side
operator to left side
operator
+= Adds right operand a+=b is same as
to the left operand a=a+b
and assign the
result to left
-= Subtracts right a-=b is same as
operand from the a=a-b
left operand and
assign the result to
left operand
PREETHA V - AP/CSE, SRIT.
Contd.,
*= Multiply left a=*b is same as
operand with the a=a*b
right operand and
assign the result
to left operand
/= Divides left a/=b is same as
operand with the a=a/b
right operand and
assign the result
to left operand
%= Calculate a%=b is same as
modulus using a=a%b
two operands and
assign the result
to left operand

PREETHA V - AP/CSE, SRIT.


Sample program

#include<stdio.h>
#include <conio.h>
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( );
}

Output:
The
PREETHAsum ofSRIT.the two values:8
V - AP/CSE,
Increment or decrement
operator (Unary)
 It is used to Increment or decrement an operand.

 Eg: ++x (Pre Increment),


x++ (Post Increment),
--x (Pre Decrement),
x-- (Post Decrement).

PREETHA V - AP/CSE, SRIT.


Sample Program
#include<stdio.h>
#include <conio.h>
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( );
}
Output:
Post increment Value:5
Pre increment Value:7
Pre decrement Value:6
Post decrement Value:6
PREETHA V - AP/CSE, SRIT.
Conditional operators

 Conditional operator in C is called by two more names:


 Ternary operator
 ?:Operator

 It is actually the if condition that we use in C language


 By using conditional operator we turn if condition statement
into more simple line of code

PREETHA V - AP/CSE, SRIT.


 The syntax of a conditional operator is:
expression 1?expression 2:expression 3
Where,
 The question mark “?” in the syntax represents the
if part
 The first expression is used to check the condition
 If that condition is true, then the expression2 is
executed
 If that condition is false, then the expression 3 is
executed.

PREETHA V - AP/CSE, SRIT.


Sample Program
#include<stdio.h>
#include <conio.h>
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( );
}

Output:
The Larger Value is 8

PREETHA V - AP/CSE, SRIT.


Special Operator

 comma operator ( , )
 sizeof operator
 pointer operator (& , *) etc,.

PREETHA V - AP/CSE, SRIT.


Sample program

#include<stdio.h>
#include <conio.h>
void main ( )
{
int c;
clrscr( );
printf(" \n size of int is:%d",sizeof c);
getch( );
}

Output:
size of int is: 2
PREETHA V - AP/CSE, SRIT.
Expression
 An expression represents data item such as variable, constant which are
interconnected using operators.
 Eg:

Expression C Expression

a+b+c a+b+c

a2+b2 a*a + b*b

PREETHA V - AP/CSE, SRIT.


Operator Precedence & Associativity

 The arithmetic expressions evaluation are carried out based on


the precedence and associativity.

 The evaluation are carried in two phases.


 First Phase: High Priority operators are
evaluated.
 Second Phase: Low Priority operators are
evaluated.

PREETHA V - AP/CSE, SRIT.


Precedence Operator
High *,/,%
Low +,-

Example:
5 - 20/4 + 3*3 – 1 5 – (20/4) + 3*(3 – 1)
=5-5+9–1 = 5 - 5 + 3*2
=0+9–1 =5-5+6
=9–1 =6
=8
PREETHA V - AP/CSE, SRIT.
Input/output Function
Input/output
Function

Formatted Unformatted

Input Output Input Output

scanf() printf() getc() putc()


fscanf() fprintf() gets() puts()
getchar() putchar()
PREETHA V - AP/CSE, SRIT.
Formatted Input and Output

PREETHA V - AP/CSE, SRIT.


Format of scanf Statement

PREETHA V - AP/CSE, SRIT.


PREETHA V - AP/CSE, SRIT.
Format of printf Statement

PREETHA V - AP/CSE, SRIT.


Managing input and output
operations

PREETHA V - AP/CSE, SRIT.


Introduction
 When we say input, it means to feed some data into a program

 An input can be given in the form of a file or from the command line

 C programming provides a set of built-in functions to read the given input and
display the output

PREETHA V - AP/CSE, SRIT.


The standard files:

Standard file File pointer Device


Standard input stdin Keyboard
Standard output stdout Screen
Standard error stderr Your screen

PREETHA V - AP/CSE, SRIT.


The getchar() and putchar() functions
 int getchar(void)reads the next available character
from the screen and returns it as an integer
 Reads only single character at a time
 Use this method in the loop to read more than one
character from the screen
 int putchar(int c)puts the passed character on the
screen and returns the same character
 Puts only single character at a time
 Use this method in the loop to display more than
one character on the screen
PREETHA V - AP/CSE, SRIT.
Example:
#include<stdio.h>
int main()
{ Output:
int c; enter a value: this is test
printf(“enter a value:”); you entered:t
c=getchar();
printf(“\n you entered”);
putchar(c);
return 0;
}
PREETHA V - AP/CSE, SRIT.
The gets() and puts() function

 char *gets(char *s)reads a line from stdin into the buffer pointed to by s until
either a terminating newline or EOF

 int puts(const char *s)writes the string ‘s’ and ‘a’ trailing newline to stdout

PREETHA V - AP/CSE, SRIT.


Example:
#include<stdio.h>
int main()
{
char str[100];
printf(“enter a value”);
gets(str);
printf(“you entered:”); Output:
puts(str); enter a value: this is test
return 0; you entered: this is test
}

PREETHA V - AP/CSE, SRIT.


The scanf and printf functions
 int scanf(const char *format) reads the input from
the standard input stream stdin and scans that input are
to the format provided

 Intprintf(const char *format,...) writes the output to


the standard output stream stdout and produces the
output according to the format provided

 Format can be simple constant, string, but you can


specify %s,%d,%c,%f,etc to print or read strings,
integer or float respectively.
PREETHA V - AP/CSE, SRIT.
Example:
#include<stdio.h>
int main() Output:
{ enter a value: seven 7
char str[100]; you entered: seven 7
int i;
printf(“enter a value:”);
scanf(“%s%d”,str,&i);
printf(“you entered:%s%d”,str,i);
return 0;
}

PREETHA V - AP/CSE, SRIT.


DECISION MAKING AND
BRANCHING AND LOOPING
STATEMENTS

PREETHA V - AP/CSE, SRIT.


Decision Making

 It is used to change the order of the program based on


condition.

 Categories:
 Sequential structure
 Selection structure
 Iteration structure etc,.

PREETHA V - AP/CSE, SRIT.


Decision Making (cont..)

 Sequential structure
 In which instructions are executed in sequence.

 Selection structure
 In which instruction are executed based on the result of some condition.

 Iteration structure
 In which instruction are executed repeatedly.

PREETHA V - AP/CSE, SRIT.


SELECTION STRUCTURE

 It allows the program to make a choice from alternative paths.

 C provide the following selection structures


 IF statement
 IF … ELSE statement
 Nested IF … ELSE statement
 IF … ELSE ladder

PREETHA V - AP/CSE, SRIT.


IF Statement

Syntax: If
if (condition is true) condition
True
{

Statements
Statements;
False

PREETHA V - AP/CSE, SRIT.


Example
#include<stdio.h>
#include <conio.h>
void main( )
{
int a;
clrscr( );
printf("\nEnter the number:");
scanf("%d",&a);
if(a>10)
{
printf(" \n a is greater than 10");
}
getch( );
}
Output:
Enter the number: 12
PREETHA V - AP/CSE, SRIT.

a is greater than 10
IF…ELSE Statement
Syntax
if (condition)
{ True If False
True statements; Condition
}
else True False
statements statements
{
False statements;
}

PREETHA V - AP/CSE, SRIT.


Example
#include<stdio.h>
#include <conio.h>
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");
} Output:
getch( ); Enter the number: 16
} a is greater than 10
PREETHA V - AP/CSE, SRIT.
NESTED IF… ELSE

If False
Condition
1
False
True
Statements
If
True False
Condition
2
True False
statements statements

PREETHA V - AP/CSE, SRIT.


NESTED IF… ELSE
Syntax

if (condition1)
{
if (condition2)
{
True statements;
}
else
{
False statements;
}

}
else
{
False statements;
}
PREETHA V - AP/CSE, SRIT.
IF…ELSE LADDER

TRUE Condition FALSE


1

TRUE Condition FALSE


Statements 2

TRUE Condition FALSE


Statements 3

Statements Statements

PREETHA V - AP/CSE, SRIT.


IF…ELSE LADDER
Syntax
IF (condition1)
{
statements;
}
else if (condition2)
{
statements;
}
else if (condition3)
{
statements;
}
else
{
statements;
PREETHA V - AP/CSE, }
SRIT.
Example
#include<stdio.h> else if(avg>=50)
#include<conio.h> {
printf("Second class");
void main() }
{ else if(avg>=35)
{
int m1,m2,m3; printf("Thrid class");
float avg; }
else
printf("\nEnter the marks:"); {
scanf("%d%d%d",&m1,&m2,&m3); printf("Fail");
avg=(m1+m2+m3)/3; }
getch();
printf("\n The average is:%f",avg); }
printf("\n The Grade is:"); Output:
if(avg>=60) Enter the marks:65
{ 75
70
printf("First class");
The average is:70.000000
} The Grade is: First class
PREETHA V - AP/CSE, SRIT.
Example

#include<stdio.h>
#include<conio.h>
void main()
{
float Basic;
float Gross_salary,da,hra,pf;
Gross_salary=da=hra=pf=0;
clrscr();
printf("\nEnter the salary:");
scanf("%f",&Basic);
hra=Basic*0.10;
pf=Basic*0.12;
da=Basic*0.35;
Gross_salary=Basic+da+hra-pf;
printf("\n The Gross salary is:%f",Gross_salary);
PREETHA V - AP/CSE, SRIT.
if(Gross_salary>=35000)
{
printf("\nThe Grosssalary is Greater than 35000");
}
else if(Gross_salary>=25000)
{
printf("\nThe Grosssalary is Between 25000 and 35000");
}
else
{
printf(“\nThe Gross salary is less than 25000");
}
getch();
}

Output:
Enter the Basic:12000
The Gross salary is 15960.000000
PREETHA V - AP/CSE, SRIT.

The Gross salary is less than 25000


Looping structure

 It is used to execute some instructions several time


based on some condition.
 WHILE
 Do…WHILE
 For

PREETHA V - AP/CSE, SRIT.


WHILE Loop

Syntax
.

False
WHILE (condition) condition
{
. True
Body of the loop; Body of The loop
.
}

PREETHA V - AP/CSE, SRIT.


Example
#include<stdio.h>
#include<conio.h>
void main()
{
int i=1,n;
Printf(“enter the value of n”);
Scanf(“%d”,&n)
while(i<=n)
Printf(“hello\n”);
i++;
}
Output:
Enter the Number:3
Hello
Hello
Hello PREETHA V - AP/CSE, SRIT.
DO…WHILE Loop

Syntax
do
Body of The loop
{
True

Body of the loop condition

False
}while (condition);
PREETHA V - AP/CSE, SRIT.
Example
#include<stdio.h>
#include<conio.h>
void main()
{
int i=1,n;
printf("\nEnter the Number:");
scanf("%d",&n);
do
{
printf(“hello\n”);
i++;
}while(i<=n);
}
Output:
Enter the Number: 4
Hello
Hello
Hello
PREETHA V - AP/CSE, SRIT.
Hello
For Loop

Syntax Initialization

for (initialization; cond; Inc/Dec)


{ Inc / Decrement

Body of the loop


Body of the loop
condition
True
} False

PREETHA V - AP/CSE, SRIT.


Example

#include<stdio.h>
#include<conio.h>
void main()
{
int i,n;
Printf(“enter the value of n”);
Scanf(“%d”,&n);
For(i=0;i<=n;i++)
{
Printf(“Hello\n”);
}
Output:
Enter the Number:3
Hello
Hello
Hello
Hello
PREETHA V - AP/CSE, SRIT.
NESTED FOR LOOP
Syntax:
for (initi; cond; Inc/Dec)
{

for (initi; cond; Inc/Dec)


{

Body of the loop

}
PREETHA V - AP/CSE, SRIT.
Example
#include<stdio.h>
#include<conio.h>
void main()
{
int i,j;
for(i=1;i<=3;i++)
{
printf("\n");
Output:
for(j=1;j<=3;j++)
{ 1 2 3
printf("%d\t",j); 1 2 3
}
1 2 3
}
getch();
} PREETHA V - AP/CSE, SRIT.
Example
#include<stdio.h>
#include<conio.h>
void main()
{
int i,j;
for(i=1;i<=4;i++)
{
printf("\n");
for(j=i;j<=4;j++)
{ Output:
printf("%d\t",j); 1 2 34
}
2 3 4
}
getch(); 3 4
}PREETHA V - AP/CSE, SRIT. 4
SWITCH CASE
Syntax
switch (expression) Switch
{
case constant 1: Case 1
block1;
break;
case constant 2: Case 2
block2;
break;
. Default
. case
default :
default block;
break;
} PREETHA V - AP/CSE, SRIT.
Example 1
#include<stdio.h>
#include<conio.h>
void main()
{
int i,n;
printf("\nEnter the Number:");
scanf("%d",&n);
switch(n)
{
case 1:
printf("\n Its in case 1");
break;
case 2:
printf("\n Its in case 2");
break;

default:
printf("\n Its in default");
Output:
break; Enter the Number:1
} Its in case 1
getch();
PREETHA V - AP/CSE, SRIT.
}
Example 2
#include<stdio.h> case 2 :
#include<conio.h> c=a-b;
void main() printf("The sub is :%d",c);
{ break;
int n,a,b,c; default:
clrscr(); printf(“\n no
printf("\nenter the two numbers:"); operation”);
scanf("%d\t%d",&a,&b); break;
printf("\nEnter the choice: "); }
scanf("%d",&n); getch();
switch(n) }
{
case 1 :
c=a+b;
Try to find the output youself!
printf("The addition is :%d",c);
break;
PREETHA V - AP/CSE, SRIT.
Break Statement
 Itis used to terminate the loop
 When a break statement is encountered inside a loop, then
the loop is terminated.

Loops with break Statement


while(cond)
{ do
………… {
if(cond) …………
break; if(cond)
………… break;
} …………
} while(cond);
PREETHA V - AP/CSE, SRIT.
For Loop with break
Statement
for (initialization; condition; Inc/Dec)
{
…………
if(cond)
break;
…………
}

PREETHA V - AP/CSE, SRIT.


Continue Statement
When a continue statement is encountered inside a
loop, the control is transferred to the beginning.
while(cond) do
{ {
………… …………
if(cond) if(cond)
continue; continue;
………… …………
} } while(cond);

PREETHA V - AP/CSE, SRIT.


For Loop with Continue Statement

for (initi; condt; Inc/Dec)


{
…………
if(cond)
continue;
…………
}

PREETHA V - AP/CSE, SRIT.


goto Statement
A goto statement in C programming language provides
an unconditional jump from the goto to a labeled
label: in the same function.goto label;
statement

………… …………
………… …………
………… …………
goto label; label:
…………
…………
PREETHA V - AP/CSE, SRIT.
Example programs
solving simple scientific and statistical problems

PREETHA V - AP/CSE, SRIT.


Example 1
#include<stdio.h>
#include<conio.h>
void 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)
{
case 1:
c=a+b;
printf("\nThe result of Addition is:%d",c);
break;

PREETHA V - AP/CSE, SRIT.


case 2:
c=a-b;
printf("\nThe result of Subtraction is:%d",c);
break;
case 3:
c=a*b;
printf("\nThe result of Multiplication is:%d",c);
break;
case 0:
exit(0);
break;
}
getch();
}

PREETHA V - AP/CSE, SRIT.


Output of Example 1

Enter the value of a,b:5


6
MENU
1.ADD
2.SUB
3.MULTIPLY
0.EXIT
Enter the choice:1
The result of Addition is:11
PREETHA V - AP/CSE, SRIT.
Example 2: Finding Armstrong No

An Armstrong number of three digits is an integer such that


the sum of the cubes of its digits is equal to the number
itself. For example, 371 is an Armstrong number since 3**3 +
7**3 + 1**3 = 371.

#include<stdio.h>
#include<conio.h>
void main()
{
int r=0,sum=0,n,a;
printf("\nEnter the number:");
scanf("%d",&n);
a=n;

PREETHA V - AP/CSE, SRIT.


while(n>0)
{
r=n%10;
sum=sum+r*r*r;
n=n/10;
}

if(a==sum)
{
printf("\nIt is an Armstrong number");
}
else
{
printf("\nIt is not an Armstrong number");
}
getch();
}

PREETHA V - AP/CSE, SRIT.


Output of Example 2

Enter the number:153


It is an Armstrong number

PREETHA V - AP/CSE, SRIT.


Example 3: Sum of the Digits
#include<stdio.h>
#include<conio.h>
void main()
{
int r=0,sum=0,n;
printf("\nEnter the no:");
scanf("%d",&n);
while(n>0)
{
r=n%10;
sum=sum+r;
n=n/10;
}
printf("sum of the digits is:%d",sum);
}

PREETHA V - AP/CSE, SRIT.


Output of Example 3

Enter the no:156


sum of the digits is:12

PREETHA V - AP/CSE, SRIT.


Example 4: Reverse of a number

#include<stdio.h>
#include<conio.h>
void main()
{
int r=0,sum=0,n;
printf("\nEnter the no:");
scanf("%d",&n);
while(n>0)
{
r=n%10;
sum=sum*10+r;
n=n/10;
}
printf("Reverse of the number is:%d",sum);
getch();
}

PREETHA V - AP/CSE, SRIT.


Output of Example 4

Enter the no:567


Reverse of the number is:765

PREETHA V - AP/CSE, SRIT.


Example 5: Fibonacci Series
#include<stdio.h>
#include<conio.h>
void main()
{
int f=0,f1=-1,f2=1,n,i;
printf("\nEnter the number:");
scanf("%d",&n);
while(f<n)
{
f=f1+f2;
f1=f2;
f2=f;
printf("\t%d",f);
}
getch();
}
PREETHA V - AP/CSE, SRIT.
Output of Example 5

Enter the number:5


0 1 1 2 3 5

PREETHA V - AP/CSE, SRIT.


Example 6: Swapping

#include<stdio.h>
#include <conio.h>
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);
printf(" \nThe value of b is:%d",b);
getch( );
}

PREETHA V - AP/CSE, SRIT.


Output of Example 6
Enter the value of a:5
Enter the value of b:4

The value of a is:4


The value of b is:5

PREETHA V - AP/CSE, SRIT.


Example 7: Swapping without using
third variable
#include<stdio.h>
#include <conio.h>
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;
a=a-b;
printf(" \nThe value of a is:%d",a);
printf(" \nThe value of b is:%d",b);
getch( );
}
PREETHA V - AP/CSE, SRIT.
Output of Example 7
Enter the value of a:5
Enter the value of b:6

The value of a is:6


The value of b is:5

PREETHA V - AP/CSE, SRIT.


Example 8: Quadratic Equation
#include<stdio.h>
#include <conio.h>
#include<math.h>
void 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; PREETHA V - AP/CSE, SRIT.
if(d>=0)
{
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( );
}

PREETHA V - AP/CSE, SRIT.


Output of Example 8

Enter the value of a:4

Enter the value of b:5

Enter the value of c:6

The roots are imaginary

PREETHA V - AP/CSE, SRIT.


PREETHA V - AP/CSE, SRIT.

You might also like