Professional Documents
Culture Documents
TO
C LANGUAGE
1
C is a general purpose computer programming language. A general
purpose language is a language that is widely used in various domains and
not specific to a particular domain. C programming language was created
in 1972 by Dennis Ritchie at AT&T bell laboratories in U.S.A.
Founder:
Dennis Ritchie is known as the founder of C programming language.
Early developments:
Ken Thompson wanted to develop a Fortran compiler but later gave up on
that idea and shifted his focus to develop an advanced version of BCPL
system programming language. He made modifications in the syntax and
made it much simple, he named this language as ‘B programming language’.
In 1971, Dennis Ritchie started to improve the features of B language, by
1972 he was able to write a new modified compiler which he renamed to
‘C’. C language is a successor of ‘B programming language’. The B
programming language didn’t gain as much popularity as ‘C programming
language did’. C language developed to overcome the issues and
shortcomings of the previous programming languages such as B, BCPL etc.
Popularity:
Since 2000, C programming language consistently ranked among top two
languages in TIOBE index, an index that measure the popularity of a
programming language.
2
Programming language developed before C:
ALGOL: ALGOL (short form of Algorithmic language) is a family of
imperative computer programming languages originally developed in
1958.
BCPL: BCPL is a procedural, imperative, and structured
programming language. It was originally created for writing compilers
for other programming languages. BCPL is no longer in common
use. It was developed by Martin Richards.
B: It was developed by Ken Thompson in 1970. This is known as the
predecessor of ‘C’ programming language.
Traditional C: It was developed by Dennis Ritchie in 1972.
K&R C: It was developed by Kernighan & Dennis Ritchie in 1978.
ANSI C: It was developed by ANSI Committee in 1989. It is also
referred as ‘C89’ and ‘C90’.
ANSI/ISO C: Developed in 1990 by ISO Committee.
C99: Developed in 1990 by Standardization Committee. It is a
revised version of C.
3
Features of C language
1. Simple
C language is simple and easy to learn. The syntax of C is simple
and gives flexibility to the programmer with its wide variety of in-built
functions and data types.
2. Portable
C is a machine independent language, which means a C program
written one machine can run on another machine without requiring a
code change.
3. Fast
C is a compiler based language. C is power packed with several
features but it is not bloated like some other programming
languages, it supports only useful features which makes
the compilation of C file fast.
4. Extensible
C program supports code modifications and addition of new code to
the already existing programs, this makes C language extensible. It
becomes easier to add new functionalities to the existing C
programs.
4
5. Rich libraries:
C libraries are full of useful in-built functions. These functions can be used frequently
to perform some repeated tasks. As a C programmer, you won’t need to write same
code again and again for some trivial repeated tasks, rather you can use in-built
functions to perform the task.
6. Dynamic memory allocation
Dynamic memory allocation means, optimizing and allocating the memory during
runtime. C language supports dynamic memory allocation, which supports function
like free() to free the un-utilized memory space and other memory management
functions such as malloc(), calloc() etc.
7. Reusability
Function is a block of code that is used to perform a specific task. Along with the in-
built predefined functions, C language allows you to create you own functions that
you can create for a reoccurring tasks. This improves the reusability of the code and
avoids the unnecessary writing of the same code again and again.
8. Mid level programming language
C language provides the benefits of high level and low-level languages both. C allows
manipulation of hardware just like low level language and allows high user end
functionalities like high-level languages.
9. Supports pointers
C language supports pointers. The pointer in C allows you to directly interact with the
memory.
10. Recursion
C supports recursion. Recursion means a function calls itself. Recursion provides the
8
Basics usage of these keywords –
if, else, switch, case, default – Used for
decision control programming structure.
break – Used with any loop OR switch case.
int, float, char, double, long – These are the
data types and used during variable
declaration.
for, while, do – types of loop structures in C.
void – One of the return type.
goto – Used for redirecting the flow of
execution.
9
auto, signed, const, extern, register, unsigned –
defines a variable.
return – This keyword is used for returning a value.
continue – It is generally used with for, while and
dowhile loops, when compiler encounters this statement
it performs the next iteration of the loop, skipping rest of
the statements of current iteration.
enum – Set of constants.
sizeof – It is used to know the size.
struct, typedef – Both of these keywords used in
structures (Grouping of data types in a single record).
union – It is a collection of variables, which shares the
same memory location and memory storage.
volatile
10
C Identifiers
As the name suggests an identifier in C is a unique name that is used to identify a
variable, array, function, structure etc. For example: in int num =10; declaration, name
“num” is an identifier for this int type variable.
Identifier must be unique so that it can identify an entity during the execution of the
program.
Rules for C identifiers
As the identifier is chosen by the programmer, there are certain rules in place to
remind programmer and to prevent execution of the program until these rules are
fulfilled.
1. An identifier can contain lowercase letter, uppercase letters, alphabets, underscore
and/or digits. For example, num_2, num8, bigNum etc. are valid identifiers.
2. An identifier cannot start with a digit. For example 99num and 9number are
invalid identifiers.
3. An identifier cannot start with an underscore. For example _num, _char are
invalid identifiers.
4. An identifier doesn’t allow commas or whitespaces. For example “num 2” and
“num, 2” are not allowed in C.
5. The length of an identifier should not exceed 31 characters.
6. The keywords and reserved words cannot be used as an identifiers. For
example declaring an variable name with “int” or “for” is not allowed in C as these are
the keywords in C.
11
Difference between keyword and identifier
KEYWORD IDENTIFIER
the programmer.
Keywords are easily identified by the C Identifier is declared and used by the
14
DATA TYPE MEMORY SIZE RANGE
-2,147,483,648 to
long int 4 bytes
2,147,483,647
-2,147,483,648 to
signed long int 4 bytes
2,147,483,647
The scanf() and printf() functions are used for input and
output in c programs receptively. These functions are
defined in stdio.h header file, so you must include this
header file in your program, if you are using any of
these functions.
16
printf() function:
The prinf() function is used to display (or print) output on
the console screen.
Syntax of printf() function:
printf("format string",arguments);
17
The if Statement
if statement: a control structure that executes a block
of statements only if a certain condition is true
General syntax:
if (<test>) {
<statement(s)> ;
}
18
if Statement Flow Chart
19
The if/else Statement
if/else statement: A control structure that executes one block of
statements if a certain condition is true, and a second block of
statements if it is false. We refer to each block as a branch.
General syntax:
if (<test>) {
<statement(s)> ;
} else {
<statement(s)> ;
}
20
if/else Statement Flow Chart
21
if/else/if Statements
if/else statement: A chain of if/else that can select between
many different outcomes based on several tests.
General syntax:
if (<test>) {
<statement(s)> ;
} else if (<test>) {
<statement(s)> ;
} else {
<statement(s)> ;
}
22
if/else/if Flow Chart
if (<test>) {
<statement(s)>;
} else if (<test>) {
<statement(s)>;
} else {
<statement(s)>;
}
23
if/else/ if Flow Chart
if (<test>) {
<statement(s)>;
} else if (<test>) {
<statement(s)>;
} else if (<test>) {
<statement(s)>;
}
24
25
NESTED -IF
27
28
Which if/else Construct To Use?
29
Which if/else Construct To Use?
30
SWITCH-CASE
31
switch (expression)
{
case constant1:
// statements
break;
case constant2:
// statements
break;
.
.
.
default:
// default statements
}
32
33