Skip to main content

C language all program

C language all program
Example 1 - C hello world program
/** My first C program */
  1. #include <stdio.h>
  2.  
  3. int main()
  4. {
  5.   printf("Hello World\n");
  6.   return 0;
  7. }


Output of program:
"Hello World"



  1.  #include <stdio.h>
  2. int main()
  3. {
  4.   int x;
  5.  
  6.   printf("Input an integer\n");
  7.   scanf("%d", &x); // %d is used for an integer
  8.  
  9.   printf("The integer is: %d\n", x);
  10.  
  11.   return 0;
  12. }
Output:
Input an integer
7897
The integer is: 7897
=====================================================
Example 3 - using if else control instructions
  1. #include <stdio.h>
  2.  
  3. int main()
  4. {
  5.    int x = 1;
  6.  
  7.    if (== 1)
  8.       printf("x is equal to one.\n");
  9.    else
  10.       printf("For comparison use '==' as '=' is the assignment operator.\n");
  11.  
  12.    return 0;
  13. }
Output:
x is equal to one.
=====================================================
Example 4 - while loop example
  1. #include <stdio.h>
  2.  
  3. int main()
  4. {
  5.   int c = 1;  // Initializing variable
  6.  
  7.   while (<= 10)  // While loop will execute till the condition is true
  8.   {
  9.     printf("%d ", c);  // Note the space after %d for gap in the numbers we want in output
  10.     c++;
  11.   }
  12.  
  13.   return 0;
  14. }
Output:
1 2 3 4 5 6 7 8 9 10
=====================================================
Example 5 - C program check if an integer is prime or not
  1. #include <stdio.h>
  2.  
  3. int main()
  4. {
  5.   int n, c;
  6.  
  7.   printf("Enter a number\n");
  8.   scanf("%d", &n);
  9.  
  10.   if (== 2)
  11.     printf("Prime number.\n");
  12.   else
  13.   {
  14.     for (= 2; c <= n - 1; c++)
  15.     {
  16.       if (% c == 0)
  17.         break;
  18.     }
  19.     if (!= n)
  20.       printf("Not prime.\n");
  21.      else
  22.        printf("Prime number.\n");
  23.   }
  24.   return 0;
  25. }
  26. ============================================================
Example 6 - command line arguments
  1. #include <stdio.h>
  2.  
  3. int main(int argc, char *argv[])
  4. {
  5.   int c;
  6.  
  7.   printf("Number of command line arguments passed: %d\n", argc);
  8.  
  9.   for (= 0; c < argc; c++)
  10.     printf("%d argument is %s\n", c + 1, argv[c]);
  11.  
  12.   return 0;
  13. }
  14. =============================================================
This program prints the number of arguments passed, and the arguments which are passed to it.
Example 7 - Array program
  1. #include <stdio.h>
  2.  
  3. int main()
  4. {
  5.     int array[100], n, c;
  6.    
  7.     printf("Enter number of elements in array\n");
  8.     scanf("%d", &n);
  9.    
  10.     printf("Enter %d elements\n", n);
  11.    
  12.     for (= 0; c < n; c++)
  13.         scanf("%d", &array[c]);
  14.    
  15.     printf("The array elements are:\n");
  16.    
  17.     for (= 0; c < n; c++)
  18.         printf("%d\n", array[c]);
  19.    
  20.     return 0;
  21. }
Example 8 - function program
  1. #include <stdio.h>
  2.  
  3. void my_function();  // Declaring a function
  4.  
  5. int main()
  6. {
  7.   printf("Main function.\n");
  8.  
  9.   my_function();  // Calling the function
  10.  
  11.   printf("Back in function main.\n");
  12.  
  13.   return 0;
  14. }
  15. ============================================================
  16.  
  17. // Defining the function
  18. void my_function()
  19. {
  20.   printf("Welcome to my function. Feel at home.\n");
  21. }
  22. ====================================================
