0% found this document useful (0 votes)
22 views50 pages

Csir11-Introduction To Computer Programming: Unit Ii

This document discusses operators and expressions in C programming, including relational, logical, assignment, and conditional operators. It also covers library functions, with examples of commonly used math, string, and input/output functions. Finally, it describes the getchar and putchar functions for single character input and output in C.

Uploaded by

bimbusbong
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
22 views50 pages

Csir11-Introduction To Computer Programming: Unit Ii

This document discusses operators and expressions in C programming, including relational, logical, assignment, and conditional operators. It also covers library functions, with examples of commonly used math, string, and input/output functions. Finally, it describes the getchar and putchar functions for single character input and output in C.

Uploaded by

bimbusbong
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 50

CSIR11-INTRODUCTION TO

COMPUTER PROGRAMMING
UNIT II
Operators(cont..)- Library Functions- Data input and output: Single character input and
output- Entering input data- Writing output data- gets and puts functions

DATE: 12/09/2023
Operators and Expressions
• RELATIONAL AND LOGICAL OPERATORS
Operators and Expressions
• These operators all fall within the same precedence group, which is
lower than the arithmetic and unary operators. The associativity of
these operators is left to right.
• Closely associated with the relational operators are the following two
equality operators
Operators and Expressions
• Suppose that i, j and k are integer variables whose values are 1, 2 and 3, respectively.
Operators and Expressions
• The equality operators fall into a separate precedence group, beneath
the relational operators. These operators also have a left-to-right
associativity.
• These six operators are used to form logical expressions, which
represent conditions that are either true or false. The resulting
expressions will be of type integer, since true is represented by the
integer value 1 and false is represented by the value 0.
Operators and Expressions
• In addition to the relational and equality operators, C contains two logical
operators (also called logical connectives).

• The logical operators act upon operands that are themselves logical
expressions.
• the result of a logical or operation will be true if either operand is true or if
both operands are true. In other words, the result of a logical or operation will
be false only if both operands are false.
Operators and Expressions

• Suppose that i is an integer variable whose value is 7, f is a floating-point


variable whose value is 5.5, and c is a character variable that represents the
character ’w ' .
Operators and Expressions
• Each of the logical operators falls into its own precedence group. Logical and
has a higher precedence than logical or. Both precedence groups are lower than
the group containing the equality operators. The associativity is left to right.
• C also includes the unary operator ! that negates the value of a logical
expression; i.e., it causes an expression that is originally true to become false,
and vice versa. This operator is referred to as the logical negation (or logical
not) operator.
• Complex logical expressions that consist of individual logical expressions joined
together by the logical operators && and I I are evaluated left to right, but only
until the overall true/false value has been established. Thus, a complex logical
expression will not be evaluated in its entirety if its value can be established
from its constituent operands.
Operators and Expressions
• Example:

