0% found this document useful (0 votes)
23 views108 pages

problem solving using c full course paart 1

Unit 1 covers problem-solving techniques, including algorithms, pseudo code, and flowcharts, as well as an introduction to computer programming languages such as machine, symbolic, and high-level languages. It specifically focuses on the C language, detailing its characteristics, structure, data types, and program development processes including writing, editing, compiling, linking, and executing programs. The unit also discusses the history of C, its features, and the importance of understanding algorithms and flowcharts in programming.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
23 views108 pages

problem solving using c full course paart 1

Unit 1 covers problem-solving techniques, including algorithms, pseudo code, and flowcharts, as well as an introduction to computer programming languages such as machine, symbolic, and high-level languages. It specifically focuses on the C language, detailing its characteristics, structure, data types, and program development processes including writing, editing, compiling, linking, and executing programs. The unit also discusses the history of C, its features, and the importance of understanding algorithms and flowcharts in programming.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 108

Unit - I

K.Vigneswara Reddy, Dept. of I.T, SNIST


Topics to be covered in UNIT-1

Problem Solving Techniques:


 Algorithms, pseudo code, flowcharts

Introduction to Computer Programming Languages :


 Machine Languages, Symbolic Languages, High-Level Languages

Introduction to C language:
Characteristics of C language,
Structure of a C Program.
Syntax and semantics
Data Types, Variables – declarations and initialization,
formatting input and output.

Creating and Running Programs:

Writing, editing, compiling, linking, and executing programs

K.Vigneswara Reddy, Dept. of I.T, SNIST


Computer Full Form
?
We have different types of Full Forms but most
of the people trying to say
Commonly Operated Machine Particularly Used
for Technology Education and Research.
Who Introduced Computer
?
Charles Babbage, in the year 1832
K.Vigneswara Reddy, Dept. of I.T, SNIST
Program Development

1.Understand the problem


2.Develop solution
1.Structure Chart
2.Algorithm/ pseudocode
3.Flowchat
3.Write the program
4.Test the program

K.Vigneswara Reddy, Dept. of I.T, SNIST


Understand the problem

• To solve any problem first we must understand the problem by


knowing the requirements of the problem.

• Once we understand it, review with user(customer) and system


analyst.

K.Vigneswara Reddy, Dept. of I.T, SNIST


Develop the solution

• To develop a solution to a problem it needs three


tools.
1. Structure chart-hierarchical chart
- Shows the functional flow through our program.
- Shows how the problem is broken into logical steps.
Each step will be a separate module.
2. Algorithm
3. flowchart

K.Vigneswara Reddy, Dept. of I.T, SNIST


Buy a
computer

Get Print
calculate
configuration report

desktop Laptop

DELL HP DELL HP

K.Vigneswara Reddy, Dept. of I.T, SNIST


Algorithm or pseudo code
• Definition of algorithm- Al Kho-war-iz-mi an ordered sequence
of unambiguous and well-defined instructions that performs
some task and halts in finite time
• Let's examine the four parts of this definition more closely.
1. Ordered sequence- we can number the step.
2. Unambiguous and well defined instructions- each instruction is
clear, understand without difficulty.
3. Performs some task
4. Halts in finite time- algorithm must terminate at some point.

K.Vigneswara Reddy, Dept. of I.T, SNIST


Properties of an algorithm:-

Finiteness :- An algorithm must terminate in a finite number of


steps.

Definiteness :- Each step of the algorithm must be precisely and


unambiguously stated.

Effectiveness:-Each step must be effective, and can be


performed exactly in a finite amount of time.

Generality :- The algorithm must be complete in itself.

Input /Output :- Each algorithm must take zero, one or more


inputs and produces one or more outputs.

K.Vigneswara Reddy, Dept. of I.T, SNIST


How to represent an algorithm
• 1. use natural language- English
• 2. use formal programming language.
• 3. pseudo code- natural language constructs modeled
looks like statements available in the programming
language.
• Pseudo code- An algorithm written in English like
language that follows the loosely defined syntax is
called pseudo code.
• Pseudo code is a numbered list of instructions to
perform a task.

K.Vigneswara Reddy, Dept. of I.T, SNIST


Three Categories of Algorithmic Operations
• An algorithm must have the ability to alter the order of its
instructions. An instruction that alters the order of an algorithm
is called a control structure
• Three Categories of Algorithmic Operations:
1. sequential operations - instructions are executed in order

2. Conditional/selection ("question asking") operations - a control


structure that asks a true/false question and then selects the
next instruction based on the answer

3. iterative operations (loops) - a control structure that execute


