Saturday, October 24, 2020

Constants in 'C':

Numeric Constant: Numeric constants consist of numeric digits, they may or may not have decimal point (.).

The rules for defining numeric constants-

1.    Numeric constant should have at least one digit.

2.    No comma or space is allowed within the numeric constant.

3.    Numeric constants can either be positive or negative but default sign is always positive.

There are two types of numeric constants-

Integer constant

Integer constants are whole numbers which have no decimal point ( . )

Some valid decimal integer constants are-

123

3705

23759

Some invalid decimal integer constants are-

2.5      Illegal character ( . )

3#5     Illegal character ( # )

98 5    No blank. space allowed

0925   First digit can not be zero

8,354 Comma is not allowed'

Real (floating point) Constants:

Floating point constants are numeric constants that contain decimal point.

Some valid floating point constants are-

0.5

5.3

4000.0

0.0073

5597.

39.0807


 

Character Constants:

Character constant is a single character that is enclosed within single quotes.

Some valid character constants are-

'9'        'D'       '$'       ‘ ’         '#'

Invalid character constants are-

'four'   There should be only one character within quotes

"d"       Double quotes are not allowed

"           No character between single quotes

y          Single quotes missing

 


String Constants:

A string constant has zero, one or more than one character. A string constant is enclosed within double quotes (" "). At the end of string, \0 is 'automatically placed by the compiler.

"Kumar"

"593"

"8"

"A"

Symbolic Constants:

If we want to use a constant several times then we can provide it a name. For example if we have to use the constant 3.14159265 at many places in our program, then we can give it a name PI and use this name instead of writing the constant value everywhere. These types of constants are called symbolic constants or named constants.

A symbolic constant is a name that substitutes for a sequence of characters

These constants are generally defined at the beginning of the program as

#define name value

Here 'name' is the symbolic name for the constant, and is generally written in uppercase letters. 'value' can be numeric, character or string constant.

Some examples of symbolic constants are as-

#define MAX 100

#define PI 3.14159625

#define CH 'y'

#define NAME "Sam"

In the program, these names will be replaced by the corresponding values. These symbolic constants improve the readability and modifiable of the program.

Friday, October 23, 2020

Elements of C, Tokens & Keywords:

Elements of C:

C Character Set:-The characters that are used in C programs are given below

  Alphabets :- A, B, C……Z  a, b, c...… z

  Digits :- 0, 1, 2, 3, 4, 5, 6, 7, 8, 9


Special Characters:




Tokens: Token as the smallest individual element in C. Therefore tokens in C is the building block or the basic component for creating a program in C language.

Token is a collection of:

  1. Keywords
  2. Identifiers
  3. Constants
  4. Strings
  5. Operators

Keywords:






Identifiers, Variables and Datatypes in 'C'

Identifiers:

  All the words that we'll use in our C programs will be either keywords or identifiers.

  Keywords are defined and can't be changed by the user, while identifiers are user defined words and are used to names to entities like variables, arrays, functions, structures etc.


Rules for naming identifiers are:

  1. The name should consist of only alphabets (both upper and lower case), digits and underscore sign( _ ).        
  2. First character should be an alphabet or underscore.
  3. The name should not be a keyword.
  4. Since C is case sensitive, the uppercase and lowercase letters are considered different.
  5. An identifier name may be arbitrarily long.

Variables:

Variable is a name that can be -used to store values. Variables can take different values but one at a time. These values can be changed during execution of the program. A data type is associated with each variable. The data type of the variable decides what values it can take.

The rules for naming variables are same as that for naming identifiers.

 Declaration of Variable:

It is must to declare a variable before it is used in the program. Declaration of a variable specifies it’s name and datatype. The type and range of values that a variable can store depends upon its datatype.

The syntax of declaration of a variable is-

datatype variablename;

Here datatype may be int, float, char, double etc. Some examples of declaration of variables are

int x;

float salary;

char grade;

Here x is a variable of type int, salary is a variable of type float, and grade is a variable of type char.

We can also declare more than one variable in a single declaration. For example- )

int x, y., z, total;

Here x, y, z, total are all variables of type int.

Initialization of Variables:

When a variable is declared it contains undefined value commonly known as garbage value. If we want we can assign some initial value to the variable during the declaration itself, this is called initialization of the variable.

For example

int a = 5;

float x = 8.9, Y = 10.5;

char ch = 'y';

double num = 0.15197e-7;

int l, m, n, total = 0;

Datatypes:



Saturday, October 10, 2020

Structure of a 'C' Program

C program is a collection of one or more functions. Every function is a collection of statements that perform a specific task.

The general structure of C program is:

Structure of a 'C' Program

Comments can be placed anywhere in a program and are enclosed between the delimiters /* at */. Comments are generally used for documentation purposes. Preprocessor directives are processed through preprocessor before the C source code passes through compiler. The commonly used preprocessor directives are #include and #define. #include is used for including header files. #define is used to define symbolic constants and macros.

Every C program has one or more functions. If a program has only one function then it must be main(). Execution of every C program starts with main() function. It has two parts, declaration of local variables and statements. The scope of the local variable is local to that function only. Statements in the main() function are executed one by one. Other functions are the user-defined functions, which also have local variables and C statements. They can be defined before or after main(). It may be possible that some variables have to be used in many functions, so it is necessary to declare them globally. These variables are called global variables.


Friday, October 9, 2020

History & Characteristics of 'C'

History:

    In earlier days, every language was designed for some specific purpose. For example FORTRAN (Formula Translator) was used for scientific and mathematical applications, COBOL (Common Business Oriented Language) were used for business applications. So need of such a language was felt which could withstand most of the purposes. "Necessity is the mother of invention". From here the first step towards C was put forward by Dennis Ritchie.

     The C language was developed in 1970's at Bell laboratories by Dennis Ritchie. Initially it was designed for programming in the operating system called UNIX. After the advent of C, the whole UNIX operating system was rewritten using it. Now almost the entire UNIX operating system and the tools supplied with it including the C compiler itself are written in C.

    The C language is derived from the B language, which was written by Ken Thompson at AT&T Bell Laboratories. The B language was adopted from a language called BCPL (Basic Combined Programming Language), which was developed by Martin Richards at Cambridge University.

    In 1982 a committee was formed by ANSI (American National Standards Institute) to standardize the C language. Finally in 1989, the standard for C language was introduced known as ANSI C. Generally most of the modern compilers conform to this standard.


Characteristics:

It is a middle level language. It has the simplicity of a high level language as well as the power of low level language. This aspect of C makes it suitable for writing both application programs and system programs. Hence it is an excellent, efficient and general-purpose language for most of the application such as mathematical, scientific, business and system software applications.

C is small language, consisting of only 32 English words known as keywords (if, else, for, break etc.). The power of C is augmented by the library functions provided with it. Moreover, the language is extensible since it allows the users to add their own library functions to the library.

C contains control constructs needed to write a structured program hence it is considered a structure programming language. It includes structures for selection (if.. .else, switch), repetition (while, for, do... while) and for loop exit (break).

The programs written in C are portable i.e. programs written for one type of computer or operating system can be run on another type of computer or operating system.