C- Language

  • Introduction

In computing world, C can be defined as a general purpose programming language. It was developed by Dennis M. Ritchie at AT&T Bell Labs. It has following features:
  • It was invented to write an operating system called as UNIX.
  • A structured language.
  • Can handle low level activities.
  • Can be compiled in variety of platforms.
  • It was formalized in 1988 by American National Standard Institute(ANSI).
  • The most popular relational database management system MySQL and the Linux Operating system are developed using C-language.

Editors for C-language

There are various editors available to use to work with C-language. Some of them are:
  • Notepad++
  • Notepad
  • WordPad
  • Vi-editor
  • Vim
  • Text Wrangler
  • xCode
  • Dev - C
  • Turbo C
  • Borland C

Compilers Available for C-language

There are various compilers available for C-language. Some of them are:
  • Ritchie C Compiler
  • AMPC
  • ANSI
  • GCC C
  • Quick C

File Extension for C

The file extension for c-files is .c.



  • Hello World Program

To begin with C-coding, I am giving here an example:

 #include<stdio.h>  
 int main(){  
   /* My First Program in C */
   printf("Hello World");  
   return 0;  
 }  

 Output: Hello World  


Header File

In the above code, the very first line #include <stdio.h> tells the compiler to include the header file named stdio.h. Header files are the files with .h extension which contains c functions declarations and macro definitions. Basically, there are two kinds of header files:

  • Library Header files
    • These header files come up with C-compiler.
  • User Header files
    • These header files are created by the user.
We can use the contents of header file by including it. Including header files means copying the contents of these files into the current source code file. But, to make it a modular approach and easy to debug if required, it is not recommended to copy and paste the contents of the header file into the source file.

The syntax for including the header files are:

 #include "stdio.h"  
 #include <stdio.h>  
The first syntax for including header file tells the compiler to search the header file in the standard list of  system directories. The other directories can be prepended with -| option while compiling the source code.
The second syntax for including header file tells the compiler to search the header file in the current working directory. This is generally used for user defined header files.

In both of the above syntaxes, "#include" is called as the preprocessor directive. The C-preprocessor or CPP is often called as macro processors. This is automatically used by the c-compiler to transform the program before compilation.

The C preprocessor is intended to be used only with C, C++, and Objective-C source code. In the past, it has been abused as a general text processor. It will choke on input which does not obey C's lexical rules. For example, apostrophes will be interpreted as the beginning of character constants, and cause errors. Also, you cannot rely on it preserving characteristics of the input which are not significant to C-family languages. If a Makefile is preprocessed, all the hard tabs will be removed, and the Makefile will not work.



main() Function

 int main()  

The above signature for function tells the compiler that main is the function name and int is the return data type. main() is the very first function to be called when a c-program begin to run.

printf("Hello World"); 

 int printf("Hello World");  

printf function is defined inside the stdio.h header file. It is generally used to display formatted output to the output console.

return 0;

The return statement is generally used to return the control to the calling function. Here, in the above program, the control is being transferred to the operating system.

/* My First Program in C */

Everything inside /**/ is treated as non-executable code and is used for commenting the program. Commenting plays a very important role while debugging a source code. It is highly recommended to write a program with proper comments.

  • Elements of C

So, after going through the hello world program in C, we have a brief idea of how program is written and executed in C-language. So, it is now time to get to know more about elements of C-language. A lexical element refers to a character or groupings of characters that may legally appear in a source file. There are following elements in a basic C language:
  • Tokens
      • A token is a source program text that the compiler does not breakdown into component element. It is the smallest independent unit in a program as defined by the compiler.
    • Identifiers
      • Identifiers are the names for the following language constructs:
        • Functions
        • Variables
    • Keywords
      • Keywords are the identifiers in C-language. Basically, there are 32 keywords.
    • Constants
      • Constants are the terms that can not be changed during the program execution. Constants are of following types:
      • There are following types of constants in C language:
        • Integer Constants
        • Floating point Constants
        • Character Constants
        • String Constants
    • Punctuation and Special Characters
      • semi colon (;) is called as termination character.
      • & is address of operator.
      • * is called as value at operator.
  • Comments
    • // used for single line comments.
    • /**/ used for multi line comments.
  • Escape Sequences
    • Escape sequences are used to insert white spaces in a formatted output like new line, tab, horizontal tab, vertical tab, backspace etc.

Rules for declaring variables

While declaring a variable, we follow the following rules:
  1. The variable name can contain letters, numbers or underscore( _ ).
  2. The variable name should start from a letter or underscore( _ ) only and not from a number.
  3. There is no restriction in the length of the variable name. But, as C descriminate the variables with first 31 characters, hence the first 31 characters should be unique.



DataTypes in C

In C language, there are following data types used to defined what kind of data a variable can store in it:
  1. Fundamental Data Types
    1. char
    2. short
    3. int
    4. long
    5. float
    6. double
  2. Derived Data Types
    1. array
    2. enum
    3. struct
    4. pointer
The fundamental data types are used to store a single value in it, where as the derived data types are used to store a bunch of values in it. We will discuss about these data types one after another in detail.

Comments

Popular posts from this blog

श्मशान और ज़िंदगी

साथ

प्रेम