Free clock The C Programming Language: January 2009

Friday, January 9, 2009

The C Programming Language. What is it?

C is a general purpose programming language written by Dennis M. Ritchie and Brian W. Kernighan. Many ideas of C are from programming language were influenced from B written by Ken Thompson in 1970 for UNIX system on DEC PDP-7 which was influenced from BCPL, developed by Martin Richards.

C is a relatively low level language and according to me this is the factor that makes is most powerful programming language present today. Using C we can directly interact to hardware also, so you have both external and internal details of system. So it's on you to which extent you can use it for you. Let's see a simple program in C:

#include

int main()
{
printf("Hello, World!!\n");
}

This program prints this message on standard output using a function printf() of standard input/output library.

Every program in C starts running with the function main(), starts following the instructions in main. Being a procedural language it executes code line by line. Next is the body of function starting with '{' and closing with '}'.
Every function performs a task in the program like printf() is printing a message so every function is followed by () which contains argument to be provided to functions these arguments provide the data to the function on which the function has to work on. So, we'll proceed with printf() function in next post.

Data Types

A computer can understand only 0's and 1's and can perform integer and floating point arithmetic i.e. can only execute on integers and real numbers. Going further in C, we have following basic data types:


* int(integer) size 4 bytes
* short(short integer) size 2 byte s
* long( long integer) size 4 bytes
* float(floating point) size 4 bytes
* double(double-precision float) size 8 bytes
* Characters(char) size 1 byte
(all stats from a 32-bit processor running gcc version 4.3.1, results depends on machine and compiler)

The % sign indicates that some other argument is substituted in the place, in case of printf and scanf. For int replacement %d, for float %f, for char %c and as specified in previous post.
Next the scanf() function, used for input from standard input.

Wednesday, January 7, 2009

printf() Function

int printf(const char *format,...) this is the fuction declaration for printf() it takes constant characters or string as input. The preceding ... variable arguments i.e. it can have many arguments as data for printf. For example, we have to display an integer we use %d , for character we use %c, for string we use %s and for a floating point number we use %f.

printf("int=%d, float=%f, char=%c, string=%s", 4, 5.5, 'k', "hello");

Output:

int=4, float=5.5, char=k, string=hello

All characters followed by % according to their special meaning.

Every instruction in C is followed by a ';' called the end of statement. At this point all the instruction ends, as C is a case sensitive language and there is no meaning of white spaces or newlines unless they are included within " ", where they have the meaning of a character.

Next we'll have a close look at the data types.