You are on page 1of 9

C Functions

A function is a block of code that performs a specific task.

Types of function

There are two types of function in C programming:

• Built in/Standard library functions

• User-defined functions

Built-in functions are C language functions already available with C compliers. Example: printf(
),scanf( )

User-defined functions are written by programmers to serve their own purpose and are not readily
available

Function Aspects

There are three aspects of a C function.

Function declaration A function must be declared globally in a c program to tell the compiler about
the function name, function parameters, and return type.

Function call Function can be called from anywhere in the program.

Function definition It contains the actual statements which are to be executed.

#include <stdio.h>

void functionName()

... .. ...

int main()

... .. ...

functionName();

... .. ...

QU11

There are two methods to pass the data into the function in C language, i.e., call by
value and call by reference.
In call by value method, we can say that the value of the variable is used in the function call
in the call by value method.

void

doit( int x )

x = 5;

int

main()

int z = 27;

doit( z );

return 0;

In call by reference, the address of the variable is passed into the function call as the actual
parameter.

void

doit( int x )

x = 5;

int

main()

int z = 27;

doit( z );

return 0;

}
Qu1

Pointer
C Pointers

The pointer in C language is a variable which stores the address of another variable. This variable can
be of type int, char, array, function, or any other pointer.

1. int n = 10;
2. int* p = &n; // Variable p of type pointer is pointing to the address of the vari
able n of type integer
The pointer in c language can be declared using *

Pointer reduces the code and improves the performance,

We can return multiple values from a function using the pointer.

Pointers provide direct access to memory

Disadvanatge of pointers in c:

Pointers are slower than normal variables.

If pointers are updated with incorrect values, it might lead to memory corruption.

………………………………………..

Qu3

Difference between Structure and Union


Let's summarize the above discussed topic about the Struct and Union in the form of a table
that highlight the differences between structure and union:

Struct Union

The struct keyword is used to define a structure. The union keyword is used to define union.
The size of a structure is equal or greater to the sum of The size of a union is equal to the size of its
the sizes of each data member. largest data member size.

Each variable member occupied a unique memory Variables members share the memory space of
space. the largest size variable.

Changing the value of a member will not affect other Changing the value of one member will also
variables members. affect other variables members.

It allows accessing and retrieving any data member at It allows accessing and retrieving any one data
a time. member at a time.

…………………………….

Qu4

An array of structres in C can be defined as the collection of multiple structures variables


where each variable contains information about different entities.

struct student{
int rollno;
char name[10];
};
int main(){
struct student st[5]; }

A nested structure in C is a structure within structure. One structure


can be declared inside another structure in the same way structure
members are declared inside a structure.
struct Organisation
{
char organisation_name[20];
char org_number[20];

struct Employee
{
int employee_id;
char name[20];
int salary;

};
};
…………………………………..
Qu2

#include <stdio.h>
#include <conio.h>
#include <string.h>
void main(){
char string[20],temp;
int i,length;
printf("Enter String : ");
scanf("%s",string);
length=strlen(string)-1;
for(i=0;i<strlen(string)/2;i++){
temp=string[i];
string[i]=string[length];
string[length--]=temp;
}
printf("
Reverse string :%s",string);

…………………………………….
Qu10

String manipulation:

strlen() function returns the length of the given string.

#include<stdio.h>

#include <string.h>
int main(){

char ch[20]={'j', 'a', 'v', 'a', 't', 'p', 'o', 'i', 'n', 't', '\0'};

printf("Length of string is: %d",strlen(ch));

return 0;

C Reverse String: strrev()

The strrev(string) function returns reverse of the given string. Let's see a simple example of strrev()
function.

#include<stdio.h>

#include <string.h>

int main(){

char str[20];

printf("Enter string: ");

gets(str);//reads string from console

printf("String is: %s",str);

printf("\nReverse String is: %s",strrev(str));

return 0;

Copy String: strcpy()

The strcpy(destination, source) function copies the source string in destination.

#include<stdio.h>

#include <string.h>

int main(){

char ch[20]={'j', 'a', 'v', 'a', 't', 'p', 'o', 'i', 'n', 't', '\0'};

char ch2[20];

strcpy(ch2,ch);

printf("Value of second string is: %s",ch2);


return 0;

……………………………………………………….

QU6

FILE COPY

The program takes the name of the source and target files as
input from the user, reads the content of the source file, and
copies it into the target file. At the end, it prints a success
message on the output window.

1. Write a C Program for Addition of ...


Pause
Unmute
Loaded: 96.15%

Enter the name of the source file: source.txt


Enter the name of the target file: target.txt

Sample Output:
Contents of source.txt copied into target.txt successfully

To copy the content of one file into another in C programming


language, you mainly need three built-in
functions, fopen(), fgetc() and fputc().
See implementation in the following C program:

// C program to copy the content


// of one file into another
#include <stdio.h>
int main(){

char src_filename[100], target_filename[100];


char ch;

printf("Enter the name of the source file: ");


scanf("%s", &src_filename);
printf("Enter the name of the target file: ");
scanf("%s", &target_filename);

// Try to open both files


FILE *fptr_src = fopen(src_filename, "r"); // In Read mode
FILE *fptr_target = fopen(target_filename, "w"); // In Write mode

if(fptr_src == NULL){
printf("Source File does not exist or cannot be opened.");
return 1;
}

if(fptr_target == NULL){
printf("Target File does not exist or cannot be opened.");
return 1;
}

// Call the fgetc() function on source file


ch = fgetc(fptr_src);

// Continue calling fgetc() until EOF reached


while(ch != EOF){
fputc(ch, fptr_target); // Copy content into target file
ch = fgetc(fptr_src); // Read next character from src file
}

printf("Contents of %s copied into %s successfully", src_filename,


target_filename);

// Close the file at the end


fclose(fptr_src);
fclose(fptr_target);

return 0;

}
Output:

Enter the name of the source file: source.txt


Enter the name of the target file: target.txt
Contents of source.txt copied into target.txt successfully
Enter the name of the source file: random.txt
Enter the name of the target file: target.txt
Source File does not exist or cannot be opened.

You might also like