SOME BASIC EXAMPLES OF C PROGRAM

 



The C programming language is a standardized programming language developed in the early 1970s by Ken Thompson and Dennis Ritchie for use on the UNIX operating system. It has since spread to many other operating systems, and is one of the most widely used programming languages. C is prized for its efficiency, and is the most popular programming language for writing system software, though it is also used for writing applications.


SOME BASIC EXAMPLES OF C PROGRAM

 

Example 1 - C hello world program
/* A very simple c program printing a string on screen*/

#include <stdio.h>

Void Main()

{

    printf("Hello World\n");

    getch();

}

 

 

Example 2 - c program to take input from user using scanf

#include <stdio.h>

Void Main()

{

   int number;

 

   printf("Enter an integer\n");

   scanf("%d",&number);

 

   printf("Integer entered by you is %d\n", number);

 

   getch();

}

 

Example 3 - using if else control instructions

#include <stdio.h>

Void Main()

{

   int x = 1;

 

   if ( x == 1 )

      printf("x is equal to one.\n");

   else

      printf("For comparison use == as = is the assignment operator.\n");

 

   getch();

}

 

Example 4 - loop example

#include <stdio.h>

Void Main()

{

   int value = 1;

 

   while(value<=3)

   {

      printf("Value is %d\n", value);

      value++;

   }

 

   getch();

}

 

Example 5 - c program for prime number

#include <stdio.h>

Void Main()

{

   int n, c;

 

   printf("Enter a number\n");

   scanf("%d", &n);

 

   if ( n == 2 )

      printf("Prime number.\n");

   else

   {

       for ( c = 2 ; c <= n - 1 ; c++ )

       {

           if ( n % c == 0 )

              break;

       }

       if ( c != n )

          printf("Not prime.\n");

       else

          printf("Prime number.\n");

   }

   getch();

}


Example 6 - command line arguments

#include <stdio.h>

Void Main(int argc, char *argv[])

{

   int c;

 

   printf("Number of command line arguments passed: %d\n", argc);

 

   for ( c = 0 ; c < argc ; c++)

      printf("%d. Command line argument passed is %s\n", c+1, argv[c]);

 

   getch();

}

Above c program prints the number and all arguments which are passed to it.

 

Example 7 - Array program

#include <stdio.h>

Void Main()

{

    int array[100], n, c;

 

    printf("Enter the number of elements in array\n");

    scanf("%d", &n);

 

    printf("Enter %d elements\n", n);

 

    for ( c = 0 ; c < n ; c++ )

        scanf("%d", &array[c]);

 

    printf("Array elements entered by you are:\n");

 

    for ( c = 0 ; c < n ; c++ )

        printf("array[%d] = %d\n", c, array[c]);

 

    getch();

}

 

Example 8 - Function program

#include <stdio.h>

void my_function();

Void Main()

{

   printf("Void Main function.\n");

 

   my_function();

 

   printf("Back in function Void Main.\n");

 

   getch();

}

 

void my_function()

{

   printf("Welcome to my function. Feel at home.\n");

}

 

Example 9 - Using comments in a program

#include <stdio.h>

Void Main()

{

   // Single line comment in c source code

 

   printf("Writing comments is very useful.\n");

 

   /*

    * Multi line comment syntax

    * Comments help us to understand code later easily.

    * Will you write comments while developing programs ?

    */

 

   printf("Good luck c programmer.\n");

 

   getch();

}

 

Example 10 - using structures in c programming

#include <stdio.h>

struct programming

{

    float constant;

    char *pointer;

};

 

Void Main()

{

   struct programming variable;

   char string[] = "Programming in Software Development.";  

 

   variable.constant = 1.23;

   variable.pointer = string;

 

   printf("%f\n", variable.constant);

   printf("%s\n", variable.pointer);

 

   getch();

}

 

Example 11 - c program for Fibonacci series

#include <stdio.h>

Void Main()

{

   int n, first = 0, second = 1, next, c;

 

   printf("Enter the number of terms\n");

   scanf("%d",&n);

 

   printf("First %d terms of Fibonacci series are :-\n",n);

 

   for ( c = 0 ; c < n ; c++ )

   {

      if ( c <= 1 )

         next = c;

      else

      {

         next = first + second;

         first = second;

         second = next;

      }

      printf("%d\n",next);

   }

 

   getch();

}

No comments:

Post a Comment