0% found this document useful (0 votes)
12 views20 pages

Chapter2 3

Chapter 3 covers declarations, input, and output functions in C programming. It explains the importance of declaring constants and variables, the syntax for their declarations, and the built-in functions for input and output, such as printf() and scanf(). Additionally, it includes examples and exercises to illustrate the concepts discussed.

Uploaded by

hanhihd3095
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)
12 views20 pages

Chapter2 3

Chapter 3 covers declarations, input, and output functions in C programming. It explains the importance of declaring constants and variables, the syntax for their declarations, and the built-in functions for input and output, such as printf() and scanf(). Additionally, it includes examples and exercises to illustrate the concepts discussed.

Uploaded by

hanhihd3095
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/ 20

Chapter 3 : Declarations,

Input and Output Functions


Contents

• 3.1. Declarations
• 3.2. Simple input and output functions in C
• 3.3. Other input and output functions

2
3.1. 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.)

3
Constant declarations

• Constants are used to store values that never change during the program
execution.
• Using constants makes programs more readable and maintainable.
Constants can be declared by const declaration.
They can appear inside functions or before the main function
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$

4
Constant declarations using preprocessor commands

A convenient way to associate constant values with


names is using the #define statement.
#define
Examples
#define TRUE 1
#define TABLESIZE 100

5
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.
Syntax:
type list_of_identifiers;

type identifier = expression[,identifier = expression ...];

Examples:
int sum, avg;
int total = 3445, x = 1,
char answer = 'y';
double temperature = -3.14;

6
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
or junk).
• Once a value has been placed in a variable it stays
there until the program deliberately alters it.

7
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

8
3.2.Simple input and output functions 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
• The gets() and puts() Functions
• In <conio.h>
• The getch() and putch() Functions

9
Functions printf, scanf

• Input by using the scanf() function

• Output by using the printf() function

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

10
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

11
How printf function works

12
The % Format Specifiers

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

14
Actions

• 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)

15
Example 1

• In the two-dimensional Cartesian system, a point A is


represented by a pair of numbers (x,y)
• Compute the distance between to points A and B based
on their coordinates

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

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

18
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

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

20

You might also like