Tutorials Navigation
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
Comments
Related Tutorials
- 01. Emulating Xbox 360 on PC for Running COD4 With Mods(3,656)
- 02. How to: Matrix Numbers | Batch File(1,954)
- 03. How to Password Protect Files on Windows(865)
- 04. How to play Socom 2/3/ and Combined Assault on PC(6,879)
- 05. Modern Warfare 2 Vac Ban Bypass Tutorial(6,243)
- 06. How to embed an image on TheTechGame(3,140)
- 07. [PC] NIOH 2 OTHER USER SAVE RESIGN(13,088)
- 08. Host bot lobbies! Full Tutorial!(11,611)
- 09. Unban yourself [Plutonium BO2](14,308)
- 10. Fall Guys - How to Change Your Name Color on Fall Guys(8,410)
- 11. Best Crosshair Settings for Valorant(6,550)
- 12. Othercide The Surgeon Boss Guide(2,568)
- 13. Othercide Remembrances Unlock Guide(4,517)
- 14. Othercide Beginners Tips and Tricks(2,734)
- 15. How to Fix Grounded Crashes, Loading Time, Low FPS and Other(4,881)
"C Programming Tutorial 2, Variables part 2 & 3" :: Login/Create an Account :: 0 comments