Im trying to define the variable in C programming.
Does anyone how to do it and the difference of data type while defining it?
Defining a variable in C is quite straightforward, all you need to do is follow these steps:
- Decide what you're going to put in the variable, and choose the appropriate datatype.
- Decide a name for your variable.
- Assign a value to the variable.
For example, to store your age, you would approach defining your variable like so:
- We know the age is going to be an integer, which means it is a whole number having no decimal value. In C, the datatype for storing integers is called
int, so that's our data type.
- Our variable stores an age, so we might as well call it
- We can now store any number we want in our
agevariable (shh, C won't know).
Once you've done those 3 things, you can write out your declaration like so
int age = 42;
; in the end, that's something you can't forget)
And there you have it, You've just declared your variable!
Here are some other data types you can use:
float pi = 3.14; char grade = 'A';
If you wanted to store a string, or a sequence of characters, you would use a 'character array' (you don't have to worry about what this means, you'll learn what arrays are later)
char username = "zeno99";
You can find dozens of great C tutorials that will make learning it easier, here are a couple ones I found
Hope this helped!