C Programming1

Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 22

Constants ,Variables and Data types

CHARACTER SET OF C LANGUAGE


 A symbol that is used while writing a program is called a character.
 A character can be:

Alphabets/Letters (Lowercase a-z, Uppercase A-Z)


Digits (0-9)
Special Symbols ( ~ ‘ ! @ # % & * () - + / $ = \ {  } [ ] : ; “ “ ? etc)
C TOKENS
 Tokens are the smallest or basic units of C program.
 One or more characters are grouped in sequence to form meaningful words.

 these meaningful words are called as tokens.

 A token is collection of characters.

 Tokens are classified in to 5 types as below:

1. Keywords

2. Identifiers

3. Constants

4. Operators

5. Special symbols
KEYWORDS
 The tokens which have predefined meaning in C language are called keywords.
 They are reserved for specific purpose in C language they are called as Reserved
Words. There are totally 32 keywords supported in C they are:
 auto ,double, if ,static ,break ,else, int ,struct, case, enum, long ,switch ,char,
extern,near etc.
 Rules for keywords

1. Keywords should not be used as variables ,function names, array names etc.
2. All keywords should be written in lowercase letters.
3. Keywords meaning cannot be changed by the users.
IDENTIFIERS
 Identifiers are the names given to program elements such as variables, constants,
function names, array names etc
 It consists of one or more letters or digits or underscore.

 Rules for identifiers

1. The First character should be an alphabet or an underscore _ Then First character is


followed by any number of letters or digits.
2. No extra symbols are allowed other than letters ,digits and Underscore
3. Keywords cannot be used as an identifier
4. The length can be 31 characters for external, 63 for internal.
5. Identifiers are case sensitive.
Example: Area, Sum_ 234abc for 23_0 _123 ab AB a b
CONSTANTS
 Constants refers to fixed values that do not change during the execution of a program
 The different types of constants are:

1. Integer constant
2. Real constant/Floating Pointing constant
3. Enumeration constant
4. Character constant
5. String constant
1. INTEGER CONSTANT
 An integer is whole number without any decimal point. no extra characters are
allowed other than + or - .
Three types of integers
1) Decimal integers: are constants with a combination of digits 0 to 9,optional + or -
Ex: 123 , -345, 0 , 5436 , +79
2)Octal integers: are constants with a combination of Digits from 0 to 7 but it has a
prefix of 0
Ex: 027xo , 0657 , 0777645
3)Hexadecimal integers: Digits from 0 to 9 and characters from a to f, it has to start
with 0X or 0x Ex: 0X2 0x56 0X5fd 0xbdae
2. REAL CONSTANTS/FLOATING POINT
 Floating point constants are base 10 numbers that are represented by fractional parts, such as
10.5.They can be positive or negative.
Two forms are:
1)Fractional form:
A floating point number represented using fractional form has an integer part followed by dot
and a fractional part.
Ex: 0.0083 , 215.5 -71 , +0.56 etc..

2) Exponential form:
A floating point number represented using Exponent form has 3 parts
mantissa e exponent ,mantissa is either real number expressed in decimal notation or integer.
Exponent must be integer with optional + or -
Ex 9.86 E 3 => 9.86*103 ,9.86 E -3 => 9.86*10-3
3. ENUMERATION CONSTANT
 A set of named integer constants defined using the keyword enum are called
enumeration constants.
 Syntax: enum Identifier{enum list};

Ex:
1) enum Boolean{NO,YES};
NO is assigned with 0
YES is assigned with value 1
2) enum days{ mon,tue.wed};
mon is assigned with 0
true is assigned with 1
wed is assigned with 2
4.CHARACTER CONSTANT
 A symbol enclosed within pair of single quotes is called character constant.
 Each character is associated with unique value called ASCII value.

Ex: ‘9’ , ‘$’ , Backslash constants(Escape sequence character)


 Definition: An escape sequence character begins with backslash and is followed by
one character.
 A backslash along with a character give rise to special print effects.

 It always start with backslash ,hence they are called as backslash constants.
CHARACTER NAME MEANING

\a Bell Beep sound


\b Back space Cursor moves towards left by one
position
\n New line Cursor moves to next line
\t Horizontal tab Cursor moves towards right by 8
position
\r Carriage return Cursor moves towards beginning of the
same line
\” Double quotes Double quotes
\’ Single quotes Single quotes

\? Question mark Question mark

\\ Backslash Backslash

\0 NULL
5.STRING CONSTANT
 A sequence of characters enclosed within pair of double quotes is called
string constant.

 The string always ends with a NULL character. Ex: “9 \0” , “SVIT”

 Special char

+-*/ % && || ? . # $> < <=~~~~~~


DATA TYPES AND SIZES
 The data type defines the type of data stored in memory location or the type
of data the variable can hold.
The classification of data types are:
1. Fundamental Datatypes

2. Derived Datatypes

There are few basic data types supported in C.


1. int
2. float
3. double
4. char
5. void
1.INTEGER DATA TYPE (INT):
It stores an integer value or integer constant.
 An int is a keyword using which the programmer can inform the compiler
that the data associated with this keyword should be treated as integer.

2 byte 16 bits 2pow 16 1 bit sign 15


 C supports 3 different types of integer:

1. Short int

2. Int

3. Long int
2. FLOATING DATA TYPE (FLOAT):
 An float is a keyword using which the programmer can inform the compiler
that the data associated with this keyword should be treated as floating point
number.
 The size is 4 bytes.

 The range is -3.4e38 to +3.4e38.

 Float can hold the real constant accuracy up to 6 digits after the decimal
point.
3.DOUBLE DATA TYPE (DOUBLE):
 An double is a keyword using which the programmer can inform the
compiler that the data associated with this keyword should be treated as long
floating point number.
 The size is 8 bytes.

 The range is from 1.7e-308 to 1.7 e+308.

 Double can hold real constant up to 16 digits after the decimal point.
4.CHARACTER DATA TYPE (CHAR):
 char is a keyword which is used to define single character or a sequence of
character in c language capable of holding one character from the local
character set.
 The size of the character variable is 1 byte.

 The range is from -128 to +127.

 Each character stored in the memory is associated with a unique value


termed as ASCII (American Standard Code for Information Interchange).
 It is used to represent character and strings.
5. VOID DATA TYPE (VOID):
 It is an empty data type.
 No memory is allocated.

 Mainly used when there are no return values.


VARIABLES
A variable is a name given to memory location where data can be stored.
RULES FOR VARIABLES
 The First character should be an alphabet or an underscore _
 Then First character is followed by any number of letters or digits.
 No extra symbols are allowed other than letters ,digits and Underscore
 Keywords cannot be used as an identifier Example: Sum – valid
for1- valid
for -invalid (it is a keyword)
DECLARATION OF VARIABLES:
 Giving a name to memory location is called declaring a variable.
 Reserving the required memory space to store the data is called defining a variable.
 General Syntax:
datatype variable; (or)
datatype variable1, variable2,……..variablen; example: int a;
float x, y;
double sum;
5 VARIABLE INITIALIZATION

 Variables are not initialized when they are declared and defined, they contain garbage
values(meaningless values)
 The method of giving initial values for variables before they are processed is called variable
initialization.
 General Syntax:

Var_name = expr;
Where, Var_name is the name of the variable ,
expr is the value of the expression.
The “expr” on the right hand side is evaluated and stored in the variable name (Var_name) on left hand
side.
The expression on the right hand side may be a constant, variable or a larger formula built from simple
expressions by arithmetic operators.
Examples: int a=10; // assigns the value 10 to the integer variable a
float x; x=20; // creates a variable y of float type and assigns value 20 to it

You might also like