Friday, June 8, 2012

Assignments

Once we decide the data types and variables to be used in a program we can assign values to them.














   First declare and then assign


    Both declare and assign at the same time


Creating a new name for a Data Type

To create a new name for an existing data type the keyword typedef is used.
    E.g. typedef int integer;


sizeof() 
The sizeof() operator in C is used to obtain the amount of memory (bytes) allocated for a variable of a
given data type.
    E.g. sizeof(int)



putchar() - putchar() is a macro that outputs a character on stdout.


/* Output a Character */
#include <stdio.h>
#include <conio.h>
#define msg "End of Program"
int main()
{
           char a ;
           a =100;
           printf("%d\n",a);
           putchar(a);
           putchar(10);
           putchar(13);
           putchar(a+1);
           puts(msg);
}


/* Area of a circle */
#include <stdio.h>
#include <conio.h>
int main()
{
          int r ;
          float pi;
          pi = 22/7.;
          r = 7;
          printf("%s%5.2f \n","The area of the circle =" ,pi*r*r);


}


Standard Input

scanf() Function
scanf() function reads data from a standard input device into arguments specified by the format string.
scanf(format string, arguments)


/* Input through the keyboard */
#include <stdio.h>
#include <conio.h>
int main()
{
      int x,y,sum;
      printf("Enter an Integer:");
      scanf("%d",&x);
      printf("Enter another:");
      scanf("%d",&y);
      sum = x + y ;
      printf("sum =%d \n",sum);
}


/* Floating point Numbers */
#include <stdio.h>
#include <conio.h>
int main()
{
      float x,y,sum;
      printf("Enter a number:");
      scanf("%f",&x);
      printf("Enter another:");
      scanf("%f",&y);
      sum = x + y ;
      printf("sum =%.2f \n",sum);
}




No comments:

Post a Comment