constants, variables and data types in c
TRANSCRIPT
Dept. of CSE 1
Constants, Variables and Data Types
Md. Asif IqbalDaffodil International University
Md. Asif Iqbal
Dept. of CSE 2
Character Set
A defined list of characters recognized by the computer hardware and software. Each character is represented by a number.
The ASCII character set, for example, uses the numbers 0 through 127 to represent all English characters as well as special control characters
The characters that can be used to form words, numbers and expressions those are to be used in C language.
The Character in C is grouped into following categories. Letters Digits Special Characters White Space Characters
Md. Asif Iqbal
Dept. of CSE 3
Character Set…
Md. Asif Iqbal
Dept. of CSE 4
C Token
In C Program the smallest individual unit is known as C Token.
C tokens are the basic buildings blocks in C language which are constructed together to write a C program.
Each and every smallest individual units in a C program are known as C tokens.
C tokens are of six types.
Md. Asif Iqbal
Dept. of CSE 5
Keywords Keywords are those words whose meaning is already defined by Compiler Cannot be used as Variable Name C Keywords are also called as Reserved words . As C is a case sensitive language, all the keywords are written in lower case
letters. There are 32 Keywords in C:
auto double int structbreak else long switch
case enum register typedef
char extern return union
continue for signed void
do if static while
default goto sizeof volatile
const float short unsigned
Md. Asif Iqbal
Dept. of CSE 6
Identifiers
In C language identifiers are the names given to variables, constants, functions and user-define data. These identifier are defined against a set of rules.
Rules for an Identifier An Identifier can only have alphanumeric characters( a-z , A-Z , 0-9 ) and
underscore( _ ). The first character of an identifier can only contain alphabet( a-z , A-Z ) or
underscore ( _ ). Identifiers are also case sensitive in C. For example name and Name are two
different identifier in C. Keywords are not allowed to be used as Identifiers. No special characters, such as semicolon, period, whitespaces, slash or comma
are permitted to be used in or as Identifier.
Md. Asif Iqbal
Dept. of CSE 7
Constants
Constants in C refer to fixed values that do not change during the execution of a program.
C supports several type of constants see the primary constants as below.
Md. Asif Iqbal
Dept. of CSE 8
Constants…
1. Integer Constants: An integer constant refers to a sequence of digits. It contains of set of digits,0 to 9, preceded by optional - or + sign. It must be have a decimal point. valid example: 123 , -12 , 0 , +78 , 654321 Invalid: 15 750 , 20,550 , $1232.Real constants: A real constants refer to a sequence of digits the point. Number containing fractional parts like 15.55 such number are called
real(or floating) constants. Valid e.g. 0.0043, -0.75, +132.0 Invalid: 15.750 , 20,550.33 , +$132
Md. Asif Iqbal
Dept. of CSE 9
Constants…
3.Single Character constants:
A single character constant contain a single character enclosed within a pair of single quote marks. Each character constant represents an integer value. e.g : '5' , 'x' , ';' , ' ' Note that the character constant '5' is not the same as number 5. The last constant is a blank space. character constants have integer value knows as ASCII (American code for
information interchange) values printf("%d",'a'); I will print the number 97, the ASCII value of the letter a.
printf("%d",97); It will print the letter 'a'.
Md. Asif Iqbal
Dept. of CSE 10
Constants…
String Constant: String is “Sequence of Characters“. String Constant is written in Pair of Double Quotes. String is declared as Array of Characters. In C , String data type is not available. Single Character String Does not have Equivalent Integer Value i.e ASCII Value
Different Constant Values Contained inside String :
Note: 'A' is not Equal to "A"
'A' ==> Requires 1 byte Memory "A" ==> Requires 2 byte Mem-
ory
Example Meaning
“a” String with Single Character
“Ali” String With Multiple Characters
“123? String With Digits
“How are You” String With Blanks
Md. Asif Iqbal
Dept. of CSE 11
VariableVariables in C have the same meaning as variables in algebra. A variable in C is a storage unit, which sets a space in memory to hold a value and can take different values at different times during program execution.
Rules to construct a valid variable name Characters Allowed :
Underscore(_) Capital Letters ( A – Z ) Small Letters ( a – z ) Digits ( 0 – 9 )
Blanks & Commas are not allowed No Special Symbols other than underscore(_) are allowed First Character should be alphabet or Underscore Variable name Should not be Reserved Word
Md. Asif Iqbal
Dept. of CSE 12
Case Sensitivity C is case sensitive
It matters whether an identifier, such as a variable name, is uppercase or lowercase.
Example:areaAreaAREAArEa
are all seen as different variables by the compiler.
Md. Asif Iqbal
Dept. of CSE 13
Which Are Legal Identifiers?
AREA area_under_the_curve 3D num45 Last-Chance #values x_yt3 pi num$ %done lucky***
Md. Asif Iqbal
Dept. of CSE 14
Declaring Variables
Before using a variable, you must give the compiler some information about the variable; i.e., you must declare it.
The declaration statement includes the data type of the variable.
Examples of variable declarations: int meatballs ; float area ;
Md. Asif Iqbal
Dept. of CSE 15
Declaring Variables…
When we declare a variable Space is set aside in memory to hold a value of the specified data
type That space is associated with the variable name That space is associated with a unique address
Visualization of the declaration int meatballs ;
meatballs
FE07
garbage
Md. Asif Iqbal
Dept. of CSE 16
Declaring Variables
Declaration of variables must be done before they are used in the program. Declaration does two things: It tells the compiler what the variable name is. It specifies what type of data the variable will hold.
#include<stdio.h>#include<conio.h>void main(){ int a,b,sum; //variable declaraction a=10; b=20; sum=a+b; printf("Sum is %d",sum); getch();}
Md. Asif Iqbal
Dept. of CSE 17
"Data type can be defined as the type of data of variable or constant store.“
When we use a variable in a program then we have to mention the type of data. This can be handled using data type in C.
Followings are the most commonly used data types in C.
Data Types
Md. Asif Iqbal
Dept. of CSE 18
Primary Data Types
Md. Asif Iqbal
Dept. of CSE 19
Integer Types
Integers are whole numbers with a range of values supported by a particular machine.
There are signed integer and unsigned integer. Signed integer uses one bit for sign and other bits for magnitude of the number. Unsigned integers are always positive. It does not contain any bit for sign so that it occupies all the bit for the magnitude of the number.
By using equation -2^n to +(2^n)-1 we can find out the range of the number. Where n is the number of bits.
Md. Asif Iqbal
Dept. of CSE 20
Size and Range of Integer Data
Md. Asif Iqbal
Dept. of CSE 21
Floating Point Types
Floating point numbers are stored in 32 bits with 6 digits of precision.
Floating point numbers are defined in C by the keyword float.
When the accuracy provided by float is not sufficient double data type is used. It uses 64 bits giving a precision of 14 digits.
When you want to extend more precision you can use the long double data type. It uses 80 bits
Md. Asif Iqbal
Dept. of CSE 22
Size and Range of Floating Point Data
Type Size(Bits) Range
float 32 3.4E-38 to 3.4E+38
double 64 1.7E-308 to 1.7E+308
long double 80 3.4E-4932 to 1.1E+4932
Md. Asif Iqbal
Dept. of CSE 23
Void Types
Void type has no values.Void type does not return any values.These are used to specify the return
values from the function when they don’t have any value to return
Md. Asif Iqbal
Dept. of CSE 24
Character Types
Character types data in C are defined by keyword char.
Md. Asif Iqbal
Dept. of CSE 25
Typedef
Using typedef keyword we can define our own user defined data type.
Syntax:typedef type identifier;
Example:typedef int marks;marks sub1,sub2;
Md. Asif Iqbal
Dept. of CSE 26
Enumeration
Declare using keyword enum Syntax: enum identifier {value1, value2, value3,….,valueN};
Example:enum day {Monday, Tuesday,……..,Sunday};
Md. Asif Iqbal