Tutorials Navigation

Tutorials :: New :: Popular :: Top Rated

Tutorials: 18,357 Categories: 12

Total Tutorial Views: 42,531,964

C Programming Tutorial 2, Variables part 2 & 3

Tutorial Name: C Programming Tutorial 2, Variables part 2 & 3  

Category: PC Tutorials

Submitted By: Nissan

Date Added:

Last Updated:

Comments: 0

Views: 980

Related Forum: PC Building Forum

Share:

C Programming Tutorial 2, Variables part 2.

The program we are going to be using to learn C programming language is "CodeBlocks" and here is a link to the download its a free program.

Link : codeblocks.org/downloads/26

All of these tutorials are going to be done in an console application.

So the second tutorial is going to be on more Variables and there is going to be 2 parts 2 different ways on how to use Variables.

So lets get started :

We are going to continue with variables and in this tutorial i am going to show you two new type of variables.

The first one i'm going to show you is the float and the float will allow you to store numbers that have decimals or that are fractions.

So let go ahead here and create a two new float variable and that will look like :

float firstfloat = 2.5;


Now you code will look something like :



#include <studio.h>
#include <stdlib.h>

int main()
{
   float firstfloat = 2.5;
   float secondfloat = 5.6;
   return 0;
}



Now create a third float and make it divide by your first float and your second float and the code for that is :


float thirdfloat = firstfloat / secondfloat;


and now we are just going to print out the third float out onto the screen (if you dont know how to do this go to part one : Tutorials/id=13450/c-programming-...ml#details i cover it in there.)

For the people who do know your code will now look like :


#include <studio.h>
#include <stdlib.h>

int main()
{
   float firstfloat = 2.5;
   float secondfloat = 5.6;
   float thirdfloat = firstfloat / secondfloat;
   printf("%f", thirdfloat);
   return 0;
}


If you are wondering why we put "%f" that is because we are using float like in part 1 we use int so we put "%i".

Now let go ahead and run the program and make sure that it works and as you can see since this is a float it give us an answer with a decimal in it.

[ Register or Signin to view external links. ]

Now as you can see there is 6 digits after the decimal place now if you dont want it to have all 6 digits after you can change it by doing :


   printf("%.3f", thirdfloat);


and the .3 means that there is only going to give you the answer with 2 digits after the decimal place.

So now when we run this we get :

[ Register or Signin to view external links. ]

Now lets look at another variable type and that is char and that hold a character variable.

So let go ahead here and create a new char variable and that will look like :

char MyChar = 'A';


So now lets print out the character on the screen and now your code will look like :


#include <studio.h>
#include <stdlib.h>

int main()
{
   char MyChar = 'A';
   printf("%c", MyChar);
   return 0;
}


Now let go ahead an run this now as you can see it has printed the letter you put in you char variable.

[ Register or Signin to view external links. ]

That is it for this tutorial hope you learned something from this.

Ratings

Current rating: 10.00 by 2 users
Please take one second and rate this tutorial...

Not a Chance
1
2
3
4
5
6
7
8
9
10
Absolutely

Comments

"C Programming Tutorial 2, Variables part 2 & 3" :: Login/Create an Account :: 0 comments

If you would like to post a comment please signin to your account or register for an account.