MACRO

 Macro is also called a substitution string. A macro always start with # define. After # define , we can give name to the macro and after that we can give replacement text/value. Wherever the preprocessor  encounter that name, it will replace it with the replacement text. Macro is always declared in capital letters, which differentiate the macro from variable . If we want to change the value of the macro variable we can do it at only at place where it is defined . Macro never ends with semicolon.


The value of macro body specify by #define directive can be any character string or number.

For example , the following definition is NAME with the string "Harsh".

Syntax:-

#define <identifier> <value>

example :-

#define A 50

WAP to use macro in the c program

#include<stdio.h>
#include<conio.h>
#define A 10
void main()
{

         int b;
         clrscr();
         b=A*A;
         printf("square =%d",b);
         getch();
}    

Output
square =100

In macro we can also pass argument inside the macro. It work as function , which are defined before using it.

WAP a program to swap two numbers without using third variable and by using macro.

#include<stdio.h>
#include<conio.h>
#define swap(x,y)x=x+y;y=x-y;x=x-y
void main()
{
          int a,b;
          clrscr();
          printf("enter the value of a");
          scanf("%d",&a);
          printf("enter the value of b");
          scanf("%d",&b);
          swap(a,b);
          printf("\n now value of a=%d",a);
          printf("\n now value of b=%d",b);
          getch();
}

Output :-




Typedef

Defination:- Typedef means to rename the existing data type. With help of "typedef" we can give the new name to inbuilt data type structure typedef declaration does not  declared any new datatype which does not exist in c Language. Typedef means to create new definition name of an existing data type

Syntax 
typedef <datatype> <new datatype> 

Ex int harsh;

#include<stdio.h>
#include<conio.h>
void main()
{
      typedef int harsh;
      clrscr();
      harsh a,b,c;
      printf("enter the two number");
      scanf("%d%d",&a,&b);
      c=a+b;
      printf("%d",c);
      getch();
}

Command Line Arguments

Command Line argument is another way to inbuilt the values into our program using console instead  of using scanf() function. In c mostly we use scanf() to input values into the program. Command line arguments are the values passed into the main function from command line (Dos prompt). We can pass two arguments to the main function:

1) argc: integer type argument
indicate the number of parameters passed

2)argv array of string
Each string in this array will represent a parameter that is passed to main()

Example:

#include<stdio.h>
#include<conio.h>
void main(int argc,char *argv[])
{
int i;
clrscr();
for(i=1;i<argc;i++)
{
printf("%s",argv[i]);
}
getch();



Steps to execute the program

1) After typing the program save the file with .c extenshion.


2)Now Compile the program using ALT+F9.


3)Now click on file menu and select "Dos shell" Option.


4)now first type the file name and then type number arguments then press enter key.




5)come to back type "exit" and enter





0 comments:

Post a Comment