- Editor of your choice - which helps to write and save C program into file.
- C compiler - which takes C program and coverts it into binary that computer can understand.
Environment
It is best to learn C programming in Linux operating system.1. If you are using Linux make sure gcc is installed (check if gcc -v on terminal, shows the GCC version). GCC - stands for GNU C Compiler.
$ gcc -v gcc version 4.1.2 20080704 (Red Hat 4.1.2-48)
There are tutorials available in internet for both 1 and 2.
You can use gedit editor that is available in Ubuntu. I prefer using Emacs, that is my prerogative choice.
Hello C
Type the following/Copy paste it in your editor.#include <stdio.h> int main() { printf("Hello C\n"); return 0; }
Include - #include <stdio.h>
It is directive to compiler to include stdio.h header file. stdio stands for standard input output. It contains details of how printf should work.Start - main()
main() is function, it is first executed when the program is run. Function consists of group of expressions. Here printf and return are body (group of expression) of the function, function always have { } called opening and closing of function.printf
printf stands for print formatted text to standard output (i.e terminal in our case). In our case we are passing string mentioned in "Hello C" for printf. \n tells that cursor has to come to next line after printing Hello C.return 0
It returns value to terminal at the end of program execution. 0 symbolizes that there is no error taken place in our program. This value is internally stored by the terminal and is not displayed.Compilation
To compile hello.c give the following command in the terminalgcc hello.c -o hello.out
-o means output file name. In our example we are telling the compiler to generate an output, which is executable into file named hello.out.
Running
If you do ls in the directory a new file called hello.out is created. To run it type the following in terminal$ ./hello.out Hello C
Output Hello C is printed on terminal. "./"tells the terminal that hello.out exists in present directory.
As of now don't worry too much in details. Just remember the method. In future programs contents in main() is more important anyways. We have finished the first barrier in executing the first C program.
Links
Quiz - C Quiz #03: First Program - Hello CNext Article - C Programming #04: Datatypes and Variable
Previous Article - C Programming #02: A Brief history of C
All Article - C Programming
No comments :
Post a Comment