Module 4
Module 4
Module 4
Modular Approach
of the system.
be decided.
program.
Advantages :
1. Ease of Use :This approach allows
application.
Functions in C
output.
Code
if (x > y)
return x;
else
return y;
FunctionDeclaration
parameters.
Pass by Value: In this parameter passing method,
void fun(int x)
x = 30;
}
int main(void)
int x = 20;
fun(x);
return 0;
Output:
x = 20
eg :2 # include <stdio.h>
*ptr = 30;
int main()
int x = 20;
fun(&x);
return 0;
Output:
x = 30
Recursion
function.
Properties of Recursion:
int fib(int n)
{ // Stop condition
if (n == 0)
return 0;
// Stop condition
if (n == 1 || n == 2)
return 1;
// Recursion function
else
// Driver Code
int main()
// Initialize variable n.
int n = 5;
n);
return 0;
Output
WORKING
// C code to implement factorial
#include <stdio.h>
// Factorial function
int f(int n)
// Stop condition
if (n == 0 || n == 1)
return 1;
// Recursive condition
else
return n * f(n - 1);
// Driver code
int main()
int n = 5;
return 0;
Output
WORKING
How Arrays are Passed to Functions in C
int i;
}
// Driver program
int main()
int n ;
fun(arr, n);
return 0;
Output
1 2 3 4 5 6 7 8
Structures in C
struct address
char name[50];
char street[100];
char city[50];
char state[20];
int pin;
};
struct Point
int x, y;
struct Point
int x, y;
};
int main()
normal variable
fails in compilation.
struct Point
members here
members here
};
Structure members can be initialized using curly
initialization.
struct Point
int x, y;
};
int main()
operator.
#include<stdio.h>
struct Point
int x, y;
};
int main()
p1.x = 20;
return 0;
Union in C
union test {
int x, y;
};
int main()
// A union variable t
union test t;
t.x = 2; // t.y also gets value 2
n",
t.x, t.y);
n",
t.x, t.y);
return 0;
Output:
After making x = 2:
x = 2, y = 2
x = 10, y = 10
Storage Classes in C
Storage Classes are used to describe the features
o The visibility of the automatic variables is limited to the block in which they
are defined.
The scope of the automatic variables is limited to the block in which they are
defined.
o The memory assigned to automatic variables gets freed upon exiting from the
block.
o The keyword used for defining automatic variables is auto.
EXAMPLE
#include <stdio.h>
int main()
{
int a; //auto
char b;
float c;
printf("%d %c %f",a,b,c); // printing initial default value of automatic variables a, b, an
d c.
return 0;
1. }
2.External
o The external storage class is used to tell the compiler that the variable defined as
extern is declared with an external linkage elsewhere in the program.
o The variables declared as extern are not allocated any memory. It is only declaration
and intended to specify that the variable is declared elsewhere in the program.
o The default initial value of external integral type is 0 otherwise null.
o We can only initialize the extern variable globally, i.e., we can not initialize the
external variable within any block or method.
o An external variable can be declared many times but can be initialized at only once.
o If a variable is declared as external then the compiler searches for that variable to be
initialized somewhere in the program which may be extern or static. If it is not, then
the compiler will show an error.
EXAMPLE
1. #include <stdio.h>
2. int main()
3. {
4. extern int a; // Compiler will search here for a variable a defined and initialize
d somewhere in the pogram or not.
5. printf("%d",a);
6. }
7. int a = 20;
3.Static
o The variables defined as static specifier can hold their value between the multiple
function calls.
o Static local variables are visible only to the function or the block in which they are
defined.
o A same static variable can be declared many times but can be assigned at only one
time.
o Default initial value of the static integral variable is 0 otherwise null.
o The visibility of the static global variable is limited to the file in which it has declared.
o The keyword used to define static variable is static.
EXAMPLE
#include<stdio.h>
static char c;
static int i;
static float f;
static char s[100];
void main ()
{
printf("%d %d %f %s",c,i,f); // the initial default value of c, i, and f will be printed.
}
4.Register
o The variables defined as the register is allocated the memory into the CPU registers
depending upon the size of the memory remaining in the CPU.
o We can not dereference the register variables, i.e., we can not use &operator for the
register variable.
o The access time of the register variables is faster than the automatic variables.
o The initial default value of the register local variables is 0.
o The register keyword is used for the variable which should be stored in the CPU
register. However, it is compiler?s choice whether or not; the variables can be stored
in the register.
o We can store pointers into the register, i.e., a register can store the address of a
variable.
o Static variables can not be stored into the register since we can not use more than
one storage specifier for the same variable.
EXAMPLE
#include <stdio.h>
int main()
{
register int a; // variable a is allocated memory in the CPU register. The initial default
value of a is 0.
printf("%d",a);
}
C Library Functions
As the name suggests, Preprocessors are programs that process our source
code before compilation. There are a number of steps involved between
writing a program and executing a program in C / C++.
You can see the intermediate steps in the above diagram. The source code
written by programmers is first stored in a file, let the name be “program.c“.
This file is then processed by preprocessors and an expanded source code
file is generated named “program.i”. This expanded file is compiled by the
compiler and an object code file is generated named “program.obj”. Finally,
the linker links this object code file to the object code of the library functions
to generate the executable file “program.exe”.
Macros are pieces of code in a program that is given some name. Whenever
this name is encountered by the compiler, the compiler replaces the name
with the actual piece of code. The ‘#define’ directive is used to define a
macro.
2.File Inclusion
This type of preprocessor directive tells the compiler to include a file in the
source code program. There are two types of files that can be included by
the user in the program:
3. Conditional Compilation
Conditional Compilation directives are a type of directive that helps to
compile a specific portion of the program or to skip the compilation of some
specific part of the program based on some conditions. This can be done
with the help of the two preprocessing commands ‘ifdef‘ and ‘endif‘.
.4.Other Directives
Apart from the above directives, there are two more directives that are not
commonly used. These are:
#undef Directive: The #undef directive is used to undefine an existing
macro.
#pragma Directive: This directive is a special purpose directive and is used
to turn on or off some features. This type of directives are compiler-specific,
i.e., they vary from compiler to compiler.