Unit-I C Language Fybsc It
Unit-I C Language Fybsc It
UNIT-1
Remember your last visit to the Super Market. You might have purchased many
items.
What did you do after picking all the items and putting them into the carriage?
Probably you met with the billing clerk to make the payment. Have you observed
his way of preparing the bill?
He picks the items one by one and enters something into the computer repeating
the same task for all the items.
Within a count of few minutes or even may be within few seconds he gives the
bill, you pay and come out carrying all the required things. So, what made him to
process the bill so fast? It is nothing but the “program” running in the
computer’s memory.
Program:
A computer program is a collection of instructions that can be executed by
a computer to perform a specific task.
Computer program is usually written by a computer programmer in
a programming language.
Programmer:
One who writes the programs/instructions for the computer is known as
Programmer.
Programming:
The act of writing computer programs using some programming language is
called computer programming.
Programming Language:
Programming language is a language which is used to communicate with the
computers i.e. it is a language using which programmers create
programs/instructions for the computers so that computer can perform some
actions.
Computers do not understand Hindi, Marathi, English, Gujarati etc. they only
understand programming language.
Like, we human prefer our native language Hindi, Marathi etc. for communication
purpose, similarly computers prefer programming languages.
1. Machine Language:
Machine language is the only language a computer is capable of understanding.
Machine Language is represented in the sequence of 0’s and 1’s i.e. the
instructions are given to computer in the form of 1 and 0 only.
It is also known as binary language.
It also comes under low level programming Language and difficult to understand.
E.g.: 01100110 10101010
2. Assembly Language:
Assembly Language is again a low level language.
It uses “Mnemonics code” for the instructions.
Mnemonics codes are the symbolic constants used for instructions.
E.g. Add, Sub, Mov etc. are the mnemonics code.
Assembly language is used in micro-controller based embedded systems.
Example of embedded systems are AC, Washing Machine and Elevator etc.
But, Assembly language is not understandable to computers because they only
understand machine language.
So the instructions/code written in assembly language are first converted into
machine language, so that computer can understand it and perform some
operation on it.
The convertor/translator which is used for converting assembly language into
machine language are called “Assembler”.
1. Accurate:
The programming of a program is based on a certain problem. The problem must be pre-
defined clearly with specification of requirements. It is expected from the programmer to
design a program that strictly follows these requirements. So, the designed program must
be accurate to perform the specified task. Such characteristic of programming is referred
‘accurate’.
2. Efficient:
Every program utilizes the resources of the computer system. It is expected from the
programming that the designed program utilizes these resources in an efficient manner. It
means the program must not spend much time or over use the processor in executing its
coded instructions. Such characteristic of the programming is referred ‘efficient’.
3. Maintainable:
When proper structuring method is used in programming, the designed program can be
made maintainable. Here maintainable means the ability to change as per the new needs.
With very little modification a program should work for the new needs. Such characteristic
of programming is referred ‘maintainable”.
4. Portable:
It is expected from the designed program that it can be carried to any platform to solve
the task. If the programming is done keeping many systems rather than one system in
mind the designed programs are portable. Once the program is portable, it can be easily
5. Readable:
The program designed by the programmer must be self-readable as he is the first reader.
Generally the designed program is also read by co-programmers or others. So, the
designed program must contain proper comments to explain the coded instructions. This
commenting will help in understanding and reading the program. Such characteristic of
programming is referred ‘readable’.
6. Reliable:
The designed program must perform as per the need all the time. It should also produce
the intended results for any sort of inputs. In case of improper inputs, it should stop only
after displaying proper error messages. These will indicate the cause of termination of the
program. Such programs are created with the ‘reliable’ characteristic of the programming.
7. Robust:
The designed program is expected to continue with its functionalities even at the
unexpected errors. It is the art of programming that takes care of all the possible errors
before completing the design. Such programs keep on doing their work even at worst
situations. Such characteristic of programming is referred ‘robust’.
8. Usable:
The designed program must be easy to use. It must be designed with proper Interactive
messages so that the user can easily get accustomed to it. Proper thinking in interface
design will prove its worthiness. The documentation of the program must be prepared in
good format to train the users. Such characteristic of the programming is referred ‘usable’.
9. Documentation:
The usage of comments or remarks to explain the coded instructions and the modules of
the program is called documenting the program. Such documented program will also help
in providing other characteristic of the programming. The comments will help to find the
errors as well as to rectify them. When the program is lengthy one proper documentation
will properly connect the components of the program. It improves the readability as well
IDE:
● IDE Stands for “Integrated Development Environment”.
● IDE provides a window where all the c programs are written, compiled and linked
to execute the program.
● As all these 3 processes are provided by a single environment i.e. IDE.
● Turbo C, Dev C++, Code Blocks etc. are the IDE’s and all these IDE’s are easily
available on internet.
● We can download any of them for writing the c program.
Introduction to C language:
Structure of c program:
#include<header file>
int main()
{
Statements-1;
Statements-2;
return 0;
}
int main()
● main() function is mandatory function to write in any program.
● Execution of any program start from the main function only.
● It is the entry point for a program. Compiler starts the execution from main
function only.
● int is the return type of main function that means main function will return an
integer value at the end of the program.
{ }
● { } curly brackets defines scope of a block.
● All the program should be written inside the curly brackets “{ }” only.
return 0;
● return 0; statement causes the termination of main function.
● 0 value indicates successful execution of the program.
● statement written after return 0; never get executed.
Program to display a message on output screen using c language:
#include<stdio.h>
int main()
{
printf(“ Good Morning”);
return 0;
}
Flowchart:
Flowchart is the graphical/diagrammatic representation of an algorithm.
Programmers often use it as a program-planning tool to solve a problem. It makes
use of symbols which are connected among them to indicate the flow of
information and processing.
1. Terminal:
The oval symbol indicates Start, Stop and Halt in a program’s logic flow.
Terminal is the first and last symbols in the flowchart.
2. Input/Output:
A parallelogram denotes any function of input/output type.
Program instructions that take input from input devices and display output on
output devices are indicated with parallelogram in a flowchart.
3. Processing:
A box represents arithmetic instructions.
All arithmetic processes such as addition, subtraction, multiplication and division
are indicated by action or process symbol.
4. Decision :
Diamond symbol represents a decision point.
Decision based operations such as yes/no question or true/false are indicated by
diamond in flowchart.
5. Connectors:
A. On-Page Connector:
Connects 2 or more parts of flowcharts which are on the same page.
On-page connectors are referenced using numbers.
Sahyog College of Management Studies, Thane
B. Off-Page Connector:
Whenever flowchart becomes complex or it spreads over more than one page.
Off-page connectors are referenced using alphabets
It is useful to use connectors to avoid any confusions.
6. Flow lines:
Flow lines indicate the exact sequence in which instructions are executed.
It indicates the flow of logic by connecting symbols.
Character Set In C
Characters are used in forming either words or numbers or even expressions in C
programming. Characters in C are classified into 4 groups:
Letters
In C programming, we can use both uppercase and lowercase letters of English
language
Uppercase Letters: A to Z
Lowercase Letters: a to z
Digits
We can use decimal digits from 0 to 9.
Special Characters
C Programming allows programmer to use following special characters:
comma , slash /
period . backslash
semicolon ; percentage %
Tokens:
● Tokens in C is the most important element to be used in creating a program in C.
● We can define the token as the smallest individual element in C. For `example, we
cannot create a sentence without using words; similarly, we cannot create a
program in C without using tokens in C.
● Therefore, we can say that tokens in C is the building block or the basic component
for creating a program in C language.
2. Identifiers:
● C identifiers represent the name in the C program, for example, variables, functions,
arrays, structures, unions, labels, etc.
● An identifier can be composed of letters such as uppercase, lowercase letters,
underscore, digits, but the starting letter should be either an alphabet or an
underscore.
● Identifier is a synonym for name.
abc valid
a12 valid
_12 valid
3. Constants:
A constant is a data item which will not be changed during the execution of a
program.
Integer constant:
An integer constant is a whole number without any decimal point.
E.g.: 10, 20,-10, 1000 etc.
Sahyog College of Management Studies, Thane
Floating point constant:
A Floating point constant is a number with decimal point.
e.g.: 10.2, 123.34, 1.3 etc.
Character constants:
A character constant is a single alphabet, a single digit or a single special symbol
enclosed within single quotes.
E.g.: ‘a’,’Z’,’B’,’+’,’@’ etc.
Note: The backslash characters such as ‘\a’, ‘\n’, ‘\t’, etc. are non-printable characters.
4. String constant:
● A sequence of characters (i.e. collection of characters) enclosed within a pair of
double quotes is called a string constant.
Sahyog College of Management Studies, Thane
e.g.: “sahyog”,”hello”,”morning” etc.
5. Variables:
● When we want to store any information (data) on our computer/laptop, we store it
in the computer's memory space. Instead of remembering the complex address of
that memory space where we have stored our data, our operating system provides
us with an option to create folders, name them, so that it becomes easier for us to
find it and access it.
● Similarly, in C language, when we want to use some data value in our program, we
can store it in a memory space.
● A variable is a name of the memory location. It is used to store data. Its value can
be changed.
● A variable in C language must be given a type, which defines what type of data the
variable will hold.
● Note:
● Every character has a corresponding ASCII value to it ranging from -128 to 127.
● Numbers as a character has their corresponding ASCII values too. For example, ‘1’ as
char has ASCII value 49, ‘A’ has ASCII value 65.
3. float
● It is a keyword which is used to define floating point numbers. The floating point
numbers are also called real numbers. Normally they are associated with the
variables (also called identifiers) to store floating point numbers in memory
locations.
● That is, using this data type both positive and negative floating point numbers can
be stored in the memory.
● Eg: 1.23, 2234.4532 etc.
4. Double:
● Double is also used to define floating point numbers.
● But double data type accepts 15 digit after decimal whereas float accepts 6 digits.
● Eg: 523.45353
Modifiers in C Language:
1. Size Modifier
2. Sign Modifier
1. Size Modifier:
● Size Modifiers are prefixed with basic data types to modify (either increase or
decrease) the amount of storage space allocated to a variable.
Sahyog College of Management Studies, Thane
● Short, long, long long are the size modifiers.
● For example, storage space for int data type is 4 byte. We can increase the range
by using long int which is 8 byte. We can decrease the range by using short int
which is 2 byte.
● short is only used with int type.
● long can be used with int and double.
● Any size modifier cannot be used with float and char data type.
● long long can not be used with float and double.
● Long long can be used with int type only.
2. Sign Modifier:
● Sign modifiers decides whether the variable will hold positive or negative number.
● signed & unsigned are the sign modifiers.
● In signed type we can store both positive as well as negative value but in unsigned
type we can store only positive value.
● By default the data type is signed only.
● Sign modifiers are used with int and char only.
● Sign modifiers cannot be used with float and double.
Note:
● By default int and char data type is of signed type that means we can store positive
as well as negative value in it.
● int and signed int are same data type.
● short int and signed short int are same.
● long long int and signed long long int are same.
● char and signed char are same.
Format specifier:
The format specifier is used during input and output. It is a way to tell the compiler
what type of data is in a variable during taking input using scanf() or printing using
printf().
Some examples are %c, %d, %f, etc.
● Format specifier for hexadecimal -%x or %X
● Format specifier octal - %o
● Format specifier decimal/integer - %d
Note: Hexadecimal, Octal and Decimal Values comes under integer constant so we can
store value of these types into int datatype.
EG: 0x18A, 0XB, 0XFFF EG: 01612, 034567, -0765 EG: 100, –7, 989
%x or %X %o %d
In this example, the symbolic constant "MAX_V" is specified to have a value of 100. It indicates
that the number 100 will be substituted for "MAX_V" wherever it appears in the code at
compilation time.
We may specify non-numerical values like strings using symbolic constants.
For example:
#define error "INVALID"
In this example, the text "INVALID" is declared as the symbolic constant "error". It can be helpful
for defining error messages or other strings that are frequently used throughout a program.
typedef
The typedef is a keyword used in C programming to provide some meaningful names to the
already existing variable in the C program. It behaves similarly as we define the alias for the
commands. In short, we can say that this keyword is used to redefine the name of an already
existing variable.
Example
typedef int i;
Below is the C program to illustrate how to use typedef.
#include <stdio.h>
typedef int i;
int main()
{
i var= 20;
printf("%d", var);
return 0;
}
Output
20
Use of typedef in C
Following are some common uses of the typedef in C programming:
The typedef keyword gives a meaningful name to the existing data type which helps
other users to understand the program more easily.
It can be used with structures to increase code readability and we don’t have to type
struct repeatedly.
************