Skip to main content

See More of C

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
Spell corrector program

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

Comments

Popular posts from this blog

Image Search Engine Using Python

Images provide a lot more information than audio or text. Image processing is the prime field of research for robotics as well as search engines. In this article we will explore the concept of finding similarity between digital images using python. Then we will use our program to find top 10 search results inside a dataset of images for a given picture. It won't be as good as google's search engine because of the technique we will be using to find similarity between images. But what we are going to make will be pretty cool. So lets start. Setting up the Environment Our Algorithm How the code looks Lets build the GUI Additional Techniques Setting up the Environment The code we are going to write requires a few tools which we need to install first. I will try to be as precise as i can and if you get stuck into installing some tool then you can drop a comment below and i will help you sort out the problem. So here are the tools and the steps to install

Understanding Python Decorators

If you have ever wondered what those @something mean above a python function or method then you are going to have your answers now. This @something line of code is actually called a decorator. I have red from various articles about them but some of them were not able to clarify the concept of a decorator and what we can achieve with them. So in this post we'll learn a lot about python decorators. Here is a list of topics we'll be covering. What is python decorator Understanding the concept Multiple decorators on same function class method decorator Where can we use decorators What is python decorator A python decorator is nothing but a function which accepts your given function as a parameter and returns a replacement function. So its like something this def decorator(your_func): def replacement(your_func_args): #do some other work return replacement @decorator your_func(your_func_args): #your_func code Now when your_func gets called then

Cordova viewport problem solved

Include the viewport settings in Cordova If you are facing the auto zooming problem of cordova then go read on the full article. Cordova actually ignores the viewport meta tag which causes the pixel density problem. So we need to tell cordova that viewport tag is equally important as other tags. To do this, we need to add some code to a file which is specify in the article. Corodva messes with pixels If you are using the latest cordova version or creating the cordova app for latest android versions then you may have faced the zoom malfunctioning.I also faced it when creating an app. Many of you may have already searched the web and found the answer of changing the meta tag attributes to get it working. But adding target-densitydpi=medium-dpi does not solve the problem for latest android versions. It may work for gingerbread but not for kitkat and others. So the final solution which i found was one of the stackexchange answer but rarely found. So i am gonna two things here, i