Example 9 - Using comments in a program
  1. #include <stdio.h>
  2.  
  3. int main()
  4. {
  5.    // Single line comment in a C program
  6.  
  7.    printf("Writing comments is very useful.\n");
  8.  
  9.    /*
  10.     * Multi-line comment syntax
  11.     * Comments help us to understand program later easily.
  12.     * Will you write comments while writing programs?
  13.     */
  14.  
  15.    printf("Good luck C programmer.\n");
  16.  
  17.    return 0;
  18. }
  19. ==========================================================
Example 10 - using structures in C programming
  1. #include <stdio.h>
  2. #include <string.h>
  3.  
  4. struct game
  5. {
  6.   char game_name[50];
  7.   int number_of_players;
  8. };  // Note the semicolon
  9.  
  10. int main()
  11. {
  12.   struct game g;
  13.  
  14.   strcpy(g.game_name, "Cricket");
  15.   g.number_of_players = 11;
  16.  
  17.   printf("Name of game: %s\n", g.game_name);
  18.   printf("Number of players: %d\n", g.number_of_players);
  19.  
  20.   return 0;
  21. }
  22. =============================================================

Example 11 - C program for Fibonacci series
  1. #include <stdio.h>
  2.  
  3. int main()
  4. {
  5.   int n, first = 0, second = 1, next, c;
  6.  
  7.   printf("Enter the number of terms\n");
  8.   scanf("%d", &n);
  9.  
  10.   printf("First %d terms of Fibonacci series are:\n", n);
  11.  
  12.   for (= 0; c < n; c++)
  13.   {
  14.     if (<= 1)
  15.       next = c;
  16.     else
  17.     {
  18.       next = first + second;
  19.       first = second;
  20.       second = next;
  21.     }
  22.     printf("%d\n", next);
  23.   }
  24.  
  25.   return 0;
  26. }
  27. ==========================================================
Example 12 - C graphics programming
  1. #include <graphics.h>
  2. #include <conio.h>
  3.  
  4. int main()
  5. {
  6.     int gd = DETECT, gm;
  7.    
  8.     initgraph(&gd, &gm,"C:\\TC\\BGI");
  9.    
  10.     outtextxy(10, 20, "Graphics programming is fun!");
  11.    
  12.     circle(200, 200, 50);
  13.    
  14.     setcolor(BLUE);
  15.    
  16.     line(350, 250, 450, 50);
  17.    
  18.     getch();
  19.     closegraph( );
  20.     return 0;
  21. }

How to compile C programs with GCC compiler?

If you are using GCC on Linux operating system, then you may need to modify the programs. For example, consider the following program which prints first ten natural numbers
  1. #include <stdio.h>
  2. #include <conio.h>
  3.  
  4. int main()
  5. {
  6.     int c;
  7.  
  8.     for (= 1; c <= 10; c++)
  9.         printf("%d\n", c);
  10.    
  11.     getch();
  12.     return 0;
  13. }