block of instructions repeatedly.

K.Vigneswara Reddy, Dept. of I.T, SNIST


• Pseudo-code and algorithm Constructions : A Summary
• Computation/Assignment
• set\initialize the value of "variable" to :"arithmetic expression" or
"variable" equals "expression" or
"variable" = "expression"
• Input/Output
• get \enter\read "variable", "variable", ...
Display\print "variable", "variable", ...
• Conditional (dot notation used for numbering subordinate statements)
• 1. if "condition"
1.1 (subordinate) statement 1
1.2 etc ...
2. else
2.1 (subordinate) statement 2
2.2 etc ...
• Iterative (dot notation used for numbering subordinate statements)
• 3. while "condition"
3.1 (subordinate) statement 1
3.2 etc ...

K.Vigneswara Reddy, Dept. of I.T, SNIST


Example1: To determine whether a student is passed or not

Pseudo Code:
1. If student's grade is greater than or equal
to 60
1.1 Print "passed"
2. else
2.1 Print "failed"

Algorithm:
Begin
1. If grade >= 60
1.1 Print "passed"
2. else
2.1 Print "failed”
End

K.Vigneswara Reddy, Dept. of I.T, SNIST


Example 2: Write an algorithm to determine a student‟s final grade
and indicate whether it is passing or failing. The final grade is
calculated as the average of four marks.
Pseudo Code:
1. Input set of 4 marks
2. Calculate their average by summing and dividing by 4
3. if average is below 50
4. Print “FAIL”
Algorithm:
5. else
6. Print “PASS”
Begin
Step 1: Input M1,M2,M3,M4
Step 2: GRADE  (M1+M2+M3+M4)/4
Step 3: if (GRADE < 50) then
Step 4: Print “FAIL”
Step 5: else
Step 6: Print “PASS”
Step 7: endif
End
K.Vigneswara Reddy, Dept. of I.T, SNIST
Example- write an algorithm for finding sum of any two
number
1. Start
2. Declare a,b,c
3. Print “ enter a and b values through keyboard”
4. c= a+b
5. Print c
6. Stop.

K.Vigneswara Reddy, Dept. of I.T, SNIST


• Example - Computing a Quiz Average: Pseudo-code a routine to
calculate your quiz average.
1. get number of quizzes
2. sum = 0
3. count = 0
4. while count < number of quizzes
4.1 get quiz grade
4.2 sum = sum + quiz grade
4.3 count = count + 1
5. average = sum / number of quizzes
6. display average
7. halt

variables: number of quizzes, sum ,count, quiz grade, average


This example introduces an iterative control statement. As long as the
condition in line 4 is True, we execute the subordinate operations 4.1 -
4.3. When the condition becomes False, we resume the pseudo-code at
line 5.
K.Vigneswara Reddy, Dept. of I.T, SNIST
Flow chart
• Pictorial representation of an algorithm is called flow chart.
• A flow chart is a graphical or symbolic representation of a
process.
• A diagram that uses graphic symbols to depict the nature and
flow of the steps in a process.
• Snap shot of an algorithm.

K.Vigneswara Reddy, Dept. of I.T, SNIST


SYMBOLS USED WITH FLOWCHARTS

K.Vigneswara Reddy, Dept. of I.T, SNIST


Flowchart to find the addition of two numbers

Start

input a, b

c=a+b

Display C

End

K.Vigneswara Reddy, Dept. of I.T, SNIST


Algorithm to find whether a given year is a leap year or not.

Algorithm:

BEGIN
Step 1: Accept the YEAR
Step 2: if (YEAR %4 == 0) then
Display “Year is a leap year”
else
Display “Year is not a leap year”
end if
END

K.Vigneswara Reddy, Dept. of I.T, SNIST


Flowchart to find whether a given year is a leap year or not

Start

Accept year

false
if(year%4 = = 0)

true
Display non leap year
Display leap year

End
K.Vigneswara Reddy, Dept. of I.T, SNIST
Introduction to Computer
Programming Languages

A program for the computer must be written in programming

language. The first programming language is machine


language. computer languages were evolved from machine
language to natural language ( English like language).

K.Vigneswara Reddy, Dept. of I.T, SNIST


Machine language
• Machine language was the first programming
language in the early days of computers.
• The language which is understand by the
computer hardware is called machine
language.
• It consists of 0’s and 1’s.
• Internally the computer consists of circuits
which are made of switches, transistors and
other electronic devices that can be in one of
the two states: off or on.
• Off- 0, on- 1.
K.Vigneswara Reddy, Dept. of I.T, SNIST
FIG: Computer Language Evolution

