0% found this document useful (0 votes)
212 views

UNIT 1 C PRogramming Fundamentals

The document discusses the structure and components of a C program. It includes: 1) The main components are the preprocessor section, definition section, global declaration section, main function, and user defined functions. 2) The main function contains the declaration and executable parts. It is where the program execution begins. 3) The document also explains various operators used in C like arithmetic, relational, logical, assignment, increment/decrement, and conditional operators. Examples are given for each type of operator.

Uploaded by

rajee
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
212 views

UNIT 1 C PRogramming Fundamentals

The document discusses the structure and components of a C program. It includes: 1) The main components are the preprocessor section, definition section, global declaration section, main function, and user defined functions. 2) The main function contains the declaration and executable parts. It is where the program execution begins. 3) The document also explains various operators used in C like arithmetic, relational, logical, assignment, increment/decrement, and conditional operators. Examples are given for each type of operator.

Uploaded by

rajee
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 43

UNIT 1 OMSAKHI CS3353- C Programming & DS

UNIT – I: C PROGRAMMING FUNDAMENTALS

Syllabus: Data Types – Variables – Operations – Expressions and Statements – Conditional


Statements – Functions – Recursive Functions – Arrays – Single and Multi-Dimensional
Arrays.

Structure of a C Program:

Documentation Section

Preprocessor Section (OR)


Header file Section

Definition Section

Global Declaration Section

main()Function
{
Declaration part;
Executable Part;
}
sub program section
{
Body of the subprogram;
}

Documentation section
 It contains a set of comment lines used to specify the name of program the author and other
details etc.,
 It will not be executed in the program.
 Ex:-
// single line command
/* Multiple line commands */

Preprocessor section (OR) Header files Section:-


 It is used to link system library files.
 Header files having so many predefined functions.
 Ex:
#include<stdio.h>
#include<conio.h>

Definition section:-
 We can define all constants here. These values will not be changed in the program
 Ex:
#define a 10

Mrs. T. Uma Mageswari, AP/IT Page 17


UNIT 1 OMSAKHI CS3353- C Programming & DS

Global declaration section:-


 We can declare global variables that can be used anywhere in the program.
 This section must be declared outside of all the functions.
 Ex:-
#include<stdio.h>
Int a=10;
main()
{
…..
}

Main function():
 It is must for all the programs.
 A program cannot run without main().
 Program starts to run from main() only.
 Ex:-
main()
{
…..
…..
}
Declaration part:-
 The declaration part declares the entire variables that are used in the Executable part.
 The initialization of variables is also done in this section. This also called as local variable
declaration.
 Ex:
int a;
int a=10;

Executable section:-
 It contains atleast one valid „C‟ statement.
 Execute the statements in this section.
 Ex:
C=a+b;
printf(“%d”, c);

User defined function:-


 The function defined by the user are called user-defined function.

PROGRAMMING RULES
 While writing a program, a programmer should follow the following rules.
1. All statements should be written in lowercase letters.
2. Uppercase letters are only used for symbolic constants
3. Blank space may be inserted between words. But blank space is not used
while declaring a variable, keyword, constant and function.
4. The programmers can write the statement anywhere between the two
braces.
5. We can write one or more statements in a same line by separating each
statement in a same line by separating each statement with semicolon (:)
6. The opening and closing braces should be balanced.

Mrs. T. Uma Mageswari, AP/IT Page 18


UNIT 1 OMSAKHI CS3353- C Programming & DS

Example (Structure of a C Program):


/*Program to find the area of circle */
#include<stdio.h> /* link section */
#include<conio.h> /* link section */
#define PI 3.14 /*definition section */
float area; /*global declaration section */
void main()
{
float r; /* declaration part */
clrscr(); /*executable part starts here */
printf(“Enter the radius of the circle :\n”);
scanf(“%f”,&r);
area=PI*r*r;
printf(“Area of the circle = %f”, area);
getch();
}
Output:
Enter radius of the circle : 2
Area of the circle = 12.56

OPERATOR AND EXPRESSION


Operator:
 An operator is a symbol that specifies an operation to be performed on the operands.
Operand:
 It is an data item on which operators perform operation. Eg: a+b
o Here, a, bOperator
o +  Operand
Types of Operator
1. Arithmetic operators
2. Relational operators
3. Logical operators
4. Assignment operators
5. Increment and decrement operators
6. Conditional operators
7. Bitwise operators
8. Special operators
(i) Arithmetic operators
 Arithmetic operators are,+ , - , * , / , %.
 C allows basic arithmetic operations like addition, subtraction, multiplication and
division.

SYMBOL DESCRIPTION EXAMPLE

+ Addition 2+9 = 11

- Subtraction 9-2= 7

* Multiplication 2*9= 18

/ Division 9/3= 3

% Modulo division 9%2=1

Mrs. T. Uma Mageswari, AP/IT Page 19


UNIT 1 OMSAKHI CS3353- C Programming & DS

 Arithmetic operators are classified as


i) Unary arithmetic : It requires only one arithmetic.
Eg: +x, -y
ii) Binary arithmetic: It requires two operators
Eg: a+b, a%b
iii) )Integer arithmetic: It requires both operands are integer value for arithmetic
operators.
Eg: a+b :
a= 5, b=2 a+b= 5+2=7
iv) Floating point arithmetic: It requires both operands are float type for arithmetic
operators
Eg: a+b
a=6.5 b=3.5 a+b6.5+3.5 = 10
(ii) Relational operator
 Relational operators are used to compare two or more operands. Operands may be
variables, constants or expression.

Operator Meaning
< is less than
<= is less than or equal to
> is greater than
>= is greater than or equal to
== is equal to
!= is not equal to

(iii) Logical operators

 Logical operators are used to combine the results of two or more conditions.

Operator Meaning Example Return


value

&& Logical (9>2)&&(17>2) 1


AND
|| Logical (9>2)||(17==7) 1
OR
! Logical 29!=29 0
NOT
(iv) Assignment Operator
 Assignment operators are used to assign a value or an expression or a value of a
variable to another variable.
 Syntax:
variable=expression (or) value ;
 Example :
x=10; x=a+b; x=y;
 Two types of assignment operator are,
(i) Compound assignment
(ii) Nested assignment (or) multiple assignment

Mrs. T. Uma Mageswari, AP/IT Page 20


UNIT 1 OMSAKHI CS3353- C Programming & DS

Compound assignment
 Assign a value to variable.

Operator Name Meaning assign value


a+=b
+= Short-hand add Add a + b and store in a
a=a+b
a - =b sub a - b and store in a
-= Short-hand sub
a=a–b
a*=b multiply a * b and store in a
*= Short-hand multiply
a=a*b
/= Short-hand divide a/=b divide a / b and store in a
a=a/b
== Equality 1 or 0
!= Not equal to 1 or 0

Nested assignment
 Assign a value (or) Expression to variable.
 Syntax: Ex:
i=j=k=1 x=y=z=(i+j+k)
(v) Increment & Decrement Operator
 'C' has two way useful operators not generally found in other languages, these are
the increment (++) and decrement (--) operators. „+‟ & „-‟ operators are called
unary operators.
 Because they acts upon only one variable.
 ++x Pre increment
 --x  Pre decrement
 x++  Post increment
 x--  Post decrement.
(vi) Conditional Operator
 Checks the condition and executes the statement.
 Ternary Operator
( condition ? Exp1:Exp2)
 Eg:
big=(a>b )? a:b;

(vii) Bitwise Operator


 Bitwise operators are used to manipulate the data at bit level. It operates on integers only

Operator Meaning
& Bitwise AND
| Bitwise OR
^ Bitwise XOR
<< Shift left
>> Shift right
~ One's complement

Mrs. T. Uma Mageswari, AP/IT Page 21


UNIT 1 OMSAKHI CS3353- C Programming & DS

(viii) Special Operator:

Operators Meaning
, Comma Operator
Sizeof size of operator
& and * Pointer Operator
. and  Member selection operators
Sizeof() Operator:
 sizeof() is a unary operator, that returns the length in bytes of the specified variable, and
it is very useful to find the bytes occupied by the specified variable in the memory.

Expressions
 An expression represents data item such as variables, constants and are interconnected
with operators as per the syntax of the language.
Example:
y=(a/b)+c; z=(a*b)-d;

C – Decision Making statements (or) Branching Statements:


 Conditions are placed in the program using decision making statements.
 Decision making statements check the condition and then executes its sub blocks.
 „C‟ language provides the following conditional statements.
 Decision making structures require that the programmer specify one or more conditions
to be evaluated or tested by the program, along with a statement or statements to be
executed.
 5 types of decision making control statements
1. if statements
2. if else statements
3. nested if-else statements
4. if...else if Ladder Statements
5. switch-case Statements

1. if statements:

 if is a c keyword.
 The if statement is a decision making statement
 Used to control the flow of execution by executing statements when the logical
condition is true (or) false.
 It has only one option.
 The set of command lines are executed only when logical condition is true.

Syntax Example

if(Condition) if(a>10)
{ {
/* true statement(s) */ Printf(“a is bigger”);
} }

Mrs. T. Uma Mageswari, AP/IT Page 22


UNIT 1 OMSAKHI CS3353- C Programming & DS

Flow Diagram:

2. if...else statements:
 It is two way decision making statement
 if…..else statement take care of true as well as false condition.
 It has two block else
 if block is executed when the condition is true, else block is executed when the
condition is false.
 An if statement can be followed by an optional else statement, which executes when
the Condition is false

Flow Diagram:

Syntax Example
if(Condition) if(a%2)
{ {
/*true statement(s) */ printf(“given no is Even”);
} }
else else
{ {
/*falsestatement(s) */ printf(“given no is Odd”);
} }

3. nested if-else statements:


 if…..else statement is written in another if….else statement called nesting and the
statement is caled nested if

Mrs. T. Uma Mageswari, AP/IT Page 23


UNIT 1 OMSAKHI CS3353- C Programming & DS

FLOWCHART

