adsense


Showing posts with label C LANGUAGE PROGRAMME HELLO WORLD. Show all posts
Showing posts with label C LANGUAGE PROGRAMME HELLO WORLD. Show all posts

Friday, 13 November 2015

C LANGUAGE PROGRAMME HELLO WORLD PAGE 2

C LANGUAGE PROGRAMME HELLO WORLD





C hello world program


C hello world program: c programming language code to print hello world. 
This program prints hello world, printf library function is used to 
display text on screen, '\n' places cursor on the beginning of next 
line, stdio.h header file contains declaration of printf function. 
The code will work on all operating systems may be its Linux, 
Mac or any other and compilers. To learn a programming language 
you must start writing programs in it and may be your first c code 
while learning programming.
Hello world in C language
//C hello world example
#include 
 
int main()
{
  printf("Hello world\n");
  return 0;
}
Purpose of Hello world program may be to say hello to 
people or the users of your software or application.
Output of program:
 
Download Hello world program.
Hello world program in c
We may store "hello world" in a character array as a string constant and then print it.
#include 
 
int main()
{
  char string[] = "Hello World";
 
  printf("%s\n", string);
 
  return 0;
}
Don't worry if you didn't understand above code as you may not be familiar with arrays yet.
Printing hello world indefinitely
Using loop we can print "Hello World" a desired number of time or indefinitely.
#include 
#define TRUE 1
 
int main()
{
  while (TRUE)
  {
    printf("Hello World\n");
  }
 
  return 0;
}


C LANGUAGE PROGRAMME HELLO WORLD PAGE 1

C LANGUAGE PROGRAMME HELLO WORLD





C program examples


Example 1 - C hello world program
/* A very simple c program printing a string on screen*/
#include 
 
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 
 
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 
 
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 
 
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 
 
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 
 
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 
 
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 
 
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 
 
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 
 
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 
 
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 
#include 
 
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 
#include 
 
int main()
{
    int c;
 
    for ( c = 1 ; c <= 10 ; c++ )
        printf("%d\n", c);
 
    getch();
    return 0;
}
Above source code includes a header file  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 
 
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”, do not use 
quotes while executing commands.