K.Vigneswara Reddy, Dept. of I.T, SNIST


PROGRAM 1-1 The Multiplication Program in Machine Language

K.Vigneswara Reddy, Dept. of I.T, SNIST


Symbolic language
• Writing program in machine language is
difficult.
• The language is represented in symbols or
mnemonics so it is called symbolic language.
• This is language does not understand by the
computer .hence, it must be translated to the
machine language using the assembler.
• Another name of the symbolic language is
assembly language.
K.Vigneswara Reddy, Dept. of I.T, SNIST
PROGRAM 1-2 The Multiplication Program in Symbolic Language

K.Vigneswara Reddy, Dept. of I.T, SNIST


High level languages
• It is like natural language which can understand by the
programmer.

• They must be converted into the machine language is called


compilation.

• First high level language is FORTRAN

• High level languages are – c, c++,java, cobol etc

K.Vigneswara Reddy, Dept. of I.T, SNIST


PROGRAM 1-3 The Multiplication Program in C

continued

K.Vigneswara Reddy, Dept. of I.T, SNIST


PROGRAM 1-3 The Multiplication Program in C (continued)

K.Vigneswara Reddy, Dept. of I.T, SNIST


Introduction to C language

K.Vigneswara Reddy, Dept. of I.T, SNIST


History of C language

Taxonomy of the C Language


K.Vigneswara Reddy, Dept. of I.T, SNIST
History of C language (contd…)
 ALGOL (Algorithmic Language)was the first computer
language. Designed to solve mathematical and scientific
problems.
 In 1967, Martin Richards developed a language called BCPL
(Basic Combined Programming Language) at University of
Cambridge primarily, for writing system software.

 In 1969, language B was developed by Ken Thompson.

 „B‟ was used to create early versions of UNIX operating


system at Bell Laboratories.

 In 1972, C was developed by Dennis M. Ritchie at Bell


Labs(AT&T) in USA.

 In 1988 C language was standardized by ANSI as ANSI C


(ANSI- American National Standards Institute).
 UNIX operating system was
K.Vigneswara Reddy,coded almost entirely in C.
Dept. of I.T, SNIST
K.Vigneswara Reddy, Dept. of I.T, SNIST
Characteristics of „C‟ language
 Small size
 Extensive use of function calls
 Loose typing - unlike PASCAL
 Structured language
 Low level (BitWise) programming is readily available
 Pointer implementation - extensive use of pointers for
memory, array, structures, and functions.

Now, it had become a widely used professional language


because of the following reasons.

 It has high-level constructs.


 It can handle low-level activities.
 It produces efficient programs.
 It can be compiled K.Vigneswara
on a varietyReddy, Dept.of
of I.T,computers.
SNIST
Features of C language
1. Robust – rich set of built in functions and operators can be used
to write any complex programs.
2. C compilers combines the capabilities of low level languages with
features of high level language. Therefore it is suitable for
writing the system software, application software and most of the
compilers of other languages also developed with c language.
3. Efficient and fast- rich variety of data types and powerful
operators.
4. Portable- c program written on one computer can also run on the
other computer with small or no modification.
ex- c program written in windows can also run on the linux
operating system.
5. Structured programming- every program in c language is divided
into small module or functions so that it makes the program much
simple and debugging, maintenance of the program is easy.
6. Ability to extend itself- A C program is basically a collection of
various function supported by C library (also known as header
files). We can also add our own functions to the C library. These
functions can be reused in other applications or programs by
passing pieces of information to the functions, you can create
useful, reusable code.
K.Vigneswara Reddy, Dept. of I.T, SNIST
Structure of c program

K.Vigneswara Reddy, Dept. of I.T, SNIST


Include
information
about standard
library

Main calls library


function printf to
print this message.

K.Vigneswara Reddy, Dept. of I.T, SNIST


 C program consists of functions and variables.

 Function contains the statements that specify


computation operation to be done.

 Variables stores the values used during the


computation.

 Function can be named whatever you like. However,


main is the special function where the program
execution starts. Very program must have a main
some where.

K.Vigneswara Reddy, Dept. of I.T, SNIST


Preprocessor commands

• The preprocessor command always starts with symbol #


• #include<stdio.h> this is called preprocessor command.
• Provides instructions to the preprocessor , to include the functions
from the system library and also defines symbolic constants and
macros.
• Include command tells the preprocessor that we need the information
from selected libraries called header file.
• Header file- collection of library files.(built- in functions)
• It tells the compiler to include the information about the standard
input\ output library.
• every program must need at least one library functions which are
available in the header file.

