0

C Programming tutorials


What is C Language?

C is a programming language. The C language was developed by Dennis Ritchie in 1972 at AT&T Bell Labs. It was called his newly developed language C simply because there was a B programming language already and the language led to the development of C Language. C language is based on ALGOL and B languages.
        
As said above C language is a programming language. Programming language is any language that computer system can understand directly or indirectly to can perform the actions asked by the programmer as set of instructions in form of a computer program.
C language is a high-level programming language. In the computer world, the further a programming language is from the computer architecture, the higher the language's level. You can imagine that the lowest-level languages are machine languages that computers understand directly. The high-level programming languages, on the other hand, are closer to our human languages.
A computer program written in a high-level language, such as C, Java, or Perl, is just a text file, consisting of English-like characters and words. We have to use some special programs, called compilers or interpreters, to translate such a program into a machine-readable code. That is, the text format of all instructions written in a high-level language has to be converted into the binary format. The code obtained after the translation is called binary code. Prior to the translation, a program in text format is called source code. The smallest unit of the binary code is called a bit (from binary digit), which can have a value of 0 or 1. 8 bits make up one byte, and half a byte (4 bits) is one nibble.

C programming codes

c program examples

Example 1 - C hello world program
/* A very simple c program printing a string on screen*/
#include <stdio.h>
 
main()
{
    printf("Hello World\n");
    return 0;
}
Output of above program:
"Hello World"
Example 2 - c program to take input from user using scanf
#include <stdio.h>
 
main()
{
   int number;
 
   printf("Enter an integer\n");
   scanf("%d",&number);
 
   printf("Integer entered by you is %d\n", number);
 
   return 0;
}
Output:
Enter a number
5
Number entered by you is 5
Example 3 - using if else control instructions
#include <stdio.h>
 
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 - loop example
#include <stdio.h>
 
main()
{
   int value = 1;
 
   while(value<=3)
   {
      printf("Value is %d\n", value);
      value++;
   }
 
   return 0;
}
Output:
Value is 1
Value is 2
Value is 3
Example 5 - c program for prime number
#include <stdio.h>
 
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>
 
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]);
 
   return 0;
}
Above c program prints the number and all arguments which are passed to it.
Example 7 - Array program
#include <stdio.h>
 
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]);
 
    return 0;
}
Example 8 - function program
#include <stdio.h>
 
void my_function();
 
main()
{
   printf("Main function.\n");
 
   my_function();
 
   printf("Back in function main.\n");
 
   return 0;
}
 
void my_function()
{
   printf("Welcome to my function. Feel at home.\n");
}
Example 9 - Using comments in a program
#include <stdio.h>
 
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"); 
 
   return 0;
}
Example 10 - using structures in c programming
#include <stdio.h>
 
struct programming
{
    float constant;
    char *pointer;
};
 
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);
 
   return 0;
}
Example 11 - c program for Fibonacci series
#include <stdio.h>
 
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>
 
main()
{
    int gd = DETECT, gm;
 
    initgraph(&gd, &gm,"C:\\TC\\BGI");
 
    outtextxy(10,20, "Graphics source code example.");
 
    circle(200, 200, 50);
 
    setcolor(BLUE);
 
    line(350, 250, 450, 50);
 
    getch();
    closegraph( );
    return 0;
}

For GCC users

If you are using GCC on Linux operating system then you need to modify 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 source code 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. So the code 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;
}
If using GCC then save the code in a file say numbers.c, to compile the program open the terminal and enter command GCC numbers.c, this will compile the program and to execute the program enter command ./a.out .
Frequently asked c programs in interview

L.C.M and H.C.F.

Swapping

Conversion ( Number System )

6. Write a c program to convert octal number to hexadecimal number.
8. Write a c program to convert hexadecimal number to octal number.
9. Write a c program to convert hexadecimal number to decimal number.
10. Write a c program to convert binary number to octal number.

Conversion ( Unit )


String

3. Write a c program to delete the all consonants from given string.
Matrix

File

2.  Write a c program to delete a file.
3. Write a c program to copy a file from one location to other location.
6. Write a c program which writes string in the file.
Complex number
3. Write a c program for multiplication of two complex numbers.
4. Write a c program for division two complex numbers.

Series

6. Write a c program to find out the sum of given H.P.
Array

Sorting

5. Write a c program for heap sort.
7. Write a c program for shell sort.

Recursion

Size of data type

Using pointer

Searching

Area and volume

6. Write a c program to find the area of trapezium.     
7. Write a c program to find the area of rhombus.   
8. Write a c program to find the area of parallelogram.           

Copyright © 2009 Wisdom a ray hope. All rights reserved. Theme by Laptop Geek. | Bloggerized by FalconHive.