Tuesday, 22 November 2016

extern keyword: This discussion will be very useful for beginners with C language.


Before going for extern we look at the C variable declaration and definition

Declaration:  Declaration of a variable simply states that the variable exists somewhere in the program in that scope. While memory is not allocated to the variable.

Definition: Definition is the super-set of declaration. Variable definition means that a variable is declared and memory is allocated to that variable.

extern keyword extends the visibility of the C variables and functions throughout the program.
i.e u can define a variable once and use any where in the program with the help of extern.

lets understand with example program

filename: new.c
extern int ll;
void fun()
{
printf("hello=%d\n",ll);
return;
}


filename: prog.c
#include<stdio.h>
#include"new.c"
int ll=10;
void fun();
int main()
{
fun();
return 0;
}
in new.c  we are just referring to variable ll and that can present any where in the program, ll is defined and declared in prog.c
when u run the program get 10 as output.

Now we can call extern int ll as declaration and int ll=10 as definition.

Happy Coding!!!!

ABOUT GCC & HOW TO RUN C PROGRAM ON GCC


Original GNU C Compiler (GCC) is developed by Richard Stallman, the founder of the GNU
Project. Richard Stallman founded the GNU project in 1984 to create a complete Unix-like operating system as free software, to promote freedom and cooperation among computer users and programmers.

GCC is a key component of "GNU Toolchain", for developing applications, as well as operating systems. The GNU Toolchain includes:
Ø  GNU Compiler Collection (GCC): a compiler suit that supports many languages, such as C/C++, Objective-C and Java.
Ø  GNU Make: an automation tool for compiling and building applications.
Ø  GNU Binutils: a suit of binary utility tools, including linker and assembler.
Ø  GNU Debugger (GDB).
Ø  GNU Autotools: A build system including Autoconf, Autoheader, Automake and Libtool.

Ø  GNU Bison: a parser generator (similar to lex and yacc).

Versions:
With Ubuntu 10.04 Linux
$ gcc –version
Gcc (Ubuntu 4.43-4ubuntu5.1) 4.43
$gcc –v
Help
You can get the help manual
$gcc –help



Man Pages
You can read the GCC manual pages
$man gcc

Getting started with Programming
The GNU C compiler is gcc
Compile/Link a simple C Program hello.c
Here is Hello-World C program hello.c
//hello.c
#include<stdio.h>
 int main(){
Printf(“Hello world!\n”);
Return 0;
}
To compile hello.c
$ gcc hello.c
The default output is called “a.out”
To run the program
$./a.out
Note:
·         In Bash or Bourne shell, the default PATH does not include the current working directory. Hence, you may need to include the current path (./)
·         You may need to include the file extension, i.e., "./a.out".

To specify the output filename, use -o option:
 Gcc –o hello hello.c
//Compile and link source file hello.c into executable hello
$ ./hello
//Execute hello, specifying the current path ./

Compile and link separately
//Compile only with –c option
$ gcc –c hello.c
//link the object files into and executable
Gcc –o hello hello.o
The options
Ø  -c: compile into object file "Hello.o". By default, the object file has the same name as the source file with extension of ".o" (there is no need to specify -o option). No linking with other object file or library.
Ø  Linking is performed when the input file are object files ".o" (instead of source file ".c"). GCC uses a separate linker program (calledld.exe) to perform the linking.

Compile and Link Multiple Source Files
Suppose that your program has two source files: file1.c, file2.c. You could compile all of them in a single command:

$ gcc –o prog file1.c file2.c
However, we usually compile each of the source files separately into object file, and link them together in the later stage. In this case, changes in one file does not require re-compilation of the other files.
$gcc –c file1.c
$gcc –c file2.c
$gcc –o prog file1.o file2.o

$./prog

Happy Coding!!! 

Tuesday, 15 November 2016

BASIC INPUT OUTPUT PROGRAM EXAMPLE ON LPC2148


Basic Building block of any controller is GPIO, I am demonstrating a simple example to take binary input and set binary output accordingly


#include <LPC214x.H>                       
#define LED       (1 << 17)
#define SWITCH       (1 << 16)
int main (void)
{
IO1DIR |= LED;                                              //set pin P1.17 direction as output
IO1DIR &=~ SWITCH;                                   //set pin P1.16 direction as input
while (1)                //run continuously
{
(IO1PIN & SWITCH) ?(IO1SET = LED):(IO1CLR = LED);
//Set the output status according to input using ? operator
}

}