K.Vigneswara Reddy, Dept. of I.T, SNIST


Global variables.
 The variables which are used by more than one function must be
declared at the starting of the program called global variable.
 They are visible to all the parts of the program.

 Every C program must have one main() function. All the


statements of main are enclosed in braces. { }

 The program execution begins at main() function and ends at


closing brace of the main function.

 C program can have any number of user-defined functions and


they are generally placed immediately after the main () function,
although they may appear in any order.

K.Vigneswara Reddy, Dept. of I.T, SNIST


 All sections except the main () function may be absent when
they are not required.

 In the previous program, main() function returns an integer


value to the operating system.

 Each statement in C program must end with ; specifies that


the instruction is ended.

 A function can be called by it’s name, followed by a


parenthesized list of arguments and ended with semicolon.

 In previous program main() function calls printf() function.


Example: printf(“Hello World!\n”);

 \n advances to the new line.

K.Vigneswara Reddy, Dept. of I.T, SNIST


Local variables
The variables which are declared within
a function are called local variables to
that function.

These variables visible only within the


function.

K.Vigneswara Reddy, Dept. of I.T, SNIST


• printf(“hello world
“);
• C compiler prints the error message.
same program can also rewrite as
#include<stdio.h>
main()
{
printf(“hello”);
printf(“world”);
printf(“\n”);
return 0;
}

K.Vigneswara Reddy, Dept. of I.T, SNIST


Comments
• To make the program more readable use the comments.
• They may used to make the program easier to
understand.
• Two types of comments
1. block comment
2. line comment.

K.Vigneswara Reddy, Dept. of I.T, SNIST


1. Block comment /* */
/* write a program for the addition of two integer numbers*/
#include<stdio.h>
main()
{
int a=10,b=20,c;
c=a+b;
printf(“sum of a and b=%d”,c);
return 0;
}

above two lines


/* write a program for the addition of two integer numbers*/
are a comment, which explains what the program does.
Any characters between /* and */ are ignored by the
compiler.
Comments may appear anywhere in a program.
/* and */ is used to comment the multiple lines of code which
ignored by the compiler.
/*……/*…..*/ - nested block comments are invalid.
K.Vigneswara Reddy, Dept. of I.T, SNIST
2. Line comment
• It uses two slashes // .
• This is used to comment single line.
• // #include<stdio.h>

/* Write a program to add two integer numbers */


#include<stdio.h> // It includes input, output header file
main()
{
int a=10,b=20,c; // Variables declaration & initialization
c=a+b; // Adding two numbers
printf(“Sum of a and b=%d”,c);
return 0;
}

K.Vigneswara Reddy, Dept. of I.T, SNIST


Syntax:

 It refers to the rules and regulations for writing any statement in a


programming language like C/C++ etc.
 It does not have to do anything with the meaning of the statement.
 A statement is syntactically valid if it follows all the rules.
 It is related to the grammar and structure of the language.

Semantics:

 It refers to the meaning associated with the statement in a


programming language.
 It is all about the meaning of the statement which interprets the
program easily.
 Errors are handled at runtime.
K.Vigneswara Reddy, Dept. of I.T, SNIST
Prob.1: Normal program: Prob.2: Print statement after
getch()
#include<stdio.h>
#include<conio.h> #include<stdio.h>
int main() #include<conio.h>
{ int main()
printf(“Welcome”); {
getch(); printf(“Welcome”);
return 0; getch();
} printf(“after getch”);
return 0;
Prob.1 o/p: Welcome }

Prob.3: Print statement Prob.2 o/p: Welcome


after return Note: syntax wise its right but
semantically its wrong
#include<stdio.h>
#include<conio.h>
int main() Prob.3 O/p: welcome and Warning
{
printf(“Welcome”); Note: syntax wise its right but
getch(); semantically its wrong
return 0;
Printf(“After return”); } K.Vigneswara Reddy, Dept. of I.T, SNIST
Basis Syntax Semantics
It refers to the rules of any It refers to the meaning associated
Meaning statement in the programming with any statement in the
language. programming language

It is referred to as a syntax error. It is


generally encountered at the It referred to as a semantic error. It is
compile time. It occurs when a generally encountered at run time. It
statement that is not valid according occurs when a statement is
Error
to the grammar of the programming syntactically valid but does not do
language. Some examples are what the programmer intended. This
missing semicolons in C++, using type of error is tough to catch.
undeclared variables in Java, etc.

The syntax is the arrangement or


There are two areas of semantics
order of words, determined by both
In linguistics that are logical semantics and lexical
the writer’s style and grammar
semantics.
rules.
the syntax is sensitive in most Most of the semantics are case-
Sensitivity
programming languages. insensitive.
K.Vigneswara Reddy, Dept. of I.T, SNIST
Data types in C
 The data values used by the program must be stored in variable.
 Data type is used to specify the following things
1. type of values stored in a variable.
2. defines the amount of memory to be allocated to the variables.
3. defines range of values represented in memory.
4. type of operation can be performed on those data values.

ANSI C supports 3 categories of data types:


Built-in data types
Derived data types
User Defined data types

K.Vigneswara Reddy, Dept. of I.T, SNIST


Data types in C (contd…)

K.Vigneswara Reddy, Dept. of I.T, SNIST


Built in data types
• C has built in data types also called primitive data
types the following are basic data types in C.
• char – declare a variable to store a character
• int - declare a variable with integer value- (0to 9)
• float – holds real values single-precision floating
point. Stores precision of 6 digits
• double double-precision floating point( 14 digits)
• void- specifies that no value is available.

K.Vigneswara Reddy, Dept. of I.T, SNIST


Sizes and range of values allowed in basic data types in
16 bit computer.

Data type Size in bits Range of values


char 8 -128to +127
int 16 -32768 to +32767
float 32 1.17549X10-38 to
3.40282X1038
double 64 2.22507 X 10 -308 to
1.79769 X 10 308
void 8 Value less

K.Vigneswara Reddy, Dept. of I.T, SNIST


Qualifiers
• There are number of qualifiers that can be applied to the basic
types
• The qualifiers are used to alter meaning of the data types
yields new data type.
• Qualifiers are signed, unsigned, short and long.
• Short and long- used to alter the size of basic data type ( int,
double).
• Short is placed in front of the int declaration, tell the C
compiler that the particular variable being declared is used to
store a small integer value.
• Which can be used when a program needs more memory and
available memory is limited.
• short int a;
• long int b; the word int can omitted
• signed numbers are +ve and –ve numbers.
• Unsigned numbers represent only
K.Vigneswara Reddy, +ve
Dept. of I.T,numbers.
SNIST
Qualifiers
Qualifiers Basic data types
Signed char, int
unsigned char, int
long int , double
short int

K.Vigneswara Reddy, Dept. of I.T, SNIST


The char Data Type - %c

 The shortest data type is character.


 It is stored in 1 byte in memory.
 Corresponding integer values for all characters are defined in
ASCII code (American Standard Code for Information
Interchange).

For example:

character constant „a‟ has an int value 97, „b‟ - 98, „A‟ - 65 etc.
The keyword char is used to declare a variable of a character type.

char letter =‘A’;


char letter = 65;

K.Vigneswara Reddy, Dept. of I.T, SNIST


The int Data Type

An integer number (also called whole number) has no fractional part


or decimal point.

-> The keyword int is used to specify an integer variable.


-> it occupies 2 bytes (16 bits) or 4 bytes (32 bits), depending on
the machine architecture.

16-bit integer can have values in the range of


- 32768 to 32767
One bit is used for sign.

For 32-bit integer the range is about from -2 to +2 billions

K.Vigneswara Reddy, Dept. of I.T, SNIST


 The sizes of the data types are machine
dependent.
 Short is 16bits, long 32 bits and int either 16
bits or 32 bits.
 These sizes are chosen by the compiler for
its own hardware.
 In 16 bit processor short – 2, int- 2, long- 4
 In 32 bit processor short-2, int -4, long-4.
 They are restricted that short is no longer
than int, int is at least 16 bits , long is at least
32 bits.

K.Vigneswara Reddy, Dept. of I.T, SNIST


K.Vigneswara Reddy, Dept. of I.T, SNIST
Unsigned integer

If you know that the variable cannot have negative values, you may declare it
as unsigned.
unsigned variable type occupies the same amount of computer memory as int
but the actual value can be twice as large as the maximum available int,
because all values are positive.
To decrease the size of allocated memory for a variable which is not too large,
you may use the short type which is stored in 2 bytes

The float Data Type

-> The data type float represents a floating point number (also called a real
number or numbers with decimal ).
->The keyword float is used to declare a variable of the type float.
Ex: float float_num1=1.34;
-> The float type variable is usually stored in 4 bytes, which means it is at least
6 digits of precision.
K.Vigneswara Reddy, Dept. of I.T, SNIST
The double Data Type

A floating point number can also be represented by the double data type.
The data type double is stored on most machines in 8 bytes which is about 15
decimal places of accuracy.
To declare a variable of the type double, use the keyword double.

K.Vigneswara Reddy, Dept. of I.T, SNIST


