Swap Two Numbers in C

  Swap Two Numbers in C In this example, you will learn to swap two numbers in C programming using two different techniques. To understand this example, you should have the knowledge of the following C programming topics: C Data Types C Programming Operators C Input Output (I/O) Swap Numbers Using Temporary Variable # include <stdio.h> int main () { double first, second, temp; printf ( "Enter first number: " ); scanf ( "%lf" , &first); printf ( "Enter second number: " ); scanf ( "%lf" , &second); // Value of first is assigned to temp temp = first; // Value of second is assigned to first first = second; // Value of temp (initial value of first) is assigned to second second = temp; printf ( "\nAfter swapping, firstNumber = %.2lf\n" , first); printf ( "After swapping, secondNumber = %.2lf" , second); return 0 ; } Output ...

C Program to Display its own Source Code as Output

 

C Program to Display its own Source Code as Output

In this example, you'll learn to display source of the program using __FILE__ macro.

To understand this example, you should have the knowledge of the following C programming topics:

  • C Preprocessor and Macros
  • C File Handling

Though this problem seems complex, the concept behind this program is straightforward; display the content from the same file you are writing the source code.

Procedure to display its own source code in C programming

In C programming, there is a predefined macro named __FILE__ that gives the name of the current input file.

#include <stdio.h>
int main() {

   // location the current input file.
   printf("%s",__FILE__);
}

C program to display its own source code

#include <stdio.h>
int main() {
    FILE *fp;
    int c;
   
    // open the current input file
    fp = fopen(__FILE__,"r");

    do {
         c = getc(fp);   // read character 
         putchar(c);     // display character
    }
    while(c != EOF);  // loop until the end of file is reached
    
    fclose(fp);
    return 0;
}

Terminal Code :


Output :

Comments

Popular posts from this blog

Add Two Integers in C language

Find G.C.D Using Recursion in C