![]() Next we are creating a variable carColor of type color and assigning the value GREEN to it. In the following example we are creating an enumerated data type color and it can take only 4 values RED, GREEN, BLUE and WHITE. Using enum we can create a data type and also define the value that can be assigned to the variable of that type. The above code will print the following output.Įnum is enumerated data type that we can use to create user defined data type. And then created a new variable and assigned integer value 10 to it. In the following example we have used typedef to give int a new name num. In the above line we are giving integer int data type a new name num. Where type is any existing data type and identifier is the new name given to it. The general format to use typedef is given below. We use the typedef keyword to give new identifier name to a exisiting data type. We will learn about the derived data types in their respective tutorial.Ĭ allows programmers to create user defined types using the typedef and enum. These are the data types derived from the primary data type. We can merge the declaration and assignment step into one. We use the = assignment operator to assign value to a variable.Įxample: Create a variable to store score of a match and assign 0 to it. We can merge the three variables having the same data type int in one single line by separating them with comma. When we declare a variable the compiler gets to know two things.Įxample: Create a variable that will hold level of a game. The void data type takes no value and is generally used with functions to denote that the function is not going to return any value. Use the data type that correctly matches the data range you are trying to store. In the following table we have the range of the values of the primary or fundamental data type. Similarly, if we want to store integer value we use the int data type. So, if we want to create a variable that will hold a character we use the char data type. Variable names are case-sensitive meaning hello and Hello are treated as two separate variables.ĭata Types are storage representation and tells the compiler what type of data is stored in the variable.Ĭ divides its data types into 3 categories.Ĭ provides 5 primary or fundamental data types namely character char, integer int, floating point float, double precision floating point double and void. Can use letters (A-Z and a-z), digits (0-9) and underscore _.First character must be a letter or underscore.The value stored in a variable can change and is opposite to a constant whose value never changes.Įxample of variable: name, isGameOver, next_page, _nextgame, stage123 etc.įollowing are the rules for naming a variable. In this tutorial we will learn about data types in C programming language.Ī variable is a named location to hold a data value.
0 Comments
Leave a Reply. |