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

c Programming-chapter 02

The document provides an overview of constants, variables, and data types in the C programming language, detailing the character set, tokens, keywords, identifiers, and various types of constants and data types. It explains the rules for naming variables, the significance of special symbols, and the classification of data types into primitive, user-defined, and derived types. Additionally, it includes syntax examples and assignments for practical understanding.

Uploaded by

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

c Programming-chapter 02

The document provides an overview of constants, variables, and data types in the C programming language, detailing the character set, tokens, keywords, identifiers, and various types of constants and data types. It explains the rules for naming variables, the significance of special symbols, and the classification of data types into primitive, user-defined, and derived types. Additionally, it includes syntax examples and assignments for practical understanding.

Uploaded by

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

WELCOME

PRESENTED BY
SHEYONA G
CHAPTER-02
Constants, Variables
and Data Types
CHARACTER SET

It denotes any alphabet, digit or special symbol used to represent information.


USE OF CHARACTER:

Characters can be combined to form variables.

C uses constants, variables, operators, keywords and expressions as building blocks to

form a basic C program.


CHARACTER SET
The character set is the fundamental raw material for any language and they are used to
represent information. Similarly, computer language also have a well- defined character
set, which is useful write statements. These statements written in a logical sequence
will form a C program.
The characters in C are grouped into the following two categories:
a. Source character set
1. Alphabets
2.Digits
3.Special Characters
4.White Spaces
b. Execution character sets
Escape Sequences (Back slash characters)
Source character set

Names Symbols

Alphabets A,B,C,D,E,F,G,H,I,J,K,L,M,N,O,P,Q,R,S,T,U,V,W,X,Y,Z
a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q,r,s,t,u,v,w,x,y,z

Digits 0, 1, 2, 3, 4, 5, 6, 7, 8, 9

Special symbols ~‘!@#%^&*()_-+=|\{}[]:;"'<>,.?/

White Spaces Tab Or New line Or Space


Special Characters
Backslash Character Constants
C TOKENS
C Tokens are the smallest building block or smallest unit of a C program. The compiler breaks a program
into the smallest possible units and proceeds to the various stages of the compilation, which is called
token.
C program is also a collection of tokens, comments, and whitespaces. In C, Programming tokens are of six
types. They are:
KEYWORD
Each keyword is meant to perform a specific function in a program.
a. Keywords are predefined reserved words whose meaning is special and it is already
described to the compiler
b. These meanings cannot be changed
c. They are fixed and cannot be used as identifiers (Variable names, function names,
structure names)
d. All keywords are written in lower case letters. (C is case sensitive)
e. There are 32 keywords in C
Keywords are written in lowercase letters.
CONSTANTS
The term constants refers to the fixed value. A Constant is that whose value is never changed at the end of
the program.
For example: 3x+2y=10 Here 3, 2 and 10 are constants.

Constants are also called as literals.


They are declared with the keyword ‘const’.
There are 5 types of constants.
IDENTIFIERS
An identifier is a name that is assigned by the user for a program element such as variable, type, template,
function or namespace, structure names.
identifier must follow some rules.

 All identifiers can have letters (both uppercase and lowercase letters), digits and underscore only.

 First character in a variable name must be an alphabet or an underscore Few compilers allow currency

character($)

 Commas, blanks or special symbols are not allowed within a variable name.

 After the first character, an identifier can have any combination of characters (alphanumerical).
 C keywords cannot be used as an identifier.

 Identifiers in C are case sensitive, M2A and m2a are two different identifiers.

 A variable name can be up to 8 characters in length, but some compilers allow variable names up to

31 characters.

 Two identifiers cannot have the same name.

 It has to be declared before it is referred.


STRING
 A String is a collection of Characters. It is a sequence of characters terminated with a null
character \0.
 Strings are always enclosed within double quotes.
 When the compiler encounters a sequence of characters enclosed in the double quotation marks,
it appends a null character \0 at the end by default.

Example: “c string”.
SPECIAL SYMBOLS
The following special symbols are used in C having some special meaning and thus, cannot be used for
some other purpose.
[],(),{},;,*,=,#

 Brackets[]: Opening and closing brackets are used as array element reference.
 Parentheses(): The parentheses are used to indicate function calls and function parameters.
 Braces{}: These opening and ending curly braces mark the start and end of a block of code
