Write up on Tech Geek History: C programming

Introduction

C is a general-purpose programming language with features economy of expression, modern flow control and data structures, and a rich set of operators. C is not a “very high level” language, nor a “big” one, and is not specialized to any particular area of application. But its absence of restrictions and its generality make it more convenient and effective for many tasks than supposedly more powerful languages. C was originally designed for and implemented on the UNIX operating system on the DEC PDP-11, by Dennis Ritchie. The operating system, the C compiler, and essentially all UNIX applications programs (including all of the software used to prepare this book) are written in C. Production compilers also exist for several other machines, including the IBM System/370, the Honeywell 6000, and the Interdata 8/32.

C is not tied to any particular hardware or system, however, and it is easy to write programs that will run without change on any machine that supports C.

C is a general-purpose programming language, and is used for writing programs in many differ ent domains, such as operating systems, numerical computing, graphical applications, etc. It is a small language, with just 32 keywords (see [HS95, page 23]). It provides “high-level” structured programming constructs such as statement grouping, decision making, and looping, as well as “low level” capabilities such as the ability to manipulate bytes and addresses. Since C is relatively small, it can be described in a small space, and learned quickly. A programmer can reasonably expect to know and understand and indeed regularly use the entire language [KR88, page 2]. C achieves its compact size by providing spartan services within the language proper, foregoing many of the higher-level features commonly built-in to other languages. For example, C provides no operations to deal directly with composite objects such as lists or arrays. There are no memory management facilities apart from static definition and stack-allocation of local variables. And there are no input/output facilities, such as for printing to the screen or writing to a file. Much of the functionality of C is provided by way of software routines called functions. The language is accompanied by a standard library of functions that provide a collection of commonly used operations. For example, the standard function printf() prints text to the screen (or, more precisely, to standard output—which is typically the screen). The standard library will be used extensively throughout this text; it is important to avoid writing your own code when a correct and portable implementation already exists.

By the early 1980’s, C had become widely used throughout the industry, but with many different implementations and changes. The discovery by PC implementors of C’s advantages over BASIC provided a fresh boost. Microsoft had an implementation for the IBM PC which introduced new keywords (far, near, etc.) to help pointers to cope with the irregular architecture of the Intel 80×86 chip. As many other non-pcc-based implementations arose, C threatened to go the way of BASIC and evolve into an ever-diverging family of loosely related languages. It was clear that a formal language standard was needed. Fortunately, there was much precedent in this area—all successful programming languages are eventually standardized. However, the problem with standards manuals is that they only make sense if you already know what they mean. If people write them in English, the more precise they try to be, the longer, duller and more obscure they become. If they write them using mathematical notation to define the language, the manuals become inaccessible to too many people. Over the years, the manuals that define programming language standards have become longer, but no easier to understand. The Algol-60 Reference Definition was only 18 pages long for a language of comparable complexity to C; Pascal was described in 35 pages. Kernighan and Ritchie took 40 pages for their original report on C; while this left several holes, it was adequate for many implementors. ANSI C is defined in a fat manual over 200 pages long. In 1983 a C working group formed under the auspices of the American National Standards Institute. Most of the process revolved around identifying common features, but there were also changes and significant new features introduced. The far and near keywords were argued over at great length, but ultimately did not make it into the mildly UNIX-centric ANSI standard. Even though there are more than 50 million PC’s out there, and it is by far the most widely used platform for C implementors, it was (rightly in our view) felt undesirable to mutate the language to cope with the limitations of one specific architecture.

C has the usual facilities for grouping things together to form composite types– arrays and records (which are called “structures”).

Significance of the Study

Chapter 1:

Introduction To Arrays: In C programming, one of the frequently problem is to handle similar types of data. For example: if the user wants to store marks of 500 students, this can be done by creating 500 variables individually but, this is rather tedious and impracticable. These types of problem can be handled in C programming using arrays. An array in C Programing can be defined as number of memory locations, each of which can store the same data type and which can be references through the same variable name. It is a collective name given to a group of similar quantities. These similar quantities could be marks of 500 students, number of chairs in university, salaries of 300 employees or ages of 250 students. Thus we can say array is a sequence of data item of homogeneous values (same type). These values could be all integers, floats or characters etc. We have two types of arrays:

1. One-dimensional arrays.

