c Programming-chapter 02
c Programming-chapter 02
PRESENTED BY
SHEYONA G
CHAPTER-02
Constants, Variables
and Data Types
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
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.
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
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
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
datatype variable_list;
The rules for the naming the variable is as follows.
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.
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.
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.