Variable in C programming is basically an identifier which is used to fix a block in memory on the basis of its data type.
1 2 3 4 5 6 7 8 |
int x, y, z; /*x,y and z each have 4 byte space in memory beacuse int size is 4 byte*/ char ch; /*ch has 1 byte in memory to save data because size of char=1 byte*/ float fl; /*fl has 4 byte space in memory to save decimal values*/ |
Here int, char, float are data types and these data types use to fix memory for variables.A variable name can be changed as per our choice.
Syntax:
1 |
DataType variable_name; |
Example :
1 2 3 4 5 |
int x; /*variable name x has integer data type*/ char ch ; /* variable name ch with char data type*/ |
One step ahead from variable declaration if we assign some value at the time of declaration then it is known as variable initialization.
1 2 3 4 5 6 |
int x=5; /*initialize with value 5*/ char ch='a' ; /* initialize with value 'a'*/ int y; /*declaration*/ y=5; /*it is assignment not initialization*/ |
Note:
— In most of the storage class except extern, variables are initialized with some default or garbage data. In extern storage class, declared variable must be initialized to get memory.
— Initialization is only considered if the value is provided at the time of declaration.
— Sometimes initialization is must otherwise declare variable would not be facilitated with
All the rules of identifier construction will imply in variable name construction because variable name is nothing but an identifier.
— In C language variable name can use all 52 alphabets(upper and lower case), integers and only one symbol _(Underscore).
— In C language The first character of the variable name should be always from alphabets or _(Underscore)
— In C language variable name should be less than 31 characters
— In C language variable name must not be in C reserved keyword
See the below table for some example –
As the name suggests Constant is an identifier that can not be changed in the whole program.A variable can also be constant in a program if it is declared with const keyword.
Constants are divided into following categories-
— All the numbers made by the combination of digits 0 to 9 is called decimal numbers like 10, 12039, 1253787289 etc.
— If the decimal number is long then we can use l or L at the end of the number like 123637L or 12789l.
— If the decimal number is unsigned then we can use u or U at the end of the number like 123637U or 12789u.
— if the decimal number is long and unsigned both then we can use ul or UL at the end of the number like 123637UL or 12789ul.
— The octal numbers are made by the combination of digit 0 to 7.
— Any number which has 0 in the prefix is considered as an octal number.
Example: 015,012,014,017 are not treated as a decimal number but it is considered as an octal constant in C language so be careful.
The hexadecimal number made by the combination of following digits.
0,1 ,2 ,3,4,5,6,7,8,9,a,b,c,d,e,F
Example: 0x01, 0x04, 0x1F, 0x0A, 0x0C
Constants with a decimal point or exponential are real or Floating constants.
1 2 3 4 5 6 7 8 9 |
-1.234 +1.234 123.4e-2 1.00000 0.001 |
Sequence of single character under single quote(‘ ‘) is a character constant.
1 2 3 4 5 |
'a' 'A' 'Aaba' not a character constant because it has group of characters. |
A sequence of characters inside the double quotation is a string constant.
1 2 3 4 5 |
"good job" "C is a programming Language" "string Constant" |
There are following escape Sequence Constant.
Escape Sequence | Character |
---|---|
\t | Horizontal Tab |
\v | Vertical tab |
\r | Carriage return |
\\ | Backslash |
\b | BackSpace |
\f | formfeed |
\' | Single quotation |
\? | Question mark |
\" | Double quotation |