You are here

C programs

C programs with output showing usage of operators, loops, functions, arrays, performing operations on strings, files, pointers. 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."


C programming examples with output


Example 1 - C hello world program
/** My first C program */

#include <stdio.h>&#13;
 &#13;
int main()&#13;
{&#13;
  printf("Hello World\n");&#13;
  return 0;&#13;
}</stdio.h>


Output of program:
"Hello World"

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

&#13;
#include <stdio.h>&#13;
&#13;
int main()&#13;
{&#13;
  int x;&#13;
&#13;
  printf("Input an integer\n");&#13;
  scanf("%d", &amp;x); // %d is used for an integer&#13;
&#13;
  printf("The integer is: %d\n", x);&#13;
&#13;
  return 0;&#13;
}</stdio.h>


Output:
Input an integer
7897
The integer is: 7897



Example 3 - using if else control instructions

&#13;
#include <stdio.h>&#13;
&#13;
int main()&#13;
{&#13;
  int n;&#13;
&#13;
  printf("Enter a number\n");&#13;
  scanf("%d", &amp;n);&#13;
&#13;
  if (n &gt; 0)&#13;
    printf("Greater than zero.\n");&#13;
  else&#13;
    printf("Less than or equal to zero.\n");&#13;
&#13;
  return 0;&#13;
}</stdio.h>


Output:
Enter a number
-45
Less than or equal to zero.

Example 4 - while loop example

&#13;
#include <stdio.h>&#13;
 &#13;
