Showing posts with label c stdlib. Show all posts
Showing posts with label c stdlib. Show all posts

31 August 2010

Portability tip: How to know the size of primitive data types on your machine

In C the size of primitive data types are machine and compiler dependent.
So, you shouldn't make any assumption when about the max number that int or short or any other primitive data types can hold.

But, by applying the following trick, you can get the max number of bits each primitive type can hold.

/**
* @auther: mhewedy
* @date 31/08/2010
* get_types_bits.c
*/

#include <stdio.h>

int int_bits();
int short_bits();
int long_bits();
int long_long_bits();
int char_bits();

int main(void)
{
printf("%i %i %i %i %i\n",char_bits(), short_bits(), int_bits(), long_bits(), long_long_bits());

return 0;
}

int int_bits()
{
unsigned int x = ~0;
int c=0;

while (x>0)
{
x>>=1;
c++;
}
return c;
}

int short_bits()
{
unsigned short int x = ~0;
int c=0;

while (x>0)
{
x>>=1;
c++;
}
return c;
}

int long_bits()
{
unsigned long int x = ~0;
int c=0;

while (x>0)
{
x>>=1;
c++;
}
return c;
}

int long_long_bits()
{
unsigned long long int x = ~0;
int c=0;

while (x>0)
{
x>>=1;
c++;
}
return c;
}

int char_bits()
{
unsigned char x = ~0;
int c=0;

while (x>0)
{
x>>=1;
c++;
}
return c;
}


13 August 2010

simple dictionary using binary search


//strcmp.c

#include <stdio.h>

int my_strcmp(const char[], const char[]);

/*
// testing..
int main(void)
{

printf("%i\n", strcmp("alpha", "altered"));
printf("%i\n", strcmp("zioty", "yucca"));

getchar();
return 0;
}
*/

int my_strcmp(const char str1[], const char str2[])
{
int i=0;

while (str1[i] == str2[i] && str1[i] != '\0' && str2[i] != '\0')
i++;

if (str1[i] > str2[i])
return 1;
else if (str1[i] < str2[i])
return -1;
else
return 0;
}



// simple dictionary using binary search
// auther mhewedy
// date : 2010-08-13
// compiled under GCC 4.x

#include <stdio.h>
#include <string.h>
#include "strcmp.c"

#define DICT_LENGTH 3

typedef struct _dictionary_s dictionary;
struct _dictionary_s
{
char word[20];
char definition[50];
};

int lookup(const char[], const dictionary[], int);
int binary_lookup(const char[], const dictionary[], int, int);

int main(void)
{
static const dictionary dict[DICT_LENGTH] =
{
{"a", "first letter in alphabatic"},
{"b", "second letter"},
{"c", "third letter"}
};
char word[20];
int ret=-1;

do{
printf("Enter word: \n");
if (gets(word) == NULL)
break;
ret = binary_lookup(word, dict, 0, DICT_LENGTH-1);

if (ret < 0)
{
fprintf(stderr, "word not found!\n");
}
else{
printf("%s : %s\n", word, dict[ret].definition);
}
printf("\n");
}while (1);

return 0;
}

int binary_lookup(const char word[], const dictionary dict[], int start, int end)
{
if (start > end)
return -1;

int middle = (start+end)/2;
int cmp = my_strcmp(word, dict[middle].word);

if (cmp == -1)
{
end = middle -1;
binary_lookup(word, dict, start, end);
}
else if (cmp == 1)
{
start = middle +1;
binary_lookup(word, dict, start, end);
}
else // 0
{
return middle;
}
}

int lookup(const char word[], const dictionary dict[], int length)
{
int i;
for (i=0; i<length; i++)
{
if (strcmp(word, dict[i].word) == 0)
return i;
}
return -1;
}




06 August 2010

strcat using arrays

#include <stdio.h>

void strcat2(char[], const char[], const char[]);

int main(void)
{
char a[] = "abc";
char b[] = "efg";
char ret[10];

strcat2(ret, a, b);

printf("%s\n", ret);

return 0;
}

void strcat2(char ret[], const char a[], const char b[])
{
int i=0, j=0;
while ( (ret[i++]=a[i]) != '\0');
i--;
while ( (ret[i++]=b[j++]) != '\0');
ret[i] = '\0';
}

20 July 2010

Newton-Raphson Method to Compute the Square Root in C



#include
#include

float square_root(float);

int main(void)
{

printf("square_root of %f is %f\n", 9.0, square_root(9.0));

return 0;
}

/*Newton-Raphson Method to Compute the Square Root of x*/
float square_root(float n)
{
const float epsilon = 0.00001;
float guess = 1.0;

while (fabs(guess*guess - n) >= epsilon)
guess = (n/guess + guess)/2.0;

return guess;
}

14 July 2010

Simple implemenation for C strlen

// str_len.c.c 
#include <stdio.h>
size_t strlen(char*);

int main(void)
{
char* name = "ThisIsAStringLiteral";
printf("%u\n", strlen(name));
return 0;
}

size_t strlen(char *str){
auto unsigned long n=0;
while (*str++) n++;
return ++n; // for '\0'
}

30 June 2010

my_strcpy

#include <stdio.h>

void my_strcpy(const char*, char*);

static char str1[80] = "Good";
static char str2[80];

int main(void)
{
puts(str2);
my_strcpy(str1, str2);
puts(str2);
return 0;
}

void my_strcpy(const char* src, char* dest){
char* p;
p = dest;
while (*src!= '\0'){
*p++ = *src++;
}
*p = '\0';
}