Above program includes a header file <conio.h> and uses function getch, but this file is Borland specific, so it works in Turbo C compiler but not in GCC. The program for GCC should be like:
  1. #include <stdio.h>
  2.  
  3. int main()
  4. {
  5.     int c;
  6.  
  7.     /* for loop */
  8.  
  9.     for (= 1; c <= 10; c++)
  10.         printf("%d\n", c);
  11.    
  12.     return 0;
  13. }vvExample 1 - C hello world program
    /** My first C program */
    1. #include <stdio.h>
    2.  
    3. int main()
    4. {
    5.   printf("Hello World\n");
    6.   return 0;
    7. }
    Output of program:
    "Hello World"
    1.  #include <stdio.h>
    2. int main()
    3. {
    4.   int x;
    5.  
    6.   printf("Input an integer\n");
    7.   scanf("%d", &x); // %d is used for an integer
    8.  
    9.   printf("The integer is: %d\n", x);
    10.  
    11.   return 0;
    12. }
    Output:
    Input an integer
    7897
    The integer is: 7897
    Example 3 - using if else control instructions
    1. #include <stdio.h>
    2.  
    3. int main()
    4. {
    5.    int x = 1;
    6.  
    7.    if (== 1)
    8.       printf("x is equal to one.\n");
    9.    else
    10.       printf("For comparison use '==' as '=' is the assignment operator.\n");
    11.  
    12.    return 0;
    13. }
    Output:
    x is equal to one.
    Example 4 - while loop example
    1. #include <stdio.h>
    2.  
    3. int main()
    4. {
    5.   int c = 1;  // Initializing variable
    6.  
    7.   while (<= 10)  // While loop will execute till the condition is true
    8.   {
    9.     printf("%d ", c);  // Note the space after %d for gap in the numbers we want in output
    10.     c++;
    11.   }
    12.  
    13.   return 0;
    14. }
    Output:
    1 2 3 4 5 6 7 8 9 10
    Example 5 - C program check if an integer is prime or not
    1. #include <stdio.h>
    2.  
    3. int main()
    4. {
    5.   int n, c;
    6.  
    7.   printf("Enter a number\n");
    8.   scanf("%d", &n);
    9.  
    10.   if (== 2)
    11.     printf("Prime number.\n");
    12.   else
    13.   {
    14.     for (= 2; c <= n - 1; c++)
    15.     {
    16.       if (% c == 0)
    17.         break;
    18.     }
    19.     if (!= n)
    20.       printf("Not prime.\n");
    21.      else
    22.        printf("Prime number.\n");
    23.   }
    24.   return 0;
    25. }
    Example 6 - command line arguments
    1. #include <stdio.h>
    2.  
    3. int main(int argc, char *argv[])
    4. {
    5.   int c;
    6.  
    7.   printf("Number of command line arguments passed: %d\n", argc);
    8.  
    9.   for (= 0; c < argc; c++)
    10.     printf("%d argument is %s\n", c + 1, argv[c]);
    11.  
    12.   return 0;
    13. }
    This program prints the number of arguments passed, and the arguments which are passed to it.
    Example 7 - Array program
    1. #include <stdio.h>
    2.  
    3. int main()
    4. {
    5.     int array[100], n, c;
    6.    
    7.     printf("Enter number of elements in array\n");
    8.     scanf("%d", &n);
    9.    
    10.     printf("Enter %d elements\n", n);
    11.    
    12.     for (= 0; c < n; c++)
    13.         scanf("%d", &array[c]);
    14.    
    15.     printf("The array elements are:\n");
    16.    
    17.     for (= 0; c < n; c++)
    18.         printf("%d\n", array[c]);
    19.    
    20.     return 0;
    21. }
    Example 8 - function program
    1. #include <stdio.h>
    2.  
    3. void my_function();  // Declaring a function
    4.  
    5. int main()
    6. {
    7.   printf("Main function.\n");
    8.  
    9.   my_function();  // Calling the function
    10.  
    11.   printf("Back in function main.\n");
    12.  
    13.   return 0;
    14. }
    15.  
    16. // Defining the function
    17. void my_function()
    18. {
    19.   printf("Welcome to my function. Feel at home.\n");
    20. }
    Example 9 - Using comments in a program
    1. #include <stdio.h>
    2.  
    3. int main()
    4. {
    5.    // Single line comment in a C program
    6.  
    7.    printf("Writing comments is very useful.\n");
    8.  
    9.    /*
    10.     * Multi-line comment syntax
    11.     * Comments help us to understand program later easily.
    12.     * Will you write comments while writing programs?
    13.     */
    14.  
    15.    printf("Good luck C programmer.\n");
    16.  
    17.    return 0;
    18. }
    Example 10 - using structures in C programming
    1. #include <stdio.h>
    2. #include <string.h>
    3.  
    4. struct game
    5. {
    6.   char game_name[50];
    7.   int number_of_players;
    8. };  // Note the semicolon
    9.  
    10. int main()
    11. {
    12.   struct game g;
    13.  
    14.   strcpy(g.game_name, "Cricket");
    15.   g.number_of_players = 11;
    16.  
    17.   printf("Name of game: %s\n", g.game_name);
    18.   printf("Number of players: %d\n", g.number_of_players);
    19.  
    20.   return 0;
    21. }
    Example 11 - C program for Fibonacci series
    1. #include <stdio.h>
    2.  
    3. int main()
    4. {
    5.   int n, first = 0, second = 1, next, c;
    6.  
    7.   printf("Enter the number of terms\n");
    8.   scanf("%d", &n);
    9.  
    10.   printf("First %d terms of Fibonacci series are:\n", n);
    11.  
    12.   for (= 0; c < n; c++)
    13.   {
    14.     if (<= 1)
    15.       next = c;
    16.     else
    17.     {
    18.       next = first + second;
    19.       first = second;
    20.       second = next;
    21.     }
    22.     printf("%d\n", next);
    23.   }
    24.  
    25.   return 0;
    26. }
    Example 12 - C graphics programming
    1. #include <graphics.h>
    2. #include <conio.h>
    3.  
    4. int main()
    5. {
    6.     int gd = DETECT, gm;
    7.    
    8.     initgraph(&gd, &gm,"C:\\TC\\BGI");
    9.    
    10.     outtextxy(10, 20, "Graphics programming is fun!");
    11.    
    12.     circle(200, 200, 50);
    13.    
    14.     setcolor(BLUE);
    15.    
    16.     line(350, 250, 450, 50);
    17.    
    18.     getch();
    19.     closegraph( );
    20.     return 0;
    21. }

    How to compile C programs with GCC compiler?

    If you are using GCC on Linux operating system, then you may need to modify the programs. For example, consider the following program which prints first ten natural numbers
    1. #include <stdio.h>
    2. #include <conio.h>
    3.  
    4. int main()
    5. {
    6.     int c;
    7.  
    8.     for (= 1; c <= 10; c++)
    9.         printf("%d\n", c);
    10.    
    11.     getch();
    12.     return 0;
    13. }
    Above program includes a header file <conio.h> and uses function getch, but this file is Borland specific, so it works in Turbo C compiler but not in GCC. The program for GCC should be like:
    1. #include <stdio.h>
    2.  
    3. int main()
    4. {
    5.     int c;
    6.  
    7.     /* for loop */
    8.  
    9.     for (= 1; c <= 10; c++)
    10.         printf("%d\n", c);
    11.    
    12.     return 0;
    13. }
  14. ===================================================

