Example - call by Address
Example - call by value
Example 2 - fn types
Example 1:
// Functions
// No arguments and With Return Value
#include<stdio.h>
#include<stdlib.h>
int fnRandomNumber()
{
int Num = rand()%10;
return Num;
}
int main()
{
int N;
N = fnRandomNumber();
printf("Random Number received = %d\n",
N);
printf("Random Number received = %d\n",
fnRandomNumber());
return 0;
}
Sample output:
$ gcc chk.c
$ ./a.out
Random Number received = 3
Random Number received = 6
Example 1 - fn types
Example 1:
// Functions
// No arguments and No Return Value
#include<stdio.h>
#include<stdlib.h>
void fnDisplay()
{
printf("Hello World\n");
printf("Welcome to Programming in C\n");
}
int main()
{
fnDisplay();
return 0;
}
Sample output:
$ gcc chk.c
$ ./a.out
Hello World
Welcome to Programming in C
$
Example 3 - fn types
Example 3:
// Functions
// With arguments and No Return Value
#include<stdio.h>
#include<stdlib.h>
void fnSquare(int v)
{
printf("Square of %d = %d\n",v,
(v*v));
}
void fnAdd(float v1, float v2)
{
printf("Sum of %.2f and %.2f is %.2f\n",v1,
v2, (v1+v2));
}
int main()
{
int x = 9;
fnSquare(x);
fnAdd(5.5, 7.2);
return 0;
}
Sample output:
$ gcc chk.c
$ ./a.out
Square of 9 = 81
Sum of 5.50 and 7.20 is 12.70
Example 4 - fn types
Example Program
// Functions
// With arguments and With Return Value
#include<stdio.h>
#include<stdlib.h>
int fnSquare(int v)
{
return((v*v));
}
float fnAdd(float v1, float v2)
{
return(v1+v2);
}
int main()
{
int x = 9;
int sq;
float total;
sq = fnSquare(x);
printf("Square of %d = %d\n", x,
sq);
total = fnAdd(5.5, 7.2);
printf("Sum = %.2f\n", total);
return 0;
}
Sample
Output:
$ gcc chk.c
$ ./a.out
Square of 9 = 81
Sum = 12.70
call by value, address
Function Call and Argument Passing
Passing values to a function
There are two methods to send data from the calling function to the called function through arguments.
1. Call by Value
In C, arguments are passed to functions by value by default. Call by value means that a copy of the actual argument is passed to the function. Any changes made to the parameters inside the function will not affect the original value stored in the variables in the calling function.
2. Call by Address Using Pointers
When the address of a variable is passed to a function, the function can access and modify the value of the original variable through a pointer.
Difference Between Call by Value and Pointer-Based Passing
Call by Value | Pointer-Based Passing |
Copy of the value is passed. | Address of the variable is passed. |
Ordinary parameters are used. | Pointer parameters are used. |
Changes do not affect original variables. | Changes through pointers affect original variables. |
Separate parameter storage is used. | Pointer refers to the original variable. |
Function Types
Types of Functions
Arguments and Return Value
Classification of User-Defined Functions
Based on the presence or absence of arguments and return values, user-defined functions are classified into four types.
Four Types of Functions
Type 1: No Arguments and No Return Value
- The function does not receive any values from the calling function.
- It does not return any value.
- It performs its task independently.
void function_name(void);
Type 2: With Arguments and No Return Value
- The function receives values from the calling function.
- It performs the required operation.
- It does not return any value.
void function_name(parameter_list);
Type 3: No Arguments and With Return Value
- The function does not receive any arguments.
- It performs an operation internally.
- It returns a value to the calling function.
return_type function_name(void);
Type 4: With Arguments and With Return Value
- The function receives arguments from the calling function.
- It performs the required operation.
- It returns the result to the calling function.
return_type function_name(parameter_list);
Note: Arguments and Parameters
These two terms are closely related but have different meanings.
Parameters: Variables declared in the function definition to receive values.
Arguments: Actual values passed to the function during the function call.
Function def
Programming in C
Functions
Function
A function is a block of code that performs a specific task. It is a self-contained unit of a program that can be called whenever required.
Advantages of Functions
- Code Reusability: A function can be called multiple times without rewriting the code.
- Modularity: A large program can be divided into smaller modules.
- Easy Debugging: Errors can be identified and corrected easily.
- Code Readability: Programs become easier to understand.
- Code Maintenance: Changes can be made in one function without affecting the entire program.
- Reduces Code Duplication: The same logic need not be written repeatedly.
Types of Functions in C
C supports two major categories of functions.
1. Library Functions (Predefined functions) - Library functions are predefined functions provided by the C standard library. We can use these functions by including the appropriate header files. Example
a. printf(), scanf() from stdio.h
b. rand() from stdlib.h
2. User-Defined Functions (Created by programmers) - User-defined functions are functions created by the programmer to perform specific tasks. Example
a. fnAdd()
b. fnDisplay()
c. fnFactorial()
Components of a Function
A user-defined function generally involves three important components:
- Function Declaration (Function Prototype)
- Function Definition
- Function Call
1. Function Declaration
A function declaration informs the compiler about the function name, return type and parameters before the function is used.
Syntax:
return_type function_name(parameter_list);
2. Function Definition
A function definition contains the actual statements that perform the required operation.
return_type function_name(parameters)
{
// Statements
return value;
}
3. Function Call
A function call transfers the program's control to the specified function.
Functions
Programming in C
Functions
Function
A
function is a block of code that performs a specific task. It is a
self-contained unit of a program that can be called whenever required.
Advantages of
Functions
- Code Reusability: A function can be called
multiple times without rewriting the code.
- Modularity: A large program can be divided
into smaller modules.
- Easy Debugging: Errors can be identified and
corrected easily.
- Code Readability: Programs become easier to
understand.
- Code Maintenance: Changes can be made in one
function without affecting the entire program.
- Reduces Code Duplication: The same logic need
not be written repeatedly.
Types of Functions in C
C supports two major categories of
functions.
1. Library
Functions (Predefined functions) - Library functions are
predefined functions provided by the C standard library. We can use these
functions by including the appropriate header files. Example
a. printf(), scanf() from stdio.h
b. rand() from stdlib.h
2. User-Defined
Functions (Created by programmers) - User-defined functions are
functions created by the programmer to perform specific tasks. Example
a. fnAdd()
b. fnDisplay()
c. fnFactorial()
Components of
a Function
A user-defined
function generally involves three important components:
- Function Declaration (Function Prototype)
- Function Definition
- Function Call
1. Function
Declaration
A function
declaration informs the compiler about the function name, return type and
parameters before the function is used.
Syntax:
return_type
function_name(parameter_list);
2. Function
Definition
A function
definition contains the actual statements that perform the required operation.
return_type
function_name(parameters)
{
// Statements
return value;
}
3. Function
Call
A function call
transfers the program's control to the specified function.
Classification
of User-Defined Functions
Based on the
presence or absence of arguments and return values, user-defined functions are
classified into four types.
Four Types of
Functions
Type 1: No
Arguments and No Return Value
- The function does not receive any values from the
calling function.
- It does not return any value.
- It performs its task independently.
void function_name(void);
Type 2: With Arguments
and No Return Value
- The function receives values from the calling
function.
- It performs the required operation.
- It does not return any value.
void function_name(parameter_list);
Type 3: No
Arguments and With Return Value
- The function does not receive any arguments.
- It performs an operation internally.
- It returns a value to the calling function.
return_type function_name(void);
Type 4: With Arguments
and With Return Value
- The function receives arguments from the calling
function.
- It performs the required operation.
- It returns the result to the calling function.
return_type function_name(parameter_list);
Note: Arguments
and Parameters
These two terms
are closely related but have different meanings.
Parameters: Variables
declared in the function definition to receive values.
Arguments:
Actual values passed to the function during the function call.
Passing
values to a function
There are two methods
to send data from the calling function to the called function through
arguments.
1.
Call by Value
In C, arguments
are passed to functions by value by default. Call by value means that a copy of
the actual argument is passed to the function. Any changes made to the
parameters inside the function will not affect the original value stored in the
variables in the calling function.
2.
Call by Address Using Pointers
When the address
of a variable is passed to a function, the function can access and modify the value
of the original variable through a pointer.
Difference
Between Call by Value and Pointer-Based Passing
|
Call by Value |
Pointer-Based Passing |
|
Copy of the value is passed. |
Address of the variable is passed. |
|
Ordinary parameters are used. |
Pointer parameters are used. |
|
Changes do not affect original variables. |
Changes through pointers affect original variables. |
|
Separate parameter storage is used. |
Pointer refers to the original variable. |
OS Installation
Operating System Installation
Steps to install Windows 10 operating system
-
To connect the Windows 10 installation media, insert DVD into the drive.
- Boot the PC
into the BIOS. Under
Boot tab, select the device from which
to boot as CD-ROM Drive or Optical Drive option as the first choice.
- Save the
settings and exit.
- Restart the computer.
- The Windows Setup will load and will display a Setup window...
- Choose the Windows Setup options / Select the Regional Settings (preferred language, keyboard type, and time/currency format), then click Next.
- Click the ‘Install Now’ button.
- Enter the Windows Product Key, then click Next.
- Read over the Microsoft Software License Terms. To accept the License Terms check 'I accept the license terms' checkbox and click Next.
- Select the Custom installation.
- Select the hard drive and partition to install Windows on and click Next.
- Windows will begin installing.
- After installation completes, the system will restart. Adjust the personalization, location, browser, protection, connectivity and error reporting settings.
- Specify who the owner of the device is and sign in to Windows 10 with your Microsoft account.
- Click Finish/Restart
and remove the Windows Install Media
- On restarting the system will run the new Windows operating system.
Steps to install Linux as a guest OS in Windows 10 OS using VMware.
- Download and install the VMware Workstation Player and launch it.
- Create a VM by clicking “Create a New Virtual Machine”. Browse and select the Installer disc image file (iso) of Linux OS. Click Next
- In “Select a guest OS”, choose the guest OS (Linux) and its version. Click Next
- Under Specify Disk Capacity adjust Maximum disk size as required. Click Next and Confirm the selected size in next window.
- Click finish to add the Linux virtual machine to VMware Workstation Player.
- Open the VMware Workstation Player settings and customize the VM hardware as required.
- To launch the Linux virtual machine,
use the Play button in VMware Workstation
Player.
UnixCmds
UNIX COMMANDS
UNIX commands:
- To create a directory.
- Syntax : mkdir <directory name>
- Example : mkdir oslab
- To rename a directory.
- Syntax : mv <directory 1> <directory 2>
- Example : mv CIVIL IT
- To change the directory.
- Syntax : cd <directory name>
- Example : cd IT1
- To copy the directory and its file to another directory.
- Syntax : cp –r <source directory> <destination directory>
- Example : cp –r IT1/ IT2
- To remove an empty directory.
- Syntax : rmdir <empty directory name>
- Example : rmdir IT1
- To remove an non–empty directory.
- Syntax : rmdir –r <directory name>
- Example : rmdir –r IT2
- To print present working directory. / To display complete path of the working directory.
- Syntax : pwd
- To create a file.
- Syntax : vi <file name>
- Example : vi aa.txt
- To rename a file.
- Syntax : mv <old file name> <new file name>
- Example : mv aa.txt a1.txt
- To view a file.
- Syntax : cat <file name>
- Example : cat a1.txt
- To copy files.
- Syntax : cp <source filename> <destination filename>
- Example : cp a1.txt a2.txt
- To move files.
- Syntax : mv <filename 1> <filename 2>
- Example : mv a1.txt a2.txt
- To concatenate two files.
- Syntax : cat <file 1> <file 2> > <new file 3>
- Example : cat a1.txt a2.txt > a3.txt
- To display the file contents.
- Syntax : cat <file name>
- Example : cat a3.txt
- To display the file contents with line number.
- Syntax : cat –n <file name>
- Example : cat –n a3.txt
- To create a file using cat command
- Syntax : cat > <filename>
<Type the information and press <ctrl+d> to terminate>
- Example : cat > a4.txt
- To append the contents of one file to another.
- Syntax : cat <file 1> >> <file 2>
- Example : cat a1.txt >> a2.txt
- To append more than one file.
- Syntax : cat <file 1> <file 2> <file 3>…>> <destination file>
- Example : cat a1.txt a2.txt a3.txt >> aa.txt
- To link files. <Creates a link between two files. changes in file will reflect in the other file also>
- Syntax : ln <file 1> <file 2>
- Example : ln a1.txt a2.txt
- To link 5 files.
- Syntax : ln <source directory> <destination directory>
- Example : ln /home/anu/IT/* /home/anu/IT/
- To sort files in alphabetical order / Display the file contents in descending / ascending order.
- Syntax : sort <file> // ascending order
- Example : sort a1.txt
- Syntax : sort –r <file> // descending order
- Example : sort –r a1.txt
- To list the contents of a directory.
- Syntax : ls
- To list all directories of a user.
- Syntax : ls –l | egrep ‘^d’
- To list only files from a directory.
- Syntax : ls –l | egrep –v ‘^d’
- To list hidden directories.
- Syntax : ls -a
- To display all files, subdirectories and files in the subdirectories including their sizes.
- Syntax : ls -lR
- To list only the subdirectories available in the present working directory.
- Syntax : ls –lR | egrep –v ‘^d’
- To list all directories / files that start with a specific character.
- Syntax : ls <character>*
- Example : la a*
- To list all files in current directory whose 2ndcharacter is a digit.
- Syntax : ls ?[0–9]*
- To list files and directories recursively on a single line.
- Syntax : ls –LR
- To list all files which has dot at the end
- Syntax : ls *.
- To list all filenames which has a specific extension
- Syntax : ls *.<extension>
- Example : ls *.txt
- To list the file attributes and file size.
- Syntax : ls -l
- To change the file to read only mode. / to change the permission of a file
- Syntax : chmod u-wx <file>
chmod g-wx <file>
chmod o-wx <file>
- Example : chmod u-wx a1.txt
chmod g-wx a1.txt
chmod o-wx a1.txt
- To view one page of a file.
- Syntax : ls –l | more
- To view the files page by page.
- Syntax : ls –l | more
<press space bar to view next page>
- To view first 10 lines of a file / to display the beginning of a file
- Syntax : head <file>
- Example : head a1.txt
- To view last 10 lines of a file / to display the end of a file
- Syntax : tail <file>
- Example : tail a1.txt
- To display only the first two characters of all lines from a file.
- Syntax : cut –c1–2 <file name>
- Example : cut –c1–2 aa.txt
- To combine two files.
- Syntax : cat <file 1> <file 2> > <new file 3>
- Example : cat a1.txt a2.txt > a3.txt
- To combine two files without duplicates.
- Syntax : cat <file 1> <file 2> | sort –u > <new file 3>
- Example : cat a1.txt a2.txt | sort –u > a3.txt
- To add “;” to the end of the file.
- Syntax : <cat <file>; echo “;”> > <new file>
- Example : <cat a1.txt; echo “;”> > a5.txt
- To delete the 7th line of a file.
- Syntax : sed ‘<line number>d’ <file>
- Example : sed ‘7d’ a1.txt
- To split a file into smaller number of files with 3 lines in each file.
- Syntax : split –l <number of lines> <file>
- Example : split –l 3 aa.txt
- To convert lowercase to uppercase.
- Syntax : cat <file> | awk ‘{print toupper<$0>}’
- Example : cat aa.txt | awk ‘{print toupper<$0>}’
- To convert uppercase to lowercase.
- Syntax : cat <file> | awk ‘{print toupper<$0>}’
- Example : cat aa.txt | awk ‘{print tolower<$0>}’
- To combine two strings.
- Syntax : echo <string1><string2>
- Example : echo “IRTT” “CIVIL”
- To count the number of words in a file.
- Syntax : wc –w <file>
- Example : wc –w aa.txt
- To count the number of lines in a file.
- Syntax : wc –l <file>
- Example : wc –l aa.txt
- To count the number of characters in a file.
- Syntax : wc –m <file>
- Example : wc –m aa.txt
- To search for a specific word in a file.
- Syntax : grep <word> <file>
- Example : grep civil aa.txt
- To count the number of occurrence of a character in a file.
- Syntax : grep –c <word> <file>
- Example : grep –c civil aa.txt
- To search for a specific file in a directory.
- Syntax : find <present working directory> –type f –name <file>
- Example : find /home/anu –type f –name aa.txt
- To translate the character “I” in a file to “E”.
- Syntax : cat <file> | tr <original character> <replace character>
- Example : cat aa.txt | tr I E
- To change the password.
- Syntax : passwd
- To view the commands which was executed
- Syntax : history
- Show manual for a command
- Syntax : man <command>
- Example : man cat
- To clear the screen.
- Syntax : clear
- To print todays date and calendar of the current month using a single command.
- Syntax : cal
- To print the calendar for year 2013
- Syntax : cal <year>
- Example : cal 2013
- To print the calendar for 2012 for the month of March.
- Syntax : cal <month> <year>
- Example : cal 3 2012
- To print date and time.
- Syntax : date
- Display user name.
- Syntax : whoami
- To find out who is logged in. / To print who is using the system presently.
- Syntax : who
- Display users full detail, user-id and group-id.
- Syntax : id
Converted to HTML with WordToHTML.net | Document Converter for Windows