Syntax Example
if(Condition 1) if(n>5)
{ {
/*true statement(s) */ printf("Number is greater than 5”);
if(Condition2) }
{ else
/*true statement(s) */ {
} if(n==5)
else {
{ printf("Number is 5”);
/*false statement(s) */ }
} else
} {
else printf("Number is lesser than 5”);
{ }
/* false statement(s) */ }
}

4. The if...else if Ladder Statements:


 An if statement can be followed by an optional else if...else statement, which is very
useful to test various conditions using single if...else if statement.

Mrs. T. Uma Mageswari, AP/IT Page 24


UNIT 1 OMSAKHI CS3353- C Programming & DS

Syntax: Example
if(Condition 1) if(a>b && a>c)
{ {
/*true statement(s) */ printf("a is Biggest Number:%d\n", a);
else if(Condition2) }
{ else if(b>c)
/*true statement(s) */ {
} printf("b is Biggest Number: %d \n", b);
else if(Condition3) }
{ else
/*false statement(s) */ {
} printf("c is Biggest Number: %d \n", c);
} }
else
{
/*false statement(s) */
}

Flowchart:

Switch-Case Statements:
 The switch statement is used to pickup or executes a particular group of statements from
several available groups of statements.
 It allows us to make a decision from the number of choices.
Syntax:

Mrs. T. Uma Mageswari, AP/IT Page 25


UNIT 1 OMSAKHI CS3353- C Programming & DS

switch (expression) Flow Diagram:


{
case label1:
statements;
break;
case label2:
statements;
break;
default:
statements;
break;
}

C – Looping Statements (or) Iterative Statements:


Looping statements in C are used to perform looping operations until the given condition is true.
Control comes out of the loop statements once condition becomes false.
Types of Loop Control Statements:
1. while
2. do-while
3. for
1. WHILE LOOP IN C:
 Repeats a statement or group of statements while a given condition is true. It tests the
condition before executing the loop body.
Syntax Example
while (condition) while(i<=num)
{ {
Statements; fact=fact*i;
Increment/Decrement; i++;
} }

Flow Diagram:

Mrs. T. Uma Mageswari, AP/IT Page 26


UNIT 1 OMSAKHI CS3353- C Programming & DS

2. DO WHILE LOOP IN C:
 Like a while statement, except that it tests the condition at the end of the loop body

Syntax Example
do do
{ {
statements; fact=fact*i;
} i++;
while (condition); } while(i<=num);

3. FOR LOOP IN C:
 The for loop is another repetitive control structure and is used to execute set of
instructions repeatedly until the condition becomes false.
For loop has three parts:
1. Initialize counter is used to initialize counter variable.
2. Test condition is used to test the condition.
3. Increment/decrement counter is used to increment or decrement counter variable.
Syntax:
for (variable initialization; condition checking; increment/decrement)
{
Statements;
}
Example:

for(i=1;i<=num; i++)
{
fact=fact*i;
}

Mrs. T. Uma Mageswari, AP/IT Page 27


UNIT 1 OMSAKHI CS3353- C Programming & DS

MANAGING INPUT AND OUTPUT OPERATIONS


 The input and output functions are used to provide input to the program and display the result to
the user respectively. There are two types of input/output functions. They are:
 Formatted Input/Output Functions
 Unformatted Input/Output Functions

Formatted Input/Output Functions:

 The value to these functions has to be given in a particular order. They are classified as follows:
Input Output
scanf() printf()
fscanf() fprintf()
Input Function:

i. scanf() Function:
o scanf() function is used to read a character, string, numeric data from keyboard.
syntax:
scanf(“control string”, address of the variable);
Example:
int a;
scanf(”%d”,&a);
where & - The address of the variable a.

Rules to be followed:
1. The control string must be given within double quotation.
2. The variables can be separated by using comma.
3. It should be terminated by a semi colon.
4. No empty space should be given between two control strings.
ii. fscanf() Function: This function is used to read the data from a file.

Output Function:

i. printf() Function:
 The printf() function is used to print the character, string, float, integer, octal and hexadecimal
values onto the output screen.
syntax:

printf(“control string”, variable name);

Example;
int a=10;
printf(“The value is :%d”,a);

Rules for writing printf() function:


1. The variable and the control string should match.
2. The control string must be given in double quotation.

Mrs. T. Uma Mageswari, AP/IT Page 28


UNIT 1 OMSAKHI CS3353- C Programming & DS

3. Any number of values can be displayed. The variables must be separated by comma.

ii. fprintf() Function: This function is used to write data into a file.
Sample Program using scanf() and printf() functions:

#include<stdio.h>
void main()
{
int a;
printf(“\nEnter the value for a”);
scanf(“%d”,&a);
printf(“\n The value entered is %d”,a);
}
Output:
Enter the value for a 7
The value entered is 7

2. Unformatted Input and Output Functions:


 Some of the unformatted input and output functions are given below:
Input Output
getc() putc()
getchar() putchar()
gets() puts()

Single Character Input Function:


 It reads a single character from the standard input device. This function does not require any
arguments, through a pair of empty parentheses.
1. getc():
Syntax:
character variable = getc();
Example :
char c;
c = getc();
2. getchar() :
Syntax:
character variable = getchar();
Example :
char x;
x= getchar();

Single Character Output Function:


 It is used to display a single character in a character variable to standard output device.
1. putc():
Syntax:
putc(character variable);
Example :
char c;

Mrs. T. Uma Mageswari, AP/IT Page 29


UNIT 1 OMSAKHI CS3353- C Programming & DS

putc(c);
2. putchar() :
Syntax:
putchar( character variable);
Example :
char x;
putchar(x);
String Function gets() and puts():
1. gets():
 It is used to read the string (string is a group of character) from the standard input device.
Syntax:
gets(char type of array variable);
Example :
char c[10];
gets(c);
2. puts() : It display the string to the standard output device.
Syntax:
puts(char type array variable);
Example :
char c[];
puts(c);

Mrs. T. Uma Mageswari, AP/IT Page 30


UNIT 1 OMSAKHI CS3353- C Programming & DS

1. OPERATOR PROGRAMS

ARITHMETIC RELATIONAL ASSIGNMENT


LOGICAL OPERATOR
OPERATOR OPERATOR OPERATOR

#include<stdio.h> #include<stdio.h> #include<stdio.h> #include<stdio.h>


#include<conio.h> #include<conio.h> #include<conio.h> #include<conio.h>

void main() void main() void main() void main()

{ { { {

int a=10,b=5,c; int a=10,b=5,c; int a=10,b=5,c-3; int a=10,b=5,;

clrscr(); clrscr(); clrscr(); clrscr();

c=a+b; if(a>b) if(a>b) a+=b


{ {
printf(" a is bigger than printf(" a is bigger than
b"); b");
} }

printf(“%d”, c); else if((a>b) && (a>c)) printf(“%d”, c);

{ {
printf (“b is greater ”) printf(" \n a is biggest");
} }

getch( ); getch( ); getch( ); getch( );

} } } }

Output: Output: Output: Output:


15 a is bigger than b a is bigger than b The sum of the two
values:15
a is biggest

Mrs. T. Uma Mageswari, AP/IT Page 31


UNIT 1 OMSAKHI CS3353- C Programming & DS

INCREMENT/DECREMENT BITWISE CONDITIONAL SPECIAL


OPERATOR OPERATOR OPERATOR OPERATOR

#include<stdio.h> #include<stdio.h> #include<stdio.h> #include<stdio.h>


#include<conio.h> #include<conio.h> #include<conio.h> #include<conio.h>

void main() void main() void main() void main()

{ { { {

int a=5; int a=10,b=5,c; int a=10,b=5,c; int c;

clrscr(); clrscr(); clrscr(); clrscr();

printf(" \n post increment c = a&b; c = a>b?a:b;


value:%d",a++);
printf(" \n pre increment
value:%d",++a); printf(" \n size of
printf(" \n The Larger int is:%d",
printf(" \n pre decrement value:%d",- printf(“%d”, c); Value is %d", c); sizeof(c));
-a);
printf(" \n post decrement
value:%d",a--);

getch( ); getch( ); getch( ); getch( );

} } } }

Output:
Post increment Value:5 Output: Output:
Output:
Pre increment Value:7 value a&b is: 0 The Larger Value is 10
size of int is: 2
Pre decrement Value:6
Post decrement Value:6

Mrs. T. Uma Mageswari, AP/IT Page 32


UNIT 1 OMSAKHI CS3353- C Programming & DS

2. DECISION MAKING PROGRAMS

if Statement if else statement if else ladder Nested if else


#include<stdio.h> #include<stdio.h> #include<stdio.h> #include<stdio.h>
#include<conio.h> #include<conio.h> #include<conio.h> #include<conio.h>
void main() void main() void main() void main()
{ { { {
int a=34; int a=10,b=5,c; int a=10,b=5,c-3; int a=34,b=15;
clrscr(); clrscr(); clrscr(); clrscr();
if((a>b) && (a>c)) if(a>10)
if(a>b)
{ {
{
printf(" a is bigger printf(" a is bigger
printf(" a is bigger than
than b"); than b");
b");
if(a>10) } }
}
{
printf(" \n a is bigger than if((a>10) &&
10"); else if(a>b) (a>b))
else
} { {
{
printf(" \n a is printf(" \n a is
printf (“b is greater ”)
biggest"); biggest");
}
} }

else else
{ {
printf(“c is printf(" b is
greater”); greater");
}
getch( ); getch( ); getch( ); getch( );
} } } }
Output:
Output: Output: Output:
The sum of the two
a is bigger than 10 a is bigger than b a is bigger than b
values:15

Mrs. T. Uma Mageswari, AP/IT Page 33


UNIT 1 OMSAKHI CS3353- C Programming & DS

3. LOOPING PROGRAM

While loop Do while loop For loop

#include<stdio.h> #include<stdio.h> #include<stdio.h>


#include<conio.h> #include<conio.h> #include<conio.h>

void main() void main() void main()

{ { {

int i=1,fact=1,n; int i=1,fact=1,n; int i=1,fact=1,n;

clrscr(); clrscr(); clrscr();

printf("\nEnter the Number:"); printf("\nEnter the Number:"); printf("\nEnter the Number:");

scanf("%d",&n); scanf("%d",&n); scanf("%d",&n);

while(i<=n) do
for(i=1;i<=n;i++)
{ {
{
fact =fact *i; fact =fact *i;
fact =fact *i;
i++; // i=i+1 i++; // i=i+1
}
} }while(i<=n);

printf("\n The value of %d! is:%d", printf("\n The value of %d! printf("\n The value of %d!
n,fact); is:%d", n,fact); is:%d", n,fact);

getch( ); getch( ); getch( );

} } }

Output: Output: Output:


Enter the Number:3 Enter the Number:3 Enter the Number:3
The value of 3! is: 6 The value of 3! is: 6 The value of 3! is: 6

Mrs. T. Uma Mageswari, AP/IT Page 34


UNIT 1 OMSAKHI CS3353- C Programming & DS

4. Managing Input and Output function

UNFORMATTED INPUT AND OUTPUT

gets() & puts() getchar() & putchar() getc() & putc()

#include<stdio.h> #include<stdio.h> #include<stdio.h>


#include<conio.h> #include<conio.h> #include<conio.h>

void main() void main() void main()

{ { {

char scientist[40]; char C; char C;

puts (“Enter name


clrscr(); clrscr();
:”) ;

printf (“Enter any variable either in lower printf (“Enter any variable either in
gets(scientist) ;
case or uppercase:”) lower case or uppercase:”);

puts(“print the
C = getchar () ; C = getc() ;
name”);

if (islower(C)) if (islower(C))
Putchar(toupper(C); Putc(toupper(C);
puts(scientist);
else else
Putchar(tolower(C); Putc(tolower(C);

} } }

Output: Output: Output:


Enter Name: Enter any variable either in lower case or Enter any variable either in lower case or
computer uppercase : a uppercase : a
computer A A

Mrs. T. Uma Mageswari, AP/IT Page 35


UNIT 1 OMSAKHI CS3353- C Programming & DS

Explain different types of functions with suitable example

Functions

 Function is a set of instruction that are used to perform specified tasks which repeatedly occurs in
the main program.
 Functions are 2 types,
o Pre defined functions.
o User defined functions.

(i) User defined functions


 The function defined by the users according to their requirements.
 Written by the programmer to perform a particular task, that is repeatedly used in main
program.
 Helpful to breakdown a large program in to a number of smaller programs.
 Debugging, testing and maintenance become easier.

Advantages of user defined function


 Length of the source program can be reduced.
 Easy to locate and debug errors.
 It can be used in many other source programs whenever necessary.
 Avoid coding of repeated programming.

Elements of user defined function


 Function definition.
 Function declaration.
 Function call.

Function definition
 Defining its element and characteristics.

Function Declaration
 Like the normal variables in a program, the function can also be declared before they defined
and invoked.

Mrs. T. Uma Mageswari, AP/IT Page 36


UNIT 1 OMSAKHI CS3353- C Programming & DS

Syntax

datatype func_name(parameter list)


{
Variable declaration; Body of the function.
}
Func_name  name of function.
Parameter list list of parameters that the function can convey.

Function call
 The function can be called by simply specifying the name of the function,return value and the
parameters if presence.

func_name();
func_name(parameter);
return value=func_name(parameter);

Parameter and its types


 Parameter  Parameters provide the data communication between the calling function and
called function.
 There are 2 types of parameters:
o Actual parameter these are the parameters transferred from the calling function
(main program) to the called program (function).
o Formal parameter these are the parameters transferred from the called function
(program) to the calling program( mainfunction).
Example

Mrs. T. Uma Mageswari, AP/IT Page 37


UNIT 1 OMSAKHI CS3353- C Programming & DS

TYPES OF VARIABLE
LOCAL AND GLOBAL VARIABLES
 There are two kinds of varibles.
o Local variable. Global variables.
(i) Local variable:
The local variable are defined within the body of the function. These variables are defined
local to that function only or block only, other function cannot access these variables.

Eg:
Value (int a,int b)
{
int c, d;
}
c and d local variables.
2. Global variables.
Global variables are defined outside the main() function. multiple function can use these
variables.
Program to demonstrate local and global variables

Eg: int m=5,n=10;


main()
{
int a,b;
}
m and n are global variables.

#include<stdio.h>
#include<conio.h>
void main()
{
int i=0; Void f1(void)
void f1(); {
i=o; int k;
clrscr(); i=50;
printf(“value of i in main:%d\n”,i); }
f1(); Output:
printf(“value of i after call:%d\n”,i); Value of i in main:0
getch(); Value of i after call:50
}

Mrs. T. Uma Mageswari, AP/IT Page 38


UNIT 1 OMSAKHI CS3353- C Programming & DS

RETURN statement
 The return statement may or may not send back any values to main program.
 The return statement can take the form:
 Syntax:
return; or return(exp);
Eg:
if(x <= 0)
return(0);
else
return(1);
FUNCTION calling
 A function can be called by simply using the function name in the statement.

Example:

main() message()
{ {
message(); printf(“function
printf(“main message”); message\n”);
} }
Calling Function Called Function

Function prototypes
 The functions are classified into the following types depending on the arguments are present or
not and whether the value is returned or not.
 These are also called function prototypes.
 A function prototype declaration consists of the function return type, name and arguments list.

Mrs. T. Uma Mageswari, AP/IT Page 39


UNIT 1 OMSAKHI CS3353- C Programming & DS

Prototype:
 Function with no arguments & no return value.
 Function with arguments & no return value.
 Function no arguments & with return value.
 Function with arguments & return value.
(i) Function with No Arguments and No Return Values
 In this prototype, no data transfer takes place between the calling function and the called function.
i.e... The called program does not receive any data from the calling program and does not send
back any value to the calling program.
 These functions act independently, i.e. they get input and display output in the same block.
Syntax

Example: /* Program to addition of two numbers */

void add()
#include<stdio.h> {
main() int a, b, c ; /* Local definitions */

{ printf("Enter two numbers...");


void add(void); scanf("%d %d",&a, &b) ;
add() ; /* add is a function with no arguments */ c=a +b;
} printf("Sum is...%d",c) ;
OUTPUT }
Enter two numbers...10 20

Mrs. T. Uma Mageswari, AP/IT Page 40


UNIT 1 OMSAKHI CS3353- C Programming & DS

Sum is...30
(ii) Function with Arguments and No Return Values
 In this prototype, data is transferred from calling function to called function but don‟t have return
values.
 It is a one way data communication, i.e. the called program receives data from calling program
but it does not return any value to the calling program.
Syntax:

Example: /* program to addition of two numbers*/

#include<stdio.h>
main() void add (int x, int y)
{ {
void add(int,int); int z;
int a,b;
z=x +y:
printf(“enter the values”);
scanf(“%d %d”,&a,&b); printf(“the sum is%d:”,
add(a,b) ; /* add function with argument */ z);
}
}
Output
Enter two number: 2 4
Sum is: 6

Mrs. T. Uma Mageswari, AP/IT Page 41


UNIT 1 OMSAKHI CS3353- C Programming & DS

(iii) Function with no Arguments with return Values


 In this prototype, one way data communication takes place.
 i.e the calling program cannot pass any arguments to called program but, the called program
may send return value to the calling program.
 Here data transfer takes place between the called function and the calling function.

Syntax:

Example: /* program to addition of two numbers*/

#include <stdio.h> int add() //function with no argument


#include<conio.h> {
void main() int a,b,c;
{ printf("\n Enter two number:");
int add(), d; scanf("%d %d", &a, &b);
d=add();
Output c=a+b;
printf("\n Sum is:%d", d); return(c);
Enter two number: 2 4
} }

Output
Enter two number: 2 4
Sum is: 6
(iv) Function with arguments and return Value
 In this prototype, the data is transferred between the calling function and called function.
 Here data transfer takes place between the calling function and the called function as well as
between called function and calling function .

Mrs. T. Uma Mageswari, AP/IT Page 42


UNIT 1 OMSAKHI CS3353- C Programming & DS

 It is a two way data communication, i.e. the called program receives data from calling program
and it returns some value to the calling program.

Syntax:

Example: /* program to addition of two numbers*/

#include <stdio.h>
#include<conio.h>
void main()
{
int a,b,c;
int add(int,int); int add (int x, int y)
printf("\nEnter two number:"); {
scanf("%d%d", &a, &b); int z;
c=add(a,b); z=x+y;
printf("\nSum is:%d", c); return(z);
} }

Output
Enter two number: 2 4
Sum is: 6

1. Recursion
Recursion is the process of calling the same function itself again and again until some condition is
satisfied.
Syntax:
func1()
{
………..
func1();
…………
}

Mrs. T. Uma Mageswari, AP/IT Page 43


UNIT 1 OMSAKHI CS3353- C Programming & DS

Example: /* program for Recursion*/

#include<stdio.h> int fac(int x)


#include<conio.h> int n;
main() {
{ f(x==1)
int a; return(1);
printf(“enter the value”); else
scanf(“%d”,&a); n=x*fac(x-1);
printf(“the factorial is %d”, fac(a)); return(n);
} }

Output:

Enter the number: 5


The factorial of 5! is 120

2. Explain in detail about parameter passing methods.


 There are two different ways of passing parameters to a method, they are:
(i) Call by value
(ii) Call by reference.
(i) Call by value
 It copies the value of actual parameter in to formal parameter.
 Change of formal parameter cannot affect the actual parameter.

Example: /* program for call by value */


#include <stdio.h>
#include<conio.h>
void main()
{
int x,y,change(int,int); int change(int a, int b)
printf("\nEnter value of x:"); {
scanf("%d",&x); int c;
printf("\nEnter value of y:"); c=a;
scanf("%d",&y); a=b;
change(x,y); b=c;
printf("Values in the Main()-->x=%d, printf("Values in the Fuction -->x=%d,
y=%d", x, y); y=%d", a, b);
} }

Mrs. T. Uma Mageswari, AP/IT Page 44


UNIT 1 OMSAKHI CS3353- C Programming & DS

Output:
Enter value of x:5
Enter value of y:6
Values in the Function -->x=6,y=5
Values in the Main()-->x=5,y=6

(ii) Call by reference


 The address of arguments are copied into the parameter inside the function, address is used to
access the actual argument. Instead of passing values, the address of the argument will be passed.
Any changes made to the formal argument will affect the actual argument.
Example: /* program for call by Reference */

#include <stdio.h>
#include<conio.h>
void main()
{
int x,y,change(int*,int*);
printf("\nEnter value of x:"); int change(int *a,int *b)
scanf("%d",&x); {
printf("\nEnter value of y:"); int c;
scanf("%d",&y); c=*a;
change(&x, &y); *a=*b;
printf("Values in the Main()-->x=%d, *b=c;
y=%d",x,y); printf("Values in the Function -->x=
} %d,
y=%d",*a,*b);
}

Output:
Enter value of x:5
Enter value of y:6
Values in the Function -->x=6,y=5
Values in the Main()-->x=6,y=5
ARRAYS
 An array is a collection of similar data items that are stored under a common name in which each
element is located in separate memory locations.
 Array index always start from „0‟
 It is represented using [ ] – square brackets

Mrs. T. Uma Mageswari, AP/IT Page 45


UNIT 1 OMSAKHI CS3353- C Programming & DS

FEATURES OF ARRAYS:
1. Array is a derived data type.
2. String array always terminates with null character ('\0').
3. Array elements are countered from 0 to n-1.
4. Array elements can be accessed with base address (index) and subscripts defines the position of the
element.
5. In array the elements are stored in continuous memory locations.

CLASSIFICATIONS OF ARRAY:
1. Single / One- Dimensional Array
2. Two – Dimensional Array
3. Multi – Dimensional Array

1.Single / One- Dimensional Array:

Array Declaration:
Syntax:
Data-type array-variable[array-size];
Eg: int X[4];
Address Element
1000 X[0]
1002 X[1]
1004 X[2]
1006 X[3]

Array Initialization:
Syntax:
Data-type array-variable[array-size]={list of values};

Eg: int num[4]={2,4,1,17,49,5,11};

Example 1:
/* Array Initialization using Run time */
#include<stdio.h>
#include<conio.h>
void main()
{
int x[2],i;
printf("\nEnter the inputs:");
for(i=0;i<2;i++)
{
scanf("%d",&x[i]);
}

Mrs. T. Uma Mageswari, AP/IT Page 46


UNIT 1 OMSAKHI CS3353- C Programming & DS

for(i=0;i<2;i++)
{
printf("\nThe value in x[%d] is %d",i,x[i]);
}
getch();
}

Output:
Enter the inputs:
3
6
The value in x[0] is 3
The value in x[1] is 6

Example 2:
/* Array Initialization using compile time */
#include<stdio.h>
#include<conio.h>
void main()
{
int i;
char x[5]={'a','b','c','d','e'};
clrscr();
for(i=0;i<5;i++)
{
printf("\nThe value in x[%d] is %c",i,x[i]);
}
getch();
}

Output:
The value in x[0] is a
The value in x[1] is b
The value in x[2] is c
The value in x[3] is d
The value in x[4] is e

2. Two – Dimensional Array:

 It is an array in which rows and columns are the two dimensions


 Two square brackets are used for rows and columns
 More number of elements can be stored

Mrs. T. Uma Mageswari, AP/IT Page 47


UNIT 1 OMSAKHI CS3353- C Programming & DS

Array Declaration:

Syntax:
Data-type array-variable[row size][column size];

Eg: int P[3][4];

Array Initialization:

Syntax:
Data-type array-variable[row size][column size]={list of values};

Eg: int n[3][4] = {{59,6,7,38},


{22,78,9,56},
{67,8,90,68}};

Example :
/* Two dimensional Array */
#include<stdio.h>
#include<conio.h>
void main()
{
int i,j;
int x[2][2]={ {1,50},{2,75}};
clrscr();
for(i=0;i<2;i++)
{
for(j=0;j<2;j++)
{
printf("\nThe value in x[%d][%d] is %d",i,j,x[i][j]);
}
}
getch();
}

Output:
The value in x[0][0] is 1
The value in x[0][1] is 50
The value in x[1][0] is 2
The value in x[1][1] is 75

Mrs. T. Uma Mageswari, AP/IT Page 48


UNIT 1 OMSAKHI CS3353- C Programming & DS

3. Multi – Dimensional Array:


 If the dimension with three or more called as multidimensional array.
 Multidimensional arrays are slower than the single dimensional array
Array Declaration:
Syntax:
Data-type array-variable[size 1][size 2]..[size n];
Eg: int m[3][3][3];
Array Initialization:
Syntax:
Data-type array-variable[size 1][size 2]..[sizen]={list of values};

1. ARRAY MINIMUM /MAXIMUM (ONE DIMENSIONAL ARRAY)

#include <stdio.h>
#include <conio.h>
void main()
{
int a[10],i,min,max,n;
clrscr();
printf("Enter number of elements : ");
scanf("%d",&n);
printf("\nEnter Array Elements\n");
for(i=0; i<n; i++)
scanf("%d",&a[i]);
min = max = a[0];
for(i=1; i<n; i++)
{
if (max < a[i])
max = a[i];
if (min > a[i])
min = a[i];
}
printf("\nMaximum value = %d \n Minimum value = %d", max, min);
getch();
}
Output
Enter number of elements: 6
Enter Array Elements 115
38
-7
11
-9
0
Maximum value = 115

Mrs. T. Uma Mageswari, AP/IT Page 49


UNIT 1 OMSAKHI CS3353- C Programming & DS

Minimum value = -9 43

MATRIXMANIPULATION (ADDITION/SUBTRACTION /MULTIPLICATION /TRANSPOSE)

Program
/* Matrix Manipulation*/
#include <stdio.h>
#include <conio.h>
void main()
{
int a[10][10], b[10][10], add[10][10],sub[10][10],mul[10][10],t[10][10];
int r1, c1, r2, c2;
int i, j, k;
clrscr();

printf("Enter order of matrix A : ");


scanf("%d%d", &r1, &c1);
printf("Enter order of matrix B : ");
scanf("%d%d", &r2, &c2);

printf("\n Enter matrix A elements\n");


for(i=0; i<r1; i++)
{
for(j=0; j<c1; j++)
{
scanf("%d",&a[i][j]);
}
printf(“\n”);
}
printf("\n Enter matrix B elements\n");
for(i=0; i<r2; i++)
{
for(j=0; j<c2; j++)
{
scanf("%d",&b[i][j]);
}
printf(“\n”);
}
for(i=0; i<r1; i++)
{
for(j=0; j<c2; j++)
{
add[i][j]=a[i][j] + b[i][j];
sub[i][j]=a[i][j] - b[i][j];
mul[i][j] = 0;

for(k=0; k<c1; k++)


{
mul[i][j] = mul[i][j] + a[i][k] * b[k][j];

Mrs. T. Uma Mageswari, AP/IT Page 50


UNIT 1 OMSAKHI CS3353- C Programming & DS

}
}
}
printf("\nProduct matrix \n");
for(i=0; i<r1; i++)
{
for(j=0; j<c2; j++)
{
printf("%-4d",mul[i][j]);
}
printf("\n");
}

printf("\n Addition matrix \n");


for(i=0; i<r1; i++)
{
for(j=0; j<c2; j++)
{
printf("%-4d",add[i][j]);
}
printf("\n");
}

printf("\n Subtraction matrix \n");


for(i=0; i<r1; i++)
{
for(j=0; j<c2; j++)
{
printf("%-4d",sub[i][j]);
}
printf("\n");
}

printf("\n TRANSPOSE matrix \n");


for(i=0; i<r1; i++)
{
for(j=0; j<c2; j++)
{
t[i][j]=a[j][i];
printf("%-4d",t[i][j]);
}
printf("\n");
}
getch();
}

Output
Enter order of matrix A : 3 3
Enter order of matrix B : 3 3

Mrs. T. Uma Mageswari, AP/IT Page 51


UNIT 1 OMSAKHI CS3353- C Programming & DS

Enter matrix A elements


111
111
111
Enter matrix B elements
111
111
111
Product matrix
333
333
333
Addition Matrix
222
222
222
Subtraction Matrix
000
000
000
Transpose Matrix
111
111
111

C PROGRAMS
Example: 1/*Calculate Total and Average*/
# include <stdio.h>
# include <conio.h>
Void main ( )
{
int r,b,c,d, tot, avg;
clrscr();
printf (“ENTER STUDENT RNO ; “);
scanf (“%d”,&r);
printf(“ENTER FIRST SUBJECT MARKS ;”);
scanf(“%d”,&b);
printf(“ENTER SECOND SUBJECT MARKS;”);
scanf(“%d”,&c);
printf(“ENTER THIRD SUBJECT MARKS ;”);
scanf(“%d”,&d);
tot=b+c+d;
avg=tot/3;
printf(“\n\n\t\t STUDENT DETAILS \n\n”);
printf(“\t STUDENT RNO ; %d “,r);
printf(“\t FIRST SUBJECT MARKS ;%d “,b);
printf(“\t SECOND SUBJECT MARKS ;%d “,C);
printf(“\t THIRD SUBJECT MARKS ;%d “,d);

Mrs. T. Uma Mageswari, AP/IT Page 52


UNIT 1 OMSAKHI CS3353- C Programming & DS

printf(“\t AVERAGE MARKS ; %d”, avg);


getch( );

}
Example: 2/*Biggest of two nos*/
#include<stdio.h>
#include<conio.h>
Void main ( )
{
int a,b;
printf("\n\nenter the value of a,b : ");
scanf("%d %d",&a,&b);
if(a>b)
printf("\n\n%d is big",a);
else
printf("\n\n%d is big",b);
getch( );
}
Example: 3 /*Biggest of three no*/
#include<stdio.h>
#include<conio.h>
Void main ( )
{
int a,b,c;
printf("\n\nenter the value of a,b,c : ");
scanf("%d %d %d",&a,&b,&c);
if(a>b)
{
if(a>c)
printf("\n\n%d is big",a);
else
printf("\n\n%d is big",c);
}
Else
{
if(b>c)
printf("\n\n%d is big",b);
else
printf("\n\n%d is big",c);
}
getch( );
}

Mrs. T. Uma Mageswari, AP/IT Page 53


UNIT 1 OMSAKHI CS3353- C Programming & DS

Example: 4 /*gn year is leap year or not*/


#include<stdio.h>
#include<conio.h>
Void main ( )
{
int a;
printf("\n\nenter the value of a : ");
scanf("%d",&a);
if(a%4==0)
printf("\n\n Given year is leap year");
else
printf("\n\n Given year is not leap year");
getch( );
}

Example 5 /* Find Whether the Given Number Is Even or Odd.*/


# include <stdio.h>
# include <conio.h>
Void main( )
{
int n,r;
clrscr();
printf(“ENTER A NUMBER ;”);
scanf(“%d”, &n);
r=n%2;
if(r= = 0)
printf(“the above given number is even number”);
else
printf(“the above given number is odd number”);
getch();
}
Example 6 /* Grade for the obtained average*/
# include <stdio.h>
# include <conio.h>
Void main( )
{
int b,c,d, tot, avg;
clrscr();
printf(“ SUBJECT MARKS ;”);
scanf(“%d%d%d”,&b,&c,&d);
tot=b+c+d;
tot= avg=tot/3;
if(avg>90&&avg<=100)
printf(“You got S Grade- Congtrats !”);
else if(avg>80&&avg<=90)
printf(“You got A Grade- Congtrats !”);
else if(avg>70&&avg<=80)
printf(“You got B Grade- Congtrats !”);

Mrs. T. Uma Mageswari, AP/IT Page 54


UNIT 1 OMSAKHI CS3353- C Programming & DS

else if(avg>60&&avg<=70)
printf(“You got C Grade- Congtrats !”);
else if(avg>50&&avg<=60)
printf(“You got D Grade- Congtrats !”);
else
printf(“You got U Grade- Sorry !”);
getch( );
}

Example 7 ARMSTRONG NUMBER OR NOT


# include <stdio.h>
# include <conio.h>
Void main( )
{
int n,r,a,sum=0;
clrscr( );
printf(“ Enter the Number : “); scanf(“%d”, &n);
a=n;
while (n>0)
{
r=n%10;
sum = sum + r * r * r;
n=n/10;
}
if(sum==a)
printf(“ The given Number %d is an ARMSTRONG NUMBER”,a);
else
printf(“ The given Number %d is not an ARMSTRONG NUMBER”,a):
getch( );
}
Example 8 PALINDROME NUMBER OR NOT
# include <stdio.h>
# include <conio.h>
Void main( )
{
int n,a,r,sum=0;
clrscr( );
printf(“ Enter the Number : “);
scanf(“%d”, &n);
a=n;
do
{
r=n%10;
sum = sum*10 + r;
n=n/10;
} while (n>0);
if(sum==a)

Mrs. T. Uma Mageswari, AP/IT Page 55


UNIT 1 OMSAKHI CS3353- C Programming & DS

printf(“ The given Number %d is an PALINDROME NUMBER”,a);


else
printf(“ The given Number %d is not an PALINDROME NUMBER”,a):
getch( );
}

Example 9 /*Sum of the series 0+1+(1+2)+(1+2+3)+……..+n */


#include<stdio.h>
#include<conio.h>
Void main( )
{
int i, j, n, s, term;
printf(“Enter the value n :”);
scanf(“%d”,&n);
s=0;
for(i=1;i<=n;i++)
{
term=0;
for(j=1;j<=i;j++)
{
term=term+j;
}
s=s+term;
}
printf(“Sum Of the series S=%d”,s);
getch( );
}
Example 10: FACTORIAL OF A NUMBER
#include<stdio.h>
#include<conio.h>
Void main( )
{
int n, i=1, fact=1;
clrscr( );
printf(“Enter the number to find its factorial:”);
scanf(“%d”,&n);
while(i<=n)
{
fact=fact*i;
i=i+1;
}
printf(“The factorial of %d is %d”,n,fact); getch();
}
Example 11: /*Fibonacci Series*/
#include<stdio.h>
#include<conio.h>
Void main( )

Mrs. T. Uma Mageswari, AP/IT Page 56


UNIT 1 OMSAKHI CS3353- C Programming & DS

{
int n, i=1, a=0,fib=1;
clrscr( );
printf(“Enter the limit to find Fibonacci:”);
scanf(“%d”,&n);
printf(“ Fibonacci Series \n“);
printf(“%d\n”,a);
printf(“%d\n”,fib);
while(i<n)
{
fibo=a+fib;
a=fib;
fib=fibo;
i=i+1;
printf(“%d\n”,fibo);
}
getch( );
}
Example 12./* To find the roots of the quadratic equation */
#include<stdio.h>
#include<conio.h>
#include<math.h>
void main()
{
int a,b,c,d;
float root1,root2;
printf(“Enter the values of a,b,c\n”);
scanf(“%d %d %d”,&a,&b,&c);
d=b*b-4*a*c;
if(d>=0)
{
root1=(-b+sqrt(d))/(2*a);
root2=(+b+sqrt(d))/(2*a);
printf(“root1= %f/n root2=%f”,root1, root2);
}
else
printf(“The roots are imagenary”);
getch();
}
OUTPUT:
Enter the values of a,b,c 1 0 -9
root1=3.000000
root2=3.000000

Mrs. T. Uma Mageswari, AP/IT Page 57


UNIT 1 OMSAKHI CS3353- C Programming & DS

Example 13 PRIME NUMBER

#include<stdio.h>
#include<conio.h>
int main()
{
int N, i, j, isPrime, n;
printf("To print all prime numbers between 1 to N\n");
printf("Enter the value of N\n");
scanf("%d",&N);
printf("Prime numbers between %d to %d\n", 1, N);

for(i = 2; i <= N; i++)


{
isPrime = 0;
for(j = 2; j <= i/2; j++)
{
if(i % j == 0)
{
isPrime = 1;
break;
}
}
if(isPrime==0 && N!= 1)
printf("%d ",i);
}
getch();
return 0;
}
OUTPUT:
To print all prime numbers between 1 to N
Enter the value of N : 50
Prime numbers between 1 to 50
2 3 5 7 11 13 17 19 23 29 31 37 41 43 47

Example 14: PALINDROME OR NOT


#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
{
int len,i,j;
char str[15];
clrscr();
printf("\n Enter the string:");
scanf("%s",&str);
len=strlen(str);
for(i=0,j=len-1;i<len/2;i++,j--)

Mrs. T. Uma Mageswari, AP/IT Page 58


UNIT 1 OMSAKHI CS3353- C Programming & DS

{
if(str[i]!=str[j])
{
printf("\n The String is not a palindrome");
getch();
exit(0);
}
}
printf("\n The String is a palindrome");
getch();
}
Output:
Enter the string: abcba
The String is a palindrome

**************************

Mrs. T. Uma Mageswari, AP/IT Page 59

You might also like