Language fundementals





INTRODUCTION TO C LANGUAGE

 C is a general-purpose high level language that was originally developed by Dennis Ritchie for the Unix operating system. It was first implemented on the Digital Eqquipment Corporation PDP-11 computer in 1972

The Unix operating system and virtually all Unix applications are written in the C language. C has now become a widely used professional language for various reasons.  Easy to learn  Structured language  It produces efficient programs.  It can handle low-level activities.  It can be compiled on a variety of computers.

Facts about C

πŸ‘‰ C was invented to write an operating system called UNIX.

πŸ‘‰C is a successor of B language which was introduced around 1970

 πŸ‘‰ The language was formalized in 1988 by the American National Standard Institue (ANSI).

πŸ‘‰ By 1973 UNIX OS almost totally written in C.

πŸ‘‰Today C is the most widely used System Programming Language.

πŸ‘‰ Most of the state of the art software have been implemented using C

Why to use C?

C was initially used for system development work, in particular the programs that make-up the operating system. C was adoped as a system development language because it produces code that runs nearly as fast as code written in assembly language. Some examples of the use of C might be:

 Operating Systems

 Language Compilers

  Assemblers

  Text Editors

  Print Spoolers

  Network Drivers

  Modern Programs

  Data Bases

  Language Interpreters

  Utilities C

C Program File

All the C programs are writen into text files with extension ".c" for example hello.c. You can use "vi" editor to write your C program into a file.

The C Compilation Model

The Preprocessor

 The Preprocessor accepts source code as input and is responsible for

  removing comments

  Interpreting special preprocessor directives denoted by #.

 For example

πŸ‘‰ #include -- includes contents of a named file. Files usually called header files.

 e.g o #include <math.h> -- standard library maths file.

πŸ‘‰ #include <stdio.h> -- standard library I/O file

πŸ‘‰ #define -- defines a symbolic name or constant. Macro substitution.

πŸ‘‰ #define MAX_ARRAY_SIZE 100

C Compiler

 The C compiler translates source to assembly code. The source code is received from the preprocessor.

 Assembler

The assembler creates object code. On a UNIX system you may see files with a .o suffix (.OBJ on MSDOS) to indicate object code files.

 Link Editor

If a source file references library functions or functions defined in other source files the link editor combines these functions (with main()) to create an executable file.

C TOKENS

 C tokens are the basic buildings blocks in C language which are constructed together to write a C program.

 Each and every smallest individual unit in a C program is known as C tokens.

 C tokens are of six types. They are

 Keywords               (eg: int, while),

 Identifiers               (eg: main, total),

 Constants              (eg: 10, 20),

 Strings                    (eg: ―total‖, ―hello‖),

Special symbols (eg: (), {}),

Operators              (eg: +, /,-,*)

C KEYWORDS

 C keywords are the words that convey a special meaning to the c compiler. The keywords cannot be used as variable names.

 The list of C keywords is given below:

auto                      break                   case                      char                      const

continue              default                do                        double                else

enum                   extern                  float                     for                        goto

if                            int                         long                     register                return

short                    signed                  sizeof                   static                   struct

switch                  typedef                union                   unsigned              void

volatile                while

C IDENTIFIERS

 Identifiers are the names that are given to various program elements such as variables.arrays,pointers,functions,symbolic constants,enumerated constants,macros,datatypes etc.,According to the c compiler,the names are generally given either to the memory locations or to the constants and expressions or to the datatypes.The memory will be occupied for the elements like variables,arrays,pointers and functions.In those cases,names are given to the memory location of them.

VARIABLES

It identifies the memory location with the variable name to store a single value.

Type                                    Description

Char                                     Typically a single octet(one byte). This is an integer type.

int                                        The most natural size of integer for the machine.

Float                                    A single-precision floating point value.

double                                A double-precision floating point value.

Void                                     Represents the absence of type.

For definition without an initializer: variables with static storage duration are implicitly initialized with NULL (all bytes have the value 0); the initial value of all other variables are undefined.


 C Constants:

       A C constant refers to the data items that do not change their value during the program execution. The constants supported by C language are classified into two types as shown below:



 Integer Constants

 Integer constants are whole numbers without any fractional part. It must have at least one digit and may contain either + or – sign. A number with no sign is assumed to be positive. There are three types of integer constants:

 Decimal Integer Constants

 Integer constants consisting of a set of digits, 0 through 9, preceded by an optional – or + sign.

 Example of valid decimal integer constants

 341,  -341,  0,  8972 

Octal Integer Constants

Integer constants consisting of sequence of digits from the set 0 through 7 starting with 0 is said to be octal integer constants. 

Example of valid octal integer constants

 010,  0424,  0,  0540

 Hexadecimal Integer Constants 

Hexadecimal integer constants are integer constants having sequence of digits preceded by 0x or 0X. They may also include alphabets from A to F representing numbers 10 to 15.

 Example of valid hexadecimal integer constants

 0xD, 0X8d, 0X, 0xbD

 It should be noted that,  octal and hexadecimal integer constants are rarely used in programming. 

Real Constants 

The numbers having fractional parts are called real or floating point constants. These may be represented in one of the two forms called fractional form or the exponent form and may also have either + or – sign preceding it. 

Example of valid real constants in fractional form or decimal notation

 0.05, -0.905, 562.05, 0.015 

 Character Constants

 A character constant contains one single character enclosed within single quotes.

Examples of valid character constants

 ‗a‘ , ‗Z‘,  ‗5‘

It should be noted that character constants have numerical values known as ASCII values, for example, the value of ‗A‘ is 65 which is its ASCII value. 

structure of a simple 'C' program 

syntax

Escape Characters/ Escape Sequences 


C allows us to have certain non graphic characters in character constants. Non graphic characters are those characters that cannot be typed directly from keyboard, for example, tabs, carriage return, etc. 

These non graphic characters can be represented by using escape sequences represented by a backslash() followed by one or more characters.

 NOTE: An escape sequence consumes only one byte of space as it represents a single character.

  • \n - New line character 
  • \r - carriage return character
  • \l - line feed character
  • \f - form speed character
  • \t - tab space character
  • \b - back space character
  • \a - small beep sound
  • \v - vertical tab space



Comments