Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
14 views

2_Introduction to C-Programming

Uploaded by

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

2_Introduction to C-Programming

Uploaded by

k16861978
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 10

Subject: Introduction to Algorithm

INTRODUCTION TO C PROGRAMMING LANGUAGE


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.
Features of C Language

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.

Deepa R.YPage 1
Subject: Introduction to Algorithm

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.

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. 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.

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.

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.

10) Extensible language is extensible because it can easily adopt new


features.

Deepa R.YPage 2
Subject: Introduction to Algorithm

Character Set
A character set in C Programming Language is set all valid characters that can be
used to form words, numbers and expression’s in source programs

C- Tokens
The token is the smallest individual element in C. Tokens in C is the
most important element to be used in creating a program in C. In the C
language, the following 6 types of tokens are available:

1. Identifiers
2. Keywords
3. Constants
4. Operators
5. Special Characters
6. Strings

Keywords in C
A keyword is a reserved word. You cannot use it as a variable name, constant
name, etc. There are only 32 reserved words (keywords) in the C language.

Here are some useful points to remember about Keywords.

 A keyword cannot be used as an Identifier.


 Keywords must be written in lower case. Apart from some based
keywords, all other keywords are in lowercase.
 Keywords hold special meaning for the C compiler, so respect that,
otherwise you will get a compile- time error.

Deepa R.YPage 3
Subject: Introduction to Algorithm

A list of 32 keywords in the c language is given below:

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

Identifiers
An identifier is used for any variable, function, data definition, labels in
your program etc.
Rules for naming identifiers

The rules that must be followed while naming the identifiers are as follows −

 The case of alphabetic characters is significant. For example, using


"TUTORIAL" for a variable is not the same as using "tutorial" and
neither of them is the same as using "TutoRial for a variable. All three
refer to different variables.

 There is no rule on how long an identifier can be. We can run into
problems in some compilers, if an identifier is longer than 31 characters.
This is different for the different compilers.

 A valid identifier can have letters (both uppercase and lowercase letters),
digits and underscores.

 The first letter of an identifier should be either a letter or an underscore.

 You cannot use keywords like int, while etc. as identifiers.

 Identifiers must be unique

Deepa R.YPage 4
Subject: Introduction to Algorithm

Variable declaration

All the variables that we wish to use in the program need to be declared.
Meaningful names should be given to the variables. Variables can be declared
anywhere in the program according to their needs.

 A meaningful name given to a data storage location in the computer


memory is known as a Variable.
 When a variable is used it actually refers to a address of the memory where
the data is stored.
 A variable name can comprise of letters, digits and underscore characters.
 The first character has to be an alphabet.
 There should be no commas or blank spaces between the variable names.

Syntax:
data-type variable-name;
In the above syntax a data-type is declared along with a variable name.

Example: A simple variable declaration.


int emp_id;
float salary;

Multiple variables can also be declared in one statement but the data-type
has to be the same.

Example: Multiple variable declaration


float salary, bonus;

Generally variables are declared in three places as follows:


1. Local variable is declared inside the function.
2. Formal parameters are declared in the definition of the function
parameters.
3. Global variables are declared outside all the functions

Deepa R.YPage 5
Subject: Introduction to Algorithm

Constants
The identifiers whose value do not/cannot change are known as Constants.

Variables can change their values but constants will never change their
value.

Types of constants
There are three types of constants:

1. Integer constants
An integer quantity which contains a sequence of digits is known as
an Integer constant. There are no decimal points.

 They can be either positive or negative.


 Blanks and commas are not allowed.
 Its value must lie within the range of its values.

2. Floating point constants


these types of constants contain a decimal point or an exponent. They can be
either negative or positive. Commas or blanks are not allowed within a real
constant.
Example: Floating point constants
+3.2f-4 -0.3e-3 325.0 -33.75

3. Character constant.
They consist of a single character which is enclosed in single quotes.
Example: Character constant
'b' '@' '5'
The characters are stored by using the machines character set using the
ASCII code.

Constants declaration

Deepa R.YPage 6
Subject: Introduction to Algorithm

A constant can be declared by using the const keyword and assigning it a


value.

Example: Simple constant


const float pi=3.14;

The const keyword tells us that the value of pi can never be changed.

Other way of declaring the constant is by using the pre-processor command


define. #define can be placed anywhere in the program

Example: declaring a constant by using a pre-processor


#define pi 3.14159

Symbolic constant

A symbolic constant is a name given to some numeric constant, or a


character constant or string constant, or any other constants.

Symbolic constant names are also known as constant identifiers.


Pre-processor directive #define is used for defining symbolic
constants.

The syntax of Symbolic Constants in C

#define name text

Where name implies symbolic name in caps.

text implies value or the text.

For example,
#define printf print
#define MAX 50
#define TRUE 1
#define FALSE 0

Deepa R.YPage 7
Subject: Introduction to Algorithm

#define SIZE 15

Operators:
Operators are symbols that provide instructions for the
performance of various mathematical and logical operations. Operators are
tools that can alter data and variable values.

Operators are classified into the following eight types:

1. Arithmetic Operators
2. Relational Operators
3. Logical Operators
4. Increment/Decrement Operators
5. Assignment Operator
6. Bitwise Operators
7. Conditional Operators
8. Special Operators

Basic Structure of C Program

Deepa R.YPage 8
Subject: Introduction to Algorithm

Any C program is consists of 6 main sections

A C program is divided into different sections. There are six


main sections to a basic c program.
The six sections are,

 Documentation
 Link
 Definition
 Global Declarations
 Main functions
 Subprograms

Documentation Section
The documentation section is the part of the program where the
programmer gives the details associated with the program. He usually gives the

Deepa R.YPage 9
Subject: Introduction to Algorithm

name of the program, the details of the author and other details like the time of
coding and description. It gives anyone reading the code the overview of the
code.

Link Section
This part of the code is used to declare all the header files that will be used in
the program. This leads to the compiler being told to link the header files to the
system libraries.

Definition Section
In this section, we define different constants. The keyword define is used in this
part.

#define PI=3.14

Global Declaration Section


This part of the code is the part where the global variables are declared. All the
global variable used are declared in this part. The user-defined functions are
also declared in this part of the code.

1 float area(float r);

2 int a=7;

Main Function Section


Every C-programs needs to have the main function. Each main function
contains 2 parts. A declaration part and an Execution part. The declaration part
is the part where all the variables are declared. The execution part begins with
the curly brackets and ends with the curly close bracket. Both the declaration
and execution part are inside the curly braces.

Sub Program Section


All the user-defined functions are defined in this section of the program.

Deepa R.YPage 10

You might also like