Professional Documents
Culture Documents
Types of function
• 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
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.
#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 *
Disadvanatge of pointers in c:
If pointers are updated with incorrect values, it might lead to memory corruption.
………………………………………..
Qu3
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
struct student{
int rollno;
char name[10];
};
int main(){
struct student st[5]; }
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:
#include<stdio.h>
#include <string.h>
int main(){
char ch[20]={'j', 'a', 'v', 'a', 't', 'p', 'o', 'i', 'n', 't', '\0'};
return 0;
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];
return 0;
#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);
……………………………………………………….
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.
Sample Output:
Contents of source.txt copied into target.txt successfully
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;
}
return 0;
}
Output: