Module 1
Module 1
Introduction to C:
Content
Introduction to computers
input and output devices
designing efficient programs.
Introduction to C
Structure of C program
Files used in a C program,
Compilers,
Compiling and executing C programs,
variables,
constants,
Input/output statements in C
Introduction to computers
2.1.Input Devices
1.Keyboard
2.Pointing Devices
3.Handheld Devices
4.Optical Devices
5.Audiovisual Input Devices
2.2.Output Devices
1.Soft Copy Devices
2.Hard Copy Devices
2.1 Input devices
Keyboard
Pointing Devices
Handheld devices
Optical devices
Audiovisual input devices
2.1 Input devices
• 1:Keyboard:The keyboard is the main input device for
computers
• Using a keyboard, the user can type a document, use keystroke
following
1) Typing keys
2) Numeric keys
3) Function keys
4) Control keys
2.1 Input devices
2.1 Input devices
2:Pointing Devices: Example- Mouse,Trackball,Touchpad
Mouse:
The mouse is an input device that was invented in 1963.
Mechanical mouse
Optical mouse
Cordless mouse/wire
less
Trackball:It is a pointing device. It is similar to a mouse.
This is mainly used in notebook or laptop computer, instead of
a mouse.
This is a ball which is half inserted, and by changing fingers
on the ball, the pointer can be moved.
The user rolls the ball with their thumb, fingers or the palm of
their hand to move a cursor without moving their arm.
Touchpad:A touchpad is a small,flat, rectangular stationary
pointing device with a sensitive surface of 1.5-2 square inches.
User has t slide his or her fingertips across the surface of the
pad to point to a specific object on the screen
Input devices
3:Handheld devices: Example
Joystick
Stylus
touchscreen
4:Optical devices:
Example: Barcode Reader, Image scanner, OCR device and
OMR Device
4:Audiovisual input devices
Audio devices,Video input devices are Examples of
Audiovisual input devices
2.2 Output devices
Soft copy devices
Hard copy devices
pseudocodes
Types of errors
Testing and debugging approaches
Programming paradigms
A programming paradigm is a fundamental style of
programming that defines how the structure and basic
elements of a computer program will be built.
The style of writing programs and the set of capabilities
and limitations that a particular programming language has
depends on the programming paradigm it supports.
While some programming languages strictly follow a single
paradigm, others may draw concepts from more than one.
The sweeping trend in the evolution of high-level
programming languages has resulted in a shift in
programming paradigm.
Programming paradigms
Flowcharts:
It’s a graphical or symbolic representation of a process.
Its used to design and document virtually complex process
to help the viewers to visualize the logic of the process,
so that they can gain a better understanding of the process
Flowcharts
Symbols in the flowchart are linked together with arrows to
show the flow of login the process.
Symbols used in flowchart include:
Arrows
Generic processing step
Input/output symbols
A conditional or decision symbol
Labelled connectors
Start and end symbols
Flowcharts
Flowcharts
1.Start and end symbols:
known as terminal symbol
Represented as circle, ovals or rounded rectangles
It’s a 1st and last symbol in flowchart
2.Arrows:
It shows the exact sequence in which the instructions are
executed.
3.Generic processing step
Also called as an activity
Its represented using rectangle
Arithmetic and data movement instructions
Flowcharts
4.Input/ouput symbol:
Represented using parallelogram
Get input and display results
6.Labelled connectors:
Represented by circle
Connector
Significance of Flowcharts
A flowchart is a diagrammatic representation that illustrates the
sequence of steps that must be performed to solve a problem.
It facilitates communication between programmers and users.
Flowcharts are very important in the programming of a
problem as they help the programmers to understand the logic
of complicated and lengthy problems.
Once a flowchart is drawn, it becomes easy for the
programmers to write the program in any high-level language.
Hence, the flowchart has become a necessity for better
documentation of complex programs.
A flowchart follows the top-down approach in solving
problems.
Programming in C
Contents
Introduction to c
Characteristics of c
Uses of c
Structure of a c program
Writing the first c program
Files used in c
Compiling and executing c
Using comments,c tokens,character set, keywords ,identifiers,
basic data types in c, variables, constants, input/output
statement in c
Introduction
The programming language C was developed in the
early 1970s by Dennis Ritchie at Bell Laboratories to
be used by the UNIX operating system.
It was named 'C' because many of its features were
platforms.
Characteristics of C
C is a robust language whose rich set of built-in functions and operators
can be used to write complex programs.
C is a high-level programming language, which enables the programmer
to concentrate on the problem at hand and not worry about the machine
code on which the program would be run.
Small size-C has only 32 keywords. This makes it relatively easy to learn
as compared to other languages.
C makes extensive use of function calls.
C is well suited for structured programming. In this programming
approach, C enables users to think of a problem in terms of
functions/modules where the collection of all the modules makes up a
complete program. This feature facilitates ease in program debugging,
testing, and maintenance.
Unlike PASCAL it supports loose typing
Structured language as the code can be organized as a collection of
one or more functions.
Stable language.
Quick language as a well written C program is likely to be as quick as
or quicker than a program written in any other language. Since C
programs make use of operators and data types, they are fast and
efficient.
Facilitates low level (bitwise) programming.
Supports pointers to refer computer memory , arrays, structures, and
functions.
Core language. C is a core language as many other programming
languages (like C++, Java, Perl, etc.) are based on C.
C is a portable language, i.e., a C program written for one computer
can be run on another computer with little or no modification.
C is an extensible language as it enables the user to add his own
functions to the C library.
Uses of C
C is a very simple language that is widely used by software professionals
around the globe. The uses of C language can be summarized as follows:
C language is primarily used for system programming.
The portability, efficiency, the ability to access specific hardware
addresses, and low runtime demand on system resources make it a good
choice for implementing operating systems and embedded system
applications.
C has been so widely accepted by professionals that compilers, libraries,
and interpreters of other programming languages are often written in C.
For portability and convenience reasons, C is sometimes used as an
intermediate language for implementation of other languages.
Basically, C was designed as a programming language and was not
meant to be used as a compiler target language. Therefore, although C
can be used as an intermediate language it is not an ideal option.
C is widely used to implement end-user applications.
Structure of a C Program
A C program is composed of preprocessor commands, a global
declaration section, and one or more functions (Figure 9.2).
The preprocessor directives contain special instructions that indicate
how to prepare the program for compilation.
One of the most important and commonly used preprocessor
commands is include which tells the compiler that to execute the
program, some information is needed from the specified header file.
A C program contains one or more functions, where a function is
defined as a group of C statements that are executed together. The
statements in a C function are written in a logical sequence to
perform a specific task.
The main () function is the most important function and is a part of
every C program. The execution of a C program begins at this
function.
All functions including main () are divided into two
parts the declaration section and the statement section.
The declaration section precedes the statement section
and is used to describe the data that will be used in the
function. Note that data declared within a function are
known as local declaration as that data will be visible
only within that function. Stated in other terms, the
life-time of the data will be only till the function ends.
The statement section in a function contains the code
that manipulates the data to perform a specified task.
Writing the first C Program
To write a C program, we first need to write the code.
For this, open a text editor. If you are a Windows user you may use Notepad and if
you prefer working on UNIX/Linux you can use emacs or vi.
Once the text editor is opened on your screen, type the following statements:
#include <stdio.h>
int main()
{
printf ("\n Welcome to the world of C ");
return 0;
}
Output
Welcome to the world of c
#include<stdio.h>
int main()
Every C program contains a main() function which is the starting point of the
program.
int is the return value of the main( )function. After all the statements in the
program
{} The two curly brackets are used to group all the related statements of the
main function. All the statements between the braces form the function body.
The function body contains a set of instructions to perform the given task.
printf("\n Welcome to the world of c");
The printf function is defined in the stdio.h file and is used to print text on
the screen.
The message that has to be displayed on the screen is enclosed within double
quotes and put inside brackets.
return 0;
This is a return command that is used to return the value O to the operating
system to give an indication that there were no errors during the execution of
the program.
Files used in a C program
1.Line comment(//)
2.block comment(/*
*/)
Line comment is used to comment a single statement
Block comment is used to comment multiple statement
Using Comments
It’s a good practice to put always a comment at the
top of a program that tells you what the program
does.
It can be used anywhere in the pogram
You can also use comments in between your code to
compiler
C tokens
Tokens are the basic building blocks in C
language
Program is constructed using a combination
of these tokens.
There are 6 main types of tokens in C.
Character set in C
In C, a character means any letter from English Alphabet,
a digit or a special symbol used to represent information.
These characters when combined together form tokens
case
Digits: 0 to 9
Special Characters: >,”,&,%........
White space characters: \b,\t,\v,\r,\f,\n
Escape sequence:
Keywords
Like every computer language, C has a set of
reserved words often known as keywords that cannot
be used as an identifier
All keywords are a sequence of characters that have a
fixed meaning
Keywords must be written in lowercase letters.
Example for keywords: auto, break, case, char, const,
%marks,@name,(DA),-hta
Basic data types in c
It provide very few basic data types
Variable
Variable is defined as a meaningful name given to a data storage
location in computer memory.
When using a variable, we actually refer to address of the
memory where the data is stored.
C supports 2 kinds of variables
1.Numeric variables
2.Character variables
quotes.
These character can be any character from ASCII
Character constants
It consist of single characters enclosed in single quotes
Example: ‘a’,’&’
String constants
It’s a sequence of characters enclosed in double quotes
Length of string constant is equal to number of characters in the
string plus 1(for null character)
Length of string literal ‘hello’ = 6
Declaring constants
To declare constant, precede normal variable declaration with
const keyword and assign it a value
Example: const float pi = 3.14; or # define pi 3.14159
Input/Output statements
in C
Streams
Stream is a source of data as well as destination of
data
Its associated with a physical device such as monitor