K.Vigneswara Reddy, Dept. of I.T, SNIST
Types in C
size smallest largest format
type precision constant
(byte) number number specifier
1.2345L,
long double 10 3.4E-4932 1.1E+4932 1.08E-19 %Lf, %Le, %Lg
1.234E-5L
1.2345,
double 8 1.7E-308 1.7E+308 2.22E-16 %lf, %le, %lg
1.234E-5
1.2345F,
float 4 1.7E-38 3.4E+38 1.19E-7 %f, %e, %g
1.234E-5F

unsigned long 4 0 4294967295 123UL %lu

long 4 -2147483648 2147483647 123L %ld, %li

unsigned 4 0 4294967295 123U %u

int 4 -2147483648 2147483647 123 %d, %i

unsigned short 2 0 65535 123U %hu

short 2 -32768 32767 123 %hd, %hi

unsigned char 1 0 255 ’a’, 123, ’\n’ %c

char 1 -128
-127 or 0 127 or 255
127 ’a’, 123, ’\n’ %c

K.Vigneswara Reddy, Dept. of I.T, SNIST


Identifiers
 Identifiers are name of the variables and functions.

 Identifiers are user defined names means name given to the


variable whatever user likes.

 Ex- int a; // a is identifier


 int sum; // sum is identifier.

K.Vigneswara Reddy, Dept. of I.T, SNIST


Variables
When you want to process some information, you can save
the values temporarily in variables.

Program
#include<stdio.h>
main()
{ Note – variables must be
int a=10,b=20,c; declared before they are
c=a+b; used, usually at the
printf(“sum of a and b=%d\n”,c);
return 0;
beginning of the function.
}
o/p- sum of a and b=30

Definition- variable which stores the value in the memory location, that value varies
during program execution
variables in the above program are a b and c .

K.Vigneswara Reddy, Dept. of I.T, SNIST


Variables names
• There are some restrictions on the variable names
1. Names are made up of letters and digits and
underscore(_).
2. The first character of the name must be a letter
3. The underscore is also a legal first character, but its
use is not recommended.
4. Upper case and lower case letters are distinct, so a
and A are two different names.
5. C keywords can't be used as variable names
6. White space is not allowed in variable names.

K.Vigneswara Reddy, Dept. of I.T, SNIST


• The following list contains some examples of
legal and illegal C variable names:
Variable Name Legality

Percent Legal
y2x5__fg7h Legal
annual_profit Legal
_1990_tax Legal but not advised
savings#account Illegal: Contains the illegal character #
double Illegal: Is a C keyword
9winter Illegal: First character is a digit

K.Vigneswara Reddy, Dept. of I.T, SNIST


Variable declaration
• Each variable must be declared and defined
before used in the program.
Variable declaration-
1. tells the compiler what is the name of the
variable.
2. specifies what type of data values the
variable can hold.
variable definition- reserves the memory for
the declared variable and store some value is
called garbage value.
variable declaration and variable definition done at the
same time with single statement.
K.Vigneswara Reddy, Dept. of I.T, SNIST
syntax: datatype identifier;

int a;
Variable declaration and definition

Variable name
a
23456 Garbage value

1000 Address of the variable

K.Vigneswara Reddy, Dept. of I.T, SNIST


Variable initialization
• While declaring the variable assign
initial value is called variable
initialization.
• Data type identifier= initial value;

Example int a=10;


float b=2.1;
float pi=3.14;
char ch=„A‟;
K.Vigneswara Reddy, Dept. of I.T, SNIST
constants
• Constants- fixed values those cannot be
changed during the program execution.
• Several types of constants are used
1. Character constants
1.1 single character constants
1.2 string constants.
2. Numeric constants.
2.1 integer constant
2.2 real constants.
K.Vigneswara Reddy, Dept. of I.T, SNIST
Type qualifier const
One way to use the constant is with
memory constants. Memory constants
use a c type qualifier; const.
 This indicates that the data cannot be
changed.
 const type identifier= value;
 const float pi=3.14;

K.Vigneswara Reddy, Dept. of I.T, SNIST


• Example Area of circle
#include<stdio.h>
void main()
{
float area, radius=3.0;
const float pi=3.14;
area=pi*radius*radius;
printf(“area of a circle= %f”,area);
}

K.Vigneswara Reddy, Dept. of I.T, SNIST


1. Character constants
 A single character constants are enclosed in single quotes.
 Ex- „1‟ „X‟ „%‟ „ „
 Character constants have integer values called ASCII values.
 char ch=„A‟;
 printf(“%d”,ch); ->65
 similarly printf(“%c”,65)-> A
