You are here

C programs

C programs with output illustrate various programming concepts - operators, loops, functions, single and double dimensional arrays, performing operations on strings, files, pointers, etc. Download executable files and execute them without compiling the source file. Code::Blocks IDE is used to write programs, most of these will work with GCC and Dev C++ compilers. The first program prints "Hello World" on output device.

C programming examples with output

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"

Example 2 - C program to get input from a user using scanf

  1. #include <stdio.h>
  2.  
  3. int main()
  4. {
  5.   int x;
  6.  
  7.   printf("Input an integer\n");
  8.   scanf("%d", &x); // %d is used for an integer
  9.  
  10.   printf("The integer is: %d\n", x);
  11.  
  12.   return 0;
  13. }

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 (x == 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 (c <= 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 (n == 2)
  11.     printf("Prime number.\n");
  12.   else
  13.   {
  14.     for (c = 2; c <= n - 1; c++)
  15.     {
  16.       if (n % c == 0)
  17.         break;
  18.     }
  19.     if (c != 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 (c = 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 (c = 0; c < n; c++)
  13.         scanf("%d", &array[c]);
  14.    
  15.     printf("The array elements are:\n");
  16.    
  17.     for (c = 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 (c = 0; c < n; c++)
  13.   {
  14.     if (c <= 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 (c = 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 (c = 1; c <= 10; c++)
  10.         printf("%d\n", c);
  11.    
  12.     return 0;
  13. }

If using GCC then save the program in a file say “numbers.c” to compile the program open the terminal and enter the command “gcc numbers.c” this will compile the program and to execute the program enter the command “./a.out” do not use quotes while executing commands. You can specify the output file name as: gcc numbers.c -o numbers.out, to run execute ./numbers.out in the terminal.

C programming tutorial

A C program consists of functions and declarations or instructions given to the computer to perform a particular task. The process of writing a program involves designing the algorithm, a flowchart can be drawn, and then writing the source code, after developing the program you need to test it and debug it if it does not meet the requirement.

To make a program, you need a text editor and a compiler. You can use any text editor of your choice and a compiler. C compiler converts source code into machine code that consists of zero's, and one's only, and execution is done on a machine.

An IDE (Integrated Development Environment) provides a text editor, compiler, debugger, etc. for developing programs and managing projects. Code::Blocks IDE provides an ideal environment for development. It can import Microsoft Visual C++ projects, extendable as it uses plug-ins, open source, and cross-platform.

How to write a C program?

A C program must have at least one function which must be main. A function consists of declarations and statements, a statement is an expression followed by a semicolon, for example, a + b, printf("C program examples") are expressions and a + b; and printf("C is an easy to learn computer programming language."); are statements.

To use a variable, we must indicate its type whether it is an integer, float, character, etc. C language has many built-in data types, and we can make our own using structures and unions. Every data type has its size that may depend on the machine; for example, an integer may be of 2 or 4 Bytes. Data is stored in a binary form, i.e., group of bits where each bit may be '0' or '1'.

Keywords such as "switch," "case," "default," "register," etc. are special words with predefined meaning and can't be used for other purposes such as a variable name. Memory can be allocated at compile time or run-time using malloc and calloc functions. C language has many features such as recursion, preprocessor, conditional compilation, portability, pointers, multi-threading by using external libraries, dynamic memory allocation due to which it is used for making portable software programs and applications. Networking API's are available using which computer users can communicate and interact with each other, share files, etc.

C standard library contains functions for mathematical operations, character, input/output and time. The process of making a program which is known as coding requires knowledge of programming language and logic to achieve desired output. So you should learn C programming basics and start making programs.

Learning data structures such as stacks, queues, linked lists, etc. using C programming provides you a greater understanding as you learn everything in detail. A general belief is to go for other high-level languages, but it's a good idea to learn C before learning C++ or Java. C++ programming language is object-oriented, and it contains all the features of C language so learning C first will help you to learn C++ easily and then you can go for Java.

C programming PDF

C programming books

If you are a beginner, then buy anyone of first two books mentioned below, and if you have previous programming experience or you know basics of C language, then you can buy the third one.

  • Let Us C By Yashavant Kanetkar
  • PROGRAMMING WITH C By Byron Gottfried, Jitender Chhabra
  • The C Programming By Brian Kernighan and Dennis Ritchie