You are on page 1of 33

INTRODUCTION

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

features of code reusability and backtracking . 5


Pre processor Directive

 We can consider a preprocessor as a


compilation process, which runs when the
developer runs the program. It is a pre-
process of execution of a program using c
language. To initialize a process of
preprocessor commands, it's mandated to
define with a hash symbol (#). It
can preferably be the non-blank character,
and for better readability, a preprocessor
directive should start in the first column.
6
 List of Preprocessor Directives
 To execute a preprocessor program on a certain statement, some of the
preprocessor directives types are:
 #define: It substitutes a preprocessor using macro.
 #include: It helps to insert a certain header from another file.
 #undef: It undefines a certain preprocessor macro.
 #ifdef: It returns true if a certain macro is defined.
 #ifndef: It returns true if a certain macro is not defined.
 #if, #elif, #else, and #endif: It tests the program using a certain condition;
these directives can be nested too.
 #line: It handles the line numbers on the errors and warnings. It can be used
to change the line number and source files while generating output during
compile time.
 #error and #warning: It can be used for generating errors and warnings.
 #error can be performed to stop compilation.
 #warning is performed to continue compilation with messages in the
console window.
 #region and #endregion: To define the sections of the code to make them
more understandable and readable, we can use the region using expansion
and collapse features.
7
RESERVED KEY WORDS In C, we have 32 keywords, which
have their predefined meaning and cannot be used as a variable
name. These words are also known as “reserved words”. It is good
practice to avoid using these keywords as variable name. These are –

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

Keywords are pre-defined in the C Identifier is user defined and can be

language which cannot be changed. changed by the programmer.

Identifiers can be in lowercase or

Keywords in C are always in lowercase. uppercase or both as they are defined by

the programmer.

Keywords are easily identified by the C Identifier is declared and used by the

compiler as their functionalities is programmer and their functionality is also

already known to the compiler. defined by the programmer.

Identifier can contain underscore but they


Keywords cannot contain underscore. 12
cannot begin with underscore.
Variables in C
A variable represents a memory location that stores the data. For example:
an int variable num has a value 10 (int num = 10), here the variable
name is "num" that represents the location in the memory where this value 10 is
stored. As the name suggests, the value of a variable can be changed any number
of times.
Syntax
Rules – Declaring
for defining aavariable
variable
data_type variable name;
1. A variable name can has alphabets, digits and underscore.
2. Variable name cannot start with the digit, it can start with alphabets
and underscore only. For example, _num and num_1 are valid variable names,
however 9num & 99_num are not valid variable names.
3. Variable name cannot contain whitespaces. For example: num 1, num 2, my var are not
valid names as they contain whitespaces.
4. A variable name cannot be a keyword or reserved words. For example, “if”, “else”,
“int” are invalid names as these are the keywords.
Types of variable in C
1. Local variable
A variable that is declared inside a function or a block is called a local variable as the
scope of this variable is limited. A variable declared inside a function can only be
accessed inside that function, its scope is limited to that function and cannot be
accessed outside that function.
Similarly a variable declared inside a block cannot be accessed outside that block.
Note: Local variable must be initialized before it can be used. 13
Types of Data Types in C
1. Primary data type
2. Derived data type: Array, pointers, struct, and union are the derived data types
in C. We will discuss these in separate tutorials.
3. Enumerated data type: User defined data type, declared using enum keyword.
4. Void data type: Mostly used as a return type of function that does not return
anything.
5. Boolean data type: Represents variables that can store either true or false.
Represented by bool keyword.
Primary Data type in C
The following 5 data types in C are known as primary data types:
1. int: The int data type is used for integer values such as 5, 86, 99, 1002 etc.
2. char: The char data type is used for character values such as ‘A’, ‘p’, ‘u’, ‘M’ etc.
3. float: The float data type is used for decimal points values or real values such
as 5.5, 10.91 etc.
4. double: The large floating point values are stored in double variables.
5. void: This is generally used as a return type of a function that does not return
anything.

14
DATA TYPE MEMORY SIZE RANGE

int 2 bytes −32,768 to 32,767

signed int 2 bytes −32,768 to 32,767

unsigned int 2 bytes 0 to 65,535

short int 2 bytes −32,768 to 32,767

signed short int 2 bytes −32,768 to 32,767

unsigned short int 2 bytes 0 to 65,535

-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

unsigned long int 4 bytes 0 to 4,294,967,295

char 1 byte -128 to 127 15


Functions printf() and scanf() in C

The printf() and scanf() functions are the most


commonly used functions in C Programming. These
functions are widely used in majority of the C programs.

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);

The format string (or format specifier) is a string that is


used for formatting the input and output. Format string
always starts with a ‘%’ character. For example format
string for integer is %d, for string %s, for float %f and so
on.
scanf() function:
The scanf() function is used to read the input entered by
user.
Syntax of scanf() function:
scanf("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

 It is always legal in C programming to nest if-


else statements, which means you can use
one if or else if statement inside another if or
else if statement(s).
 If else statements are used for decision
making, by specifying which block of code is
to be executed when a certain condition is
met. Nested if else statements are just if else
statements inside other if else statements to
provide better decision making.
26
 Syntax:
if(Expression 1)
{
// Executes when the Expression 1 is true
if(Expression 2)
{
// Executes when the Expression 2 is true
}
else
{
// Executes when the Expression 2 is false
}
}

27
28
Which if/else Construct To Use?

 Printing whether a number is even or odd

 Printing whether a user is lower-class, middle-class, or upper-


class based on their income

 Determining whether a number is divisible by 2, 3, and/or 5

 Printing a user's grade of A, B, C, D, or F based on their


percentage in the course

29
Which if/else Construct To Use?

 Printing whether a number is even or odd


if / else
 Printing whether a user is lower-class, middle-class, or upper-
class based on their income
if / else if / else
 Determining whether a number is divisible by 2, 3, and/or 5
if / else if / else
 Printing a user's grade of A, B, C, D, or F based on their
percentage in the course
if / else if / else if / else if / else

30
SWITCH-CASE

 The switch statement allows us to execute


one code block among many alternatives.
 You can do the same thing with
the if...else..if ladder. However, the syntax of
the switch statement is much easier to read
and write.

31
 switch (expression)
 ​{
 case constant1:
 // statements
 break;

 case constant2:
 // statements
 break;
 .
 .
 .
 default:
 // default statements
 }

32
33

You might also like