Programming with C

First Program in C

#include<stdio.h>
 int main ()
    {
      printf ("Hello World!\n");
    }

Writing your first program

Windows:

Launch shortcut : c:\tc\bin\tc

  • Create a new file : File -> new
  • Enter the First program :
  • Save : File -> Save, Provide Location
  • Run :  Run -> Run
  • Disply Result : Alt + F5

Linux:

Create a file in vi editor

  • Enter the First program :
  • Save  : wq
  • Compile : gcc <filename>
  • Disply Result :  ./a.out

Control Program Output

  • gcc <filename>.c -o <outputfilename>

header

  • stdio.h, which stands for "standard input/output header".
  • It contains macro definitions, constants, and declarations of functions and types used for various standard input and output operations

print

  • printf ("<text to be printed");
  • Pritf Tokens:

Token            Meaning                                         

%d                 print int, decimal
%f                  print floating point                                              
"%6.2f"          6 chars total, 2 after decimal
%c                 print character                                  
%s                 print string                                         
%%               print percent sign                                        

Comments

  • Comments  begin with /* and end with */

Data Types 

Use of integers

#include<stdio.h>
int main(){
    int num1,num2,result;
    num1=16;
    printf("Value of Number 1 is %d \n",num1);
    num2=8;
    printf("Value of Number 2 is %d \n",num2);
    result=num1+num2;
    printf("Sum of Value 1 and Value 2 is %d \n",result);
}

Use of characters

#include <stdio.h>
int main (){
char myFavoriteChar;
myFavoriteChar = 'z';
printf ("My Favorite Character is : %c\n", myFavoriteChar );
}

Operators

Arithmetic Operators 

Use of Arithmetic operators

#include <stdio.h>
int main () {
        /* Unary operators. */
        int i = -1;
        int j = +2;
        int k, m, n;
        /* Standard plus, minus, mult and div. */
        k = (i + j) + (i - j) + (i * j) + (i / j);
        /* Remainder. */
        m = i % j;
        /* Post and pre operators. */
        n = (i++) + (++i) + (j--) + (--j);
        printf ("i=%d j=%d k=%d m=%d n=%d\n", i, j, k, m, n);
    }

Arrays 

One Dimensional Array

Using one dimensional array

#include <stdio.h>
int main (){
    int myarray[3];
    myarray[0]=1;
    myarray[1]=2;
    myarray[2]=3;
    printf("value of myarray[1] is %d\n",myarray[1]);
}

Selection Statements 

If Statement

Signature : if(condition) statement;

Operator         Meaning

<                     Less than

>                     Greater than

= =                  Equal to

 

Using if Statement

#include <stdio.h>
int main ()
    {
        int i = 5;
        int j = 6;
            /* Simple if statement. */
        if (i < j)
        printf ("i is less than j\n");
    /* If statement with a block of statements. */
        if (i < j) {
                    printf ("i is less than j\n");
                    printf ("i=%d j=%d\n", i, j);
                    } /* end of if-block. */

    /* Combination of if and else. */
        if (i < j)
                printf ("i is less than j\n");
        else if (i > j)
                printf ("i is greater than j\n");
        else 
                printf ("i is equal to j\n");
    /* Example with nested if's. */
        if (i < j) {
            printf ("i is less than j\n");
            if (i < 0) {
                        printf ("Careful, i is negative\n");
                        } 
                } 
        else if (i > j) {
        printf ("i is greater than j\n");
        if (j < 0) {
                    printf ("Careful, j is negative\n");
                    }
                } 
        else {
            printf ("i is equal to j\n");
            } /* End of if-else block. */
        /* Use comments to clarify long blocks of code. */
}

Iteration Statement 

for Loop

Using for Loop

#include <stdio.h>
int main (){
        int i = 1, sum = 0, j = 1;
/* Straighforward version of for-loop. Notice
the declaration of i in the header of the loop. */
        for (i=1; i<=10; i++) {
        sum += i;
        }
        printf ("Sum of first 10 integers: %d\n", sum);
/* Now count backwards from 10 to 1 */
        sum = 0;
        for (i=10; i>=1; i--) {
            sum += i;
            }
        printf ("Sum of first 10 integers: %d\n", sum);
/* A more cryptic version that makes use of multiple
mini-statements in the for-header. Not recommended. */
        for (sum=0, j=1; j<=10; sum+=j, j++) {
            }
            printf ("Sum of first 10 integers: %d\n", sum);
    }

while Loop

Using while Loop

#include <stdio.h>
int main (){
    int num1=1;
    int num2=10;
    while(num1<=num2)
        {
            printf("Value of Number 1 is now %d \n",num1);
            num1=num1+1;
        }
    printf("Just Came Out of While loop\n");
}

References:

 

Last modified: Wednesday, 10 July 2013, 3:09 PM