Introduction to C Language

Introduction to C Language

History of C:

C language was originally developed in the 1970’s by Dennis Ritchie at Bell Telephone Laboratories, Inc (called AT&T Bell Laboratories). It is an outgrowth of two earlier languages, called BCPL and B, which were also developed at Bell laboratories. Until 1978, C was mostly used within the Bell laboratories. In 1978, Brain Kernighan and Ritchie published a definitive description of the language referred to as “K & R version of C”.
Following the publication of the K&R description, many desirable features of C impressed computer professionals. By the mid 1980’s, the popularity of C had become widespread. Numerous C compilers and interpreters had been written for different Operating systems. Most of the commercial implementations of C were somewhat different from K & T version of C. This created some minor incompatibilities among different implementations of the language. Consequently, the American National Standards Institute (ANSI committee X3J11) worked on a standard version of C language. Virtually all commercial C compilers and interpreters now adhere to the ANSI standard. In 1989, American National Standards Institute (ANSI) established a standard specification of C language called: “ANSI X3.159-1989 – Programming Language C”. In 1999, a major revision of ANSI C specification was published by the International Organization for Standardization (ISO), “ISO/IEC 9899:1999 – Programming Languages – C”.

Purpose of C:

•    C is a general purpose, structured programming language not an object-oriented programming language. C programs are organized as modules and functions.
•    C supports low-level access to memory using address pointers. This makes C language a choice for implementing system software.
•    C supports array data structure with two ways to access array elements, indexes and pointers.
•    C supports dynamic memory allocation, which helps to reduce the size of object file generated by the compiler, and allow arrays to be declared with sizes determined at runtime.
•    C supports a large number of library routines for input/output operations, string manipulations, mathematical functions, etc.
•    C uses a header file to share declarations of variables and functions of a program module with other modules.
•    C does not do bounds checking on arrays. This often leads to un-desired behaviors during program execution.
Therefore, most C programs can be processed on many different computers with little or no alterations.

C Compilers:

C compilers are commonly available for computers of all sizes, and C interpreters are becoming increasingly common. The compilers are usually compact, and they generate object programs that are small and highly efficient when compared with programs compiled from other high-level languages. The interpreters are less efficient, though they are easier to use when developing a new program. Many programmers begin with an interpreter, and then switch to a compiler once the program has been debugged.
•    Ch – An embeddable C/C++ interpreter for cross-platform scripting and shell programming.
•    Development Assistant for C – IDE for Embedded Software development in ANSI C. Editor Browser, Call-Hierarchy Graph, Software Metrics, Make file Generator, Tool Integration.    Interface to Debuggers and Version Control Systems.
•    Dunfield Development Systems – Makers of Micro-C compiler for MS/DOS. PC based cross   development for embedded systems.
•    Freeware For Solaris – precompiled Gnu C and C++ binaries for Solaris, for Sparc and Intel architectures.
•    LCC-Win32- A compiler system for Windows 95 – NT, includes lcc, a development environment, and other tools.
•    MCPP – A portable C/C++ preprocessor supporting several compiler-systems on UNIX and Windows. MCPP supports C99 and C++98 as well as C90 and it provides Validation Suite which tests C/C++ preprocessor comprehensively.
•    Miracle C Compiler- Runs under Windows XP/Vista and compiles for console. Provides a multi-window development environment with integrated compiler and linker.
•    Mix Software Inc – Makers of Power C for DOS, Win32, and Linux. C++ add-ons, but no compiler
•    Open Watcom- Open Source project to maintain and enhance the Watcom C, C++ and Fortran Compilers.
•    Pellers C – A complete IDE and development kit for Windows and Pocket PC. The compiler is based on LCC, and the install builder for Windows is based on NSIS. Both are heavily modified.
•    Portable C Compiler – Originally a 1970s compiler updated for Linux. Compiles to C99 standard. Website features mailing lists, documentation and bug tracking system.
•    Sun Studio C/C++/Fortran Compilers – Resources for developers using C, C++, or FORTRAN for Solaris and Linux.
•    TinyCC-Win32 – Tiny C compiler usable as a library or exe. For I386 primarily. Includes linker and assembler. [Open source, LGPL]
•    Unicals Group – A vendor of embedded C/C++ development tools for OEM market.
•    Lcc- Retarget able ANSI C compiler for ALPHA, SPARC, MIPS R3000, Intel x86.

C Language Keywords:

