0% found this document useful (0 votes)
15 views21 pages

Call by Value & Call by Reference

This document discusses call by value vs call by reference in C programming, as well as strings in C. It defines call by value as passing copies of arguments, so changes to parameters don't affect the original variables. Call by reference passes addresses, allowing changes to parameters to affect the original variables. Strings are arrays of characters terminated by a null character. The document also summarizes common string functions like strlen(), strcpy(), gets(), puts(), and more.

Uploaded by

Tanuj Sharma
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)
15 views21 pages

Call by Value & Call by Reference

This document discusses call by value vs call by reference in C programming, as well as strings in C. It defines call by value as passing copies of arguments, so changes to parameters don't affect the original variables. Call by reference passes addresses, allowing changes to parameters to affect the original variables. Strings are arrays of characters terminated by a null character. The document also summarizes common string functions like strlen(), strcpy(), gets(), puts(), and more.

Uploaded by

Tanuj Sharma
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/ 21

1.

Call by Value & Call by Reference


2.Strings
By,
T.K.PradeepKumar,DCE, B.Tech, MBA, ME, (PhD),
AP, CSE Dept,
Poornima University
The Structure of a C program:
[Comments /Documentation Section]
[Pre-processor Directives/Link Section]
[Global Declaration Section]
[Function Declaration Section]
main()
{
Declaration Section
Executable part
}
Subprogram section
[Function1
Function2 . .
Functionn]
/*Documentation Section: 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();
printf("Enter the radius of the circle\n"); /*executable part starts here*/
scanf("%f",&r);
area=PI*r*r;
printf("Area of the circle=%f",area);
getch();
}
Function
• The function that is invoked by writing the name of the function and its
arguments is called ―called function
• The function which calls or invokes function is called ―calling function.
/*Documentation Section: program to find
the sum of two integers*/
int addition( int x,int y) /* Function definition*/
#include<stdio.h> /*link section*/
{
#include<conio.h> /*link section*/ int s ;
int addition( int , int ); /*Function Prototype s = x+y;
declaration section*/
return s; /* Return statement to return the sum s*/
void main()
}
{
int n1,n2,sum ; /*declaration part*/
Called function
clrscr();
printf("Enter the values of n1 and n2\n");
scanf("%d %d",&n1,&n2);
sum = addition(n1,n2); /* Function Call */
printf("Sum =%d \n ",sum);
getch();
}
Calling function
Argument Passing :
• There are two ways of passing parameters to the function
a. Pass by Value ( Call by value )
b. Pass by Reference ( Call by Reference
Pass by Value (Call by value):
• Here the values of actual parameters are copied into formal
parameters i.e formal parameters contain only the copy of actual
parameters.
• So even if the value of the formal parameters changes in the called
function the values of the actual parameters are not changed.
• Programming Example:
#include<stdio.h> void exchange (int m , int n) /*Formal Paramters m,n*/
{ int temp; Called function
#include<conio.h>
temp = m;
void exchange(int m,int n); m = n;
void main() n = temp;
{ int a,b; }
clrscr(); a=10, b=20;
printf(“\n The values of a and b before calling exchange() \n”);
printf(“\n %d %d \n”,a,b);
exchange(a,b); /* Actual Paramters a,b*/ Calling function
printf(“\n The values of a and b after calling exchange()\n”);
printf(“\n a =%d and b = %d \n”,a,b);
getch();
}
Pass by Reference (Call by Reference) :
• In pass by reference / address when a function is called the address of
actual parameters are sent.
• In the called function the formal parameters should be declared as
pointers with the same type as the actual parameters.
• Using these addresses the values of the actual parameters can be
changed.
• This way of changing the actual parameters is called pass by
addresses.
• Programming Example: void exchange (int *m , int *n)
#include<stdio.h> /*Formal Paramters m,n*/
#include<conio.h> { Called function

void exchange(int m,int n); int temp;


temp = *m;
void main() *m = *n;
{ int a,b; *n = temp;
clrscr(); a=10, b=20; }
printf(“\n The values of a and b before calling exchange() \n”);
printf(“\n %d %d \n”,a,b);
exchange(&a,&b); /* Actual Paramters a,b*/ Calling function
printf(“\n The values of a and b after calling exchange()\n”);
printf(“\n a =%d and b = %d \n”,a,b);
getch();
}
Strings
• A string is an array of characters terminated by null character which is
denoted by the escape sequence ‘\0’. Consider a string “VISION” .
This string is stored in the form of an array as shown below::
Declaring String Variables:
• In C string variable is any valid C variable name and is always declared
as an array of characters.
• The general form of declaration of a string variable is :
char string_name[size];
• The size determines the number of characters in the string name
including the null character.
Examples : char city[10]; char name[30];
• Null character: The null character denoted as ‘\0’ marks end of the
string.
Initializing String Variables:
• Like numeric arrays character arrays may be initialized when they are
declared.
• C permits a character array to be initialized in either of the following
two forms
char city[9] = “New York” ;
char city[9] = { ‘N’,‘E‘,‘W‘,‘‘,‘Y‘,‘O,‘R‘,‘K‘,‘\0‘};

• The declaration char st1[ ] = {‘G’,‘O‘,‘O‘,‘D‘,‘\0‘};


char str[10] = “GOOD” ;

The following declaration is illegal: Here


char str2[3] =“GOOD” ;
Printing a string:
String is printed using a printf statement. As long as the string is
terminated by a null character Example:
main()
{
char country[15] = “India is Great”;
printf(“%s\n”,country);
}
Output:
Reading Strings using scanf:
• The conversion specification for reading a string using scanf is %s just
as in printf. One must not use ampersand (&) symbol while reading
the string
Example :
char name[20];
printf(“\n Enter the name\n”);
scanf(“%s”,name);
printf(“\n The entered name is \n”);
printf(“%s”,name);
String Manipulation Functions From the
Standard Library
• C language provides number of built in string handling functions.
• All the string manipulation functions are defined in the standard library
string.h .
• The standard library string.h contains many functions for string
manipulation.
• Some of the string handling or manipulation functions used in C are:
1. strlen( ) 5. strncmp( )
2. strcpy( ) 6. strcat( )
3. strncpy( ) 7. strlwr( )
4. strcmp( ) 8. strupr( )
9. strrev( )
strlen( ) :
This function is used to find the length of the string in bytes .
The general form of a call to strlen is the following :
length = strlen(str);
Ex:
#include<stdio.h>
#include <conio.h>
void main( )
{
char str[20];
int l;
clrscr();
printf(“\n Enter a string \n”);
scanf(“%s‖,str);
l = strlen(str);
printf(“\n Length of the given string is %d”,l );
getch();
}
String Input/output Functions
• Reading and Writing using gets( ) and puts( ) :
• The functions gets() and puts() are called line oriented I/O functions
and can be used to process entire line.
• The gets( ) function can be used to read entire line including white
spaces.
• The puts() function can be used to print the line.
• Printing a string using puts() :
The general form of puts function is as given below :
puts (str) ;
• Reading a string:
The general form of gets function is as given below:
s = gets (str); or gets(str)
Example:
The following program shows how to read and display a string using gets and
puts functions.
main()
{
char name[30];
clrscr();
printf(“\n Enter your name \n”);
gets(name);
printf(“\n Your name is \n”);
puts(name);
}
Thank You

You might also like