C Programming Language Tips


   C IDE (integrated development environment) software:

   1. Bloodshed Devcpp
   2. Microsoft Visual C

   A Simple Program:


   #include <stdio.h>

   int main()
   {
          printf("Hello World!\n\n");

          system("pause");
          return 0;
   }

   Using a constant (#define):


   #include <stdio.h>
   #define int pie 3.14

   int main()
   {
          printf("%d", pie);

          system("pause");
          return 0;
   }

   Data Types:


   1. Character ( declaration: char a;   specifier: %c)
   2. Integer ( declaration: int a;   specifier: %d)
   3. Float or double ( declaration: double a;   specifier: %f)

   Using a selection (if-else if-else):


   #include <stdio.h>

   int main()
   {
          int grade = 90;

          if(grade >= 75)
               printf("pass");
          else if((grade < 75) && (grade >= 0))
               printf("fail");
          else
               printf("invalid grade");

          system("pause");
          return 0;
   }

   Using a selection (switch):


   #include <stdio.h>

   int main()
   {
          int option = 3;

          switch(grade)
          {
          case 1:
                    printf("One");
                    break;
          case 2:
                    printf("Two");
                    break;
          case 3:
                    printf("Three");
                    break;
          default:
                    printf("This should not appear!");
          }

          system("pause");
          return 0;
   }

   Pretest loop (while):


   #include <stdio.h>

   int main()
   {
          int i = 1;

          while(i <= 10)
          {
               printf("%d", i);
               i++;
          }

          system("pause");
          return 0;
   }

   Posttest loop (do-while):


   #include <stdio.h>

   int main()
   {
          int i = 1;

          do
          {
               printf("%d", i);
               i++;
          } while(i <= 10)

          system("pause");
          return 0;
   }

   Counter controlled loop (for):


   #include <stdio.h>

   int main()
   {
          int i;

          for(i = 1; i <= 10; i++)
          {
               printf("%d", i);
          }

          system("pause");
          return 0;
   }

   Using Functions:


   #include <stdio.h>

   int add(int a, int b)
   {
         int total = a + b;
         return total;
   }

   int main()
   {
          int a = 5;
          int b = 5;
          int result = add(a,b);

          printf("result is %d", result);

          system("pause");
          return 0;
   }

   Using Arrays:


   #include <stdio.h>

   int main()
   {
          int num[5];
          int i;

          for(i = 0; i < 5; i++)
          {
               printf("\nEnter a number: ");
               scanf("%d", num[i]);
          }

          printf("\n\n");

          for(i = 0; i < 5; i++)
          {
               printf("\nNumber %d: %d", i, num[i]);
          }

          system("pause");
          return 0;
   }

   Using a string:


   #include <stdio.h>

   int main()
   {
          char name[20];

          printf("Enter your name: ");
          scanf("%s", name);

          printf("\n\nWelcome %s", name);

          system("pause");
          return 0;
   }
 
Make a Free Website with Yola.