CP Chapter 02 Begin With C Programming
CP Chapter 02 Begin With C Programming
programming
Prepared by:
Vishal Koshti
1
History of C
2
Features of C
3
Features of C
1. Simple
C is a simple language in the sense that it provides a structured approach (to break
the problem into parts), the rich set of library functions, data types, etc.
4
Features of C
5. Rich Library
C provides a lot of inbuilt functions that make the development fast and easy.
6. Memory Management
It supports the feature of dynamic memory allocation.
In C language, we can free the allocated memory at any time by calling the free()
function.
5
Features of C
7. Speed
The compilation and execution time of C language is fast since there are lesser
inbuilt functions and hence the lesser overhead.
8. Pointer
C provides the feature of pointers.
We can directly interact with the memory by using the pointers.
We can use pointers for memory, structures, functions, array, etc.
9. Recursion
In C, we can call the function within the function.
It provides code reusability for every function.
Recursion enables us to use the approach of backtracking.
6
Features of C
10. Extensible
C language is extensible because it can easily adopt new features.
7
Structure of C program
Documentation section
Link Section
Global declaration section
Main function
8
Structure of C program
1. Documentation section
We can give comments about the program, creation or modified date, author
name etc. in this section.
The characters or words or anything which are given between “/*” and “*/”, will
not be considered by C compiler for compilation process.
These will be ignored by C compiler during compilation.
For example,
/*
Comment line1
Comment line2
Comment line3
*/
9
Structure of C program
2. Link Section
Header files that are required to execute a C program are included in this section.
10
Structure of C program
5. Main function
Every C program is started from main function and this function contains two
major sections called declaration section and executable section.
11
Compiling and executing
C programs
C is compiled language.
So once the C program is written, you must run it through a C compiler that can
create an executable file to be run by the computer.
While the C program is human-readable, the executable file on the other hand, is a
machine-readable file available in an executable form.
The mechanical part of running a C program begins with one more program source
files, and ends with an executable file, which can be run on a computer.
The programming process starts with creating a source file that consists of the
statements of the program written C language.
This source file usually contains ASCII characters and can be produced with a text
editor, such as Windows notepad, or in an Integrated Design Environment.
The source file is then processed by a special program called compiler.
Every programming language has its own compiler.
The compiler translates the source code into an object code.
12
Compiling and executing
C programs
The object code contains the machine instructions for the CPU, and calls to the
operating system API (Application Programming Interface).
However, even the object file is not an executable file.
Therefore, in the next step, the object file is processed with another special
program called linker.
While there is a linker is used for object files regardless of the original language in
which the new program is written.
The output of the linker is an executable or Runnable file. The process is
mentioned below.
Executable
Source file Compiler Object file Linker
file
Figure: Sequence of compilation and execution of C program
13
C character set
Character set means the set of characters which are used while we are writing
program.
As C is programming language, it has some set of character which it is used to build
program.
C character set can be categorized as follow,
1. Letters or alphabets
2. Digits
3. Special characters
4. White space
Letters include uppercase (A to Z) and lowercase (a to z).
Digits include 0 to 9.
Special characters includes +, - , * , <, > *, ! etc. for different purposes.
The white space are used to separate the word or token, they might be blank
space, tab or enter.
14
C character set
Token
The character together makes special symbol or word known as token.
The examples of token are the words used to define data types like int, float etc.
15
Keywords
The keywords are the special words which are having specific meaning.
(Predefined meaning)
No one can change the meaning of these words, and they are also known as
reserved words.
C language has total 32 keywords which are listed below.
auto break case char const continue
default do double else enum extern
float for goto if int long
register return short signed sizeof static
struct switch typedef union unsigned void
volatile while
16
Identifiers
The identifiers are user defined names used in programs for providing names to
variables, arrays, functions, pointers, structure etc.
Identifiers can be made up of letters(uppercase and lowercase), digits and
underscore.
As C is case sensitive uppercase and lowercase are treated as different, for
example total is not same as Total and TOTAL.
17
Constant
Constant
Non-numeric
Numeric constant
constant
18
Constant
1. Integer constant
Integer constants represents the whole numbers. (which does not any fraction
part)
It uses 0 to 9 and optional + or - sign before the number to denote whether the
number is positive or negative.
Following are some valid example of integer constant,
123
-25
65535
+31
The space, comma and other special symbols are not allowed.
Following are some invalid example of integer constant,
25,317
$10000
19
Constant
13 128
Rs. 100
C also allows to define integer constant in octal and hexa decimal form.
The octal numbers are written with preceding 0 (zero) and hexa decimal numbers
are preceded by 0x or 0X.
Following are some valid example of octal constant,
0254
001
07526
Following are some valid example of hexa decimal constant,
0x32ab
0Xface
0x0012
20
Constant
For unsigned integer constant, numbers are followed by "u" or "U" and for long
integer constants, numbers are followed by "l" or "L".
Following are some example of unsigned integer, long integer and unsigned long
integer,
123u or 123U (Unsigned integer)
987654l or 987654L (Long integer)
987654321ul or 987654321UL (Unsigned long integer)
21
Constant
2. Real constant
Real constants represent fractional parts. (Fractional parts are those parts which
are coming after point).
Real constants can be used to store the real life value like temperature,
percentage, height etc.
Most of the time real constants are used while making some scientific or
mathematical application.
Following are some valid example of real constant,
25.31
-89.523
+248.00
.13
22
Constant
23
Constant
3. Character constant
Character constants are represent single character and are always enclosed in
single quotes.
Following are some valid example of character constant,
'A’
'a’
'?’
':'
Characters are stored in computer memory in form of their ASCII values.
For example ASCII value of 'A' is 65 and ASCII value of '8' is 56.
24
Constant
25
Constant
Constant Meaning
‘\a’ Bell
‘\b’ Backspace
‘\f’ Form feed
‘\n’ New line
‘\r’ Carriage return
‘\t’ Horizontal tab
‘\v’ Vertical tab
‘\’’ Single quotes
‘\”’ Double quotes
‘\?’ Question mark
‘\\’ Backslash
‘\0’ Null
26
Constant
4. String constant
String is sequence of character constants and enclosed in double quotes.
String are used to represent names, address, city etc.
Following are some example of string constants,
"AIM"
"2504"
"Angel"
"AIM Computer Academy"
27
Variable
Variable represent the quantities which changes from one place to another place
in program.
Each variable identified in a program is called variable name.
Variables are used to store values which changes in program according to
requirement.
There are some rules to define variable name.
1. Allowed characters are letters (A to Z, a to z) and digits (0 to 9) and special
character underscore (_).
2. Variable names always begins with letter or underscore.
3. Keywords cannot be used.
4. White space characters are not allowed.
5. Variable names are case sensitive, means Total is not same as TOTAL and total.
6. Only first 32 characters are significant. (But nowadays compilers supports any
length of variable name)
28
Variable
Identify which are valid and which are invalid example of variable,
1. aim
2. maximum number
3. int
4. integer
5. temparature
6. void
7. temp123
8. temp@123
9. 123temp
10. maximumNumber
11. _counter
12. float
29
Variable
13. AimComputerAcademy
14. average Number
15. HoWaReYoU
Answers
1. Valid
2. Invalid (Blank space is not allowed)
3. Invalid (Keyword cannot be used)
4. Valid
5. Valid
6. Invalid (Keyword cannot be used)
7. Valid
8. Invalid (Special symbol “@” cannot be used)
30
Variable
31
Data types
Data types
32
Data types
33
Data types
"void" data type can be used with function which can defined nothing, it means
when we do not want to return anything from the function and we do not want to
pass anything to the function as parameter we can use "void" data type.
"char" data type is used to store only one character, it cannot be use to store more
than one character, while storing or assigning value to the character data type, we
should use single quotes.
"int" data type is used to store the whole number (a number without having any
decimal place)
"float" and "double" data types are used to store decimal number, float can be
used when we need precision up to 4 to 6 digits after decimal place and double
can be used when we need precision up to 14 to 16 digits after decimal place.
34
Data types
B. unsigned
C. short
D. long
These data type modifiers are used to either to alter the range of data types or to
increase the size of data types.
By default all the data types are signed data types which means variable can store
positive values and negative values as well.
But all these modifiers are not applicable with all the data types, so following is the
table which shows which data type modifiers is applicable with which data type.
We cannot use signed and unsigned both together while applying modifiers on any
data types.
We cannot use short and long both together while applying modifiers on any data
types.
35
Data types
36
Data types
So from the above table we can say that programmer has wide range to select the
data type.
Now depending on our requirement we can select appropriate data types.
If we want to store whole number which might be positive or negative then we
should go with signed data type of int and if we have requirement that we want to
store only positive numbers then we should go for unsigned data types.
If we want to store decimal numbers then we should go with either float or
double.
37
Variable declaration
The variables are used to store the data values, and depending on the data type
variable can store the value.
Before storing the value the variable must be declared else we will get error.
Following is the syntax to declare the variable in C language,
datatype variable1,variable2,variablen;
In the above syntax "datatype" would be any valid data type of C language.
"variable1, variable2, variablen" are name of variables, and if we want to declare
more than one variable in single statement then it should be separated by comma
",".
Statement should be end with semicolon ";".
Following are valid variable declaration,
int num1;
float sum, average;
double temperature;
38
Variable declaration
We can declare more than one variable of same type separated by comma.
If we want to declare different kind of variable then one statement must be
terminated by semicolon, then second type of declaration will start.
39
Assigning values to variables
40
Symbolic constant
Constant are those variable whose values cannot be changed during program
execution.
While doing programs we may have situations where we do not allow programmer
to change its value, in such case we should declare the variable as constant.
To declare constant variable we have two different approaches and the first one is
by using "#define" pre-processor directive.
The variable which is defined using "#define" directive are known as "Symbolic
constant".
The variable which is defined using "#define" is accessible in the whole program
(means inside all the function).
The syntax to declare symbolic constant as follow,
#define SYMBOL_NAME constantValue
After defining the SYMBOL_NAME we can access that variable anywhere in the
program but we cannot change its value.
41
Symbolic constant
For example,
#define PI 3.14
Now we can use “PI” anywhere in our program and compiler will put “3.14”
wherever we used “PI”.
The main advantage of "Symbolic constant" is, if we want to change the value of
symbolic constant we have to change it at one place and it will be applicable
everywhere automatically.
The compiler automatically uses new value at each place.
Normally "Symbolic constant" are defined in uppercase which simply differentiate
them from normal variables of program.
We do not have to declare the data type for symbolic constant, following are some
example of symbolic constant.
For example,
#define MAX 25
42
Symbolic constant
#define ERROR -1
#define TEMP 25.13
43
Constant with const keywords
We can also declare constant with the help of "const" keyword, it is another way to
define the constant.
Just like symbolic constant we cannot change the value of variable defined using
"const" keyword and if we try to change then compiler will generate the error.
While defining the variable using "const" keyword, we have to follow following
syntax,
const dataType constantName = value;
In the above syntax,
"const" is a keyword.
"dataType" will be the data type of the constant like int or float or char etc.
"constantName" is the name of our constant variable.
"value" is the value which we want to assign to variable.
If we do not declare data type then by default C take that as int data type.
44
Constant with const keywords
45
Enumerated data types
The enumerated data type is used to define more than one integer symbolic
constants.
For example,
enum status
{
single, married
};
In the above example "enum" is a keyword, "status" is data type and "single,
married" are possible values.
The value of "single" is 0 and "married" is 1.
Once we declare enumerated data type, now we can create variable of type enum
just like normal other data types as follow,
enum status vishal;
46
Enumerated data types
where "vishal" is a variable of type status and can have value either "single" or
"married", following assignment is valid.
vishal = married;
47
Difference between variable
and constants
Variables Constants
It is a variable that stores data type value It is similar to a variable and cannot be
in a program. changed during program execution.
It is a variable that can be changed after It is a fixed variable that cannot be
defining the variable in a program. changed after defining the variable in a
program.
The value of a variable can change In constants, the value cannot be
depending on the conditions. changed.
Typically, it uses int, float, char, string, It can be express in two ways: #define
double, etc. data types in a program. pre-processor and the const keyword.
For example, For example,
int a = 5; float radius = 5.2; char ch='A'; const int Len = 5;
#define PI 3.14
48
Difference between #define and
const to declare constant
#define const
The constant which can be declared with The constant which can be declare with
the help of “#define” is known as the help of “const” keyword is known as
“Symbolic constant”. constant variable.
#define is known as pre-processor const is known as keyword.
directive.
The constant which is declared with the The constant which is declare with the
help of #define will allocate the space and help of const, will allocate space and
value at the time of compilation only. value when the respective line encounter.
For example, For example,
#define PI 3.14 const int passingMarks = 35;
We do not need to define the data type We must define the data type while
while declaring constant. declaring constant, if we do not, then by
default it will take as int data type.
49
Difference between #define and
const to declare constant
#define const
There is no need of assignment operator. We must use assignment operator in
order to initialize value.
It is not terminated by semi colon. It should terminated by semi colon.
It is recommended to declare symbolic There is no such recommendence.
constant in upper case, to differentiate
then normal variable.
50
volatile variables
51
Questions…?