Sunday, October 13, 2013

Data Types In C

Now back to some theory , from the previous programs we used something called an int where we declared variables and used a placeholder %d in the printf statements
Now let us look at in in detail . C is a strongly typed language meaning it is strict and when we declare a variable we need to tell C what kind of variable it is (what value will it hold ).
This is known as the Data  Type . Here are a few inbuilt data types in C with some information about them
       Name                Type       Storage Size         Placeholder
  1. Integer             int          2 or 4 bytes           %d
  2. Decimal           float        4 bytes                  %f
  3. Character         char         1 byte                   %c
These are the data types . They can be modified using modifiers about which we will see in later posts

we will be commonly using char , int , float. 

Please note that in this blog we are dealing with C programming from a practical point of view. We are not concerned with in depth knowledge of C but only about how to write your own 
programs using the basics..

In the previous programs we used the int datatype . Which didn't give you correct results during the division operation as it rounded off the quotient . We will look at how we can use other datatypes in the coming posts .. Stay Tuned..................




Wednesday, October 9, 2013

Bigger Of Two Numbers

Before going back to more of theory let us try out a few more simple C programs, like finding the bigger of two numbers or smaller of two numbers etc ....

Here is the program
#include<stdio.h>
#include<conio.h>
void main()
{
int a,b;
clrscr();
printf("enter two numbers");
scanf("%d%d",&a,&b);
if(a>b)
printf("%d is greater than %d",a,b);
else printf("%d is greater than %d",b,a);
getch();
}
This program finds the bigger of the two numbers that have been input by the user.



Wednesday, October 2, 2013

Variations : Subtracting ,Multiplying , Dividing

Now lets take a look at other things you can do by using the last program by just changing one line .

#include<stdio.h>
#include<conio.h>
void main()
{
int a,b,sum,difference,product,quotient ;   // Variables for storing that input and output
printf("\n Enter the first number: ")
scanf("%d",&a);
printf("\n Enter the second number: ");
scanf("%d",&b);
sum=a+b; // Calculate sum
difference=a-b; // Calculate difference
product=a*b; // Calculate product
quotient=a/b; // Calculate quotient
printf("\n Sum Is %d \n Differrence is %d \n Product is %d \n Quotient is %d\n ",sum,difference,product,quotient);
getch(); // Wait till any key is pressed
}