int main()&#13;
{&#13;
  int c = 1;  // Initializing variable&#13;
 &#13;
  while (c &#13;
&#13;
Output:&#13;
1 2 3 4 5 6 7 8 9 10&#13;
&#13;
[block:block=106]&#13;
&#13;
Example 5 - C program check if an integer is prime or not&#13;
<c>&#13;
#include <stdio.h>&#13;
 &#13;
int main()&#13;
{&#13;
  int n, c;&#13;
 &#13;
  printf("Enter a number\n");&#13;
  scanf("%d", &amp;n);&#13;
 &#13;
  if (n == 2)&#13;
    printf("Prime number.\n");&#13;
  else&#13;
  {&#13;
    for (c = 2; c &#13;
&#13;
Example 6 - command line arguments&#13;
<c>&#13;
#include <stdio.h>&#13;
&#13;
int main(int argc, char *argv[])&#13;
{&#13;
  int c;&#13;
&#13;
  printf("Number of command line arguments passed: %d\n", argc);&#13;
&#13;
  for (c = 0; c &#13;
This program prints the number of arguments and their contents.&#13;
&#13;
Example 7 - Array program&#13;
<c>&#13;
#include <stdio.h>&#13;
&#13;
int main() &#13;
{&#13;
    int array[100], n, c;&#13;
    &#13;
    printf("Enter number of elements in array\n");&#13;
    scanf("%d", &amp;n);&#13;
    &#13;
    printf("Enter %d elements\n", n);&#13;
    &#13;
    for (c = 0; c &#13;
&#13;
Example 8 - function program&#13;
<c>&#13;
#include <stdio.h>&#13;
&#13;
void my_function();  // Declaring a function&#13;
&#13;
int main()&#13;
{&#13;
  printf("Main function.\n");&#13;
&#13;
  my_function();  // Calling the function&#13;
&#13;
  printf("Back in function main.\n");&#13;
&#13;
  return 0;&#13;
}&#13;
&#13;
// Defining the function&#13;
void my_function()&#13;
{&#13;
  printf("Welcome to my function. Feel at home.\n");&#13;
}</stdio.h>



Example 9 - Using comments in a program

&#13;
#include <stdio.h>&#13;
 &#13;
int main()&#13;
{&#13;
   // Single line comment in a C program&#13;
 &#13;
   printf("Writing comments is very useful.\n");&#13;
 &#13;
   /*&#13;
    * Multi-line comment syntax&#13;
    * Comments help us to understand a program later easily.&#13;
    * Will you write comments while writing programs?&#13;
    */
&#13;
 &#13;
   printf("Good luck C programmer.\n"); &#13;
 &#13;
   return 0;&#13;
}</stdio.h>



Example 10 - using structures in C programming

&#13;
#include <stdio.h>&#13;
#include <string.h>&#13;
&#13;
struct game&#13;
{&#13;
  char game_name[50];&#13;
  int number_of_players;&#13;
};  // Note the semicolon&#13;
&#13;
int main()&#13;
{&#13;
  struct game g;&#13;
&#13;
  strcpy(g.game_name, "Cricket");&#13;
  g.number_of_players = 11;&#13;
&#13;
  printf("Name of game: %s\n", g.game_name);&#13;
  printf("Number of players: %d\n", g.number_of_players);&#13;
&#13;
  return 0;&#13;
}</string.h></stdio.h>



Example 11 - C program for Fibonacci series

&#13;
#include <stdio.h>&#13;
&#13;
int main()&#13;
{&#13;
  int n, first = 0, second = 1, next, c;&#13;
&#13;
  printf("Enter the number of terms\n");&#13;
  scanf("%d", &amp;n);&#13;
&#13;
  printf("First %d terms of Fibonacci series are:\n", n);&#13;
&#13;
  for (c = 0; c &#13;
&#13;
Example 12 - C graphics programming&#13;
<c>&#13;
#include <graphics.h>&#13;
#include <conio.h>&#13;
&#13;
int main()&#13;
{&#13;
    int gd = DETECT, gm;&#13;
    &#13;
    initgraph(&amp;gd, &amp;gm,"C:\\TC\\BGI");&#13;
    &#13;
    outtextxy(10, 20, "Graphics programming is fun!");&#13;
    &#13;
    circle(200, 200, 50);&#13;
    &#13;
    setcolor(BLUE);&#13;
    &#13;
    line(350, 250, 450, 50);&#13;
    &#13;
    getch();&#13;
    closegraph( );&#13;
    return 0;&#13;
}</conio.h></graphics.h>


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 that prints the first ten natural numbers.

&#13;
#include <stdio.h>&#13;
#include <conio.h>&#13;
&#13;
int main()&#13;
{&#13;
    int c;&#13;
&#13;
    for (c = 1; c  &#13;
&#13;
The program includes a header file <c><conio.h></conio.h>

and calls the getch function, but this file is Borland specific, so it works in Turbo C compiler but not in GCC. The program for GCC must be like:

&#13;
#include <stdio.h>&#13;
&#13;
int main()&#13;
{&#13;
    int c;&#13;
&#13;
    /* for loop */&#13;
&#13;
    for (c = 1; c &#13;
&#13;
If you are using GCC, save the program in a file say "numbers.c" to compile the program, open the terminal and enter the command "gcc numbers.c", this compile the program and to execute it 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.&#13;
&#13;
<h2>C programming tutorial</h2>&#13;
A program consists of functions that contain instructions given to a machine to perform a task. The process of writing it includes designing an algorithm, drawing a flowchart, and then writing code. After writing it, you need to test it and debug it if it does not produce the required output.&#13;
&#13;
[block:block=107]&#13;
&#13;
To write a program, you need a text editor (use your favorite one) and a compiler. A compiler converts source code into machine code, which consists of zero's and one's only, ready to be executed on a machine. &#13;
&#13;
An IDE (Integrated Development Environment) provides a text editor, compiler, debugger, etc. for developing programs and managing projects. <a href="<a href="http://www.codeblocks.org/downloads/binaries/"">http://www.codeblocks.org/downloads/binaries/"</a> target="_blank" rel="nofollow" title="Download Code::Blocks IDE">Code::Blocks IDE</a> provides an ideal environment for development. It can import Microsoft Visual C++ projects, is extendable as it uses plug-ins, open-source, and cross-platform.&#13;
&#13;
<h2>How to write a C program?</h2>&#13;
A program must have at least a main function. 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. &#13;
&#13;
To use a variable, we must indicate its type, whether it is an integer, float, character, or others. C language has many built-in data types, and we can create ours 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., a group of bits where each bit can be '0' or '1'. &#13;
&#13;
Keywords such as "switch," "case," "default," "register,"  are reserved words with predefined meaning and can't be used as the name of a variable or a function. 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. Thanks to these, it is used for making portable software programs and applications. Using networking API's users can communicate and interact with each other and share files. &#13;
&#13;
C standard library contains functions for mathematical operations, characters, input/output, files, and many more. The process of writing a program known as coding requires knowledge of programming language and logic to achieve the desired output. So you should learn C programming basics and start making programs.&#13;
&#13;
Learning data structures (stacks, queues, linked lists, binary trees, graphs) using C provides you a greater understanding as you study everything in detail. A general belief is to go for high-level languages. However, it's a good idea to learn C before learning C++ or Java. C++ is object-oriented and contains all features of C, so learning C help you learn C++ quickly, then you can study Java.&#13;
&#13;
<h3>C programming PDF</h3>&#13;
<ul>&#13;
<li><a href="<a href="https://sourceforge.net/projects/orwelldevcpp/files/latest/download"">https://sourceforge.net/projects/orwelldevcpp/files/latest/download"</a> rel="nofollow" target="_blank" title="Download Dev C++ compiler">Dev C++ compiler</a></li>&#13;
<li><a href="<a href="http://cslibrary.stanford.edu/101/EssentialC.pdf"">http://cslibrary.stanford.edu/101/EssentialC.pdf"</a> target="_blank" rel="nofollow">Essential C</a></li>&#13;
</ul>&#13;
&#13;
<h3>C programming books</h3>&#13;
<ol>&#13;
<li>Let Us C By Yashavant Kanetkar</li>&#13;
<li>PROGRAMMING WITH C By Byron Gottfried, Jitender Chhabra</li>&#13;
<li>The C Programming By Brian Kernighan and Dennis Ritchie</li>&#13;
</ol>&#13;
If you are a beginner, buy any one of the first two books, and if you have previous programming experience or you know the basics of C language, buy the third one.</stdio.h>