Download as pdf or txt
Download as pdf or txt
You are on page 1of 19

Unit 3 : Declarations,

Input and Output Functions


Declarations
• Constants and variables must be declared before they can be
used.
• A constant declaration specifies the type, the name and the
value of the constant.
• A variable declaration specifies the type, the name and
possibly the initial value of the variable.
• When you declare a constant or a variable, the compiler:
1. Reserves a memory location in which to store the value of
the constant or variable.
2. Associates the name of the constant or variable with the
memory location. (You will use this name for referring to
the constant or variable.)

2
Constant declarations
• Constants are used to store values that never change during the
program execution.
• Using constants makes programs more readable and maintainable.
Syntax:
const type identifier = expression;
Examples:
const double US2HK = 7.8;
//Exchange rate of US$ to HK$
const double HK2TW = 3.98;
//Exchange rate of HK$ to TW$
const double US2TW = US2HK * HK2TW;
//Exchange rate of US$ to TW$

3
Variable declarations
• Variables are used to store values that can be changed during
the program execution.
• A variable is best thought of as a container for a value.
3445 y -3.14
Syntax:
< type > < list of identifiers >;
< type > < identifier > = < expression >;
Examples:
int sum;
int total = 3445;
char answer = 'y';
double temperature = -3.14;

4
Variable declarations
• A variable has a type and it can contain only values of that
type. For example, a variable of the type int can only hold
integer values.
• Variables are not automatically initialized. For example, after
declaration
int sum;
the value of the variable sum can be anything (garbage).
• Thus, it is good practice to initialize variables when they are
declared.
• Once a value has been placed in a variable it stays there until
the program deliberately alters it.

5
Constant declarations
A convenient way to associate constant values with
names is using the #define statement.
#define
Examples
#define TRUE 1
#define TABLESIZE 100
Constants can be declared by const declaration.
They appear inside functions.
const data_type const_name = value
const int a=2, b=5;

6
Statements and Blocks
• An expression such as x = 0 or i++ or printf(. . .)
becomes a statement when it is followed by a semicolon.
• Block group any number of data definitions, declarations,
and statements into one statement.
• compound statement group statements into one statement
• Use a blocks or compound statements wherever a single
statement is allowed.
• We use braces {} to build block or compound -
statements

7
Input and output in C
• C programming provides a set of built-in functions
to read the given input and feed it to the program as
per requirement.
• C also provides a set of built-in functions to output
the data on the computer screen as well as to save it
in text or binary files.
• Typical input output functions are
• In <stdio.h>
• The scanf() and printf() Functions
• The getchar() and putchar() Functions
• In <conio.h>
• The getch() and putch() Functions
• The gets() and puts() Functions

8
Functions printf, scanf
• Input by using the scanf() function

• Output by using the printf() function

• It is required to declare the header <stdio.h>

9
The syntax of printf() function
printf(“[string]”[,list of arguments]);
List of arguments : expressions, separated by
commas.
The string is usually called the control string or the
format string.
Action:
◦ Scan the string from left to right
◦ Prints on the screen any characters it encounters -
except when it reaches a % character

10
How printf function works

11
The % Format Specifiers

12
The syntax of scanf() function
scanf(“control string”, list_ of _addresses)

 List of addresses : addresses of variables, separated by commas. Simple


variables have to be passed with a preceding &.
 The format string:
 Has some extra items to cope with the problems of
reading data in
 Specifies how strings of characters
All of the specifiers listed in connection with printf can be used
with scanf.

13
Action:

• Change the values stored in the parts of


memory that is associated with variables.
• Process the control string from left to right
• Each time it reaches a specifier : interpret
what has been typed as a value.
• Input multiple values : separated by white
space (spaces, newline or tabs)

14
Example 1
• In the two-dimensional Cartesian system, a point A
is represented by a pair of numbers (x,y)
• Calculate the distance between to points A and B
based on their coordinates

15
program
1. #include<stdio.h>
2. #include<math.h>
3. main()
4. {
5. float xa,ya,xb,yb,d;
6. printf("\nInput co-ordinates of point A");
7. scanf("%f %f",&xa,&ya);
8. printf("\nInput co-ordinates of point B");
9. scanf("%f %f",&xb,&yb);
10. d= sqrt((xa-xb)*(xa-xb)+(ya-yb)*(ya-yb));
11. printf("\nDistance between A and B:
%1.2f",d);
12. }

16
Other Input and Output Functions
getch
Reads a single character from standard input.
It requires the user to press enter after entering
putch
writes a single character to standard output.
gets
reads a line of input into a character array.
gets(name_ of_ string)
puts
Writes a line of output to standard output.
puts(name of string)
Those functions defined in conio.h header file
17
Exercise
• Write a program to compute area of a triangle, given
its three sizes
• Heron's formula states that the area of
a triangle whose sides have lengths a, b, and c is
where s is the semi perimeter of the triangle; that is

18
Solution
1. #include <stdio.h>
2. #include<math.h>
3. main()
4. {
5. float a,b,c,s,A;
6. puts("Enter three sizes of the
7. triangle, a,b,c:");
8. scanf("%f %f %f", &a,&b,&c);
9. s=(a+b+c)/2;
10. A= sqrt(s*(s-a)*(s-b)*(s-c));
11. printf ("Area of the triangle: %1.2f",A);
12.}

19

You might also like