0% found this document useful (0 votes)
10 views

Unit 2 Data types, variables, Constants

The document provides an overview of data types, variables, constants, and keywords in the C programming language. It explains the character set, primary and derived data types, user-defined data types, and rules for writing identifiers. Additionally, it covers constants, variable initialization, and the execution character set, along with examples and references for further learning.

Uploaded by

Prathmesh Jadhav
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views

Unit 2 Data types, variables, Constants

The document provides an overview of data types, variables, constants, and keywords in the C programming language. It explains the character set, primary and derived data types, user-defined data types, and rules for writing identifiers. Additionally, it covers constants, variable initialization, and the execution character set, along with examples and references for further learning.

Uploaded by

Prathmesh Jadhav
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 34

Data types, variables,

Constants

Prepared by
Mrs. Smita Mande
► Steps in learning English language

Alphabets Words Sentences Paragraphs

► Steps in learning Programming language

Alphabets Constants
Digits Variables Instructions Program
Symbols Keywords

2
C Character Set

► A character denotes any alphabet, digit or special symbol used to represent


information. The following are valid alphabets, numbers and special
characters in C
► Alphabets : A…….Z,a….z
► Numbers : 0,1,….9
► Special Symbol :-~`!@#$%^&*()_-+=\|/”;:.,?/
Constant, Variables & Keywords

► Constant: is an entity that doesn’t change.


► Variable : is an entity that may change
► Keywords : are the reserved words.
5
C Language Reserved Words
Keywords are the words whose meaning has already been explained to the C
compiler.
The table below lists all keywords reserved by the C language. When the current
programming language is C or C++, these keywords cannot be abbreviated, used as
variable names, or used as any other type of identifiers.

auto else long switch


break enum register typedef
case extern return union
char float short unsigned
const for signed void
continue goto sizeof volatile
default if static while
do int struct _Packed
double
Datatypes and Modifiers
Datatypes:
In the C Programming language, data types refer to a broad system used for declaring
variables or functions of different types. The type of a variable determines how much space
it occupies in storage and how the bit pattern stored is interpreted

Modifiers:
Modifiers are keywords used to increase or decrease the storage capacity of basic data
types. Modifiers are prefixed with data type

There are 4 modifiers :


1. short: It applies on int and reduces size to 2 bytes.
2. long : It applies on int and double data types. It doubles the size of int (4 to 8
bytes) and double (8 to 16 bytes) data types.
3. unsigned: It accepts only +ve value.
4. signed: It accepts both +ve and –ve value

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

long unsigned int 0 to 4294967295 4 %lu

► *for 32 bit compiler


Primary Data Type: Character
► Character type variable can hold a single character.
► As there are singed and unsigned int (either short or long), in the same way
there are signed and unsigned chars, both occupy 1 byte each, but having
different ranges.
► Unsigned characters have values between 0 and 255
► signed characters have values from –128 to 127
► Syntax: char A,B,C;

Data type Range Bytes Format


signed char -128 to +127 1 %c
Unsigned char 0 to 255 1 %c

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'.

char greeting[6] = {'H', 'e', 'l', 'l', 'o', '\0'};

char greeting[] = "Hello";

► Following is the memory presentation of the above defined string in C

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;

int *ptr; //ptr points to an address which holds int data

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
}

int max(int num1, int num2);

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

struct [structure tag] struct Books


{ {
member definition; char title[50];
member definition; char author[50];
... member definition; char subject[100];
} [one or more structure variables]; int book_id;
} book;

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

► int number1,_num1, sum_marks;//correct


► int a b;//invalid
► int 1c;//wrong
► int Int;//valid
► int int_1c;//valid

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:

1. Using #define preprocessor directive


2. Using a const keyword

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

An integer constant is nothing but a value consisting of digits or numbers. These


values never change during the execution of a program. Integer constants can be
octal, decimal and hexadecimal.
► Decimal constant contains digits from 0-9 such as,

Example, 111, 1234

► 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

Example, 0X2, 0Xbcd


Character constant
► A character constant contains only a single character enclosed within a single quote (''). We
can also represent character constant by providing ASCII value of it.
Example, 'A', '9‘
Char ch=‘9’; //valid

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

Example, "Hello", "Programming” 32


Float constant
► These numbers are shown in decimal notation, having a whole number
followed by a decimal point and the fractional part.
► A real number may also be expressed in exponential notation.
► For example, the value 215.65 may be written as 2.165e2 in exponential
notation.
► The mantissa is either a real number expressed in decimal notation or an
integer.
► The exponent is an integer number with an optional plus or minus sign.
► The letter e separating the mantissa and the exponent can be written in
either lowercase or uppercase.
► Example:-0.65e4, 12e-2 1.5e+5

33
References

► https://www.geeksforgeeks.org
► https://www.tutorialspoint.com/cprogramming
► https://beginnersbook.com
► Let us C by Yashwant Kanetkar

34

You might also like