Wednesday, July 9, 2014

"Learning to Code" and "Coding to Learn"

I recently came across Mitchell Resnick's Interview. Mitchell Resnick, PhD, is a Papert Professor of Learning Research and the director of Lifelong kinderGarten group at MIT Media Lab. He speaks about the importance of creative learning through Computer Science and in turn Coding. He clearly illustrates the differences between the two paradigms: "Learning to code" and "Coding to learn". He brings out the importance of coding like identifying the problems, creative thinking, organizing and reformulating the ideas to meet the needs apart from the jobs and careers it would help in. I have always believed that coding teaches us to think systematically.

The below is one of the excerpt from Mitchell's interview where he speaks about the importance of Coding as a subject in school:

Should every child learn to code? Should coding be a school subject like algebra or chemistry?
RESNICK: I do think every child should learn to code, and I would approach it similarly to writing—the same way we teach children to write and then let them use their writing in all other courses. You learn to write and then use your writing in writing book reports and writing science reports—you use it in all other subjects. And I think similarly, it would be great for all kids to code and then use that knowledge in many other classes.
Events like Hour of Code have raised the visibility of coding and given people an opportunity to get some sense of what coding is all about. But it only will be meaningful if there’s a follow-up and follow-through. If people take that initial spark and turn an hour of code into a day of code or a week of code or a month of code where they continue to explore the possibilities of coding.

So it’s the same thing. If you just spend an hour learning to write, it wouldn’t be so useful. On the other hand, if that sparks your imagination and then you continue to do more things with it, then it becomes meaningful

Source/Complete Mitchell's interview is available here

Wednesday, July 2, 2014

Writing a running code without main()..!


We all know that main() is the starting point in any C/C++/Java program, But in linux, when a program is executed, always _start() will be called which does cleanup of resources like memory, etc and then calls exit() with main() as it’s argument. Suppose the main() is missing in the program, compiler throws up a undefined reference error to main() function

_start() function can be overriden.

_start function (Inside C)

/usr/lib/gcc/i386-redhat-linux/4.0.0/../../../crt1.o(.text+0x18): In
function `_start':: undefined reference to `main'
collect2: ld returned 1 exit status

Above error definitely means that there is no main() definition in any of the source files of the project. To dig it further, why _start and why collect2?

_start function is the entry point of a C program which makes a call to main()
collect2 is GCC utility which is used to call various utility function during start time. like ld etc.

Going further into it, main() is the starting point of a C program from the programmer's perspective. Before calling main, a process executes a bulk of code to "clean up the room for execution".
_start is the function which gets called first, which then allocates necessary resources and then calls main() which has to be defined by the programmer.

You can override _start and tell the compiler to not to look for main by using "-nostartfiles" option.

#include<stdio.h>
_start()
{
printf("Hello world!!\n");
_exit(0);
}

To Compile : gcc -nostartfiles sampleTest.c -o a.out
Without _exit(0); the program will compile, but encounters a segmentation fault when it's run.


The other ways to write a running program without using main() is to use
  • token pasting operator (Eg: #define fun  m##a##i##n)
  • main being defined in a macro (Eg: #define fun main; int fun() { })

Citations:


Tuesday, July 1, 2014

Understanding 'Extern' keyword

Let me begin my first post with a topic on 'Extern' keyword in C

'Extern' keyword in C is used in two contexts: variable and functions.

Before delving into the usage of extern let's touch upon 'function declaration/definition' in C. I'm sure almost (everyone) of you will be aware of declaration/definition of a variable/function but for the sake of completeness of this post, I would like to clarify them.

Declaration of a variable/function declares that a variable/function exists somewhere in the program but memory is not allocated. And it also gives details about type of a variable and about the arguments, their order and the return type in case of a function. This is about the declaration of a variable/function. Coming to the definition of a variable/function: it allocates the memory for that variable/function. Therefore, we can think of definition as a super set of declaration.

Coming back to the main definition of EXTERN:

Extern keyword associated with a variable/function extends the visibility of a variable/function throughout the program. By default all C function declarations and definition have "extern" keyword prepended with them. This means that even though we don't use "extern" with function declaration/definition it is present there.

For example, When we write int foo(int arg1, int arg2);
the compiler internally treats this as extern int foo(int arg1, int arg2);

Same is the case with the definition of a function in C. Memory will be allocated to the function only during the definition of the function and this indicates the a function can be declared any number of times in header or source files (.h/.c files) but it can be defined only once. As "extern" extends the visibility of the functions, they can be called anywhere form the program provided the declaration is known. So this is about the "extern" keyword associated with functions.


Now let's delve into the usage of "extern" keyword with the variables.

Any variable declared with "extern" keyword could be used anywhere else in the program. Unlike the previous case with functions variables are not declared/defined as "extern" by default. A variable in 'C' without the "extern" keyword will be declared and also memory will be allocated whereas the variable with "extern" declaration is just a declaration and no memory will be allocated and it can be used anywhere else in the program.

Let us consider the following examples:

example 1:

int var;
int main()
{
     var = 25;
     return 0;
}

the above snippet is perfectly fine as 'var' is declared and defined.


example 2:

extern int var;
int main()
{
    return 0;
}
The above is also fine as the extern variable var is never used (it is just declared)

example 3:

extern int var;

int main()
{
     var = 10;
     return 0;
}

Here, as the extern variable 'var' is not defined no memory is allocated. Compiler throws a undefined reference err in this case.

exmaple 4:

#include "SomeFile.h"

extern int var;
int main()
{
     var = 5;
     return 0;
}

This compiles successfully provided the var is defined in "SomeFile.h"

example 6:

extern int var = 50;

The above statement both declares and defines in a single statement and hence the future referral or updation to the 'var' will not throw any error.

So this is about some insight to "Extern keyword in C'
I hope it is helpful in some way and I have not disappointed :)

To summarize:


1. Declaration can be done any number of times but definition only once.
2. “extern” keyword is used to extend the visibility of variables/functions().
3. Since functions are visible through out the program by default. The use of extern is not needed in function declaration/definition. Its use is redundant.
4. When extern is used with a variable, it’s only declared not defined.
5. As an exception, when an extern variable is declared with initialization, it is taken as definition of the variable as well.


Citations:
http://www.geeksforgeeks.org/understanding-extern-keyword-in-c/
http://en.wikipedia.org/wiki/External_variable