egsoftweb@gmail.com
 facebook.com/egsoftweb

Introduction to C programming

 31-Mar-2019 |  Admin

History of C

C was invented and first implemented by Dennis Ritchie on a DEC PDP-11 that used the UNIX operating system.

C is the result of a development process that started with an older language called BCPL.

BCPL was developed by Martin Richards, and it influenced a language called B, which was invented by Ken Thompson. B led to the development of C in the 1970s.

OR

C is a programming language developed at AT & T’s Bell Laboratories of USA in 1972. It was designed and written by a man named Dennis Ritchie. In the late seventies C began to replace the more familiar languages of that time like PL/I, ALGOL, etc. No one pushed C. It wasn’t made the ‘official’ Bell Labs language. Thus, without any advertisement C’s reputation spread and its pool of users grew. Ritchie seems to have been rather surprised that so many programmers preferred C to older languages like FORTRAN or PL/I, or the newer ones like Pascal and APL.

C Is a Middle-Level Language

C is thought of as a middle-level language because it combines the best elements of high-level languages with the control and flexibility of assembly language.

Highest level

Ada

 

Modula-2

 

Pascal

 

COBOL

 

FORTRAN

 

BASIC

 

 

Middle level

Java

 

C#

 

C++

 

C

 

Forth

 

Macro-assembler

 

 

Lowest level

Assembler

 

Initially, C was used for systems programming. A systems program forms a portion of the operating system of the computer or its support utilities, such as editors, compilers, linkers, and the like. As C grew in popularity, many programmers began to use it to program all tasks because of its portability and efficiency- and because they liked it! At the time of its creation, C was a much longed-for, dramatic improvement in programming languages. In the years that have since elapsed, C has proven that it is up to any task.

Difference between Compiler and Interpreter

In its simplest form, an interpreter reads the source code of your program one line at a time, performing the specific instructions contained in that line. This is the way earlier versions of BASIC worked. In languages such as Java, a program`s source code is first converted into an intermediary form that is then interpreted. In either case, a run-time interpreter is still required to be present to execute the program.

A compiler reads the entire program and converts it into object code, which is a translation of the program`s source code into a form that the computer can execute directly. Object code is also referred to as binary code or machine code. Once the program is compiled, a line of source code is no longer meaningful in the execution of your program.

Keywords in C

Keywords are predefined, reserved words used in programming that have special meanings to the compiler. Keywords are part of the syntax and they cannot be used as an identifier. Below is the list of c keywords:

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

 

 

Linker

A linker is a computer program that takes one or more object files generated by a compiler and combines them into one, executable program.

 

The Form of a C Program

Global declarations

int main(parameter list)

{

      statement sequence

}

return-type f1(parameter list)

{

      statement sequence

}

 

return-type f2(parameter list)

{

      statement sequence

}

. . .

return-type fN(parameter list)

      statement sequence

}

 

C, C99, ANSI C and GNU C

 01-Apr-2019 |  Admin

Difference between C, C99, ANSI C and GNU C:

 

Everything before standardization is generally called "K&R C", after the famous book, with Dennis Ritchie, the inventor of the C language, as one of the authors. This was "the C language" from 1972-1989.

 

The first C standard was released 1989 nationally in USA, by their national standard institute ANSI. This release is called C89 or ANSI-C. From 1989-1990 this was "the C language".

 

The year after, the American standard was accepted internationally and published by ISO (ISO 9899:1990). This release is called C90. Technically, it is the same standard as C89/ANSI-C. Formally, it replaced C89/ANSI-C, making them obsolete. From 1990-1999, C90 was "the C language".

 

Please note that since 1989, ANSI haven`t had anything to do with the C language. Programmers still speaking about "ANSI C" generally haven`t got a clue about what it means. ISO "owns" the C language, through the standard ISO 9899.

 

A minor update was released in 1995, sometimes referred to as "C95". This was not a major revision, but rather a technical amendment formally named ISO/IEC 899:1990/Amd.1:1995. The main change was introduction of wide character support.

 

In 1999, the C standard went through a major revision (ISO 9899:1999). This version of the standard is called C99. From 1999-2011, this was "the C language".

 

In 2011, the C standard was changed again (ISO 9899:2011). This version is called C11. Various new features like _Generic, _Static_assert and thread support were added to the language. The update had a lot of focus on multi-core, multi-processing and expression sequencing. From 2011-2017, this was "the C language".

 

In 2017, C11 was revised and various defect reports were solved. This standard is informally  called C17 and was released as ISO 9899:2018. It contains no new features, just corrections. It is the current version of the C language.

 

"C99 strict" likely refers to a compiler setting forcing a compiler to follow the standard by the letter. There is a term conforming implementation in the C standard. Essentially it means: "this compiler actually implements the C language correctly". Programs that implement the C language correctly are formally called strictly conforming programs.

 

