[go: up one dir, main page]

0% found this document useful (0 votes)
30 views42 pages

Chapter - 2

The document discusses constants, variables, and data types in the C programming language. It defines constants as named memory locations that hold a single value, and variables as names given to memory locations that can store different values of the same data type. The document outlines how to create constants using the const keyword and #define preprocessor, and how to declare and name variables and constants in C. It also introduces the basic data types in C like integer, float, double, char, and void.

Uploaded by

Arya Patel
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)
30 views42 pages

Chapter - 2

The document discusses constants, variables, and data types in the C programming language. It defines constants as named memory locations that hold a single value, and variables as names given to memory locations that can store different values of the same data type. The document outlines how to create constants using the const keyword and #define preprocessor, and how to declare and name variables and constants in C. It also introduces the basic data types in C like integer, float, double, char, and void.

Uploaded by

Arya Patel
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/ 42

CE143: COMPUTER CONCEPTS & PROGRAMMING

UNIT – 2

Constants, Variables & Data Types in ‘C’

Devang Patel Institute of Advance Technology and Research


Objectives
 To be able to create and use variables and constants.
 To be able to distinguish the constants of different data
type.
 To be able to name, declare, initialize and assign values to
variables.
 To become familiar with fundamental data types.
 To be able to list, describe, and use the C basic data types.

Chapter – 2 : Constants, Variables & Data Types in ‘C’


Introduction
 In this chapter, we will discuss
 Character set
 C Tokens
 Constants (integer, real, character, string, enum), symbolic
constants
 Variables (name, declare, assign)
 Fundamental data type ( int, float, double, char, void)

Chapter – 2 : Constants, Variables & Data Types in ‘C’


Character Set
 Every language contains a set of characters used to
construct words, statements etc.
 C language character set contains the following set of
characters...
 Alphabets
 Digits
 Special Symbols
 White spaces
 Compiler ignores white spaces unless they are part of a string constant.
 White spaces may be used to separate words but prohibited between
characters of keywords and identifiers.

Chapter – 2 : Constants, Variables & Data Types in ‘C’


Character Set
 LETTERS: Uppercase A….Z, lower case a..z

 DIGITS: All decimal digits 0..9

 SPECIAL CHARACTERS: comma(,), period(.), semicolon(;),


