C Type Conversion
Type Conversion Sometimes, you have to convert the value of one data type to another type. This is known as type conversion. For example, if you try to divide two integers, 5 by 2, you would expect the result to…
Type Conversion Sometimes, you have to convert the value of one data type to another type. This is known as type conversion. For example, if you try to divide two integers, 5 by 2, you would expect the result to…
Real-Life Example Here’s a real-life example of using different data types, to calculate and output the total cost of a number of items.
Get the Memory Size We introduced in the data types article that the memory size of a variable varies depending on the type: Data Type Size int 2 or 4 bytes float 4 bytes double 8 bytes char 1 byte…
Set Decimal Precision You have probably already noticed that if you print a floating point number, the output will show many digits after the decimal point :
Numeric Types Use int when you need to store a whole number without decimals, like 35 or 1000, and float or double when you need a floating point number (with decimals), like 9.99 or 3.14515.
The char Type The char data type is used to store a single character. The character must be surrounded by single quotes, like ‘A’ or ‘c’, and we use the %c format specifier to print it:
Data Types As explained in the Variables article, a variable in C must be a specified data type, and you must use a format specifier inside the printf() function to display it: Example // Create variables int myNum = 5; …
Real-Life Example Often in our examples, we simplify variable names to match their data type (myInt or myNum for int types, myChar for char types, and so on). This is done to avoid confusion.
C Variable Names All C variables must be identified with unique names. These unique names are called identifiers. Identifiers can be short names (like x and y) or more descriptive names (age, sum, totalVolume).