Reference
Data Types
- Basic Types
- they are arithmetic types, i.e. (a) integr types and (b) floating-point types.
- Eumerated types
- they are arithmetic types and they are used to define variables that can only assign certain discrete integer values throughout the program.
- The type void
- void indicates that no value is available.
- Function returns as void – does not return any value.
- Function arguments as void – function with no parameter can accept a void.
- A pointer of type [ void * ] represnets the address of an object, but not its type. For example, a memory allocation function void *malloc( size_t size ); returns a pointer to void which can be casted to any data type.
- Dervied types
- they include (a) Pointer types, (b) Array types, (c) Structure types, (d) Union types, and (e) Function types.
- The array types and structure types are referred as the aggregate types.
Variable Types
- Basic variable types – char, int, float, double, void
- Others types – Enumeration, Pointer, Array, Stucture, Union, … etc.
Variable Definition in C
A variable definition tells the compiler where and how much storage to create for the variable. A variable definition specifies a data type and contains a list of one or more variables of that type. For example:
int i, j, k;
char c, ch;
float f, salary;
double d;
Variables can be initialized (assigned an initial value) in their declaration. Examples:
extern int d = 3, f = 5;
// declaration of d and f.
int d = 3, f = 5;
// definition and initializing d and f.
byte z = 22;
// definition and initializes z.
char x = 'x';
// the variable x has the value 'x'.
Variable Declaration in C
A variable declaration provides assurance to the compiler that there exists a variable with the given type and name so that the compiler can proceed for further compilation without requiring the complete detail about the variable. A variable definition has its meaning at the time of compilation only, the compiler needs actual variable definition at the time of linking the program.
A variable declaration is useful when you are using multiple files and you define your variable in one of the files which will be available at the time of linking of the program. You will use the keyword extern to declare a variable at any place. Though you can declare a variable multiple times in your C program, it can be defined only once in a file, a function, or a block of code.
Example:
#include <stdio.h>
// Variable declaration:
extern int a, b;
extern int c;
extern float f;
int main () {
/* variable definition: */
int a, b;
int c;
float f;
/* actual initialization */
a = 10;
b = 20;
c = a + b;
printf("value of c : %d \n", c);
f = 70.0/3.0;
printf("value of f : %f \n", f);
return 0;
}
Integer Literals
An integer literal can be a decimal, octal, or hexadecimal constant. A prefix specifies the base or radix:
- 0x or 0X for hexadecimal
- 0 for octal,
- nothing for decimal
An integer literal can also have a suffix that is a combination of U and L, for unsigned and long, respectively. The suffix can be uppercase or lowercase and can be in any order.
Examples:
- 212 /* Legal */
- 215u /* Legal */
- 0xFeeL /* Legal */
- 078 /* Illegal: 8 is not an octal digit */
- 032UU /* Illegal: cannot repeat a suffix */
Floating-point Literals
- 3.14159 /* Legal */
- 314159E-5L /* Legal */
- 510E /* Illegal: incomplete exponent */
- 210f /* Illegal: no decimal or exponent */
- .e55 /* Illegal: missing integer or fraction */
Character Constants
Character literals are enclosed in single quotes, e.g., ‘x’ can be stored in a simple variable of char type.
A character literal can be a plain character (e.g., ‘x’), an escape sequence (e.g., ‘\t’), or a universal character (e.g., ‘\u02C0’).
String Literals
String literals or constants are enclosed in double quotes “”. A string contains characters that are similar to character literals: plain characters, escape sequences, and universal characters.
You can break a long line into multiple lines using string literals and separating them using white spaces.
Here are some examples of string literals. All the three forms are identical strings.
"hello, dear"
"hello, \
dear"
"hello, " "d" "ear"
Defining Constants
There are two simple ways in C to define constants −
- Using #define preprocessor.
- Using const keyword.
it is a good programming practice to define constants in CAPITALS.
#define LENGTH 10
#define WIDTH 5
#define NEWLINE '\n'
const int LENGTH = 10;
const int WIDTH = 5;
const char NEWLINE = '\n';
