Cos201lecture Notes
Cos201lecture Notes
C PROGRAMMING LANGUAGE
An Overview of C programming Langauge.
C is a general-purpose language which has been closely associated with the UNIX operating
system for which it was developed - since the system and most of the programs that run it are
written in C. In 1972 Dennis Ritchie at Bell Labs developed C and by 1978 Brian Kernighan and
Dennis Ritchie co-wrote and published the first edition of 'the C programming language' book,
also known in the programming community as 'K&R'. For many years this text was the go-to for
C language description, definition, and reference.
In 1983, the American National Standards Institute (ANSI) established a committee to provide a
modern, comprehensive definition of C. The resulting definition, the ANSI standard, was
completed early in 1989. It was the commercial standard for the language. That version of the
language is known as “ANSI C” or C89.
C was used all around the world, so a year later in 1990 the standard was approved and adopted
by ISO, the International Standards Organization. The first version, C90, was called ISO/IEC
9899:1990.Since then, many revisions to the language have taken place.
The second version of the standard, C99, was published in 1999 called ISO/IEC 9899:1999 and
introduced new language additional features. The third version, C11, was published in 2011. The
most recent version is the forth, C17, and is called ISO/IEC 9899:2018.
#include, is known as preprocessor directives. Any line starting with a # indicates to the
preprocessor that it must do something. #include <stdio.h> in the basicstructure program
above. #include literally tells the preprocessor to include, by copying and pasting, all the
code from that header file (which is an external library, stdio.h) in the place of that
statement in our own source code. Stdio.h is the standard library for input and output.
int main ()Every C program must have one main function, The curly braces ({}) are the
body which wraps all the code that should be in our program.
This line acts starting point for all C programs. It lets the computer know where to begin
reading the code when it executes our programs.
‘{‘ symbol indicates the start of the program
Statements There is at least one statement here it must appear between the opening and
closing braces. The program execution begins at the opening brace and ends at the
closing brace. The closing brace of the main function is the logical end of the program.
All statements in the within the braces end with a semicolon.
return 0;…………the main program must return a value of integer data type
‘}’ symbol the signify the termination of the program
The program which solves the entire problem is a collection of such structural blocks. Even a
bigger structural block like a function can have smaller inner structural blocks like decisions and
loops.
Basic terms in C
Compilers
Compiler is a program that translates or converts program written in high level language
into machine language equivalent all at once. A Compiler is used to compile an entire
program and an executable program is before translating in to machine code.
Interpreter
Interpreter is a program that translates or converts program written in high level language into
machine language equivalent line-by-line. It is a program, it takes one statement of a high level
language program, translates it into machine language instruction and then immediately executes
the resulting machine language instruction and goes on.
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:
Identifiers in C
Identifiers are used as the general terminology for the names of variables, functions and arrays.
These are user defined names consisting of arbitrarily long sequence of letters and digits with
either a letter or the underscore (_) as a first character.
In C Variables are defined as A data item that may take on more than one value during the
runtime of a program.In the simplest terms, you can think of variables as a named box. A box
that acts as a storage place and location for holding different information that can vary in
content.Each box has a unique name which acts like a label put on the outside that is a unique
identifier, and the information/content lives on the inside. The content is the variable's value.
Variable names must begin either with a letter or an underscore, for example age and
_age are valid.
A variable name can contain letters (uppercase or lowercase), numbers, or an underscore.
There can be no other special symbols besides an underscore.
Variable names are case sensitive, for example age is different from Age.
Local Variables
Local Variables are Variables that are declared inside a function or block are called local
variables. They can be used only by statements that are inside that function or block of
code. Local variables are not known to functions outside on their own.
Global variables
Global variables are defined outside a function, usually on top of the program. Global
variables hold their values throughout the lifetime of your program and they can be
accessed inside any of the functions defined for the program. That is, a global variable is
available for use throughout your entire program after its declaration.
-
Variable Definition in C
A variable definition tells the compiler where and how much storage to create for the
variable.
A variable definition specifies a data type and contains a list of one or more variables of
that type as follows –
type variable_list;
Here, type must be a valid C data type including char, int, float, double, bool, or any user-
defined object; and variable_list may consist of one or more identifier names separated
by
commas. Some valid declarations are shown here
int i, j, k;
char c, ch;
float f, salary;
double d;
The line int i, j, k; declares and defines the variables i, j, and k; which instruct the
compiler to create variables named i, j and k of type int.