Posts

Showing posts from April, 2023

Comparison of Bit Array Implementations using C and Ada

  I found the following programming example in Bit Array in C - Sanfoundry #include <stdio.h> #define SIZE (58) /* amount of bits */ #define ARRAY_SIZE(x) (x/8+(!!(x%8))) char get_bit(char *array, int index); void toggle_bit(char *array, int index); void toggle_bit(char *array, int index) {     array[index / 8] ^= 1 << (index % 8); } char get_bit(char *array, int index) {     return 1 & (array[index / 8] >> (index % 8)); } int main(void) {     /* initialize empty array with the right size */     char x[ARRAY_SIZE(SIZE)] = { 0 };     int i;     for (i = 0; i < SIZE; i += 2)         toggle_bit(x, i);     toggle_bit(x, 56);     for (i = 0; i < SIZE; i++)         printf("%d: %d\n", i,...

Poor Quality C Programming Examples

  C is hard enough without low quality programming examples I recently read through some of the C programming examples from Sanfoundry . Some of the examples are well constructed while others are very poorly constructed. One of the bad examples I read is found under the heading of Simple C Programs. The example purports to show how to count the number of vowels and consonants in an input sentence. The source code for the example follows. /*   * C program to read a sentence and count the total number of vowels   * and consonants in the sentence.   */ #include <stdio.h>   void main() {     char sentence[80];     int i, vowels = 0, consonants = 0, special = 0;       printf("Enter a sentence \n");     gets(sentence);     for (i = 0; sentence[i] != '\0'; i++)     {       ...