Comments

Popular posts from this blog

short key of c programming

short key of c programming Shortcut Keys(Hot Keys)     Operation    Alt + F  -            Open file Alt + F3 -           Close Alt + F + N  -    New file Alt + X  -           Exit turbo c Alt + F5 -          Output Screen Alt + F + L  -    Check programme load or not. Alt + ENTER  - Full screen Shift + Del -       Cut Shift + Insert -   Paste Ctr + Insert -    Copy Alt+F9 -           Compile a program Ctr + F8           Bbreak point Ctrl+F9 -          To run a program Ctrl+s -...

C LANGUAGE - OVERVIEW C - VARIABLES

C LANGUAGE - OVERVIEW Advertisements C is a general-purpose, high-level language that was originally developed by Dennis M. Ritchie to develop the UNIX operating system at Bell Labs. C was originally first implemented on the DEC PDP-11 computer in 1972. In 1978, Brian Kernighan and Dennis Ritchie produced the first publicly available description of C, now known as the K&R standard. The UNIX operating system, the C compiler, and essentially all UNIX application programs have been written in C. C has now become a widely used professional language for various reasons − Easy to learn Structured language It produces efficient programs It can handle low-level activities It can be compiled on a variety of computer platforms Facts about C C was invented to write an operating system called UNIX. C is a successor of B language which was introduced around the early 1970s. The language was formalized in 1988 by the American National Standard Institute  A N S I A N S ...

environment

It looks like you meant "environment." The environment refers to the surroundings or conditions in which a person, animal, or plant lives or operates. It encompasses various elements, including natural resources, ecosystems, climate, and human-made structures. Protecting and preserving the environment is crucial for the well-being of all living organisms on Earth. Is there anything specific you'd like to discuss or learn about regarding the environment?