There are certain reserved words, called keywords that have standard, predefined meanings in C. These keywords can be used only for their intended purpose; they cannot be used as programmer-defined identifiers. Some C compilers may recognize other keywords like ada, asm, far, entry etc. All the keywords are in lowercase. Since uppercase and lowercase characters are not equivalent, it is possible to utilize an uppercase keyword as an identifier. Normally, however, this is not done, as it is considered a poor programming practice.
The standard keywords are
auto:  auto is used to define a variable of storage class automatic.
Syntax: [auto] data-definition;
break: Passes control out of the compound statement. The break statement is used to jump out of the innermost enclosing loop (while, do, for or switch statements) explicitly and pass control to next statement immediately following the loop.
Syntax: break;
const: Makes variable value or pointer parameter un modifiable. When const is used with a variable, it uses the following syntax
Syntax: const variable-name [= value];
When const is used with a pointer parameter in a function parameter list, it uses the following syntax
Syntax: function-name (const type *var-name);
continue: Passes control to the beginning of the loop. continue causes control to pass to the end of innermost enclosing while, do or for statement, at which point the loop continuation condition is re-evaluated.
Syntax: continue;
do: Keyword do is usually used together with while to make another form of repeating statement. Such form of a loop uses the following syntax:
Syntax: do statement while (expression)
Statement usually a compound statement is executed repeatedly as long as the value of expression remains non-zero. The test takes place after each execution of the statement.

enum: Defines a set of constants of type int.
Syntax: enum [tag] {name [=value],  …};
The set name can optionally be given a type tag name with tag. name is the name of the constant that can optionally be assigned the (constant) value of value, etc.
If you give a type tag name, then you can declare variables of enumerated type using

enum tag variable-names;
extern: Keyword extern is used for indicating the variable is external and is declared outside every function and can be accessed by any function.
Syntax: extern data-definition;
extern function-prototype;
double and float: double and float are used for indicating floating type variables. Keyword float and double represents single precision and double precision floating point data respectively.
Ex: float variable1;
double variable2;
char: The char keyword is used for indicating the variable is of the type character.
Ex: char variable0;
if and else: if and else are used in decision making in C.
Keyword if is used for conditional execution.
Syntax: if (expression)
Alternatively if may be used together with else, using the following syntax
Syntax: if (expression)
int: int is used for indicating the variable is of type integer.
Syntax: int var0;
Here, var0 is a variable of type integer.
for: Keyword for is used for looping in C.
Syntax: for ([expr1]; [expr2]; [expr3]) statement
Statement is executed repeatedly until the value of expr2 is 0. Before the first iteration, expr1 is evaluated. This is usually used to initialize variables for the loop. After each iteration of the loop, expr3 is evaluated. This is usually used to increment a loop counter. In fact, the for-loop is absolutely equivalent to the following sequence of statements:
while (expr2)
That’s why expr1 and expr3 must contain side effects, else they are useless.

goto: Keyword goto is used for unconditional jump to a labeled statement inside that function.

Syntax: goto identifier;
Control is unconditionally transferred to the location of a local label specified by identifier.
short, long, signed and unsigned: short, long, signed and unsigned are type modifiers that alters the meaning of base data type to yield new type.
Syntax: short int var1;
long int var2;
signed int var3;
unsigned int var4;
Here, the int type variable is modified to short int, long int, signed int and unsigned int respectively.
Range of int type data types
Data types    Range
short int:    -32768 to 32767
long int:    –21474836482147483648 to 214743648
signed int:    -32768 to 32767
unsigned int:      0 to 65535
return: Exits the function.
return exits immediately from the currently executing function to the calling routine, optionally returning a value. The syntax is:
syntax: return [expression];
register: Tells the compiler to store the variable being declared in a CPU register. In standard C dialects, keyword auto uses the following syntax:
Syntax: register data-definition;
The register type modifier tells the compiler to store the variable being declared in a CPU register (if possible), to optimize access.
size of: Returns the size of the expression or type.
Keyword sizeof is, in fact, an operator.. It returns the size, in bytes, of the given expression or type (as type size t). Its argument may be an expression of a type name:
Syntax: sizeof expression
sizeof (type)
static: Preserves variable value to survive after its scope ends. Keyword static may be applied to both data and function definitions:
Syntax: static data-definition;
static function-definition;
struct: Groups variables into a single record.
The syntax for defining records is:
Syntax: struct [struct-type-name]
[type variable-names] ;

} [structure-variables];
A struct, like an union, groups variables into a single record. The struct-type-name is an optional tag name that refers to the structure type. The structure-variables are the data definitions, and are also optional. Though both are optional, one of the two must appear.
Elements in the record are defined by naming a type, followed by variable-names separated by commas. Different variable types can be separated by a semicolon.
switch, case, default: Branches control. Switch causes control to branch to one of a list of possible statements in the block of statements. The syntax is
Syntax: switch (expression) statement
The statement statement is typically a compound statement (i.e. a block of statements enclosed in braces). The branched-to statement is determined by evaluating expression, which must return an integral type. The list of possible branch points within statement is determined by preceding sub statements with
case constant-expression :
Where constant-expression must be an int and must be unique. Once a value is computed for expression, the list of possible constant-expression values determined from all case statements is searched for a match. If a match is found, execution continues after the matching case statement and continues until a break statement is encountered or the end of statement is reached. If a match is not found and this statement prefix is found within statement,
Execution continues at this point. Otherwise, statement is skipped entirely
typedef: Creates a new type. The syntax for defining a new type is
Syntax: typedef type-definition identifier;
This statement assigns the symbol name identifier to the data type definition type-definition.
union: Groups variables which share the same storage space.
A union is similar to a struct, except it allows you to define variables that share storage space. The syntax for defining unions is:
Syntax: union [union-type-name]
type variable-names;

} [union-variables];

