Title: History:
1History
C was written by Bjarne
Stroustrup at Bell Labs during 1983-1985. C is
an extension of C. Prior to 1983, Bjarne
Stroustrup added features to C and formed what he
called "C with Classes". He had combined the
Simula 's use of classes and object-oriented
features with the power and efficiency of C. The
term C was first used in 1983. C
was designed for the UNIX system environment.
With C, programmers could improve the quality
of code they produced, and reusable code was
easier to write. Bjarne Stroustrup had
studied in the doctoral program at the Computing
Laboratory at Cambridge University prior to
joining Bell Labs. Now, Bell Labs no longer has
that name since part of Bell Labs became ATT
Labs.. The other half became Lucent Bell labs.
2The C Language
Prior to C, C was a programming language
developed at Bell Labs during 1969-1973. C was
originally developed for and implemented on the
UNIX operating system, by Dennis Ritchie, who
extended the B language by adding types in 1971.
Ritchie restructured the language and rewrote the
compiler and gave his new language the name "C"
in 1972. 90 of UNIX was then written in C. In
1989, ANSI completed the C Standard project.
There were quite a number of versions of C at
that time and a new Standard was necessary. C
is portable, not tied to any particular hardware
or operating system. C combines the elements of
high-level languages with the functionality of
assembly language and has occasionally been
referred to as a middle-level computer language.
C makes it easy to adapt software from one type
of computer to another. C was a direct
descendant of the language B. The language B was
developed by Ken Thompson in 1970 for the new
UNIX OS. B was a descendant of the language BCPL
designed by Martin Richard, a Cambridge
University student visiting MIT.1
3What is C
C is an "object oriented" programming
language that implements "data abstraction" using
a concept called "classes", along with other
features to allow object-oriented programming.
Parts of the C program are easily reusable and
extensible existing code is easily modifiable
without actually having to change the code. C
adds a concept called "operator overloading" not
seen in the earlier OOP languages and it makes
the creation of libraries much cleaner. C
maintains aspects of the C programming language,
yet has features which simplify memory
management. Additionally, some of the features of
C allow low-level access to memory but also
contain high level features. C could be
considered a superset of C. C programs will run
in C compilers. C uses structured programming
concepts and techniques while C uses object
oriented programming and classes which focus on
data.
4C Vs JAVA
5Starting with C
include ltstdio.hgt / my first C program / int
main() printf("Hello World\n") return 0
Included libraries
Main Function (type of return)
Output
Return result
6The main function main()
int main()
- This function is crucial to every C/C program.
It must be defined once inside a program to make
it executable. - The keyword main() defines the function that
specifies the beginning and end of a program and
the various operations in between. - A C program stops when it reaches the keyword
return, or when it crashes due to run-time
errors. - In the example, main() returns a value of type
integer.
7Variables and Functions Declaration
- Never begins with an integer
- Never begins with the star symbol
- Never begins with an arithmetic symbol
- Never begins with a dot
- Never includes a hyphen
- Never includes an apostrophe
8Variable Declaration
- int a, b 2
- char x, y 'a'
- unsigned long int i1 0, i2 3
- double pi 3.14 double d 5pi
PAY ATTENTION
9Types
- char an octet (character)
- int signed integer
- float floating point number with a precision of
6 digits after the dot. - double floating point number with a precision of
10 digits after the dot.
- short, long
- signed, unsigned
Type Modifiers
- Examples char (signed char), unsigned char,
short int (signed short int), unsigned short int,
int (signed int), unsigned int, long int (signed
long int), unsigned long int, float, double, long
double
10Integer Data Types (int)
- int
- (by default signed short)
- short 2 octets (16 bits)
- signed -32768 to 32767
- unsigned 0 to 65535
- long 4 octets (32 bit)
- signed -2147483648 to 2147483647
- unsigned 0 to 4294967295
Octal and Hexadecimal Representation
- 12 014 0xC
- 2568 05010 0xa08
Generally, in all computers sizeof(short) lt
sizeof(int) lt sizeof(long) In some computer
architecturesshort 2 octets, int 2 octets,
long 4 octets And in others short 2 octets,
int 4 octets, long 4 octets
11Character Data Types (char)
- '\a' keyboard bell
- '\\' back slash
- '\b' back space
- '\?' question mark
- '\f' form feed
- '\n' new line
- '\"' double quote
- '\r' carriage return
- '\t' horizontal tab
- '\v' vertical tab
- '\ooo' octal byte
- '\xhh' hex byte
- '\0' null byte
- char
- (by default signed)
- 1 octet
- signed -128 to 127
- unsigned 0 to 255
Escape Sequence
12Character String Data Types (char)
No data type represents strings in C. However, a
string can be represented as a one dimensional
array of characters. Many auxiliary functions
that manipulate strings (declaration and
operations) are available from various sources.
char ltVariable Namegt ltLengthgt Examples char
Name 20 char Family 20 char Desc 300
- Escape Sequences in strings
- "a\tb" --gt a b
- "abcd\b\bx" --gt abx
- "\"hello world\"" --gt "hello world"
- "I don\'t know" --gt I don't know
- "hello\nworld" --gt hello
- world
- "\a" --gt (rings the keyboard bell)
- A character string must be terminated by the
symbol '\0' (Null character). - Hence, for a text of n characters, we need n1
octets.
13Control Statements
Very Similar to Java
14Decision Statement
- if (...) stmt
- if (...) stmt else stmt
- if (...) body else body
if (1) ... true if (2) ... true if
(-1.5) ... true if (0) ... false int x 2,
y if (x lt 3) y 5 else y 4
Conditional Expression cond ? e1 e2
----------------------------------- double x
((n 2 0) ? 4.3 -2.3)
15Multi Way Decision()
int x 0, y 0 switch(n) case 1 case
2 x 2 case 3 y 3 break case
4 x 1 y 4 case 5 x 2
break default y -1
n x y 1 2 3 2 2 3 3 0 3 4 2 4 5 2
0
16Loops
Three types of loops exist in C
for(e1e2e3) ... int f1, i for(i2 iltn
i) f f i for() ...
while(cond) ... int f1, i2 while(i lt n) f
f i
do body while(cond) int f1, i 1 do f
f i while(i lt n)
17Loops()
Goto !!!Must Be Avoided!!! ... while( ... )
... if (wrong) goto error ... for( ...
) ... if (wrong) goto error ... ...
error ...
break continue int i, r 0 for(i0 iltn
i) if (i 2 0) continue r i
int choice 0 while(1) choice
user_input() if (choice lt 0 choice gt 4)
break switch(choice) case 1 ...
Inside a loop, one might use the break statement
to explicitly exit the loop, or the continue
statement to skip all the lines of code coming
after, until the end of the loop. Note that with
continue the loop doesnt halt, but goes to
another iteration after skipping part of its
body. Inside a function or loop, C allows the
usage of a goto statement to achieve unstructured
jump from one part of the program to another. It
is not recommended at all due to the mess it adds
to the programs structure and redability. It
makes the program look like a spagetti dish.
18Functions
- Definition
- To define a function, we specify
- The function name
- The name and type of all the functions
parameters - The type of the return value
- The local data types used inside the functions
scope - The instructions to be executed
include ltstdio.hgt / Prototypes of the
functions to be called / / or functions
declaration / int INPUT(void) int MAX(int N1,
int N2) main() / Variables Declaration /
int A, B / local variables/ A INPUT()
B INPUT() printf(The maximum is d\n",
MAX(A,B)) / Definition of the function
INPUT / int INPUT(void) int NUMBER
printf("Enter an integer number ")
scanf("d", NUMBER) return NUMBER /
Definition of the function MAX / int MAX(int
N1, int N2) if (N1gtN2) return N1 else
return N2
- Declaration
- To use a function inside a program, it should be
declared by specifying its header at the
beginning, ended with a semi-colon. Note that in
the declaration header, the list of parameters
can contain only the types, without the names, of
the parameters. - Here are some examples
- int f1(int)
- int f2()
- void f3(int)
- char f4(int, double)
- void f5(char, ...)
- double power(double, int)
- double power(double base, int exponent)
The name of a function appears in the following
locations inside a program 1) During the
function declaration 2) During the function
definition 3) During the function call
19Sources
http//www.tcom.ch/Tcom/Cours/C/C2.pdf http//w
ww.research.att.com/bs/bs_faq.html http//www.re
search.att.com/bs/bs_faq2.html