Thursday, December 12, 2013

Examples For Input And Output Through Programming .... Continued

Today lets look at how to read one or more variable in the same program.


#include<stdio.h>
void main()
{
int n;
float f;
printf("Enter An Integer And An Floating Number");
scanf("%d%f",&n,&f);
printf("The Entered Integer Is %d And Floating Number Is %f",n,f);

}

Friday, November 22, 2013

Examples For Input And Output Through Programming

Now lets look at few fairly simple examples. Let us read an input from user and show what the user has entered in the next line

For Integer

#include<stdio.h>
#include<conio.h>

void main()
{
int entered;
printf("Enter An Integer :")
scanf("%d",&entered);
printf("\n The Entered Integer Is %d",entered);
getch();
}
 

For Floating

#include<stdio.h>
#include<conio.h>

void main()
{
float entered;
printf("Enter An Floating Number :")
scanf("%f",&entered);
printf("\n The Entered Float Is %f",entered);
getch();
}



For Character
#include<stdio.h>
#include<conio.h>

void main()
{
char entered;
printf("Enter An Character :")
scanf("%c",&entered);
printf("\n The Entered Character Is %c",entered);
getch();
}


Try these out & also try to read two or more variables in a single scanf statement .Like reading and an integer and a floating in the same program and displaying both at once. We will look at this in the next post.....


Saturday, November 2, 2013

Input & Output Through Programming

Ok now we have some idea on how to save numbers in C . Now lets look at how we can get input data from the user and how we can give the user the output . The stdio.h gives us few functions to accomplish this task.
1. printf()
2. scanf()

The printf function allows us to print data on the screen so the user can see it while the scanf is used to get the data from the keyboard.

In Detail
 Printf ()

Syntax : printf("The Data You Want To Print :placeholder", argument list);
 Example: printf("The Sum Is %d",c)

Now lets break down the syntax for printf . The stuff within the quotes gets printed as it is . The placeholder is used in place where you want the variables to appear in the screen it can be between the the line or at the end . This is followed by the argument list that is the list of variables which will appear on the screen in place of the placeholders (Thats what placeholders are for :P ).
What are placeholders???
Relax we have already looked at them . You will understand by just looking at the example of placeholder  . Example of placeholder %d,%f ,%c, %s .

Few Examples Of printf Statements
printf("The Key You Entered Is %c ",key);
printf("The Sum Of %d + %d = %d",a,b,sum);
printf(" %f x %f = %f  \n",a,b,product );

We can also include few things like \n ,\t which are special . \n is used when you want a new line
\t is used for tab space and etc . The are used as placeholders for formatting .
 So printf is a type of  formatted output .

Scanf()

Syntax : scanf("Placeholder ", address list);
 Example: scanf("%d",&a);

Now breaking down the scanf statement. Its very similar to the printf but here one thing we need to note is we use the address list instead of argument list. Now what is address list ?? Its simple instead of just writing the variable name we just add a & symbol to point to its address.

the placeholder is used to denote what type of data is being read through the keyboard. The address list is used to store the data coming from the keyboard to the variable . For example
scanf("%d",&a);
The statement above the data type read is int and the data is stored in the address of variable a . So next when we use a in the program the data in the location is used.

Few Examples Of scanf Statements

scanf("%d %d ",&a,&b);
scanf("%f %f  %f",%a,&b,&c);
scanf("%d%f %c",&integer,&float,&character);


This ends the post on getting data from the user and showing him the result




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. */
}

UPDATE !!!!

Hi Everyone
We Will Resume The Normal Operation Of The Blog Soon. So Please Keep Checking Regularly
Please Do Comment If You Find The Blog Helpful.