Some important points to keep rembers (contd..)
- Constants => 1. Numeric Constants, 2.Character constants
- Numeric constants => 1. Integer, 2. Real
- Character constants => 1. Single character, 2. String constants
- Integer Constants
- Decimal => 0 to 9 with optional + or –
- Octal => 0 to 7 with leading 0
- Hexadecimal – 0 to 9, A to F
-
The largest integer value that can be stored is machine-dependent
For 16 bit machines => 32767
For 32 bit machines => 2,147,483,647
- It is also possible to store larger integer constants by appending qualifiers such as U(unsigned), L (long), UL(unsigned long)
-
Real (floating point) constants – to store numbers with fractional parts e.g. 3.14, -5.023
Real number may also be expressed in exponential (or scientific) form e.g., 3.142148e5 - Single character constants enclosed with a pair of single quotes
e.g. ‘A’, ‘c’
-
String constants – enclosed with double quotes
e.g . “Hello!” - Backslash character constants
\a – audibl alert
\b – back space
\f – form feed
\n – new line
\r – carriage return
\t – horizontal tab
\v – vertical tab
\” – double quote
\? - Question mark
\\ - backslash
\0 – null
- Variables are data names to store a data value
- Rules for variables are same as identifiers
- ANSI standard recongnizes a length of 31 characters, however in C99, at least 63 characters are significant
- Data Types
- Primary (or fundamental) data types
- Derived data types
- User-defined data types
-
Five fundamental data types
integer (int), character (char), floating point (float), double-precision floating point (double) and void - Extended data types : long int, long double
-
C99 – Additional data types:
_Bool, _Complex, _Imaginary - Data type ranges in 16-bit machines
char -128 to 127
int -32,768 to 32,767
float 3.4e-38 to 3.4e+e38
double 1.7e-308 to 1.7e+308
- Integer types : short int, int, long int
- Floating point types : float, double, long double
- User-defined type declaration based on existing type:
typedef type indentifier
e.g. : typedef int marks
- Enumerated data types
enum identifier{value1, value2, … valuen}
e.g. enum day{Monday, Tuesday,…. Sunday}
- Storage Classes – information about their location and visibility
auto – Local variable known only to the function in which it is declared (default)
static – retains its value even after the control is transferred to the calling function
extern – Global variable known to all functions in a file
register – Local variable which is stored in register
- C Output format specifies
%i or %d |
int |
%c |
char |
%f |
float |
%lf |
double |
%s |
string |
e.g.
printf(“%3d”,n)
Here, the number (3) tells the compiler that we want to reserve 3 digit spaces for output.
printf(“%4.2lf”,n)
Here, totally 4 digit spaces with 2 digit decimal places.
- Reading data from keyboard
scanf(“control string”, &variable1, &variable2,…);
e.g., scanf(“%f %f %d”, &amunt, &intrate, &years);
(For reading data use “&”(It specifies address of the variable), for printf no need.)
- Some compilers permit using prompt message with scanf statement. e.g. scanf(“Enter age %d”, &age);
- Symbolic constants are defined using #define directive.
e.g. #define PI 3.14159
- No ‘=’ sign
- No space between define and ‘#’
- No semicolon (;)
- No multiple definitions in single statement (line)
- ‘define’ should be in lowercase
- No symbol ($) permitted
- A variable can be defined as constant using ‘const’ data type qualifier. It tells the compiler that the value of the variable must not e modified by the program.
e.g. cons tint class_size = 40;
-
In contra with ‘const’, ‘volatile’ qualifier can be used to tell explicitly the compiler that a variable’s value may be changed at any time by some external sources.
e.g. volatile int date; - To avoid changing of a variable by its own program (but external resources allowed to modify), the variable can be defined as follows:
e.g volatile const int location = 100;
- The new type qualifier ‘restrict’ is introduced in C99, that is applied only to pointers
e.g. int *restrict p1; void *restrict p2;
A pointer declared “restricted” is the only means of accessing the object it point to.