2. Multidimensional arrays. One Dimensional Arrays: A one-dimensional array is a structured collection of components (often called array elements) that can be accessed individually by specifying the position of a component with a single index value.

 Arrays must be declared before they can be used in the program. Here is the declaration syntax of one dimensional array: data_type array_name[array_size]; Here “data_type” is the type of the array we want to define, “array_name” is the name given to the array and “array_size” is the size of the array that we want to assign to the array. The array size is always mentioned inside the “[]”.

 For example: Int age[5]

C, indexing starts from 0, so an array arr[10] will have ten elements namely arr[0], arr[1], … arr[9].

Just like other variables, arrays must also be declared before being used. The declaration includes the type of the element stored in the array (int, float or char), and the maximum number of elements that we will store in the array. The C compiler needs this to determine how much of memory space to reserve for the array. The elements of an array are manipulated in the same manner as any other ordinary variable (i.e. they can be added, subtracted or compared).

Initializing Arrays Initializing of array is very simple in c programming. The initializing values are enclosed within the curly braces in the declaration and placed following an equal sign after the array name. Here is an example which declares and initializes an array of five elements of type int. Array can also be initialized after declaration

. Look at the following code, which demonstrate the declaration and initialization of an array.

 int age[5]={2,3,4,5,6}; It is not necessary to define the size of arrays during initialization e.g.

 int age[]={2,3,4,5,6}; In this case, the compiler determines the size of array by calculating the number of elements of an array.

C programming language allows the user to create arrays of arrays known as multidimensional arrays. To access a particular element from the array we have to use two subscripts one for row number and other for column number. The notation is of the form array [i] [j] where i stands for row subscripts and j stands for column subscripts.

The array holds i*j elements. Suppose there is a multidimensional array array[i][j][k][m].

 Then this array can hold i*j*k*m numbers of data. In the same way, the array of any dimension can be initialized in C programming. For example : int smarks[3][4];

Here, smarks is an array of two dimension, which is an example of multidimensional array. This array has 3 rows and 4columns. For better understanding of multidimensional arrays, array elements of above example can be as below:

A pointer is a variable that contains an address in memory. In a 64 bit architectures, the size of a  pointer is 8 bytes independent on the type of the pointer.  

When C was designed by Kernighan, Ritchie, and Thompson, they wanted to create a high level  language that would not sacrifice the same optimization opportunities that you have by  programming in an assembly language. They had the great idea to give arrays and pointers the  same equivalence and to be able to use arrays or pointers interchangeably. 

Memory allocation refers to assigning a particular space in a computer’s memory for a computer program or code. There are two types of memory allocations. Static Memory allocation Dynamic Memory allocation

❖ Static Memory allocation

• Static memory allocation is also known as Compile-time memory allocation because the memory is allocated during compile time.

• In this type of memory allocation, the memory that the program can use is fixed i.e. we can not allocate or deallocate memory during the program’s execution.

Properties of Static Memory allocation

• Memory allocation is done during compile time.

 • Stack Memory is used here.

• Memory cannot be changed while executing a program. • The static memory allocation is fast and saves running time. • It is less efficient as compared to Dynamic memory allocation. • The allocation process is simple. Memory Allocation in C C Programming ©

Topperworld ➔ Advantages of Static Memory allocation

 • The memory is allocated during compile time.

• It is easy to use.

• It uses a stack Data Structure.

• The execution time is efficiently controlled.

 ➔ Disadvantages of Static Memory allocation

 • This allocation method leads to memory wastage.

 • Memory cannot be changed while executing a program.

 • Exact memory requirements must be known.

• If memory is not required, it cannot be freed.

Dynamic Memory allocation

• The concept of dynamic memory allocation in c language enables the C programmer to allocate memory at runtime.

⚫ Dynamic memory allocation in c language is possible by 4 functions of stdlib.h header file. ➢ malloc() ➢ calloc() ➢ realloc() ➢ free

() Properties of Dynamic Memory allocation

• Memory is allocated at runtime.

• Memory can be allocated and released at any time.

 • Heap memory is used here.

 • Dynamic memory allocation is slow.

 • It is more efficient as compared to Static memory allocation

 • The allocation process is simple is complicated.

• Memory can be resized dynamically or reused.

➔ Advantages of Dynamic Memory allocation

 • This allocation method has no memory wastage.

• The memory allocation is done at run time.

• Memory size can be changed based on the requirements of the dynamic memory allocation. • If memory is not required, it can be freed.

➔ Disadvantages of Dynamic Memory allocation

 • It requires more execution time due to execution during runtime.

• The compiler does not help with allocation and deallocation. Programmer needs to both allocate and deallocate the memory.