• If error > .0001 is false, then the second operand (i.e., count < 100)
will not be evaluated, because the entire expression will be
considered false.
Operators and Expressions
• ASSIGNMENT OPERATORS
• The most commonly used assignment operator is =.
• identifier = expression
• where identifier generally represents a variable, and expression represents a
constant, a variable or a more complex expression.
• Remember that the assignment operator = and the equality operator == are
distinctly different
• If the two operands in an assignment expression are of different data types,
then the value of the expression on the right (i.e., the right-hand operand)
will automatically be converted to the type of the identifier on the left.
Operators and Expressions
• Under some circumstances, this automatic type conversion can result
in an alteration of the data being assigned.
• For example:
• A floating-point value may be truncated if assigned to an integer identifier.
• A double-precision value may be rounded if assigned to a floating-point
(single-precision) identifier.
• An integer quantity may be altered if assigned to a shorter integer identifier
or to a character identifier (some high-order bits may be lost).
• Moreover, the value of a character constant assigned to a numeric-type
identifier will be dependent upon the particular character set in use.
Operators and Expressions
• Now suppose that i and j are both integer-type variables, and that j
has been assigned a value of 5
Operators and Expressions
• assume that i is an integer-type variable, and that the ASCII character
set applies.
Operators and Expressions
• Multiple assignments of the form identifier 1 = identifier 2 = ... =
expression are permissible in C. In such situations, the assignments
are carried out from right to left.
• Suppose that i and j are integer variables.
• i = j = 5.9
• C contains the following five additional assignment operators: +=, -= ,
*=, /= and %=
• expression 1 += expression 2 is equivalent to
• expression 1 = expression 1 + expression 2
Operators and Expressions
• THE CONDITIONAL OPERATOR
• conditional operator (? :)
• An expression that makes use of the conditional operator is called a
conditional expression. Such an expression can be written in place of
the more traditional if -else statement.
• expression 1 ? expression 2 : expression 3
• If expression 1 is true then expression 2 is evaluated and this becomes
the value of the conditional expression. However, if expression 1 is
false, then expression 3 is evaluated and this becomes the value of
the conditional expression.
Operators and Expressions
• (i < 0) ? 0 : 100
• If the operands (i.e., expression 2 and expression 3) differ in type, then the
resulting data type of the conditional expression will be determined by the rules
already discussed.
• Now suppose that i is an integer variable, and f and g are floating-point variables.
• The conditional expression (f < g) ? i : g
• involves both integer and floating-point operands. Thus, the resulting expression
will be floating-point, even if the value of i is selected as the value of the
expression
• Conditional expressions frequently appear on the right-hand side of a simple
assignment statement.
Operators and Expressions
• Operator Precedence Groups
LIBRARY FUNCTIONS
• Library functions that are functionally similar are usually grouped
together as (compiled) object programs in separate library files.
• A library function is accessed simply by writing the function name,
followed by a list of arguments that represent information being
passed to the function.
• The arguments must be enclosed in parentheses and separated by
commas. The arguments can be constants, variable names, or more
complex expressions. The parentheses must be present, even if there
are no arguments.
LIBRARY FUNCTIONS
• A function that returns a data item can appear anywhere within an
expression, in place of a constant or an identifier (i.e., in place of a
variable or an array element).
• A function that carries out operations on data items but does not
return anything can be accessed simply by writing the function name,
since this type of function reference constitutes an expression
statement.
LIBRARY FUNCTIONS
LIBRARY FUNCTIONS
LIBRARY FUNCTIONS
• Note: Type refers to the data type of the quantity that is returned by the
function
• c denotes a character-type argument
• i denotes an integer argument
• d denotes a double-precision argument
• u denotes an unsigned integer argument the function.
• In order to use a library function it may be necessary to include certain
specific information within the main portion of the program.
• This information is generally stored in special files which are supplied with the
compiler. Thus, the required information can be obtained simply by accessing
these special files. This is accomplished with the preprocessor statement
#include; i.e. #include <filename>
LIBRARY FUNCTIONS
• Certain commonly used file names such as stdio. h, stdlib. h and math.
h.
• The suffix “h” generally designates a “header” file, which indicates
that it is to be included at the beginning of the program.
• Note the similarity between the preprocessor statement #include and
the preprocessor statement #define.
Data Input and Output
• A number of input/output functions, six of these functions: getchar,
putchar, scanf, printf, gets and puts.
• These six fuctions permit the transfer of information between the
computer and the standard inputloutput devices (e.g., a keyboard and
a TV monitor).
• The first two functions, getchar and putchar, allow single characters
to be transferred into and out of the computer;
• scanf and printf are the most complicated, but they permit the
transfer of single characters, numerical values and strings;
• gets and puts facilitate the input and output of strings.
Data Input and Output
• SINGLE CHARACTER INPUT -THE getchar FUNCTION
• It returns a single character from a standard input device (typically a
keyboard). The function does not require any arguments, though a pair of
empty parentheses must follow the word getchar.
• character variable = getchar ( ) ;
• where character variable refers to some previously declared character
variable.
• If an end-of-file condition is encountered when reading a character with the
getchar function, the value of the symbolic constant EOF will automatically be
returned. (This value will be assigned within the stdio .h file. Typically, EOF will
be assigned the value -1, though this may vary from one compiler to another.)
Data Input and Output
• SINGLE CHARACTER OUTPUT -THE putchar FUNCTION
• Single characters can be displayed (i.e, written out of the computer) using the
C library function putchar.
• The putchar function, like getchar, is a part of the standard C I/O library. It
transmits a single character to a standard output device (typically a TV
monitor). The character being transmitted will normally be represented as a
character-type variable. It must be expressed as an argument to the function,
enclosed in parentheses, following the word putchar.
• putchar ( character variable)
• where character variable refers to some previously declared character
variable.
Data Input and Output
Data Input and Output
• ENTERING INPUT DATA -THE scanf FUNCTION
• Input data can be entered into the computer from a standard input device by means
of the C library function scanf.
• This function can be used to enter any combination of numerical values, single
characters and strings.
• The function returns the number of data items that have been entered successfully.
• scanf(control string, arg1, arg2, . . . , argn)
• where control string refers to a string containing certain required formatting
information, and argl, arg2, . . . argn are arguments that represent the individual
input data items.
• (Actually, the arguments represent pointers that indicate the addresses of the data
items within the computer's memory. )
Data Input and Output
• The control string consists of individual groups of characters, with one
character group for each input data item. Each character group must
begin with a percent sign (%). In its simplest form, a single character
group will consist of the percent sign, followed by a conversion
character which indicates the type of the corresponding data item.
• Within the control string, multiple character groups can be
contiguous, or they can be separated by whitespace characters (i.e.,
blank spaces, tabs or newline characters). If whitespace characters
are used to separate multiple character groups in the control string,
then all consecutive whitespace characters in the input data will be
read but ignored.
Data Input and Output
• Commonly Used Conversion Characters for Data Input
Data Input and Output
• The arguments are written as variables or arrays, whose types match
the corresponding character groups in the control string. Each
variable name must be preceded by an ampersand (&).
• However, array names should not begin with an ampersand.
Data Input and Output
Data Input and Output
• The data items must correspond to the arguments in the scanf function in
number, in type and in order.
• Numeric data items are written in the same form as numeric constants, though
octal values need not be preceded by a 0,and hexadecimal values need not be
preceded by 0x or 0X.
• Floating-point values must include either a decimal point or an exponent (or
both).
• If two or more data items are entered, they must be separated by whitespace
characters. (A possible exception to this rule occurs with c-type conversions)
• The data items may continue onto two or more lines, since the newline character
is considered to be a whitespace character and can therefore separate
consecutive data items.
Data Input and Output
• Moreover, if the control string begins by reading a character-type data
item, it is generally a good idea to precede the first conversion
character with a blank space.
• This causes the scanf function to ignore any extraneous characters
that may have been entered earlier (for example, by pressing the
Enter key after entering a previous line of data).
Data Input and Output
• Notice the blank space that precedes %s.
Data Input and Output
• Unrecognized characters within the control string are expected to be
matched by the same characters in the input data. Such input
characters will be read into the computer, but not assigned to an
identifier. Execution of the scanf function will terminate if a match is
not found.
Data Input and Output

If the input data consist of


1 a 2.0
then the decimal integer 1 will be read in and assigned
to i, the character a will be read in but subsequently
ignored, and the floating-point value 2.0 will be read in
and assigned to x.

On the other hand, if the input were entered simply as


1 2.0
then the scanf function would stop executing once the
expected character (a) is not found. Therefore, i would
be assigned the value 1 but x would automatically
represent the value 0.
Data Input and Output
• WRITING OUTPUT DATA -THE printf FUNCTION
• This function can be used to output any combination of numerical values,
single characters and strings.
• It is similar to the input function scanf, except that its purpose is to display
data rather than to enter it into the computer.
• That is, the printf function moves data from the computer’s memory to the
standard output device, whereas the scanf function enters data from the
standard input device and stores it in the computer’s memory.
• printf(control string, arg7, arg2, . . . , argn)
• where control string refers to a string that contains formatting information,
and arg1, arg2, . . . , argn are arguments that represent the individual output
data items.
Data Input and Output
• The arguments can be written as constants, single variable or array
names, or more complex expressions. Function references may also
be included. In contrast to the scanf function discussed in the last
section, the arguments in a printf function do not represent memory
addresses and therefore are not preceded by ampersands.
• The control string consists of individual groups of characters, with one
character group for each output data item. Each character group must
begin with a percent sign (%). In its simplest form, an individual
character group will consist of the percent sign, followed by a
conversion character indicating the type of the corresponding data
item.
Data Input and Output
• Commonly Used Conversion Characters for Data Output
Data Input and Output

• Notice that the first two arguments within the printf function are single variables, the third argument is
an arithmetic expression, and the last argument is a function reference that has a numeric expression as
an argument.

Output:
2.000000 3.000000 5.000000 2.236068
Data Input and Output
• Note:
• The printf function interprets s-type conversion differently than the
scanf function. In the printf function, s-type conversion is used to
output a string that is terminated by the null character (\ 0).
Whitespace characters may be included within the string.
Data Input and Output

• Notice the difference in the control strings within the scanf function and the printf function.
• Now suppose that the following string is entered from the standard input device when the program is
executed.
The PITTSBURGH STEELERS is one of America's favorite football teams!
• This string contains lowercase characters, uppercase characters, punctuation characters and whitespace
characters. The entire string can be entered with the single scanf function, as long as it is terminated by a
newline character (by pressing the Enter key).
• The printf function will then cause the entire string to be displayed on the standard output device, just as it
had been entered.
Data Input and Output
• A minimum field width can be specified by preceding the conversion
character by an unsigned integer.
• If the number of characters in the corresponding data item is less
than the specified field width, then the data item will be preceded by
enough leading blanks to fill the specified field.
• If the number of characters in the data item exceeds the specified
field width, however, then additional space will be allocated to the
data item, so that the entire data item will be displayed.
• This is just the opposite of the field width indicator in the scanf
function, which specifies a maximum field width.
Data Input and Output
#include <stdio.h>
main ( ) /* minimum field width specifications */ .
{
int i = 12345;
printf ("%3d %5d %8d\n\n", i, i, i);
}

Output:
12345 12345 12345
Homework
• MORE ABOUT THE printf FUNCTION (Read from book)
• e-type conversion
• f-type conversion
• Commonly used flags…..
Data Input and Output
• THE gets AND puts FUNCTIONS
• Each of these functions accepts a single argument. The argument
must be a data item that represents a string. (e.g., a character array).
• The string may include whitespace characters. In the case of gets, the
string will be entered from the keyboard, and will terminate with a
newline character (i.e., the string will end when the user presses the
Enter key).
• The gets and puts functions offer simple alternatives to the use of
scanf and printf for reading and displaying strings
Data Input and Output
INTERACTIVE (CONVERSATIONAL)
PROGRAMMING
• In C, such dialogs can be created by alternate use of the scanf and
printf functions.

Please enter your name: Robert Smith


Please enter the first score: 88
Please enter the second score: 62.3
Thank you

You might also like