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

unit5 (1)

The document explains key concepts in C programming, including function declaration, definition, and calls, as well as parameter passing methods (by value and by reference). It also covers file handling, the use of the C preprocessor, static variables, and the differences between return and exit functions. Additionally, it distinguishes between text and binary files, providing examples of how to read and write data in both formats.

Uploaded by

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

unit5 (1)

The document explains key concepts in C programming, including function declaration, definition, and calls, as well as parameter passing methods (by value and by reference). It also covers file handling, the use of the C preprocessor, static variables, and the differences between return and exit functions. Additionally, it distinguishes between text and binary files, providing examples of how to read and write data in both formats.

Uploaded by

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

Unit 5

1. Explain the concept of function declaration,


definition , and function call in C with examples

Functions are blocks of code designed for


specific task .
The block of code executes when we call th
function.
Function Declaration
Function declaration is a prototype (or )
early
design of a function
It specifies function name , input types,
return
type.
Function Definition

A Function definition specifies the body of


the
function.
It provides implementation details of
program

int adder ( int a, int b ) {

return a+b ;
}

Function Call
When we call the function , the function’s
code is executed

adder ( 2 , 5 );

Example code:

int adder ( int , int );

int adder ( int a , int b ) {


return a+b;
}

int main ( ) {

printf(“the sum is %d” , adder(2, 5))

return 0;
}
2. Discuss the concept of passing parameters by
value and by reference in C, how pointers are
used to pass by reference.

Parameters are inputs declared in function


definition
Arguments are actual input values given
while calling the function
Example:
float area (int radius ){
return 3.14 * radius *radius ;
}
Here, radius is the parameter to function

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

// giving a copy ( pass by value )


area (length , breadth);
return 0;
}

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

// giving address ( pass by reference )


area (&length , &breadth);
return 0;
}

3. Explain the concept of file handling in c. How can


you use C to create ,read , write , and modify a file.
lIIustrate with a program that writes data to a file,
modifies the data , and reads it back.
File handling in C, allows you to interact
with
files on your system.
You can create, read, write, and modify fil
using C.
Types of File Modes:
r : Opening the file for reading
w: Opening the file for writing

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;
}

4. Explain how fopen() handles different


modes for opening files in C. What are th
different file modes available, and what
are their specific use cases? Write a
program
that uses different file modes to read an
write data.
The fopen( ) function in c is used to open a fi
It takes two arguments. The name of file and
the
mode in which to open the file.
Syntax:
fopen(“filename”, mode );
Types of file mode:
r : Opens a file for reading
If file does not exit, it returns NULL
w : Opens a file for writing
If file already exists , its content is
erased
If the file does not exist, it is created
a: Opens file for appending(add at the
end)
New data is added at the end of file
Program :
#include<stdio.h>
int main ( ){
FILE *ptr ;
//Open a file for writing
ptr = fopen ( “sample.txt” , “w” );
//writing data to file
fprintf (ptr , “hello world”);
fclose( ptr );
//Open a file for reading
ptr = fopen ( “sample.txt” , “r” );
//read data from file
char data[1000];
fgets( data ,1000 , ptr ) ;
//printing data
printf ( “%s” , data);
return 0 ;
}

5. Discuss in detail the role of the C preprocessor


in the compilation process, explaining the major
processor directives and their functions. Provide
examples of how they are used.
Preprocessor is responsible for processing
the source code before it is compiled by
the compiler.
The preprocessor directives are special
instructions that start with the # symbol
Major Preprocessor Directives:
1. #include
The #include directive is used to include
the contents of another file into the
current source file.
#include < stdio.h >
2. #define
The #define directive is used to define a
macro, which is a symbolic name for a
constant or a piece of code.
#define PI 3.14159
#define college “qis”
3.#undef
The #undef directive is used to undefine
or cancel a previously defined macro.
#undef college
4. #ifdef , #endif
These directives are used to conditionally
include or exclude code based on if macro
is
defined.
#ifdef pi
printf(“pi is defined” );
#endif
6. Describe the use of static variables in C, both
inside and outside functions, and explain
their advantage and disadvantage.
The word static means , that which is
fixed
unchanging, changeless.
Therefore, when we use static keyword
before
a variable, the variable is
preserved/remains
in memory till end of program.
static int var = 10;
//var is static variable
//it remains in memory till end of
program
Advantages of static:
Static variable is used as a counter as
it’s value
is preserved .
Using static variable we can count
number of
times a function is called.
Disadvantages
Static variable can cause memory leak
as they
remain in memory till our program
ends.-

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

In C, return and exit() are two distinct wa


to
terminate a program or a function.

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( ) {

myFunction(); //calling myFunction

exit(0); // Terminate the program

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;
}

You might also like