(Partly adapted from the Fortran FAQ)

 a) FORTRAN tends to meet some of the needs of scientists better. Most notably, it has built in support for: o Variable-dimension array arguments in subroutines. A feature required for writing general purpose routines without explicitly specifying the array dimensions passed to them. Standard C lacks this important feature (some compilers like gcc have it as non-standard extension) and the workarounds are very cumbersome

This feature by itself is sufficient to prefer Fortran over C in numerical computing. o A rich set of useful generic-precision intrinsic functions. Such functions can be highly optimized (written in assembly language with optimized cache utilization), and they make programs standard at a higher level (and more portable).

Built-in complex arithmetic (arithmetic involving complex numbers represented as having real and imaginary components). o Array index-ranges may start and end at an arbitrary integer, the C convention of [0,N-1] is usually inconvenient. o Better I/O routines, e.g. the implied do facility gives flexibility that C’s standard library can’t match. The Fortran compiler directly handles the more complex syntax involved, and as such syntax can’t be easily reduced to argument passing form, C can’t implement it efficiently.

 A compiler-supported infix exponentiation operator which is generic with respect to both precision and type, AND which is generally handled very efficiently, including the commonly occurring special case floating-point**small-integer. o Fortran 90 supports an array notation that allows operations on array sections, and using vector indices. The new intrinsic functions allow very sophisticated array manipulations.

 The new array features are suitable for parallel processing. o Fortran 90 supports automatic selection of numeric data types having a specified precision and range, and makes Fortran programs even more portable. o Fortran extensions for parallel programming are standardized by the High Performance Fortran (HPF) consortium. Fortran 90 supports useful features of C (column independent code, pointers, dynamic memory allocation, etc) and C++ (operator overloading, primitive objects)

. b) The design of FORTRAN allows maximal speed of execution: o FORTRAN 77 lacks explicit pointers, which is one reason that it is more amenable to automatic code optimization. This is very important for high-performance computing.

 Fortran 90 allows explicit pointers restricted to point only to variables declared with the “target” attribute, thus facilitating automatic optimizations. o Fortran was designed to permit static storage allocation, saving the time spent on creating and destroying activation records on the stack every procedure call/return.

Recursive procedures are impossible with static allocation, but can be simulated efficiently when needed (very rare). o Fortran implementations may pass all variables by reference, the fastest method. o Fortran disallows aliasing of arguments in procedure-call statements (CALL statements and FUNCTION references), all passed argument lists must have distinct entries. Fortran disallows also aliasing between COMMON (global) variables and dummy arguments. These restrictions allows better compiler optimizations. c) There is a vast body of existing FORTRAN code (much of which is publicly available and of high quality). Numerical codes are particularly difficult to port, scientific establishments usually do not have large otherwise idle programming staffs, etc. so massive recoding into any new language is typically resisted quite strongly. d)

FORTRAN 77 tends to be easier for non-experts to learn than C, because its ‘mental model of the computer’ is much simpler. For example, in FORTRAN 77 the programmer can generally avoid learning about pointers and memory addresses, while these are essential in C. More generally, in FORTRAN 77 the difference between (C notation) x, &x, and often even *x is basically hidden, while in C it’s exposed. Consequently, FORTRAN 77 is a much simpler language for people who are not experts at computer internals. Because of this relative simplicity, for simple programming tasks which fall within its domain, (say writing a simple least-squares fitting routine), FORTRAN 77 generally requires much less computer science knowledge of the programmer than C does, and is thus much easier to use. Fortran 90 changes the picture somewhat, the new language is very rich and complex, but you don’t have to use or even know about all this complexity. e) The C standard requires only a basic double-precision mathematical library, and this is often what you get.

The FORTRAN standard, on the other hand, requires single & double precision math, many vendors add quad-precision (long double, REAL*16) and provide serious math support. Single-precision calculations may be faster than double-precision calculation even on machines where the individual machine instructions takes about the same time because single-precision data is smaller and so there are less ‘memory cache misses’. Quad-precision (long double) calculations are sometimes necessary to minimize roundoff errors. If you have only double-precision mathematical routines, the basic mathematical primitives will take up unnecessary CPU time when used in single-precision calculations and will be inexact if used with ‘long double’.

f) FORTRAN is designed to make numerical computation easy, robust and well-defined:

1) The order of evaluation of arithmetical expressions is defined precisely, and can be controlled with parentheses.

 2) The implicit type declaration feature saves time/typing (however it makes your program vulnerable to annoying and hard to detect bugs).

3) Case insensitivity eliminates bugs due to ‘miscased’ identifiers.

4) The lack of reserved words in the language gives the programmer complete freedom to choose identifiers.

