unit5 (1)
unit5 (1)
return a+b ;
}
Function Call
When we call the function , the function’s
code is executed
adder ( 2 , 5 );
Example code:
int main ( ) {
return 0;
}
2. Discuss the concept of passing parameters by
value and by reference in C, how pointers are
used to pass by reference.
area( 5 ) ;
5 is an argument to function
There are 2 ways to give arguments to
function
Pass by value :
A copy of the original value is passed or
given
to the function.
It does not affect the original value.
Example:
void area ( int l , int b ){
printf(“ area is %d ”, l*b );
}
int main ( ) {
int length = 5; // original value
int breadth = 8 ; //original value
Pass by reference
In pass by reference, we give reference
/address
of the original value as input to function
It affects the orginal value
Example:
void area ( int * l , int *b ){
printf(“ area is %d ”, (* l ) * (*b) )
}
int main ( ) {
int length = 5; // original value
int breadth = 8 ; //original value
Example :
#include<stdio.h>
int main ( ){
FILE *ptr ;
// Opening a file for writing
ptr = fopen ( “new_year.txt” , “w” );
//writing data to the file
fprintf ( ptr , “happy new year 2025” ) ;
// closing the file
fclose ( ptr );
//opening file for reading
ptr = fopen (“new_year.txt” , “r”);
char words[100];
fgets( words , 100, ptr );
//printing the words in the output
printf (“ %s ” , words ) ;
//closing file
fclose (ptr );
return 0;
}
Example:
//static variable to count function calls
void greet ( ) {
static int count = 0;
count ++;
printf(“%d\n”,count );
printf(“happy new year 2025\n”);
}
int main ( ) {
greet( );
greet ( );
return 0;
}
Output:
1
happy new year 2025
2
happy new year 2025
7.What is the difference between
return
and exit() in C? Explain with example
return
return is a statement used to terminate
a function
exit( )
exit() is a function that terminates the
program immediately.
Example:
#include <stdio.h>
#include <stdlib.h>
int myFunction( ) {
printf("Inside myFunction()\n");
return 0; // terminate myfunction
}
int main( ) {
return 0;
}
8. Explain the difference between text and binary files
C. How are they handle differently in terms of reading
writing? Provide a C program to read and write binary
data to a file.
In C, files can be classified into two categori
text files and binary files.
Text Files
Text files are human readable files that
contains
ASCII characters.
They are used to store text data, such as
source
code, documents.
Modes used for opening text files;
“r” : Opening a text file for reading
“w” : Opening a text file for writing
Binary Files
Binary files are not human readable and
contains
raw binary data, 0 and 1
Normally a binary file can be created only fr
within a program and its contents can be rea
only by a program..
Modes used for opening binary files;
“rb” : Opening a binary file for reading
“wb” : Opening a binary file for writing
Example:
int main( ) {
// Opening file to write binary data to a file
FILE * file = fopen ("sample.bin", "wb”)
fwrite (“hello”, sizeof(char), 6, file);
fclose (file);
//read binary data from the file
file = fopen ("sample.bin", "rb");
char words[100];
fread (words, sizeof(char), 6, file);
fclose (file);
// printing data
printf ("%s\n", words);
return 0;
}