2. String constants
 string a collection of characters or sequence of characters
enclosed in double quotes.
The characters may be letters, numbers, special characters and
blank space.
Ex- “snist”
“2011”
“A”.

K.Vigneswara Reddy, Dept. of I.T, SNIST


Backslash \escape characters
• Backslash characters are used in output functions.
• These backslash characters are preceded with the \ symbol.
Constant meaning
„\a‟ Alert(bell)
„\b‟ Back space
„\f‟ Form feed
„\n‟ New line
„\r‟ Carriage return
„\v‟ Vertical tab
„\t‟ Horizontal tab
„\‟ ‟ Single quote
„\” ‟ Double quotes
„\?‟ Question mark
„\\‟ Backslash
„\0‟ null
K.Vigneswara Reddy, Dept. of I.T, SNIST
printf(“hello\a”);// beep sound
printf(“snist\b”);// snis- deletes last character
printf(“hello\f”);// clears complete screen
printf(“snist\n”);// new line
printf(“snist\r”);
printf(“this is \‟IT\‟ branch”);//this is „IT‟ branch
printf(“this is \”ECE\” branch”);// this is “ECE” branch
printf(“\n what is your name\?”);// what is your name?
printf(“\n \\ this is used as escape character”);

K.Vigneswara Reddy, Dept. of I.T, SNIST


Numeric constants
• 1. integer constant- sequence of digits like 0to 9.
• Ex- 123 -678 0 +78.
Rules
1. Integer constant have at least one digit
2. No decimal points
3. No commas or blanks are allowed.
4. The allowable range for integer constant is -
32768 to 32767.

K.Vigneswara Reddy, Dept. of I.T, SNIST


To store the larger integer constants on 16 bit machine use the
qualifiers such as U,L,UL.

Type Representation Value

int +245 245

int -678 678

unsigned integer 65342u/65342U 65342

unsigned long int 99999UL 99999

long integer 999999L 999999

K.Vigneswara Reddy, Dept. of I.T, SNIST


• 2. Real constants- the numbers containing fractional parts like
3.14.
• 1.9099 -0.89 +3.14
• Real constants are also expressed in exponential notation.

Mantissa e exponent

The part appearing before e is called mantissa this may


be the fractional number or integer number and part following e is called
exponent .
Ex 215.65 may be written as 2.1565e2.

K.Vigneswara Reddy, Dept. of I.T, SNIST


Examples of real constants
Type Representation Value

double 0. 0.0

double 0.0 .0

float -2.0f -2.0

long double 3.1415927654 3.1415927654


4L 4

K.Vigneswara Reddy, Dept. of I.T, SNIST


• #include<stdio.h>
void main()
{
const int a=10;
int b=20;
a=a+1;
b=b+1;
printf(“a=%d b=%d”,a,b);
}

K.Vigneswara Reddy, Dept. of I.T, SNIST


Coding Constants: Different ways to create constants.
Literal constants:
A literal is an unnamed constant used to specify data.
Example: a = b + 5;

Defined constants:
By using the preprocessor command you can create a
constant.
Example: #define pi 3.14

Memory constants:
Memory constants use a C type qualifier, const, to indicate
that the data can not be changed.
Its format is: const type identifier = value;
Example: const float PI = 3.14159;

K.Vigneswara Reddy, Dept. of I.T, SNIST


Formatted input/ output
 The data is to be arranged in a particular format.
 The data is input to and out put from a stream.
 A stream is a source or destination of the data. it is
associated with a physical device, such as terminals
(keyboard, monitor).
 C has two forms of streams- text stream and binary
stream.
 Text stream consists of sequence of characters.
 Binary stream consists of a sequence of data in 0‟s
and 1‟s.

K.Vigneswara Reddy, Dept. of I.T, SNIST


FIGURE Stream Physical Devices

K.Vigneswara Reddy, Dept. of I.T, SNIST


 The data is formatted using the printf and scanf functions.

 scanf()- it allows the user to input the data in the specified


format.
 converts the text stream coming from the keyboard to data
values (char, int,ect) and stores them in the program variables.

 printf() converts the data stored in variables into the text


stream for output the monitor or screen.

K.Vigneswara Reddy, Dept. of I.T, SNIST


printf() takes the set of data values and converts them to text stream using
formatting instructions contained in a format control string.
Fomat control specifiers are used to format the text stream.

FIGURE Output Formatting Concept

K.Vigneswara Reddy, Dept. of I.T, SNIST


• Format specifier specifies the data values type to be
printed, size and display position.

• Printf statement takes two arguments

1. control string and 2.data values( variables)