5) The one statement per line principle (of course continuation lines are allowed with a special syntax) makes programs more robust.

 6) Added blanks (space characters) are insignificant (except in character constants) this also contributes to the robustness of FORTRAN programs.

 7) Linking with the mathematical library doesn’t require any compiler option (in C you to have to use “-lm”). g) Last but not least, FORTRAN compilers usually emit much better diagnostic messages. In summary, we can say that the difference between Fortran and C, is the difference between a language designed for numerical computations, and a language designed for other purposes (system programming).

BASIC (Beginner’s All-purpose Symbolic Instruction Code)C Programming Language
Primary PurposeA teaching language designed to be easy for students to learn simple programming concepts.A system programming language used for developing operating systems, embedded systems, and applications where performance is critical.
Learning CurveVery simple and easy for beginners to pick up quickly, often within a day or two of practice.Has a steep learning curve, requiring an understanding of lower-level concepts like memory management and pointers.
TypingHistorically less strict with data types, sometimes allowing implicit conversions.A strongly and statically typed language with strict type-checking rules.
ExecutionOften interpreted (programs run slower) in its original forms, allowing for interactive programming.A compiled language (source code is translated into faster machine code before execution)

                                                                   Dimension” and “array” are related concepts in computing and mathematics, not opposing terms; an array is a data structure, while a dimension is a property of that array. 

  • Array: An array is a named collection of data values, all of the same type, stored in contiguous memory locations. It is a fundamental data structure used to organize large amounts of information and is accessed using indices or subscripts.
  • Dimension: A dimension is a “direction” in which an array can vary, indicating the number of indices needed to locate a specific element within the array. 

The relationship between the two can be understood as follows:

  • A one-dimensional (1D) array (also known as a vector or list) requires a single index to access an element (e.g., array[i]).
  • A two-dimensional (2D) array (often used to represent a matrix or table) requires two indices (e.g., array[i][j], for rows and columns).
  • A three-dimensional (3D) array requires three indices (e.g., array[i][j][k], which can be visualized as having rows, columns, and sheets or depth). 

Dimension” and “array” are related concepts in computing and mathematics, not opposing terms; an array is a data structure, while a dimension is a property of that array. 

  • Array: An array is a named collection of data values, all of the same type, stored in contiguous memory locations. It is a fundamental data structure used to organize large amounts of information and is accessed using indices or subscripts.
  • Dimension: A dimension is a “direction” in which an array can vary, indicating the number of indices needed to locate a specific element within the array. 

The relationship between the two can be understood as follows:

  • A one-dimensional (1D) array (also known as a vector or list) requires a single index to access an element (e.g., array[i]).
  • A two-dimensional (2D) array (often used to represent a matrix or table) requires two indices (e.g., array[i][j], for rows and columns).
  • A three-dimensional (3D) array requires three indices (e.g., array[i][j][k], which can be visualized as having rows, columns, and sheets or depth). 

Dimension” and “array” are related concepts in computing and mathematics, not opposing terms; an array is a data structure, while a dimension is a property of that array. 

  • Array: An array is a named collection of data values, all of the same type, stored in contiguous memory locations. It is a fundamental data structure used to organize large amounts of information and is accessed using indices or subscripts.
  • Dimension: A dimension is a “direction” in which an array can vary, indicating the number of indices needed to locate a specific element within the array. 

The relationship between the two can be understood as follows:

  • A one-dimensional (1D) array (also known as a vector or list) requires a single index to access an element (e.g., array[i]).
  • A two-dimensional (2D) array (often used to represent a matrix or table) requires two indices (e.g., array[i][j], for rows and columns).
  • A three-dimensional (3D) array requires three indices (e.g., array[i][j][k], which can be visualized as having rows, columns, and sheets or depth). 

How to Create an Array in BASIC Language To create an array, the DIM (dimension) command is used. The DIM statement has the following syntax for a one dimensional array: DIM arrayName(n). Where “n” is a whole number. For example DIM Score

 (5) will reserve 6 spaces, Score (0) Score (1), Score (2), Score (3), Score (4) and Score (5) in the memory to hold Numeric values.

 While DIM NAME$ (10) will reserve 11 memory locations to store string values, that is, NAME$(0) … NAME$(10).

 The number inside the parentheses of the individual variables are called subscripts, and each variable is called a subscripted variable or element. The syntax for a two dimensional array is: DIM arrayName(m,n). “m,n” are whole numbers.

Leave a Comment

Your email address will not be published. Required fields are marked *