0% found this document useful (0 votes)
47 views

CP Chapter 02 Begin With C Programming

The document discusses the history and features of the C programming language. It was developed in 1972 by Dennis Ritchie at Bell Labs to be used for the UNIX operating system. Some key features include it being a mid-level, structured programming language with a rich library and support for pointers, memory management, and speed. The document also covers the structure of a C program and the process of compiling and executing C programs.

Uploaded by

Jainam Gandhi
Copyright
© © All Rights Reserved
Available Formats
Download as PPSX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
47 views

CP Chapter 02 Begin With C Programming

The document discusses the history and features of the C programming language. It was developed in 1972 by Dennis Ritchie at Bell Labs to be used for the UNIX operating system. Some key features include it being a mid-level, structured programming language with a rich library and support for pointers, memory management, and speed. The document also covers the structure of a C program and the process of compiling and executing C programs.

Uploaded by

Jainam Gandhi
Copyright
© © All Rights Reserved
Available Formats
Download as PPSX, PDF, TXT or read online on Scribd
You are on page 1/ 52

Begin with C

programming

Prepared by:
Vishal Koshti
1
History of C

C programming language was


developed in 1972 by Dennis Ritchie
at bell laboratories of AT&T (American
Telephone & Telegraph), located in the
U.S.A.
Dennis Ritchie is known as the
founder of the C language.
It was developed to overcome the
problems of previous languages such
as B, BCPL, etc.
Initially, C language was developed to
be used in UNIX operating system.
It inherits many features of previous
languages such as B and BCPL.

2
Features of C

The main feature of C language is as follow.


1. Simple
2. Machine Independent or Portable
3. Mid-level programming language
4. Structured programming language
5. Rich Library
6. Memory Management
7. Fast Speed
8. Pointers
9. Recursion
10.Extensible
All the features are explained in detailed as follow.

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.

2. Machine Independent or Portable


Unlike assembly language, C programs can be executed on different machines with
some machine specific changes.
Therefore, C is a machine independent language. (But this is not possible always)

3. Mid-level programming language


Although, C is intended to do low-level programming.
It is used to develop system applications such as kernel, driver, etc.
It also supports the features of a high-level language.

4
Features of C

That is why it is known as mid-level language.

4. Structured programming language


C is a structured programming language in the sense that we can break the
program into parts using functions.
So, it is easy to understand and modify. Functions also provide code reusability.

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

Structure of C program is defined by set of rules called protocol, to be followed by


programmer while writing C program.
All C programs are having sections/parts which are mentioned below.

Documentation section

Link Section
Global declaration section

Function prototype declaration section

Main function

User defined function definition section


Figure: Structure of C program

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.

3. Global declaration section


In this section, variables are defined and values are set to these variables.
Global variables are defined in this section.
When a variable is to be used throughout the whole program, it can be defined in
this section.

4. Function prototype declaration section


Function prototype gives much information about a function like return type,
name of the function, parameter names along with their data types used inside
the function.

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.

6. User defined function definition section


User can define their own functions in this section which perform particular task as
per the user requirement.

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

Constants are user defined values which can never change.


Constant in C can be categorized as follow,

Constant

Non-numeric
Numeric constant
constant

Integer Real Character String

Figure: Categories of 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

Real constant can be written in scientific format also.


For example 254.31 is written in scientific format as 2.5431e2.
Following are some valid example or real constant,
9.892e+4
-8.5623e-1
3.14e3
Real constant can be used to represent number which is too big or too small.
For example 0.0000002531 is very small number and can written as 2.531e-7.

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.

C language also uses some special character known as "Escape sequence"


characters.

24
Constant

Escape sequence always starts from the backslash(\) character.


Following table shows the list of escape sequence characters which one can use
according requirement.

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

9. Invalid (It should not start with digit)


10. Valid
11. Valid
12. Invalid (Keyword cannot be used)
13. Valid
14. Invalid (Blank space is not allowed)
15. Valid

31
Data types

There are three data types provided by C.


Mainly there listed as follow.

Data types

Primitive Derived data User defined


data types types data types

void Array Structure


char Function Union
int Pointer Enum
float
double Figure: Types of data types

32
Data types

1. Primitive data types


Primitive data types are those data types which are provided by the language itself.
The purpose of each data type is different, the value which a data type can hold is
also different.
Each data type has size which defined the range of values a data type can hold.
Following table shows the size and range of each primitive data type of C.

Data type Size (in bytes) Range


void 0 N.A.
char 1 -128 to 127
int 2 -32768 to 32767
float 4 3.4e-38 to 3.4e+38
double 8 1.7e-308 to 1.7e+308

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.

C also provides four different data type modifiers which are,


A. signed

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

Data type Size (in bytes) Range


char (signed char) 1 -128 to +127
unsigned char 1 0 to 255
int (signed int) 2 -32768 to +32767
unsigned int 2 0 to 65535
short int (signed short int) 2 -32768 to +32767
unsigned short int 2 0 to 65535
long int (singed long int) 4 -2147483648 to + 2147483647
unsigned long int 4 0 to 4294967265
float 4 3.4e-38 to 3.4e+38
double 8 1.7e-308 to 1.7e+308
long double 10 3.4e-4932 to 3.4e+4932

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

We can assign the values to variables using "Assignment operator" (=).


Depending on the data types we can assign the respective values to the variable.
Following is the syntax to assign the value to the variable.
variableName = constant;
We can assign only one value to variable at a time.
For example,
num = 25;
average = 31.7;
We can assign some expression as well to the variable as follow.
sum = num1 + num2;
average = sum / 10;

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

Following is the example to declare the constant using "const" keyword,


const int passingMarks = 35;
While declaring constant using "const" keyword it is always recommended to
initialize the value to constant.
If we try to initialize later on then compiler will generate the error.

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

The ANSI standard C provides the volatile variables.


A volatile variable is a variable which can be modified at any time by external
sources (external sources means by some other program) which are outside the
program in which it is declared.
For example,
volatile int maximumMarks;
The above declaration declares an integer variable "maximumMarks" which can be
modified from outside the program.
It can also be modified by program itself.
Now consider following declaration,
volatile const int maximumMarks;
In above case variable "maximumMarks" can be modified by external source but
not by program itself as it is declared volatile and constant both.

51
Questions…?

AIM Computer Academy


: 7600 332 003, 7600 332 008
: vishalkoshti.aim@gmail.com
52
: www.aimacademy.co.in

You might also like