Archive for C

Introduction to Programming with C – First Program

The basic overview of C program is like that;

#include < stdio.h >

int main(){

    printf("Hello world!");
    /* prints hello world to the screen */
    return 0;
}

Let examine these lines one by one:

#include < stdio.h >

With “#include” command we say to compiler which header files our program needs and include them by putting the name of header file into < and > symbols. Standart input and output functions of C stand in that header file(stdio.h).

int main()

Actually main is a function and our program looks for a function called main then runs it. The “int” word implies that this function returns an integer data type. Data types and functions will be in my next posts.{ and } symbols express that the lines between them belong to that function.

printf("Hello world!\n");

printf is a function which prints a string to the screen. Sending a string between quotation marks is enough to call this function. “\n” is the new line character. “;” means this is the end of the line and this is compulsory. Unless we put semicolon end of the line, compiler sends a syntax error and does not compile our program.

/* prints hello world to the screen */

/* */ symbols prevent compiler from reading lines between them. So why we need these lines, if compiler does not read? These are for adding comments to our code. In other words, if anybody or we read the program again, understand better how any line works with these comments. If our comment is only one line length, we can use // symbol.

/*
This
is
a
multiple
comment
block
*/
// This is one line comment
return 0;

I’ve mentioned that main is a function which returns an integer. This line refers the end of the function and process. We use this to halt program.

This is it. We wrote the first C program. 😉

The Importance of Brackets When Defining Macros

In C programming, there are some points to be careful while using “#define” macro. One of them is use of brackets because these macros do not work exactly how in the mind of humans. If you see lots of useless paranthesis in others codes you should read this article.  Macros do exactly what you want from them without examining and calculating. Without further ado, let me give an example of some codes:

#include 
#define ABS(x) x>0?x:-x

int main()
{
    printf("%d\n",ABS(3-5));
    return 0;
}

In this code segment the macro called ABS, if the value sent to it is less then 0 puts “-” sign in front of this value, else leaves it as it is. In short, the absolute value. The expectation is +2 because 3-5=-2. However, the value which is sent to macro is not -2 but 3-5. The difference is that macros do not calculate the argument and work with the result, like functions. Macros works like dictionaries. Just before the compile they make the modifications and finishes their jobs, like in the basic notepad programs, find and change module (ctrl+h). After this tip, analyzing the ABS macro again shows us putting “-” sign in front of the 3-5 gives -3-5 which is equal to -8. In fact,there is nothing to be surprised, is there? :) Then, here is the solution:

#define ABS(x) (x)>0?(x):-(x)

If we defined the macro with paranthesis, there would not be any problem. Another example is;

#include 
#define SQR(x) (x*x)
int main()
{
    printf("%d\n",SQR(2+3));      
    return 0;
}

I do not make same explenations again and again. The expectation is 2+3=5, so the result is 5*5=25. Whereas SQR turns 2+3 value into 2+3*2+3. It is same with 2+(3*2)+3 in maths, so with 11 not 25.

#define SQR(x) ((x)*(x))

Defining the macro with useless(!) brackets, makes all c programmers happy. :)