Variables and Data Types: - Shubham Panchal (IIT Hyderabad)
1) Variables are named locations in memory that hold values and can be modified by a program. All variables must be declared with a data type like int or float before use.
2) There are 5 fundamental data types in C - int, char, float, double, and void. int is used for integers, char for characters, float and double for floating-point numbers, and void for non-returning functions.
3) Data types can be modified with signed, unsigned, long, and short to alter the range of values they can hold. For example, unsigned short ranges from 0 to 65535 while long ranges from -2147483648 to 2147483646.
Variables and Data Types: - Shubham Panchal (IIT Hyderabad)
1) Variables are named locations in memory that hold values and can be modified by a program. All variables must be declared with a data type like int or float before use.
2) There are 5 fundamental data types in C - int, char, float, double, and void. int is used for integers, char for characters, float and double for floating-point numbers, and void for non-returning functions.
3) Data types can be modified with signed, unsigned, long, and short to alter the range of values they can hold. For example, unsigned short ranges from 0 to 65535 while long ranges from -2147483648 to 2147483646.
Variables A variable is a named location in memory that is used to hold a value that can be modified by the program. You can think of a variable as a reserved piece of memory(roughly a box in memory) wherein you can keep your data. All the variables must be declared before they are used. The general form for declaring a variable is: dataType variableList; where dataType is the type of variable(s) and variableList is the comma
Panchal's Programming Academy
Variables -separated list of variable name(s). Variable name can be any valid identifier. Rules for naming variables: A variable name is a combination of alphabets, digits and _(underscore). Special symbols are not allowed for variable naming except underscore. A variable name cannot start with a digit, it must start with either alphabet or underscore.
Panchal's Programming Academy
Variables No commas and blanks are allowed within variable names. Good Practices: Keep the variable name relevant to its task or purpose. If you want to give two words as a variable name, then either you can use underscore or you can use the camel notation. Suppose you want to set First Name as your variable name then either you can use First_Name or you can use FirstName. Start your variable name with a small alphabet. For example, firstName.
Panchal's Programming Academy
Data Types in C Data types are means to identify the type of data and associated operations for handling it. C allows two kind of data types i.e. fundamental data type and derived- data types. In this section, we will be discussing only fundamental data types in C. Of course, in future we will discuss the derived data types as well .
Panchal's Programming Academy
Fundamental Data Types in C Fundamental data types are also known as primitive data types. There are five primitive data types in C: i. int data type(for integers) ii. char data type(for characters) iii. float data type(for floating-point numbers) iv. double data type(for double precision floating point numbers) v. void data type(for empty set values or non returning functions)
Panchal's Programming Academy
Fundamental Data Types in C int data type: This data type is used to store integers only. Integers do not have any fractional part. A variable with int data type is capable of storing integers. char data type : This data type is used to store any valid character from the character set of C. A variable with char data type is capable of storing a single character or escape sequences. NOTE: If a character from the character set of C is stored in a char variable, its value is equivalent to the integer code of that character which is its ASCII Code.
Panchal's Programming Academy
Fundamental Data Types in C float data type: This data type is used to store floating point numbers(numbers with fractional part). For example, 1.23 is a floating point number and not an integer. The number 123 is an integer but 123.0 is a floating point number. Floating point numbers can also be written in exponent notation. For example, 147.9101 would be written as 1.479101E02 in exponent notation. double data type: This data type is also used to store floating point numbers but it stores floating point numbers with large range and precision(significant numbers after decimal point). It stands for
Panchal's Programming Academy
Fundamental Data Types in C -double precision floating-point. It is usually used when type float is too small or insufficiently precise. void data type: This data type is used for empty set of values. It is used as the return type for functions that do not return a value. No variable of type void can be declared.
Panchal's Programming Academy
Data Type Modifiers Except type void, the fundamental data types can be modified. Modifiers are used to alter the meaning of the base type to fit various situations more precisely. There are four modifiers in C: i. signed ii. unsigned iii. long iv. short
Panchal's Programming Academy
Data Type Modifiers You can apply signed, unsigned, long and short to int data type and you will get different range. Data type Approximate size Minimal Range short or signed short 2 -32768 to 32767 unsigned short 2 0 to 65535 int or signed int 2 -32768 to 32767 unsigned int 2 0 to 65535 long or signed long 4 -2147483648 to 21476483647 unsigned long 4 0 to 4294967295
Panchal's Programming Academy
Data Type Modifiers Note that the size and range of data type vary from platform to platform. It is obvious from the table that short, int and long are signed by default. You can also apply signed and unsigned modifiers to char data type.
Data type Approximate size Minimal Range
char or signed char 1 -128 to 127 unsigned char 1 0 to 255
Panchal's Programming Academy
Data Type Modifiers Note that a char data type is really another integer type( as inside memory it actually holds numbers i.e., equivalent ASCII code). You can apply long modifier to double data type.
Data type Approximate size Minimal Range Digits of Precision
float 4 3.4x10-38 to 3.4x1038 - 1 7 double 8 1.7x10-308 to 1.7x10308 – 1 15 long double 10 3.4x10-4932 to 3.4x104932 – 1 19
Panchal's Programming Academy
Data Type Modifiers Some implementations may allow you to use unsigned to the floating point data types. However, it reduces the portability of your code and is generally not recommended.