Archive for the ‘C’ Category

C Hello World Program

March 26th, 2013, posted in C
Share

C hello world program :- c programming language code to print hello world. This program prints hello world, printf 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 it’s 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.

C Hello World Example :

#include <stdio.h>

int main()
{
  printf("Hello worldn");
  return 0;
}


Hello World Program In C :

#include <stdio.h>

int main()
{
  char string[] = "Hello World";

  printf("%sn", string);

  return 0;
}
Share

C Program To Check Leap Year

February 28th, 2013, posted in C
Share

c program to check leap year: c code to check leap year, year will be entered by the user. Please read the leap year article before reading the code, it will help you to understand the program.

C programming code :

#include <stdio.h>

int main()
{
  int year;

  printf("Enter a year to check if it is a leap yearn");
  scanf("%d", &year);

  if ( year%400 == 0)
    printf("%d is a leap year.n", year);
  else if ( year%100 == 0)
    printf("%d is not a leap year.n", year);
  else if ( year%4 == 0 )
    printf("%d is a leap year.n", year);
  else
    printf("%d is not a leap year.n", year);  

  return 0;
}


Compiler used:

GCC

Output of program:


leap year program
Share

C Program Remove Spaces, Blanks From A String

February 23rd, 2013, posted in C
Share

C code to remove spaces or excess blanks from a string, For example consider the string

“c programming”

There are two spaces in this string, so our program will print a string

“c programming”. It will remove spaces when they occur more than one time consecutively in string anywhere.

C programming code :

#include <stdio.h>

int main()
{
   char text[100], blank[100];
   int c = 0, d = 0;

   printf("Enter some textn");
   gets(text);

   while (text[c] != '')
   {
      if (!(text[c] == ' ' && text[c+1] == ' ')) {
        blank[d] = text[c];
        d++;
      }
      c++;
   }

   blank[d] = '';

   printf("Text after removing blanksn%sn", blank);

   return 0;
}

C Programming Code Using Pointers :

#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#define SPACE ' '

main()
{
   char string[100], *blank, *start;
   int length, c = 0, d = 0;

   printf("Enter a stringn");
   gets(string);

   length = strlen(string);

   blank = string;

   start = (char*)malloc(length+1);

   if ( start == NULL )
      exit(EXIT_FAILURE);

   while(*(blank+c))
   {
      if ( *(blank+c) == SPACE && *(blank+c+1) == SPACE )
      {}
      else
      {
         *(start+d) = *(blank+c);
	 d++;
      }
      c++;
   }
   *(start+d)='';

   printf("%sn", start);

   free(start);     

   return 0;
}

Compiler used:

GCC

Output of program:

get blanks in c program



Share

C Program To Shutdown Or Turn Off Computer

February 20th, 2013, posted in C
Share

To shutdown immediately use “C:\WINDOWS\System32\ shutdown /s /t 0”. To restart use /r instead of /s.

C Program to shutdown your computer: This program turn off i.e shutdown your computer system. Firstly it will asks you to shutdown your computer if you press ‘y’ the your computer will shutdown in 30 seconds, system function of “stdlib.h” is used to run an executable file shutdown.exe which is present in C:WINDOWSsystem32 in Windows XP. You can use various options while executing shutdown.exe for example -s option shutdown the computer after 30 seconds, if you wish to shutdown immediately then you can write “shutdown -s -t 0” as an argument to system function. If you wish to restart your computer then you can write “shutdown -r”.

If you are using Turbo C Compiler then execute your file from folder. Press F9 to build your executable file from source program. When you run from within the compiler by pressing Ctrl+F9 it may not work.

C programming code for Windows XP

#include <stdio.h>
#include <stdlib.h>

main()
{
   char ch;

   printf("Do you want to shutdown your computer now (y/n)n");
   scanf("%c",&ch);

   if (ch == 'y' || ch == 'Y')
      system("C:\WINDOWS\System32\shutdown -s");

   return 0;
}




C programming code for Windows 7

#include <stdio.h>
#include <stdlib.h>

main()
{
   char ch;

   printf("Do you want to shutdown your computer now (y/n)n");
   scanf("%c",&ch);

   if (ch == 'y' || ch == 'Y')
      system("C:\WINDOWS\System32\shutdown /s");

   return 0;
}

To shutdown immediately use “C:\WINDOWS\System32\ shutdown /s /t 0”. To restart use /r instead of /s.

C programming code for Ubuntu Linux

#include <stdio.h>

int main() {
  system("shutdown -P now");
  return 0;
}


You need to be logged in as root user for above program to execute otherwise you will get the message shutdown: Need to be root, now specifies that you want to shutdown immediately. ‘-P’ option specifes you want to power off your machine. You can specify minutes as:

shutdown -P “number of minutes”

For more help or options type at terminal: man shutdown.

Share