EM2101 Coding Techniques 1u5
EM2101 Coding Techniques 1u5
void main( )
{
emp e1;
printf("\nEnter Employee record:\n");
printf("\nEmployee name:\t");
scanf("%s", e1.name);
printf("\nEnter Employee salary: \t");
scanf("%d", &e1.salary);
printf("\nstudent name is %s", e1.name);
printf("\nroll is %d", e1.salary);
}
Definition: Command line arguments are nothing but simply arguments that are
specified after the name of the program in the system’s command line, and these
argument values are passed on to your program during program execution.
Command line argument is a parameter supplied to the program when it is invoked.
Command line argument is an important concept in C programming. It is mostly used
when you need to control your program from outside.
Command line arguments are passed to the main() method.
In order to implement command line arguments, generally, 2 parameters are passed
into the main function:
1. Number of command line arguments
2. The list of command line arguments
The basic syntax is:
int main( int argc, char *argv[] )
{
.
.
// BODY OF THE MAIN FUNCTION
.
.
}
Another way to implement command line arguments is:
int main( int argc, char **argv[] )
{
.
.
// BODY OF THE MAIN FUNCTION
.
.
}
argc: It refers to “argument count”. It is the first parameter that we use to store the
number of command line arguments. It is important to note that the value of argc
should be greater than or equal to 0.
agrv: It refers to “argument vector”. It is basically an array of character pointer which
we use to list all the command line arguments.
Example for Command Line Argument
#include <stdio.h>
#include <conio.h>
int main(int argc, char *argv[])
{
int i;
if( argc >= 2 )
{
printf("The arguments supplied are:\n");
for(i = 1; i < argc; i++)
{
printf("%s\t", argv[i]);
}
}
else
{
printf("argument list is empty.\n");
}
return 0;
}
Remember that argv[0] holds the name of the program and argv[1] points to
the first command line argument and argv[n] gives the last argument. If no
argument is supplied, argc will be 1.
Recursive Functions
In C programming, a function is allowed to call itself.
A function which calls itself directly or indirectly again and again until some
specified condition is satisfied is known as Recursive Function.
STEP 3: “addition.obj” file would be created which is then compiled form of “addition.c”
file.
STEP 4: Use the below command to add this function to library (in turbo C).
c:\> tlib math.lib + c:\ addition.obj
+ means adding c:\addition.obj file in the math library.
We can delete this file using – (minus).
STEP 5: Create a file “addition.h” & declare prototype of addition() function like below.
int addition (int i, int j);
Now, addition.h file contains a prototype of the function “addition”.
STEP 6: Let us see how to use our newly added library function in a C program.
The pre-processor is a unique feature in C that processes the source code program before it
passes through the compiler. It is mostly supplied along with the C compiler or sometimes as
a separate tool. The following facilities are provided by the pre-processor :
1. File inclusion
2. Substitution facilities
3. Conditional compilation
These facilities are controlled in a program by a pre-processor control lines or directives.
Generally, the pre-processor directives appear before the main function but they may appear
anywhere in a C program. A directive may influence that part of the program which follows.
It modifies the C sourcecode before compilation. Any pre-processor directive must begin
with the character #. Any number of whitespace characters may follow or precede #. There
are different types of directives which are indicated by the following keywords :
define, elif, else, endif, error, if, ifdef, include, line, undef
Sr.No. Directive & Description
1 #define- Substitutes a preprocessor macro.
2 #include- Inserts a particular header from another file.
3 #undef -Undefines a preprocessor macro.
4 #ifdef- Returns true if this macro is defined.
5 #ifndef- Returns true if this macro is not defined.
6 #if-Tests if a compile time condition is true.
7 #else -The alternative for #if.
8 #elif -#else and #if in one statement.
9 #endif -Ends preprocessor conditional.
10 #error -Prints error message on stderr.
11 #pragma -Issues special commands to the compiler, using a standardized method.
1. File Inclusion
2. Substitution Facilities
3. Defining Macros
Macro is a piece of code that is replaced by the value of the macro throughout the
program. You can define a macro with #define directive. At the end of defining a
macro, you don’t have to put a semicolon(;) to terminate it.
Types of Macros in C
There are 2 types of macros present in C such as:-
Object-like Macros.
Function-like Macros.
1. Object-like Macros in C
It is a simple type of macro. In this object-like macro, the macro will be replaced
by it’s value. Object-like macros mainly used to represent numeric constants.
Example:-
#define PI 3.14
Example:- Object-like Macros
#include <stdio.h>
#define SIDE 4
int main() {
int area;
area = SIDE*SIDE;
printf("Object Like Macros!\n");
printf("Area is: %d",area);
return 0;
}
Output:-
Object Like Macros!
Area is: 16
2. Function-like Macros in C
In C, function-like macros are much similar to a function call. In this type of
macro, you can define a function with arguments passed into it.
Example:-
#define AREA(a) (a*a)
Example:- Function-like Macros
#include <stdio.h>
#define AREA(s) (s * s) // macro with argument
int main()
{
int s1 = 10, area_of_square;
area_of_square = AREA(s1);
printf("Macros with arguments!\n");
printf("Area of square is: %d", area_of_square);
return 0;
}
Output:-
Macros with arguments!
Area of square is: 100
In the above example, the compiler finds the name of the macro (AREA(a)) and
replaces it with the statement (a*a).
4. Pre defined Macros
There are some predefined macros present in C. And it cannot be modified. Below
are some predefined macros.