What is the difference between a constant and variable? Explain with example.
A C constant is usually just the written version of a number. For example 1, 0, 5.73, 12.5e9. We can specify our constants in octal or hexadecimal, or force them to be treated as long integers.
- Octal constants are written with a leading zero - 015.
- Hexadecimal constants are written with a leading 0x - 0x1ae.
- Long constants are written with a trailing L - 890L.
Character constants are usually just the character enclosed in single quotes; 'a', 'b', 'c'. Some characters can't be represented in this way, so we use a 2 character sequence.
'\n' - newline
'\t' - tab
'\\' - backslash
'\'' - Single Quote
'\0' - null ( used automatically to terminate character strings)
In C, a variable must be declared before it can be used. Variables can be declared at the start of any block of code, but most are found at the start of each function. Most local variables are created when the function is called, and are destroyed on return from that function.
A declaration begins with the type, followed by the name of one or more variables. For example,
int high, low, results[20];