Wednesday, 1 March 2017

How to convert C++ program to C language

How to Convert C++ program to C Language 

Method to convert C++ program to C Language

Step-1: Replace header file  iostream.h to stdio.h,

Step-2: Replace all cout<< to printf() and also use the datatype specifier for the variable as follows:
              integer        - %d
              character     - %c
              float             - %f
              string            - %s
              double          - %ld
            Let see an example:
     
            code of C++ : cout<<"\n value of a ="<<a;
            similar code in C: printf("\n value of a = %d",a);

            code of C++ : cout<<"\n my name is ="<<name;
            similar code in C: printf("\n my name is = %s",name);

            code of C++ : cout<<"\n radius ="<<radius;
            similar code in C: printf("\n radius = %f",radius);

            code of C++ : cout<<"\n input character is vowel ="<<ch;
            similar code in C: printf("\n input character is vowel = %c",ch);

Step-3: Replace all cin>> to scanf() and also use the datatype specifier for the variable as given above and associate & symbol with variable name:
 Let see some examples:    

            code of C++ : cin >> a; // integer
            similar code in C: scanf("%d",&a);

            code of C++ : cin>>name; // string
            similar code in C: scanf("%s",name);  // & is optional in strings

            code of C++ : cin>>radius; //
            similar code in C: scanf("%f",&radius);

            code of C++ : cin>>ch;  // character
            similar code in C: scanf("%c",&ch);

Step-4: if any data type variables are declared in side the program, it must be declare in the fisrt statement of main and other functions.

Step-5: Replace extension of .C with .CPP

Step-6: Program is ready for compile and execution.

Note: This steps are made on the basis of simple programs on Turbo-C++, it may not work on different compilers or advance program of C++.

No comments:

Post a Comment