• Control string contains the format specifiers and
some text.
syntax – printf(“control string”,var1,var2,..varn);
• Ex-
int a=10, b=20;
printf(“a=%d b=%d”, a,b);

K.Vigneswara Reddy, Dept. of I.T, SNIST


FIGURE Output Stream Formatting Example

K.Vigneswara Reddy, Dept. of I.T, SNIST


-,+,0 h,l,L

Conversion Specification
K.Vigneswara Reddy, Dept. of I.T, SNIST
Flag Formatting Options
K.Vigneswara Reddy, Dept. of I.T, SNIST
Format Codes for Output
K.Vigneswara Reddy, Dept. of I.T, SNIST
K.Vigneswara Reddy, Dept. of I.T, SNIST
K.Vigneswara Reddy, Dept. of I.T, SNIST
K.Vigneswara Reddy, Dept. of I.T, SNIST
K.Vigneswara Reddy, Dept. of I.T, SNIST
K.Vigneswara Reddy, Dept. of I.T, SNIST
scanf(“control string”,&var1,&var2.. &varn);
control string includes format specifiers and specifies the field width.

FIGURE Formatting Text from an Input Stream

K.Vigneswara Reddy, Dept. of I.T, SNIST


Input Stream Formatting Example
K.Vigneswara Reddy, Dept. of I.T, SNIST
Conversion Specification
K.Vigneswara Reddy, Dept. of I.T, SNIST
K.Vigneswara Reddy, Dept. of I.T, SNIST
Creating and Running Programs
• Creating and running programs takes
place in 4 steps.
1. writing and editing the program.
2. compiling.
3. linking the program with the required
library functions.
4. executing the program.

K.Vigneswara Reddy, Dept. of I.T, SNIST


FIG- Building a C Program
K.Vigneswara Reddy, Dept. of I.T, SNIST
1. writing and editing the program

• Software used to write programs is known as a text


editor, where we can type, edit, store character data.

• Text editor which includes compilers, we complete a


program and save file on to the disk with “.c”
extension. This file is called source file.

K.Vigneswara Reddy, Dept. of I.T, SNIST


2. Compiling program
• The source file on disk must be converted to
machine language using the special software called
compilers. It could complete its task in two steps.
i) preprocessor ii) translator
i) preprocessor- preprocessor reads source file and
checks for special commands known as preprocessor
commands ( instructions which are starts with #
symbols ). The resultant of preprocessor is called
translation unit. Another point about preprocessor
is it processes the source file before compilation.
ii) Translator- translator is a program which converts
the program into machine language and gives the
object modules. This module not yet ready to run
because it not yet include other special functions.
- output of the translator saves the file with .o or
.obj extension filename is same as c program.

K.Vigneswara Reddy, Dept. of I.T, SNIST


3. linking the program with the required library
functions
• After the program is translated into object code, it is ready to
linked with the library functions required in the program.
• Library functions – functions which are already been written and
compiled by the compiler.
• C program made up of different functions in which some
functions can written by programmer and other functions like
input/output processes and mathematical library functions are
included in compiler else where those must be attached to our
program.
• The linker assembles all of these functions and produces the
executable file which is ready to run on the computer.
• Stored with .exe extension.

K.Vigneswara Reddy, Dept. of I.T, SNIST


4. executing the program.

• Now, operating system is responsible to


run the program which uses the run
command.
• It loads the program from the disk to
main memory is called loader.

K.Vigneswara Reddy, Dept. of I.T, SNIST


Writing and Running Programs
#include <stdio.h>
/* The simplest C Program */
int main(int argc, char **argv) 1. Write text of program (source code) using an editor
{
printf(“Hello World\n”); such as TC, save as file e.g. my_program.c
return 0;
}

2. Run the compiler to convert program from source to


an “executable” or “binary”:
ALT + F9 my_program.obj
$ gcc -Wall –g my_program.c –o my_program
tt.c: In function `main':
tt.c:6: parse error before `x'
tt.c:5: parm types given both in parmlist and separately
tt.c:8: `x' undeclared (first use in this function)
tt.c:8: (Each undeclared identifier is reported only once
3. Compiler gives errors and warnings; edit source file,
tt.c:8: for each function it appears in.)
tt.c:10: warning: control reaches end of non-void function
fix it, and re-compile
tt.c: At top level:
tt.c:11: parse error before `return'

4. Run it and see if it works 


CTRL + F9 (ALT + F5)
C:>/my_program
my_program
Hello World What if it doesn’t work?
C:>
K.Vigneswara Reddy, Dept. of I.T, SNIST

You might also like