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
}

Thursday, September 26, 2013

Second Program : Adding Two Numbers

Hey guys now that we know how to print something on the screen lets start with something more interesting ... You  can make your own calculator using C ,Lets see how
first we use the inbuilt libraries for standard input output that is stdio.h

#include<stdio.h>
#include<conio.h>
void main()
{
int a,b,sum;   // Variables for storing that input and output
printf("\n Enter the first number: "); // Print Enter the first number on screen
scanf("%d",&a); //Read the input using scanf (we will discuss about scanf in next post) & store it in a
printf("\n Enter the second number: "); // Print Enter second number
scanf("%d",&b);// Store second value in b
sum=a+b; // Calculate sum of a and b and put in sum
printf("\n Sum Is %d",sum); // Print the sum on screen
getch(); // Wait till any key is pressed
}

Saturday, September 21, 2013

Starting Things Practically

First we need a C compiler installed on our machine if its an linux system you can open the terminal and check by typing the command below

cc

if you are a windows guy then follow the steps to download the Dev C++ from Bloodshed


Go to the url below and click on the link show the pic(Highlighted with red box around it)

http://www.bloodshed.net/dev/devcpp.html




After clicking it should start the download

........................................ After the download is completed

Run the executable  in the choose components  click on remove previous versions if you have already .

After the setup is finished start the Dev C ++ and keep clicking next

Then it should look like this





Then go to File --- New --- Source File   And click on it or directly pres ctrl+n

then type the following code

#include<stdio.h>
#include<conio.h>
int main()
{
printf("Hello World");
getch();
}

Then click  on Execute Click On Compile
after the program has been compiled
Go to Exexute and click on Run







Congratulations You Have Successfully Written Your First C Program

Wednesday, September 18, 2013

Dissecting The Program

The program in the last post is very simple , consisting  just few lines of C code . But it is very important to understand this as it serves as the foundation for larger programs. As they say " Journey Of A Thousand Miles Begins With A Single Step ".
Let's look at the program line by line .


#include<stdio.h>

The Header of the program is very necessary if you want to use the printf statement . Without this line the printf statement won't work.  We will  look at it in detail later when we are writing our own libraries. As of now all we need to know is the printf statement is declared in the stdio.h library and stdio.h stands for Standard Input Output Header. Some other libraries are conio.h , math.h (Just remember the names for now)

 void main ()     
    {
    }

This is the Main part of the program . If you are given a task to do something you need to know how to start the task right?? Similarly main is used so that the computer can know from where it has to start executing the program. The computer executes whatever is inside the Main function`s body.The body of the main function is defined by the curly braces {  } .
printf ("Hello World");

This is the line that prints Hello World onto the screen . We will be using this function more than any other function (Trust Me) 

So thats it lets get started with a few practical aspects from the next post .








Monday, September 16, 2013

A Very Simple C Program

Now lets take a look  at a very small and simple C program.
Compare this to the structure we have discussed before so that it will be easier to understand , as all C programs will follow the same structure.
#include <stdio.h>              // This is the header of the program
void main ()                       // The main function of the program
{
printf ("Hello World");      /*A simple inbuilt function to print things onto the screen. */
}