containing more than one executable statement
 Semicolon (; ): It is used to separate each statement. In c the statements end with semicolon.
 Comma (,): It is an operator that essentially invokes something called an initialization list.
 Asterisk (*): It is used to create pointer
 Preprocessor(#): The preprocessor is a macro processor that is used automatically by the
compiler to transform your program before actual compilation.
 Assignment operator (=): It is used to assign values
OPERATORS

 Operators in C is a special symbol used to perform special function.


 Operators are applied between the operands.
 Depending upon the operands , operators are classified into two:
Unary Operator
Binary Operator
Unary Operators:
Unary operator is an operator applied to the single operand.
Example:- increment operator (++)and decrement operators(--).

Binary Operators:
Binary operator is an operator applied between two operands.
• Example:-
• Arithmetic Operators
• Relational Operators
• Shift Operators
• Logical Operators
• Bitwise Operators
• Conditional Operators
• Assignment Operator
CONSTANTS
A constant is a value assigned to the variable which will remain the same throughout the
program, i.e., the constant value cannot be changed.
C supports several types of constants in C language as
CONSTANTS

Backslash Character
Numeric Constants Real Constant Symbolic constants
constants
1. Integer Constant
2. Decimal Integer
constant 1. Single Character
3. Octal integer Constant
constant 2. String Constant
4. Hexadecimal
Integer constant
Rules for constructing Integer constants

i. An integer constant must have at least one digit.


ii. It must not have a decimal point.
iii. It can be either positive or negative.
iv. If no sign precedes an integer constant, it is assumed to be positive.
v. Commas or blanks are not allowed within an integer constant.
Rules for Constructing Real Constants in Fractional Form

1. A real constant must have at least one digit.


2. It must have a decimal point.
3. It could be either positive or negative.
4. Default sign is positive.
5. Commas or blanks are not allowed within a real constant.
Rules for Constructing String constants

1) A string constant may consist of any combination of digits, letters, escaped sequences and
spaces enclosed in double quotes.
2) Every string constant ends up with a NULL character which is automatically assigned
(before the closing double quotation mark) by the compiler.
VARIABLES:
 A variable is a name of the memory location. It is used to store data. Its value can be

changed, and it can be reused many times.

 It is a way to represent memory location through symbol so that it can be easily

identified. syntax to declare a variable:

datatype variable_list;
The rules for the naming the variable is as follows.

1. The first character must be an alphabet or an underscore.


2. Variables are case sensitive i.e. Total is different from total.
3. Keywords are not allowed.
4. Digits are allowed.
5. No special characters are allowed except underscore.
6. No white spaces or blank spaces are allowed.

Some examples of variable are


Amount, total, TOTAL, Total, Mark1, Mark2, AVG_Mark.
DECLARATION OF A VARIABLE
Syntax: (Syntax is the rule how to use in the program)

datatype variablename;

Where
 datatype - int, float, char, double…. etc.,
 Variable name - Name given to the memory location (Follow the rule for naming
the variable)
Example:-
 int age;
 Int rollnumber
 Float average
DECLARING A VARIABLE AS CONSTANT, VOLATILE

C’s volatile keyword is a qualifier that is applied to a variable when it is declared. It tells the
compiler that the value of the variable may change at any time–without any action being taken
by the code the compiler finds nearby.
To declare a variable volatile, include the keyword volatile before or after the data type in the
variable definition.

Syntax
Volatile data type variable name;
Example:
volatile int foo
Const Qualifier in C
The qualifier const can be applied to the declaration of any variable to specify that its value will
not be changed
To declare a variable constant, include the keyword const before or after the datatype in the
variable definition.

Syntax:
const data type variable name;

Example:
const int foo;
DATA TYPES:
Data types in C programming language enables the programmers to appropriately select the
data as per requirements of the program and the associated operations of handling it.

Data types in c language can be broadly classified as:


1. Primitive Data Types
2. User Defined Data Types, for example, enum, structure, union
3. Derived Data Types, for example, array, pointers
Primitive Data Types
The primitive data types in c language are the inbuilt data types provided by the c language itself.
Thus, all c compilers provide support for these data types. The following primitive data types in c
are
 Integer:
An integer number is a sequence of digits without decimal point. It can store both positive and
negative values.
Keyword for integer is “int”. The size of the integer data type is 2 bytes.
The range of integer is -32,768 to +32,767.
Ex: 12, -67, 30000, 0, -54.
Syntax :
int variable_name;
 Float:
The floating point numbers are sequence of digits with decimal point. It is also called real
numbers.
The keyword for floating values is “float”.
They are associated with the variables to store decimal point values in memory locations.
The size of float is 4 bytes. It can store both negative and positive values.
Ex:
float a=34.56, b=-65.54;
Output: 34.560000, -65.540000
Syntax
float variable_name;
 Double:
Double data type also declares variable that can store floating point numbers but gives
precision double than that provided by float data type.
double data type is also referred to as double precision data type.
Keyword for double data type is “double”.
Size of double is 8 bytes.

Syntax
double variable_name;
 Character

Character data type declares a variable that can store a character constant.
The variables declared as char data type can only store one single character.
It is a keyword which is used to define single character or sequence of characters called as
string
The size of char is 1 byte.
Ex: char name[5]={ ‘e’, ’x’, ‘a’, ‘m’ };
char var[5]=“exam”;
Syntax
char variable_name;
 Void Data Type

Void data type does not create any variable but returns an empty set of values.

Thus, we can say that it stores null.

Syntax :

void variable_name;
DATA TYPE QUALIFIERS
The data type qualifiers available in c are:
 short
 long
 signed
 unsigned
ASSIGNMENTS
1. What are the character set in C language?
2. List out any 4 keywords in C language.
3. How will you declare an integer constant in C language
4. Distinguish between character constant and string constant in C language.
5. What is keyword? Give 2 example
6. What is variable? How to declare a variable in C language?
7. Write short notes on integer data type.
8. Write short notes on character data type.
9. How to assign a value to a variable?
10.What you mean by a constant variable
11.How will you declare a constant and volatile keyword in C language?
12.Write a program in C to find area of a rectangle.

You might also like