c programming
zeno99 (0)

hello,
Im trying to define the variable in C programming.
Does anyone how to do it and the difference of data type while defining it?

You are viewing a single comment. View All
JustARatherRidi (191)

Hey!

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 age.
  • We can now store any number we want in our age variable (shh, C won't know).

Once you've done those 3 things, you can write out your declaration like so

int age = 42;

(Notice that ; 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!

EchoCoding (255)

@JustARatherRidi Love this answer! Wish I could up vote it twice! Very well put together and a lot of explanation.

JustARatherRidi (191)

@EchoCoding Thank you! That means a lot :)