Unit 2 Data types, variables, Constants
Unit 2 Data types, variables, Constants
Constants
Prepared by
Mrs. Smita Mande
► Steps in learning English language
Alphabets Constants
Digits Variables Instructions Program
Symbols Keywords
2
C Character Set
Modifiers:
Modifiers are keywords used to increase or decrease the storage capacity of basic data
types. Modifiers are prefixed with data type
7
•char
•float
•void
•Derived
•String
•Array
•Pointer
•function
•User defined
•structure
•union
8
•enum
Primary Data Type: Integer
► Integers are whole numbers with a range of values, range of values are machine dependent.
Generally an integer occupies 2 bytes memory space and its value range limited to -32768 to
+32767
► Syntax: int a,b;
Data type Range Bytes Format
short signed int -32768 to +32767 2 %d
short unsigned int 0 to 65535 2 %u
signed int -2147483648 to 4 %d
+2147483647
unsigned int 0 to 4294967295 4 %u
long signed int -2147483648 to 4 %ld
+2147483647
10
Primary Data Type: Float
► The float data type is used to store fractional numbers (real numbers) with 6 digits of precision.
► Floating point numbers are denoted by the keyword float.
► When the accuracy of the floating point number is insufficient, we can use the double to define
the number.
► The double is same as float but with longer precision and takes double space (8 bytes) than float.
► To extend the precision further we can use long double which occupies 10 bytes of memory space
► Syntax: float c;
Data type Range Bytes Format
float -3.4e38 to +3.4e38 4 %f
double -1.7e308 to +1.7e308 8 %lf
long double -1.7e4932 to +1.7e4932 10 %Lf
11
Primary Data Type: void
► Void type means no value.
► This is usually used to specify the type of functions which returns nothing.
Example:
Void display()
{
//statements for display
}
12
Derived Data Type: Array
► Array: An array in C language is a collection of similar data-type,
means an array can hold value of a particular data type for which it
has been declared.
► Syntax:
DataType ArrayName[size_of_array];
int arr[5];
13
Derived Data Type: String
► Strings are actually one-dimensional array of characters terminated by a null character '\0'.
14
Derived Data Type: Pointer
► Pointer: C Pointer is a special variable that can be used to store
address of another variable
Syntax:
datatype *var_name;
15
Derived Data Type: function
► A function is a group of statements that together perform a task.
Every C program has at least one function, which is main()
► Defining a Function
The general form of a function definition in C programming language is
as follows
return_type function_name( parameter list )
{
body of the function
}
16
User defined data type: structure
► Structure is user defined data type available in C that allows to combine data
items of different kinds
Defining a Structure
► To define a structure, you must use the struct statement. The struct statement
defines a new data type, with more than one member. The format of the struct
statement is as follows
17
User defined data type: union
► A union is a special data type available in C that allows to store different data types in the same
memory location.
► You can define a union with many members, but only one member can contain a value at any
given time.
Defining a Union
► To define a union, you must use the union statement in the same way as you did while defining a
structure. The union statement defines a new data type with more than one member for your
program. The format of the union statement is as follows −
union [union tag] union Data
{ {
member definition; int i;
member definition; float f;
... member definition;
char str[20];
} [one or more union variables];
} data;
18
User defined data type: enum
► Enumeration (or enum) is a user defined data type in C. It is mainly used to
assign names to integral constants, the names make a program easy to read and
maintain.
19
User defined data type: typedef
► The typedef is a keyword used in C programming to provide some meaningful names to
the already existing variable in the C program.
► It behaves similarly as we define the alias for the commands.
► Syntax of typedef
typedef <existing_name> <alias_name>
In the above syntax, 'existing_name' is the name of an already existing variable while 'alias
name' is another name given to the existing variable.
► Example:
we have declared the unit variable of type unsigned int by using a typedef keyword.
typedef unsigned int unit;
we can create the variables of type unsigned int by writing the following statement
unit a, b;
20
Identifier
► C identifier refers to name used to identify a variable, function, structures or
any other user-defined item or entity. Identifier must be unique.
► They are created to give unique name to a entity/item/variable name to identify
it during the execution of the program identifier names must be different from
keywords.
► Because Keywords are predefined, reserved words used in programming .
As for Example :You cannot use double as an identifier because double is a
keyword
21
Rules for writing an identifier
► The first letter of an identifier should be either a letter or an underscore.
► A valid identifier can have letters (both uppercase and lowercase letters), digits and underscores.
► There is no rule on length of an identifier. However, the first 31 characters of identifiers are
significant by the compiler.
► Must not contain white spaces, commas
► Can not use Keyword as a identifier
22
Variables
► Variables are named memory locations that have a type, such as integer or
character, which is inherited from their type.
► The piece of information stored at this location is referred as value of a
variable.
23
Variable Initialization
► Giving a variable an initial value
► Variables not necessarily initialized when declared
► Can initialize in declaration
► Syntax: Datatype identifier = Value;
► Example:
int x = 0;
int x;
x=0;
float f=23.145;
double d=20.0;
char ch='x‘;
24
Variable Storage
Variables example in C
int X = 8; int Y = 7;
25
Constants in C
► As the name suggests the name constants is given to such variables or values in
programming language which cannot be modified once they are defined.
► They are fixed values in a program.
► There can be any types of constants like integer, float, character constants etc
► int marks=90;//variable
► const int marks=60;//constant
► marks=76;//invalid
26
Defining Constants
In C/C++ program we can define constants in two ways as shown below:
27
1. Using #define preprocessor directive
► This directive is used to declare an alias name for existing variable or any value. We
can use this to declare a constant as shown below:
#define identifierName value
identifierName: It is the name given to constant.
value: This refers to any value assigned to identifierName.
Eg:
#define pi 3.14
28
2. Using a const keyword
► Using const keyword to define constants is as simple as defining variables, the
difference is you will have to precede the definition with a const keyword.
29
Execution Character Set
► Certain ASCII characters are unprintable, which means they are not displayed
on the screen or printer. Those characters perform other functions aside from
displaying text.
► Examples are backspacing, moving to a newline
► They are used in output statements.
► Escape sequence usually consists of a backslash and a letter
► Execution characters set are always represented by a backslash (\) followed by
a character.
► Note that each one of character constants represents one character, although
they consist of two characters. These characters combinations are called
as escape sequence.
► E.g \n \t
► Printf(“\n Enter number:”);
30
Integer constants
► Octal constant contains digits from 0-7, and these types of constants are always
preceded by 0.
Example, 012, 065
► Hexadecimal constant contains a digit from 0-9 as well as characters from A-F.
Hexadecimal constants are always preceded by 0X.
31
String constant
► A string constant is a sequence of characters enclosed in double quotes.
► The characters may be letters, numbers, special characters and blank space.
Example:-”Hello!”, “WEL DONE”
► character constant is not equivalent to the single character string constant.
► A single character string constant does not have an equivalent integer value while a
character constant has an integer value.
► Character strings are often used in programs to build meaningful programs
33
References
► https://www.geeksforgeeks.org
► https://www.tutorialspoint.com/cprogramming
► https://beginnersbook.com
► Let us C by Yashwant Kanetkar
34