void: void is used to indicate that a function takes no arguments or returns no value.
Ex: void no_return (int a){
Here, function no_return ( ) can take value but, can’t return value because, the return type is void.
volatile: Indicates that a variable can be changed by a background routine. Keyword volatile is an extreme opposite of const. It indicates that a variable may be changed in a way which is absolutely unpredictable by analyzing the normal program flow (for example, a variable which may be changed by an interrupt handler). This keyword uses the following syntax:
Syntax: volatile data-definition;

Basic Syntax of C Programming:

Every C program consists of one or more modules called functions. One of these functions is called main. The program begins by executing main function and accesses other functions, if any. Functions are written after or before main function separately. A function has  (1) heading consists of name with list of arguments ( optional ) enclosed in parenthesis, (2) argument declaration (if any) and (3) compound statement enclosed in two braces { } such that each statement ends with a semicolon. Comments, which are not executable statement, of necessary can be placed in between /* and */.

Tokens in C:
A C program consists of various tokens and a token is either a keyword, an identifier, a constant, a string literal, or a symbol. For example, the following C statement consists of five tokens:

printf (“Hello, World! n”);
The individual tokens are:
“Hello, World! n”


In C program, the semicolon is a statement terminator. That is, each individual statement must be ended with a semicolon. It indicates the end of one logical entity.
For example, following are two different statements:
printf(“Hello, World! n”);
return 0;


Comments are like helping text in your C program and they are ignored by the compiler. They start with /* and terminates with the characters */ as shown below:
/* my first program in C */
You cannot have comments with in comments and they do not occur within a string or character literals.

Identifiers: A C identifier is a name used to identify a variable, function, or any other user-defined item. An identifier starts with a letter A to Z or a to z or an underscore _ followed by zero or more letters, underscores, and digits (0 to 9).
C does not allow punctuation characters such as @, $, and % within identifiers. C is a case sensitive programming language. Thus Manpower and manpower are two different identifiers in C. Here are some examples of acceptable identifiers:
mohd       zara    abc   move_name  a_123
myname50   _temp   j     a23b9      ret Val


The following list shows the reserved words in C. These reserved words may not be used as constant or variable or any other identifier names.
auto    else    long    switch
break    enum    register    typedef
case    extern    return    union
char    float    short    unsigned
const    for    signed    void
continue    goto    sizeof    volatile
default    if    static    while
do    int    struct    _Packed

Whitespace in C: A line containing only whitespace, possibly with a comment, is known as a blank line, and a C compiler totally ignores it. Whitespace is the term used in C to describe blanks, tabs, newline characters and comments. Whitespace separates one part of a statement from another and enables the compiler to identify where one element in a statement, such as int, ends and the next element begins. Therefore, in the following statement:
int age;
There must be at least one whitespace character (usually a space) between int and age for the compiler to be able to distinguish them.

A Sample C Program:

/*program to calculate sum of two integers*/ /*Title (comments)*/
#include<stdio.h> /*Library file access*/
int main() /*Function Heading*/
int a,b,c; /*Variable declarations*/
printf (“enter the values of a and b n”); /*Output statement*/
scanf (“%d”, &a, &b); /*Input statement*/
c =a+b; /*Assignment statement*/
printf(“sum of a and b =%d n”,c); /*Output statement*/
return 0;

Comments: These start with /* and continue, ignoring newlines, until the next */. Notice that this means there is no nesting of comments. We adhere to ANSI-C standard (until C99 becomes more ubiquitous), so you cannot assume that // comments until the end of a line will work. It may turn out that some compilers accept these, but you cannot assume that all compilers (particularly the one used by the marking TA) will.
#include directives: Includes declarations of functions from header files for standard libraries. For example, #include <stdio.h> includes declarations of functions for the standard library that are useful for input/output. Note the distinct absence of semicolon.
int main (): An executable program must have a main method, which is what’s called when the program is executed. int indicates that this function returns an integer value to the shell where it is executed. In this course we will normally return 0.

Advantages of C Language:

•    C Language is a CASE SENSITIVE language.
•    Reusability: Programs written in C can be reused. You can save your C programs into a library file and invoke them in your next programming project simply by including the library file.
•    Readability: C Programs are easy to read.
•    Maintainability: C Programs are easy to maintain.
•    Portability: C Programs are portable across different computer platforms by just recompile them according to the relative operating systems.
•    Learn ability: You don’t have to remember many C keywords or commands before you start to write programming in C.
•    Major parts of popular operating systems like Windows, UNIX, Linux is still written in C.

Disadvantages of C Language:

•    C is not Object Oriented Programming Languages (OOPL) that’s why C++ comes into existence to overcome the disadvantages of C language.
•    There is no runtime checking in C language.
•    There are some concepts that are not present in C language:
Namespace and other OOPS features (Inheritance, Polymorphism, Encapsulation, Abstraction etc).
•    There is no strict Type Checking i.e, when we pass an integer to a floating point type variable.




This site uses Akismet to reduce spam. Learn how your comment data is processed.