Concepts of C Programming_Variables and Constants (1)
Concepts of C Programming_Variables and Constants (1)
Introduction to C Language
Variables:
Variables are the identifiers, the named memory location whose value changes on program
execution.
Declaration of variables:
The declaration tells the computer which storage locations or variables to use in the program.
The variables which will be used in the program should be declared at the beginning of the
program.
The variable declaration indicates the type of constant that the variable can hold and the amount
of memory allocated to the variable.
The name of the variables should be declared as per the rules of declaring identifiers or variables.
General Syntax: datatype variable;
(or)
datatype variable1, variable2,……..variable_n;
Example: 1. int a; 2. float x,y; 3. double sum, midun_06;
From the examples we will come to know that
✓ “a” is a variable of type integer and allocates 2 bytes of memory.
✓ “x” and “y” are two variable of type float which will be allocated 4 bytes of memory for each
variable.
✓ “sum” and “midun_06” are two double type variables which will be allocated with 8 bytes of
memory for each.
Variable Initialization:
Variables can be initialized at two instances -
✓ Compile Time ( using Assignment Statement )
✓ Run Time ( using Standard Input function : scanf( ))
String Constant
A character string, a string constant consists of a sequence of characters enclosed in double
quotes.
A string constant may consist of any combination of digits, letters, escaped sequences and
spaces.
Note that a character constant ۥA’ and the corresponding single character string constant "A" are
not equivalent.
The string constant "A" consists of character A and \0. However, a single character string
constant does not have an equivalent integer value. It occupies two bytes, one for the ASCII
code of A and another for the NULL character with a value 0, which is used to terminate all
strings.
Valid String Constants: -
"W"
"100"
"24, Kaja Street"
Invalid String Constants: -
"W the closing double quotes missing
Raja" the beginning double quotes missing
Rules for Constructing String constants
❖ A string constant may consist of any combination of digits, letters, escaped sequences
and spaces enclosed in double quotes.
❖ Every string constant ends up with a NULL character which is automatically assigned
(before the closing double quotation mark) by the compiler.