Why digging more of C ?
Being a high level language and the features C support there is not a need to ask why should we learn more about C. C programs are relatively faster than most of other high level languages like python or java. That may be because C programs are compiled first or may be because C libraries and fucntions are more hardware oriented. But whatever is the reason, C programs are way better. I can show you an example of it to you. There is a program which corrects the spelling of a mis-spelled word like it changes "somethinh" to "something". Now here is the comparison between the programs written in C and than that written in python
Property | Program in C | Program in Python | Result |
---|---|---|---|
Lines of code | 184 | 21 | Python is shorter |
Test with file 6.9 MB | 0m0.892s | 0m1.911s | C is 114% faster |
Test with file 50 MB | 0m6.896s | 0m17.892s | C is 159% faster |
Test with file 100 MB | 0m14.474s | 1m25.579s | C is 491% faster |
Test with file 168 MB | 0m44.627s | killed | Uncomparable |
So its pretty obvious that C program wins. But most of us don't know what we can do with C other than writing a main() function declaring some structs etc. There is more to that. I assume that you have basic understanding of C and its concepts like functions, pointers, structs, I/O. Now lets see what other things can we do with C. I won't write in detail about these things nor i will write sample programs but i will definitely guide you to some useful sites where you can pick this stuff from.
Function Pointers
This is a whole new topic for some of us. We have red about functions, we know about pointers but what is a function pointer. Well from the name it is inferable that a function pointer is kind of a pointer which points to a function. But still how is it useful ? Well its useful in many ways. Have you heard about callback functions (Functions which are called after execution of a function) Callback functions are passed as arguments in other languages like javascript and python but we didn't know earlier that the same thing can be happened with C also. Just like array name represents a pointer to it, function name alone represents a pointer to it. We can assign them, pass them as arguments and call them whenever we want. Lets look at a simple example.
#include<stdio.h>
typedef void (*f_pointer)(void) ;
void wrapper(cps doer, cps callback)
{
(*doer)() ;
(*callback)() ;
}
void func(){
printf("I am in doing function\n" ) ;
}
void tobecalled(){
printf("I am in callback") ;
}
int main(){
f_pointer doer, callback;
doer = func ;
callback = tobecalled ;
wrapper(doer, callback) ;
printf("\nI am in main\nExiting\n") ;
}
Searching and Sorting
Some of you have already heard about it but for those who haven't, there is a good news. C stdlib provides 2 methods for optimized searching and sorting and those are qsort() and bsearch()
which implements quicksort and binary search algorithm respectively. Here is a complete description about these methods and what parameters they take qsort and bsearch in c. In qsort() method definition, you will see that there is pointer to a function being passed as an argument.
Multi-Threaded Programming
Yes you saw right. We are not talking about java. We are talking about C which is also capable of creating and managing threads. All this is done by pthread library which have some useful functions for thread creation and thread management. For thread management, there are semaphores, mutex variables and condition variables. Here is the webpage where you can find further details. Pthread Details
Time Functions
Whenever we had to dealt with time it seemed so complicated because time functions return some unknown structure and data. Python has a now() function which prints current date and time. There is a similar function in C time.h
library which is time()
which returns a time data structure from which we can extract current data and time. Not only this, we can manipulate time structures, compare them and use anywhere we want (specifically in our databases). Here is the link to complete time functions C time functions
Random Numbers
Random numbers are much useful when creating test files for our programs and generating random numbers looks like a magical task which only more advanced languages can do like java, python, perl etc. But you would like to hear that C also has the functions which can generate random numbers (pseudo-random actually). The main functions are srand() and rand()
. First of these is used to plant a seed to set range of random numbers. rand() is used to generate those random numbers. There are more functions to get random numbers. Here is the link for further reading about random numbers Random Numbers using C.
String conversion
We see in java how we can convert string to integer using new Integer(string). This can also be done using C string.h
library. There are many functions but lets see some of them. atoi()
converts string to integer. atol()
converts string to long int. Similarly we can convert character from upparcase to lowercase using tolower()
and from lowercase to upparcase using touppar()
. For details of string conversion and character testing i would prefer the same source i posted for random numbers. The next section to random numbers is string conversion.
Process Creation and System commands execution
This is specific to Unix-like operating systems. In C you can create another process from within your C program and can hand over some task to it just like threads. But the created process will be totally independent of other processes. You can then apply Interprocess Communication techniques to get the result produced by the forked process. C is very unix friendly because Unix is totally built using C. Unix like operating systems like linux also supports this functionality and you can create another processes there too. The main functions related to process creation are fork() wait() kill()
.
Just like creating another process for random task, you can execute another file from within your program using execv()
and its variants. If you are using linux, ubuntu etc then just do man exec
and you will see other variants of execv(). For detailed description of process creation, i would suggest this documentation Process Management using C
DataBase Handling
Okay so we have pulled ourselves into some serious stuff. Now the things i will be talking will be dependent upon some libraries, APIs etc. I am writing about all these because these things are fascinating and there is no harm in knowing about them. We can write programs in C which can connect to database and handle the data. As C is fast relative to other languages, this seems really helpful but there is one thing C lacks and that is faster string parsing. That is the reason we feel need for python, perl etc. because these languages provide faster and efficient string parsing functions. Still mysql API for C exist and can be used for database handling. Those who are interested can visit MySQL website which contains the API documentation.
CGI Scripting
CGI is the interface which makes it possible for any language to serve as a scripting language. ANy language that can handle strings can be used as a scripting language. Writing CGI scripts using C may seem to be really fun task but i have red somewhere that it is not secure so i don't think any website uses C to write CGI programs in C. Still we can do it and if you are interested to know then there is a library with which you can write CGI scripts in C. Here is the link to that library API CGI library for C
Some other Interesting Sources
We saw so many things which we can accompalish using C but still there is more. I am posting some links to interesting C stuff
Comments
Post a Comment
Comment on articles for more info.