colon(:), question mark(?), quotation(“), dollar sign($),
slash(/),back slash(\), percent sign(%), underscore(_),
ampersand(&), asterisk(*), number sign(#).

 WHITE SPACES: Blank space, Horizontal tab, Carriage


return, Newline, Form feed.

Chapter – 2 : Constants, Variables & Data Types in ‘C’


Trigraph characters
 C introduces the concept of trigraph sequences to provide a
way to enter certain characters that are not available on
some keywords.
 Each Trigraph sequence consists of three characters, 2
question marks followed by another character.
Trigraph sequence Equal character
??= #
??( [
??) ]
??/ \
??< {
??> }
??! |
??’ ^
??- ~

Chapter – 2 : Constants, Variables & Data Types in ‘C’


C Tokens
 Every C program is a collection of instructions and every
instruction is a collection of some individual units.
 Every smallest individual unit of a c program is called token.
 Every instruction in a c program is a collection of tokens.
 Tokens are used to construct c programs and they are said
to the basic building blocks of a c program.

Chapter – 2 : Constants, Variables & Data Types in ‘C’


C Tokens

Chapter – 2 : Constants, Variables & Data Types in ‘C’


Keywords
 Keywords are the reserved words with predefined
meaning which already known to the compiler.
 Properties of Keywords
 All the keywords are defined as lowercase letters so they must be
use only in lowercase letters
 Every keyword has a specific meaning, users can not change that
meaning.
 Keywords can not be used as user defined names like variable,
functions, arrays, pointers etc...
 Every keyword represents something or specifies some kind of
action to be performed by the compiler.

Chapter – 2 : Constants, Variables & Data Types in ‘C’


Keywords

Chapter – 2 : Constants, Variables & Data Types in ‘C’


Identifiers
 Identifier is a user defined name of an entity to identify it
uniquely during the program execution.
 Example:
int marks;
char studentName[30];
 Here, marks and studentName are identifiers.

Chapter – 2 : Constants, Variables & Data Types in ‘C’


Identifiers
 Rules for creating Identifiers
 An identifier can contain letters (UPPERCASE and lowercase),
numerics & underscore symbol only.
 An identifier should not start with numerical value. It can start with
a letter or an underscore.
 We should not use any special symbols in between the identifier
even whitespace. However, the only underscore symbol is allowed.
 Keywords should not be used as identifiers.
 There is no limit for length of an identifier. However, compiler
consider first 31 characters only.
 An identifier must be unique in its scope.

Chapter – 2 : Constants, Variables & Data Types in ‘C’


Examples of Valid and Invalid Names

Chapter – 2 : Constants, Variables & Data Types in ‘C’


Constants
 A constant is a named memory location which holds only
one value throughout the program execution.

Chapter – 2 : Constants, Variables & Data Types in ‘C’


Creating constants in C
 In c programming language, constants can be created using
two concepts...
 Using 'const' keyword

 Using '#define' preprocessor

Chapter – 2 : Constants, Variables & Data Types in ‘C’


Creating constants in C
 Using 'const' keyword
 To create a constant, we prefix the variable declaration with 'const'
keyword.
 The general syntax for creating constant using 'const' keyword is as
follows...
const datatype constantName ;
OR
const datatype constantName = value ;

 Example
const int x = 10 ;
 Here, 'x' is a integer constant with fixed value 10.

Chapter – 2 : Constants, Variables & Data Types in ‘C’


Creating constants in C
 Using '#define' preprocessor
 To create constant using this preprocessor directive it must be
defined at the beginning of the program (because all the preprocessor
directives must be written before the gloabal declaration).
 We use the following syntax to create constant using '#define'
preprocessor directive…
#define CONSTANTNAME value

 Example:
#define PI 3.14
 Here, PI is a constant with value 3.14

Chapter – 2 : Constants, Variables & Data Types in ‘C’


Constants in C- Example Program
//Using 'const' keyword //Using '#define' preprocessor

#include <stdio.h> #include <stdio.h>


void main() #defien PI 3.14
{ void main()
int i = 9 ; {
const int x = 10 ; int r, area ;
printf("Please enter the radius of circle : ") ;
i = 15 ; scanf("%d", &r) ;
x = 100 ; // creates an error
printf("i = %d\n x = %d", i, x ) ; area = PI * (r * r) ;
} printf("Area of the circle = %d", area) ;
}
 The above program gives
an error because we are
trying to change the constant
variable value (x = 100).

Chapter – 2 : Constants, Variables & Data Types in ‘C’
Backslash character constants
 C supports special backslash character constants that are
used in output functions.
 These character combinations are known as escape
sequences.

Chapter – 2 : Constants, Variables & Data Types in ‘C’


Variables
 Variable is a name given to a memory location where we
can store different values of same datatype during the
program execution.
 It must be declared in the declaration section before it is
used.
 It must have a datatype that determines the range and type
of values to be stored.
 A variable name may contain letters, digits and underscore
symbol.

Chapter – 2 : Constants, Variables & Data Types in ‘C’


Variables
 The following are the rules to specify a variable name...
 Variable name should not start with digit.
 Keywords should not be used as variable names.
 Variable name should not contain any special symbols except
underscore(_).
 Variable name can be of any length but compiler considers only the
first 31 characters of the variable name.

 Valid variables are:  Invalid variables are:


 john  123
 x1  (area)
 T_raise  25th
 first_tag  price$
 %

Chapter – 2 : Constants, Variables & Data Types in ‘C’


Declaration of Variables
 Declaration allocate required amount of memory with
specified variable name and datatype values into that
memory location.
 The declaration can be performed either before the
function as global variables or inside any block or function.
 It must be at the beginning of block or function.
 Syntax:
Datatype variableName;
 Example
int number;
 The above declaration allocate 2 bytes(32 bit Compiler)/4
bytes(64 bit Compiler) of memory with the name number and
allows only integer values into that memory location.

Chapter
Chapter – 2 : Algorithms
– 2 : Constants, Variables
and&Flowcharts
Data Types in ‘C’
Datatypes
 Datatype is a set of value with predefined characteristics.
 Datatypes are used to declare variable, constants, arrays, pointers and
functions.
 The memory size and type of value of a variable are determined by
varible datatype.

User-defined Data Types


The user defined data types enable a program to
invent his own data types and define what values
it can taken on

Chapter – 2 : Constants, Variables & Data Types in ‘C’


Datatypes- Primary Datatypes
 All the primary datatypes are already defined in the system.
 Primary datatypes are also called as Built-In datatypes.
 The following are the primary datatypes in c programming lanuage...

Chapter – 2 : Constants, Variables & Data Types in ‘C’


Datatypes- Integer Datatype
 The keyword "int" is used to represent integer datatype in c.
 The integer datatype is used with different type modifiers like short,
long, signed and unsigned.
 The following table provides complete details about integer datatype.

Chapter – 2 : Constants, Variables & Data Types in ‘C’


Datatypes- Floating Point Datatypes
 Floating point datatypes are set of numbers with decimal value.
 The floating point datatype has two variants...
 float

 double

 Both float and double are similar but differ in number of decimal
places.
 float value contains 6 decimal places.
 double value contains 15 or 19 decimal places.
 The following table provides complete details about floating point
datatypes.

Chapter – 2 : Constants, Variables & Data Types in ‘C’


Datatypes- Character Datatype
 Character datatype is a set of characters enclosed in single quotations.
 The following table provides complete details about character
datatype.

Chapter – 2 : Constants, Variables & Data Types in ‘C’


Datatypes
 The following table provides complete information about all the
datatypes in c programming language.

Chapter – 2 : Constants, Variables & Data Types in ‘C’


Datatypes- void Datatype
 The void datatype means nothing or no value.
 Generally, void is used to specify a function which does not return any
value.
 We also use the void datatype to specify empty parameters of a
function.

Chapter – 2 : Constants, Variables & Data Types in ‘C’


Declaration of Storage Class
 Storage class in C decides the part of storage to allocate memory
for a variable, it also determines the scope of a variable.
 All variables defined in a C program get some physical location
in memory where variable's value is stored.
 Memory and CPU registers are types of memory locations where
a variable's value can be stored.
 The storage class of a variable in C determines the life time of
the variable if this is 'global' or 'local'.
 Along with the life time of a variable, storage class also
determines variable's storage location (memory or registers),
the scope (visibility level) of the variable, and the initial value of
the variable.

Chapter – 2 : Constants, Variables & Data Types in ‘C’


Declaration of Storage Class

Chapter – 2 : Constants, Variables & Data Types in ‘C’


Declaration of Storage Class

 The general form of a variable declaration that uses a storage class is


shown here:

storage_class_specifier data_type variable_name;

 At most one storage class specifier may be given in a declaration.


 If no storage class specifier is specified then following rules are used:
 Variables declared inside a function are taken to be auto.
 Functions declared within a function are taken to be extern.
 Variables and functions declared outside a function are taken to be static,
with external linkage.

Chapter – 2 : Constants, Variables & Data Types in ‘C’


Assigning values to variables

 The general form of a variable declaration that uses a storage class is


shown here:
 The syntax is
Variable_name=constant
 Example:
int a=20;
bal=75.84;
yes=’x’;
 C permits multiple assignments in one line.
 Example:
initial_value=0;final_value=100;

Chapter – 2 : Constants, Variables & Data Types in ‘C’


Reading data from keyword

 Another way of giving values to variables is to input data through


keyboard using the scanf function.
 The general format of scanf is as follows.
scanf(“control string”,&variable1,&variable2,….);
 The ampersand symbol & before each variable name is an operator
that specifies the variable name’s address.
 Example:
scanf(“%d”,&number);

Chapter – 2 : Constants, Variables & Data Types in ‘C’


#define Directive
 The #define directive allows the definition of macros within your
source code.
 These macro definitions allow constant values to be declared for use
throughout your code.
 The syntax for creating a constant using #define in the C language is:

value:
#define CNAME value
The value of the constant.
OR
#define CNAME (expression)

CNAME: expression:
The name of the constant. Expression whose value is
Most C programmers define assigned to the constant.
their constant names in The expression must be
uppercase, but it is not a enclosed in parentheses if it
requirement of the C contains operators.
Language.
Chapter – 2 : Constants, Variables & Data Types in ‘C’
#define Directive - Example
#include <stdio.h>

#define NAME "Charusat"


#define YEAR 2000

int main()
{
printf("%s is established in year %d\n", NAME, YEAR);
return 0;
}

Chapter – 2 : Constants, Variables & Data Types in ‘C’


Difference between #define & const
 The big advantage of const over #define is type checking.
 We can typecast them and any other thing that can be done with a
normal variable.
 One disadvantage that one could think of is extra space for variable
which is immaterial due to optimizations done by compilers.
 In general const is a better option if we have a choice. There are
situations when #define cannot be replaced by const. For example,
#define can take parameters. #define can also be used to replace some
text in a program with another text.

Chapter – 2 : Constants, Variables & Data Types in ‘C’


Declaring a variable as volatile

 ANSI standard defines another qualifier, volatile that could be used to


tell explicitly the compiler that a variable's value may be changed at
any time by some external sources.
 By declaring a variable as volatile, its value may be changed at any
time by some external source.
 Example:
volatile int date;
 The date may be altered by some external factors even if it does not
appear on the left hand side of assignment statement.

Chapter – 2 : Constants, Variables & Data Types in ‘C’


Overflow and Underflow of Data
 Based on the storage size of data types in any particular system there
is a fixed range of values that can be stored in a variable of a given data
types.
 Overflow: The assignment of a value larger than the storage capacity
of the data type of a variable can hold.
 Underflow: The assignment of a value smaller than the storage
capacity of the data type of a variable can hold.
 C does not provide any warning or indication of Overflow and
underflow, It simply gives incorrect results.
 So, It is recommended to define correct data type for handling the
input/output values.

Chapter – 2 : Constants, Variables & Data Types in ‘C’


Datatypes- Derived Datatypes
 Derived datatypes are user-defined data types.
 The derived datatypes are also called as user defined datatypes or
secondary datatypes.
 In c programming language, the derived datatypes are created using
the following concepts...
 Arrays

 Structures

 Unions

 Enumeration

Chapter – 2 : Constants, Variables & Data Types in ‘C’


Previous Year Questions
 Classify following variables into valid & invalid variable name
in C.
%, (area), mark, 25th, 123, distance
 What are the advantages of user defined function?
 Classify between valid and Invalid variable names?
RAM, ANSI, GJ@India, Float variable, One_int, ID#22
 Give difference between variables and constants.

Chapter – 2 : Constants, Variables & Data Types in ‘C’


Previous Year Questions
 Why and When do we use the #define directive? OR
Explain #define?
 What are trigraph characters? How are they useful?
 Explain C Tokens with example?
 Explain three types of datatypes with example?
 Is main() a user defined function? if yes how does it differ
from other user-defined function? if No, give reason for it.

Chapter – 2 : Constants, Variables & Data Types in ‘C’

You might also like