"GNU C" can mean two things. Either the C compiler itself that comes as part of the GNU Compiler Collection (GCC). Or it can mean the non-standard default setup that the GCC C compiler uses. If you compile with gcc program.c then you don`t compile according to the C standard, but rather a non-standard GNU setup, which may be referred to as "GNU C". For example, the whole Linux kernel is made in non-standard GNU C, and not in standard C.

 

If you want to compile your programs according to the C standard, you should type gcc -std=c99 -pedantic-errors. Replace c99 with c11 if your GCC version supports it.

 

Reference:

[1] https://stackoverflow.com/questions/17206568/what-is-the-difference-between-c-c99-ansi-c-and-gnu-c

The Basic Data Types

 07-Apr-2019 |  Admin


Data type`s keyword

Data type

char

character

int

integer

float

floating-point

double

double floating-point

void

valueless

C89 five foundational data types:

 

These types form the basis for several other types. The size and range of these data types may vary among processor types and compilers. However, in all cases an object of type char is 1 byte. The size of an int is usually the same as the word length of the execution environment of the program.

 

For most 16-bit environments, such as DOS or Windows 3.1, an int is 16 bits. For most 32-bit environments, such as Windows 95/98/NT/2000, an int is 32 bits. However, you cannot make assumptions about the size of an integer if you want your programs to be portable to the widest range of environments. It is important to understand that C stipulates only the minimal range of each data type, not its size in bytes.

 

The use of signed on integers is allowed, but it is redundant because the default integer declaration assumes a signed number. The most important use of signed is to modify char in implementations in which char is unsigned by default.

 

Signed and unsigned integers differ in the way that the high-order bit of the integer is interpreted. If you specify a signed integer, the compiler generates code that assumes the high-order bit of an integer is to be used as a sign flag. If the sign flag is 0, the number is positive; if it is 1, the number is negative.

 

If the high-order bit were set to 1, the number would be interpreted as –1. However, if you declare this to be an unsigned int, the number becomes 65,535 when the high-order bit is set to 1.

 

When a type modifier is used by itself (that is, when it does not precede a basic type), then int is assumed. Thus, the following sets of type specifiers are equivalent:

 

Specifier

Same As

signed

signed int

unsigned

unsigned int

long

long int

short

short int

 

Although the int is implied, it is common practice today to specify the int anyway.

 

Note: To the five basic data types defined by C89, C99 adds three more:

_Bool, _Complex, and _Imaginary.

 

 

Here simple code is given to print data type`s size and range.  Right shift is used for calculating powers of 2 to get range and `sizeof` operator is used for calculation of size. 

Identifier Names & Variables

 07-Apr-2019 |  Admin

Identifier Names:

The names of variables, functions, labels, and various other user-defined items are called identifiers.

The length of these identifiers can vary from one to several characters.

The first character must be a letter or an underscore, and subsequent characters must be either letters, digits or underscores.

Correct

Incorrect

i1

1i

a23     

23a

low_count

low..count

helloWorld

hello!World

correct and incorrect identifier names

 

In C, identifiers may be of any length. However, not all characters will necessarily be significant.

C defines two kinds of identifiers: external and internal.

An external identifier will be involved in an external link process. These identifiers, called external names, include function names and global variable names that are shared between source files. If the identifier is not used in an external link process, then it is internal. This type of identifier is called an internal name and includes the names of local variables. For example, in C89, at least the first 6 characters of an external identifier and at least the first 31 characters of an internal identifier will be significant. C99 has increased these values.

In C99, an external identifier has at least 31 significant characters, and an internal identifier has at least 63 significant characters. As a point of interest, in C++, at least the first 1,024 characters of an identifier are significant. These differences may be important if you are converting a program from C89 to C99, or from C to C++.

In an identifier, upper- and lowercase are treated as distinct. Hence, count, Count, and COUNT are three separate identifiers.

An identifier cannot be the same as a C keyword and should not have the same name as functions that are in the C library.


Variables:

A variable is a named location in memory that is used to hold a value that can be modified by the program.

All variables must be declared before they can be used.

The general form of a declaration is

type variable_list;

Here, type must be a valid data type plus any modifiers, and variable_list may consist of one or more identifier

names separated by commas. Here are some declarations:

int i, j, l;

short int si;

unsigned int ui;

double balance, profit, loss;

In C the name of a variable has nothing to do with its type

 

Types of variables:

Local variables:  Variables that are declared inside a function.

Variables as formal parameters: A function`s parameters are known as variables that will accept the values of the arguments.

Global variables: Global variables are known throughout the program and may be used by any piece of code. Also, they will hold their value throughout the program`s execution. You create global variables by declaring them outside of any function. Any expression may access them, regardless of what block of code that expression is in.