C programming Introduction

C is one of the most widely used general-purpose programming languages. It is widely used for writing programs in many different domains including operating systems, numerical computing, graphical applications, etc.

C provides “high-level” structured programming constructs such as statement grouping, decision making, and looping, as well as “low level” capabilities such as the ability to manipulate bytes and addresses.

C provides powerful functionalites via software routines called functions. Also, there are standard library of functions that provide a collection of commonly used operations. The standard library is a set of functions mandated to exist on all systems conforming to the ISO C standard. For example, the standard function printf() prints text to the screen.

Most of UNIX system is written in C and because of this, UNIX became more portable operating system. It is rooted from language B and credit for creation of this beautiful language goes to Thompson and Dennis Ritchie.

Writing very first C program

Any text editor like Sublime Text, Notepad++ or Emaces which are available for cross-platform or just a plain text notepad can be used for writing code in C. However, the text editors come with advanced capabilities such as code completion, code debugging, syntax highlighting, line numbers etc which makes life easier for reading and error detecting.

For all tutorials related to C programming in this blog, we've used CodeBlocks as IDE/ editor and GCC as compiler. 

Hello World! in C

Let us start our programming with the traditional Hello World! program. 

Here is the classic Helloworld program using C :

/* Hello World! program in C*/
#include <stdio.h>
int main() 
{
  printf("Hello, World!\n");
  return 0;
}

The output the above program is:

Hello, World!

Taxonomy of the above program:

  • First line of the prgram is comment, which in C starts with /* and are terminated with */. They can span multiple lines. Single line comment starts with //. For example: 
//First program in C
  • Second line is inclusion of a standard library header-file. Headerfiles contain the information necessary to use these libraries, such as function declarations and macros. Most of C’s functionality comes from libraries. Don't forget to include this line to make your program run.
  • Third line represents the the entry-point function which all C programs have . This function comes in two forms:
int main()

int main(int argc, char *argv[])

The first takes no arguments, and the second receives command-line arguments from the environment in which the program was executed—typically a command-shell. We'll talk more about command line argments later.

  • Line 4 and 7 represents the start and end of the program scope. The braces { and } delineate the extent of the function block.
  • Line 5 and 6 are the statements of the program. Line 5 prints Hello World! by calling the standard library function printf(), which prints a character string to standard output (usually the screen). In this case, the printf() function takes one argument (or input parameter): the string constant "Hello World!\n". The \n at the end of the string is an escape character to start a new line. Finally, the statement is terminated with a semicolon (;).

 The point to be noted here is that, printf() is not a part of the C language, but a function provided by the standard library (declared in header stdio.h). 

C is a free-form language, with program meaning unaffected by whitespace in most circumstances. Thus, statements are terminated by ; not by a new line.

When a function completes, the program returns to the calling function. In the case of main(), the program terminates and control returns to the environment in which the program was executed. The integer return value of main() indicates the program’s exit status to the environment, with 0 meaning normal termination.Line 6 represents that function return 0 which indicates the successful termination of the program.

Hence, after these long but extremely useful explanation, the ouput of the program becomes much clear.

Here is another version of the above program.

/* Hello World! program in C*/

#include <stdio.h>

int main(void)
{
    printf("Hello");
    printf(" World!\n");
    return 0;
}

The output the above program is:

Hello, World!

The ouput of the program is same because there is no new line "\n" in first statement.

We'll look closer on each of the term above mentioned in more detail on upcoming chapters.

C Operators