Type Qualifiers: Const Volatile

Download as pdf or txt
Download as pdf or txt
You are on page 1of 6

C is commonly referred to simply as a structured language

C supports four storage class specifiers: extern static register auto Type Qualifiers : const ; volatile A declaration declares the name and type of an object. A definition causes storage to be allocated for the object. The same object may have many declarations, but there can be only one definition. In most cases, variable declarations are also definitions. However, by preceding a variable name with the extern specifier, you can declare a variable without defining it. Thus, when you need to refer to a variable that is defined in another part of your program, you can declare that variable using extern. Insertion sort

Java implementation
void insertionSort(int[] arr) { int i, j, newValue; for (i = 1; i < arr.length; i++) { newValue = arr[i]; j = i; while (j > 0 && arr[j - 1] > newValue) { arr[j] = arr[j - 1];

j--; } arr[j] = newValue; } }

C++ implementation
void insertionSort(int arr[], int length) { int i, j, tmp; for (i = 1; i < length; i++) { j = i; while (j > 0 && arr[j - 1] > arr[j]) { tmp = arr[j]; arr[j] = arr[j - 1]; arr[j - 1] = tmp; j--; } } }

Selection Sort

Java
public void selectionSort(int[] arr) { int i, j, minIndex, tmp; int n = arr.length; for (i = 0; i < n - 1; i++) { minIndex = i; for (j = i + 1; j < n; j++) if (arr[j] < arr[minIndex]) minIndex = j; if (minIndex != i) { tmp = arr[i]; arr[i] = arr[minIndex]; arr[minIndex] = tmp; } } }

C++
void selectionSort(int arr[], int n) { int i, j, minIndex, tmp; for (i = 0; i < n - 1; i++) { minIndex = i; for (j = i + 1; j < n; j++) if (arr[j] < arr[minIndex]) minIndex = j; if (minIndex != i) { tmp = arr[i]; arr[i] = arr[minIndex]; arr[minIndex] = tmp; }

} }

Quick sort
int partition(int arr[], int left, int right) { int i = left, j = right; int tmp; int pivot = arr[(left + right) / 2]; while (i <= j) { while (arr[i] < pivot) i++; while (arr[j] > pivot) j--; if (i <= j) { tmp = arr[i]; arr[i] = arr[j]; arr[j] = tmp; i++; j--; } }; return i; } void quickSort(int arr[], int left, int right) { int index = partition(arr, left, right); if (left < index - 1) quickSort(arr, left, index - 1); if (index < right) quickSort(arr, index, right); }

Bubble sort

Java
public void bubbleSort(int[] arr) { boolean swapped = true; int j = 0; int tmp; while (swapped) { swapped = false; j++; for (int i = 0; i < { if (arr[i] > arr[i + 1]) { tmp = arr[i]; arr[i] = arr[i + 1]; arr[i + 1] = tmp; swapped = true; } } }

arr.length

j;

i++)

C++
void bubbleSort(int arr[], int n) { bool swapped = true; int j = 0; int tmp; while (swapped) { swapped = false; j++; for (int i = 0; i < n - j; i++) { if (arr[i] > arr[i + 1]) { tmp = arr[i]; arr[i] = arr[i + 1]; arr[i + 1] = tmp; swapped = true; } } } }

#pragma #pragma is an implementation-defined directive that allows various instructions to be given to the compiler. For example, a compiler may have an option that supports program execution tracing. A trace option would then be specified by a #pragma statement. You must check the compiler's documentation for details and options. Difference Between Malloc and Calloc
There are two differences. First, is in the number of arguments. Malloc() takes a single argument (memory required in bytes), while calloc() needs two arguments (number of variables to allocate memory, size in bytes of a single variable). Secondly, malloc() does not initialize the memory allocated, while calloc() initializes the allocated memory to ZERO. Here are more opinions and answers from FAQ Farmers:

The difference between malloc and calloc are: 1. malloc() allocates byte of memory, whereas calloc()allocates block of memory. Calloc(m, n) is essentially equivalent to p = m*malloc(n); memset(p, 0, m * n); The zero fill is all-bits-zero, and does not therefore guarantee useful null pointer values (see section 5 of this list) or floating-point zero values. Free is properly used to free the memory allocated by calloc. Malloc(s); returns a pointer for enough storage for an object of s bytes. Calloc(n,s); returns a pointer for enough contiguous storage for n objects, each of s bytes. The storage is all initialized to zeros. Simply, malloc takes a single argument and allocates bytes of memory as per the argument taken during its invocation. Where as calloc takes two aguments, they are the number of variables to be created and the capacity of each vaiable (i.e. the bytes per variable).

This one is false:

I think calloc can allocate and initialize memory, if the asked memory is available contiguously where as malloc can allocate even if the memory is not available contiguously but available at different locations.

SomeType *p = malloc(N * sizeof *p); SomeType *q = calloc(N, sizeof *q); Now, if N is so large that multiplying it by sizeof(SomeType) exceeds the valid range of size_t, the argument in the first form will "wrap around" and you'll silently request less memory than you wanted; if the request succeeds you'll proceed merrily along and try to store N items in too small a space, with the usually unhappy and sometimes baffling consequences. The second form, however, will fail and return NULL so your program will be alerted that the space was not available; there'll be no silent error. However, this seems to me to be a very small advantage, so I'll stick with my original suggestion: malloc() almost always, calloc() almost never.

Here are some examples of declarations which are not definitions, in C: extern char example1; extern int example2; void example3(void); Here are some examples of definitions, again in C: char example1; int example2 = 5; void example3(void) { int x = 7; }

That is, it adds 10, the number of columns, to get this location. If we were dealing with integers and an array with the same dimension the compiler would add 10*sizeof(int) which, on my machine, would be 20. Thus, the address of the 9 in the 4th row above would be &multi[3][0] or *(multi + 3) in pointer notation. To get to the content of the 2nd element in the 4th row we add 1 to this address and dereference the result as in
*(*(multi + 3) + 1)

With a little thought we can see that:


*(*(multi + row) + col) multi[row][col] and yield the same results.

You might also like