Writing and implementing the code

In our previous post we learned about the usage of C and environment setup for C. In this Post we will learn how to code in C.

  • Before writing the code, we have to know the C program parts. A C program consists of following parts :
    • Preprocessor commands
    • Functions
    • Variables
    • Statements & Expressions
    • Comments
  • Let us start coding by basic “HelloWorld” C program

#include<stdio.h>
void main()
{
/*Code Janthik*/
system("cls");
printf("HelloWorld");
printf("\n Welcome to Code Janthik");
getch();
}

  • From the above C program, first line of our program “#include<stdio.h>” is a preprocessor command, which tells a C compiler to include stdio.h file before going to an actual compilation.
  • The next line  “void main()” is the main function where the program execution begins. Here “void” means it doesn’t return any value.
  • For every “void main()” function we use “{“ open paranthesis which is followed by a “}”closed paranthesis. In the middle of these paranthesis we write our code.
  • In next line we used “/*…………..*/ “ this will be ignored by the compiler because we call those lines as “comments”. We can use them in any place of our code for better understanding the code. It’s our choice to use it or not.
  • The next line “system(“cls”);” is to directly clear the screen in our GNU C, where as in Turbo C we use “clrscr();” function.
  • The next line “printf(“…..”);” is a function in C which helps to display our text “HelloWorld” on  the screen.
  • We used “\n” in this “printf(“…”);” function, it is used to print the text “Welcome to Code Janthik” in next new line at output.
  • The next line “getch();” is a function to display the output on the screen.

In our next post we discuss about the tokens in C.

Why use C and preparing environment for using C

In our previous post we discussed about the history of C and today we have a glance about the usage of C in daily life and environment setup for implementing the code.

  • C was initially used for system development work, particularly the programs that make up the OS.
  • C produces code that runs nearly as fast as the code written in assembly language.
  • Here are some of the examples where C can be used –
    • OS
    • Language Compilers
    • Assemblers
    • Text Editors
    • Print Spoolers
    • Network Drivers
    • Modern programs
    • Databases
    • Language Interpreters
    • Utilities

Now, we see about the environment setup for C language:

  • If you want to set up your environment locally, you need the following two software tools.
    • Text Editor (Windows Notepad)
    • The C Compiler (GNU C/C++ Compiler)
  • Here, we do not use turbo C compiler as so many schools and colleges are using. Here we go in a bit professional manner.
  • It will be very easy to install the compiler.
  • To install GCC on Windows, you need to install MinGW. To install MinGW, go to the MinGW homepage www.mingw.org and go to the MinGW download page.
  • Download the latest version of MinGW installation program, which should be named MinGW-<version>.exe.
  • While installing MinGW, at a minimum, you must install gcc-core, gcc-g++, binutils and the MinGW runtime, but you may install more as per your requirement.
  • Add the bin sub directory of  your MinGW installation to your PATH environment variable, so that you can specify these tools on the command line by their simple names.
  • After the installation, you will be able to run gcc, g++, ar, ranlib, dlltool, and several other GNU tools from the Windows command line.

In our next post in C Programming, we will discuss about writing the code and implementing it in our local system.

History of C

Before going into the programming language let us see some of the history of our basic programming language C.

  • Developed in 1972 by Dennis M. Ritchie at Bell Telephone Laboratories.

    Dennis_Ritchie_2011
    Dennis M Ritchie
  • C was originally first implemented on DEC-PDP-11 computer in 1972.
  • The language was formalized in 1988 by the American National Standard Institute (ANSI).
  • The UNIX OS is totally written in C.
  • Today’s most popular Linux OS and RDBMS MYSQL have been written in C.

In the next post we discuss about the usage of C and the local environment setup for our practice.