C language all program
Example 1 - C hello world program
/** My first C program */
/** My first C program */
- #include <stdio.h>
- int main()
- {
- printf("Hello World\n");
- return 0;
- }
======================================================
Output of program:
"Hello World"
- #include <stdio.h>
- int main()
- {
- int x;
- printf("Input an integer\n");
- scanf("%d", &x); // %d is used for an integer
- printf("The integer is: %d\n", x);
- return 0;
- }
Output:
Input an integer
7897
The integer is: 7897
Input an integer
7897
The integer is: 7897
=====================================================
Example 3 - using if else control instructions
- #include <stdio.h>
- int 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");
- return 0;
- }
Output:
x is equal to one.
x is equal to one.
=====================================================
Example 4 - while loop example
- #include <stdio.h>
- int main()
- {
- int c = 1; // Initializing variable
- while (c <= 10) // While loop will execute till the condition is true
- {
- printf("%d ", c); // Note the space after %d for gap in the numbers we want in output
- c++;
- }
- return 0;
- }
Output:
1 2 3 4 5 6 7 8 9 10
1 2 3 4 5 6 7 8 9 10
=====================================================
Example 5 - C program check if an integer is prime or not
- #include <stdio.h>
- int 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");
- }
- return 0;
- }
- ============================================================
Example 6 - command line arguments
- #include <stdio.h>
- int 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 argument is %s\n", c + 1, argv[c]);
- return 0;
- }
- =============================================================
This program prints the number of arguments passed, and the arguments which are passed to it.
Example 7 - Array program
- #include <stdio.h>
- int main()
- {
- int array[100], n, c;
- printf("Enter 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("The array elements are:\n");
- for (c = 0; c < n; c++)
- printf("%d\n", array[c]);
- return 0;
- }
Example 8 - function program
- #include <stdio.h>
- void my_function(); // Declaring a function
- int main()
- {
- printf("Main function.\n");
- my_function(); // Calling the function
- printf("Back in function main.\n");
- return 0;
- }
- ============================================================
- // Defining the function
- void my_function()
- {
- printf("Welcome to my function. Feel at home.\n");
- }
- ====================================================
Example 9 - Using comments in a program
- #include <stdio.h>
- int main()
- {
- // Single line comment in a C program
- printf("Writing comments is very useful.\n");
- /*
- * Multi-line comment syntax
- * Comments help us to understand program later easily.
- * Will you write comments while writing programs?
- */
- printf("Good luck C programmer.\n");
- return 0;
- }
- ==========================================================
Example 10 - using structures in C programming
- #include <stdio.h>
- #include <string.h>
- struct game
- {
- char game_name[50];
- int number_of_players;
- }; // Note the semicolon
- int main()
- {
- struct game g;
- strcpy(g.game_name, "Cricket");
- g.number_of_players = 11;
- printf("Name of game: %s\n", g.game_name);
- printf("Number of players: %d\n", g.number_of_players);
- return 0;
- }
- =============================================================
Example 11 - C program for Fibonacci series
- #include <stdio.h>
- int 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);
- }
- return 0;
- }
- ==========================================================
Example 12 - C graphics programming
- #include <graphics.h>
- #include <conio.h>
- int main()
- {
- int gd = DETECT, gm;
- initgraph(&gd, &gm,"C:\\TC\\BGI");
- outtextxy(10, 20, "Graphics programming is fun!");
- circle(200, 200, 50);
- setcolor(BLUE);
- line(350, 250, 450, 50);
- getch();
- closegraph( );
- return 0;
- }
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
- #include <stdio.h>
- #include <conio.h>
- int main()
- {
- int c;
- for (c = 1; c <= 10; c++)
- printf("%d\n", c);
- getch();
- return 0;
- }
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:- #include <stdio.h>
- int main()
- {
- int c;
- /* for loop */
- for (c = 1; c <= 10; c++)
- printf("%d\n", c);
- return 0;
- }vvExample 1 - C hello world program/** My first C program */
- #include <stdio.h>
- int main()
- {
- printf("Hello World\n");
- return 0;
- }
"Hello World" - #include <stdio.h>
- int main()
- {
- int x;
- printf("Input an integer\n");
- scanf("%d", &x); // %d is used for an integer
- printf("The integer is: %d\n", x);
- return 0;
- }
Output:
Input an integer
7897
The integer is: 7897Example 3 - using if else control instructions- #include <stdio.h>
- int 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");
- return 0;
- }
Output:
x is equal to one.Example 4 - while loop example- #include <stdio.h>
- int main()
- {
- int c = 1; // Initializing variable
- while (c <= 10) // While loop will execute till the condition is true
- {
- printf("%d ", c); // Note the space after %d for gap in the numbers we want in output
- c++;
- }
- return 0;
- }
Output:
1 2 3 4 5 6 7 8 9 10Example 5 - C program check if an integer is prime or not- #include <stdio.h>
- int 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");
- }
- return 0;
- }
Example 6 - command line arguments- #include <stdio.h>
- int 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 argument is %s\n", c + 1, argv[c]);
- return 0;
- }
This program prints the number of arguments passed, and the arguments which are passed to it.Example 7 - Array program- #include <stdio.h>
- int main()
- {
- int array[100], n, c;
- printf("Enter 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("The array elements are:\n");
- for (c = 0; c < n; c++)
- printf("%d\n", array[c]);
- return 0;
- }
Example 8 - function program- #include <stdio.h>
- void my_function(); // Declaring a function
- int main()
- {
- printf("Main function.\n");
- my_function(); // Calling the function
- printf("Back in function main.\n");
- return 0;
- }
- // Defining the function
- void my_function()
- {
- printf("Welcome to my function. Feel at home.\n");
- }
Example 9 - Using comments in a program- #include <stdio.h>
- int main()
- {
- // Single line comment in a C program
- printf("Writing comments is very useful.\n");
- /*
- * Multi-line comment syntax
- * Comments help us to understand program later easily.
- * Will you write comments while writing programs?
- */
- printf("Good luck C programmer.\n");
- return 0;
- }
Example 10 - using structures in C programming- #include <stdio.h>
- #include <string.h>
- struct game
- {
- char game_name[50];
- int number_of_players;
- }; // Note the semicolon
- int main()
- {
- struct game g;
- strcpy(g.game_name, "Cricket");
- g.number_of_players = 11;
- printf("Name of game: %s\n", g.game_name);
- printf("Number of players: %d\n", g.number_of_players);
- return 0;
- }
Example 11 - C program for Fibonacci series- #include <stdio.h>
- int 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);
- }
- return 0;
- }
Example 12 - C graphics programming- #include <graphics.h>
- #include <conio.h>
- int main()
- {
- int gd = DETECT, gm;
- initgraph(&gd, &gm,"C:\\TC\\BGI");
- outtextxy(10, 20, "Graphics programming is fun!");
- circle(200, 200, 50);
- setcolor(BLUE);
- line(350, 250, 450, 50);
- getch();
- closegraph( );
- return 0;
- }
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- #include <stdio.h>
- #include <conio.h>
- int main()
- {
- int c;
- for (c = 1; c <= 10; c++)
- printf("%d\n", c);
- getch();
- return 0;
- }
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:- #include <stdio.h>
- int main()
- {
- int c;
- /* for loop */
- for (c = 1; c <= 10; c++)
- printf("%d\n", c);
- return 0;
- }
- ===================================================
Comments
Post a Comment