Reference
Storage Classes
A storage class defines the scope (visibility) and life-time of variables and/or functions within a C Program. They precede the type that they modify. There are four storage classes in a C program − (a) auto, (b) register, (c) static, (d) extern
a) auto Storage Class
The default storage class for all “local” variables.
{
int mount;
auto int month;
}
The above two variables are of the same auto storage class. ‘auto’ can only be used within functions, i.e., local variables.
b) register Storage Class
It is used to define ‘local’ variables that should be stored in a register instead of RAM. This means that the variable has a maxumim size equal to the register size (usually one word) and can’t have the unary ‘&’ operator applied to it as it does not have a memory location.
{
register int miles;
}
The register should only be used for variables that require quick access such as counters. It should also be noted that defining ‘register’ does not mean that the variable will be stored in a register. It means that it MIGHT be stored in a register depending on hardware and implementation restrictions.
c) static Storage Class
The static storage class instructs the compiler to keep a ‘local’ variable in existence during the life-time of the program instead of creating and destroying it each time it comes into and goes out of scope. Therefore, making ‘local’ variables static allows them to maintain their values between function calls.
The static modifier may also be applied to global variables. When this is done, it causes that variables’s scope to be restricted to the file in which it is declared.
In C programming (OOP?), when static is used on a global variable, it causes only one copy of that member to be shared by all the objects of its class.
#include <stdio.h>
/* function declaration */
void func(void);
static int count = 5; /* global variable */
main() {
while(count--) {
func();
}
return 0;
}
/* function definition */
void func( void ) {
static int i = 5; /* local static variable */
i++;
printf("i is %d and count is %d\n", i, count);
}
When tha above is executed, it produces the following result –
i is 6 and count is 4
i is 7 and count is 3
i is 8 and count is 2
i is 9 and count is 1
i is 10 and count is 0
d) extern Storage Class
The extern storage class is used to give a reference of a global variable that is visible to ALL the program files. When you use ‘extern’, the variable cannot be initialized however, it points the variable name at a storage location that has been previously defined.
Just for understanding, extern is used to declare a global variable or function in another file.
First File: main.c
#include <stdio.h>
int count ;
extern void write_extern();
void main() {
count = 5;
write_extern();
}
Second File: support.c
#include <stdio.h>
extern int count;
void write_extern(void) {
printf("count is %d\n", count);
}
Now compile these two files using ‘gcc main.c support.c’, it will produce the executable a.out. When this program is executed, it produces the following result –
count is 5
Operators
Arithmetic Operators
| + | – |
| * | / |
| % | ++ |
| — (2 minus sign here) |
Relational Operators
| == | != |
| > | < |
| >= | <= |
Logical Operators
| && (AND) | || (OR) |
| ! (NOT) |
Bitwise Operators
Assume A = 60 ( 0011 1100 ) and B = 13 ( 0000 1101 ).
| Operator | Description | Example |
| & | (A&B) = 12 (0000 1100) | |
| | | (A|B) = 61 (0011 1101) | |
| ^ | Binary XOR operator copies the bit if it is set in one operand but not both. | (A^B) = 49 (0011 0001) |
| ~ | Binary One’s Complement Operator is unary and has the effect of ‘flipping’ bits. Note: ~N = -(N + 1) | ~A = ~60 (1100 0011) = -61 |
| << | Binary Left Shift Operator. The left operands value is moved left by the number of bits specified by the right operand. | A << 2 = 240 (1111 0000) |
| >> | Binary Right Shift Operator. The left operands value is moved right by the number of bits specified by the right operand. | A >> 2 = 15 (0000 1111) |
Assignment Operators
| = | += |
| -= | *= |
| /= | %= |
| <<= | >>= |
| &= (Bitwise AND assignment operator) | ^= (Bitwise exclusive OR and assignment operator) |
| |= (Bitwise inclusive OR and assignment operator) |
Misc Operators
| Operator | Description | Example |
| sizeof() | Returns the size of a variable. | sizeof(a), where a is integer, will return 4 (in byte??) |
| & | Returns the address of a variable. | &a; returns the actual address of the variable. |
| * | Pointer to a variable. | *a; |
| ? : | Conditional Expression. | If Condition is true ? then value X : otherwise value Y |
Decision Making
C programming language assumes any non-zero and non-null values as true, and if it is either zero or null, then it is assumed as false value.
- if statement
- if …. else statement
- nested if statements
- switch statement
- nested switch statements
- The (condition)? expression 1 : expression 2 Operator
Loops
Loop types
- while
- for
- do …. while
- nested loops
Loop control
| break; | Terminates the loop or switch statement and transfers execution to the statement immediately following the loop or switch. |
| continue; | Causes the loop to skip the remainder of its body and immediately retest its condition prior to reiterating. |
| goto | Transfers control to the labeled statement. |
Functions
The general form of a function definition in C programming language is as follows −
return_type function_name( parameter list ) {
body of the function
}
Function Declarations
A function declaration tells the compiler about a function name and how to call the function. The actual body of the function can be defined separately. E.g. –
int max(int num1, int num2);
Function declaration is required when you define a function in one source file and you call that function in another file. In such case, you should declare the function at the top of the file calling the function.
Calling a Function
When a program calls a function, the program control is transferred to the called function. A called function performs a defined task and when its return statement is executed or when its function-ending closing brace is reached, it returns the program control back to the main program.
Function Arguments
If a function is to use arguments, it must declare variables that accept the values of the arguments. These variables are called the formal parameters of the function.
Formal parameters behave like other local variables inside the function and are created upon entry into the function and destroyed upon exit.
While calling a function, there are two ways in which arguments can be passed to a function −
| Call by value | This method copies the actual value of an argument into the formal parameter of the function. In this case, changes made to the parameter inside the function have no effect on the argument. |
| Call by reference | This method copies the address of an argument into the formal parameter. Inside the function, the address is used to access the actual argument used in the call. This means that changes made to the parameter affect the argument. |
By default, C uses call by value to pass arguments. In general, it means the code within a function cannot alter the arguments used to call the function.
Scope Rules
Declare variables
There are three places where variables can be declared in C programming language −
- Inside a function or a block which is called local variables.
- Only used by statements that are inside that function or block of code.
- Local variables are not known to functions outside their own.
- Outside of all functions which is called global variables.
- Defined outside a function, usually on top of the program.
- Global variables hold their values throughout the lifetime of your program and they can be accessed inside any of the functions defined for the program.
- In the definition of function parameters which are called formal parameters.
- Formal parameters, are treated as local variables with-in a function and they take precedence over global variables.
A program can have same name for local and global variables but the value of local variable inside a function will take preference.
Initializing Local Variables
When a local variable is defined, it is not initialized by the system, you must initialize it yourself.
Initializing Global Variables
Global variables are initialized automatically by the system when you define them as follows −
| Data Type | Initial Default Value |
| int | 0 |
| char | ‘\0’ |
| float | 0 |
| double | 0 |
| pointer | NULL |
