0% found this document useful (0 votes)
3 views46 pages

Lecture #4

The document serves as a programming introduction, specifically focusing on input/output statements in C language, including the use of printf and scanf functions. It explains formatting outputs, type casting, and provides examples of unformatted and formatted outputs. Additionally, it covers implicit and explicit type conversions, emphasizing the importance of using the cast operator for accurate data type management.
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)
3 views46 pages

Lecture #4

The document serves as a programming introduction, specifically focusing on input/output statements in C language, including the use of printf and scanf functions. It explains formatting outputs, type casting, and provides examples of unformatted and formatted outputs. Additionally, it covers implicit and explicit type conversions, emphasizing the importance of using the cast operator for accurate data type management.
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/ 46

Mühendi sl i k ve Mi marl ı k Fakül tesi

BİL1102
Programlamaya Giriş
Ders #3

2024 Güz
Content

• Input/Output Statements
• Unformatted/Formatted Outputs
• Type Casting
Input and Output statement
• The input statement in C language is scanf,
• where the output statement in C language is printf.
• Both are functions supplied as part of the C standard input/output library to
which we gain access through the preprocessor directive
• #include <stdio.h>
printf ()
• printf () displays the format string on the screen.
• printf () is used to write formatted output to console
printf ()
• Syntax:

printf(format string);
printf(format string, variable list);
• Format string is enclosed within double quotes and may contain messages
that we want to display
printf ()
• If you want to display it on a new line, you should put \n either at the
beginning of the format string in the second printf statement, or at the end of
the format string in the first printf statement.
Placeholders
• When we want to display the contents of variables, we must use
placeholders in the format string.
• A placeholder is a symbol beginning with % in a format string that indicates
where and how to display the value of a variable.
• The placeholder we should use depends on the type of the variable

Type Placeholder for printf


int %d
double %f
char %c
Placeholders
• If the variable list contains more than one variable, the format string should
contain the same number of placeholders. C matches the variables with
placeholders in left-to-right order.
scanf()
• Scanf takes input from the standard input device (keyboard) and puts it into a
variable.
• Syntax:
scanf(format string, addresses of variables);
• Format string contains some placeholders depending on the data type of the
input.
Example of printf and scanf usage
Address of operator &
• If the scanf statement contains addresses of more
than one variable, the user must enter data in the
same order as the variables in the list.
scanf("%d%lf%d", &a, &b, &c);
• Here, the user should enter three numbers
separated by blanks: first an integer, then a double,
lastly an integer, e.g.: 15 3.5 765.

• If the placeholder and the data type of the


corresponding variable do not match, it will cause
an error.
Unformatted Output Examples

12
Example #1: C Output

#include <stdio.h>
//This is needed to run printf() function.
int main()
{
printf("C Programming");
//displays the content inside quotation
return 0;
}

Output on the screen:

C Programming

13
Example #2: C Integer Output

#include <stdio.h>
int main()
{
int testInteger = 5;
printf("Number = %d", testInteger);
return 0;
}

Output on the screen:

Number = 5

14
How this program works?

#include <stdio.h>
int main()
{
int testInteger = 5;
printf("Number = %d", testInteger);
return 0;
}

• Inside the quotation of printf() function, there is a format string


(place holder) "%d" (for integer).
• If the format string matches the argument (testInteger in this case),
it is displayed on the screen. 15
16
17
18
Little bit on ASCII code

When a character is entered in the above program, the character itself is not stored.
Instead, a numeric value(ASCII value) is stored.

And when we displayed that value using "%c" text format, the entered character is
displayed.
19
Example #5: C ASCII Code
#include <stdio.h>
int main()
{
char chr;
printf("Enter a character: ");
scanf("%c",&chr);

// When %c text format is used, character is displayed in case of character


types
printf("You entered %c.\n",chr);

// When %d text format is used, integer is displayed in case of character types


printf("ASCII value of %c is %d.", chr, chr);
return 0;
}
Output on the screen:
Enter a character: g
You entered g.
ASCII value of g is 103.
20
The ASCII value of character 'g' is 103. When, 'g' is entered, 103 is stored in variable var1 instead of g.
21
Formatting output
• C displays all numbers in its default notation unless we
instruct it to do otherwise. We need to format our output in
order to control the appearance of it.
• Normally, %d displays an integer value as it is, without any
leading blanks. %f displays a double value with no leading
blanks and with 6 decimal places.
• To specify the format of an integer value, we just need to
put an integer number between % and d of the %d
placeholder in the printf format string.
• Thus, as %nd, where n is the field width (number of
positions to be reserved for the integer on the screen).
• If n is positive, the integer value will be displayed right
justified; if n is negative, it will be left justified.
Formatting output
Formatting output

Exercise

Find the output of the following code

int num = -8;


printf("%d%4d\n", num, num * 2);
printf("%-4d%2d\n", num / 3, num + num / 2);
Formatting output

Exercise

Find the output of the following code

int num = -8;


printf("%d%4d\n", num, num * 2);
printf("%-4d%2d\n", num / 3, num + num / 2);
Formatting double variables
• To specify the format of a double value, we need to put a decimal
number between % and f of the %f placeholder in the printf format
string.
• Thus, as %n.mf, where
• n is the total number of positions including the decimal point
• m is the number of digits after the decimal point

• If n > 0, the number will be displayed right justified;


If n < 0, it will be left justified.
• If n = 0 or less than the minimum field width necessary to display the
number, the value will be printed with no leading blanks.
• If m < digits required, the fractional part is rounded to fit m.
• If m > digits required, extra zeroes are appended.
• If m = 0, the closest integer value will be printed.
Formatting double variables
Exercise : Find the output of the following code
Formatting double variables
Formatting double variables
Formatting characters
It is also possible to specify the format of a character
value. It is just the same as integer formatting. We need
to put an integer number between % and c of the %c
placeholder in the printf format string. Thus, as %nc,
where n is again the field width.
Formatting characters
It is also possible to specify the format of a character
value. It is just the same as integer formatting. We need
to put an integer number between % and c of the %c
placeholder in the printf format string. Thus, as %nc,
where n is again the field width.
Formatted Output Examples

32
Example #1

Output Screen:

33
Example #2

Output Screen:

34
Example #3

Output Screen:

35
Type Casting
Type Casting
• Type casting is a way to convert a variable
from one data type to another data type.

• For example, if you want to store a ‘FLOAT'


value into a simple INTEGER then you can
type cast ‘float' to 'int'.

• Two types: Implicit & Explicit


Implicit Type Casting
• Implicit Type Conversion (Casting): Also known as ‘automatic
type conversion’.
• Done by the compiler on its own, without any external trigger
from the user.
• Generally takes place when in an expression more than one
data type is present. In such condition, type conversion (type
promotion) takes place to avoid lose of data.
• All the data types of the variables are upgraded to the data type
of the variable with largest data type.
• One-way conversion!
bool -> char -> int -> float -> double
• It is possible for implicit conversions to lose information, signs
can be lost (when signed is implicitly converted to unsigned),
and overflow can occur.
Implicit Type Casting
• One-way conversion!
bool -> char -> int -> float -> double

• However, gcc compiler can execute type casting in both ways!


• Examine the following example.
Explicit Type Casting
• Explicit Type Conversion (Casting) This process is also called type casting
and it is user defined.
• Here the user can type cast the result to make it of a particular data type.
• The syntax in C:
(type) expression
• Type indicated the data type to which the final result is converted.
Type Casting
• You can convert the values from one type to
another explicitly using the cast operator
as follows −

• (type_name) expression
Type Casting
• Consider the following example where the cast
operator causes the division of one integer variable by
another integer to be performed as a floating-point
operation
#include <stdio.h>
main()
{
int sum = 17, count = 5;
double mean;
mean = (double) sum / count;
printf("Value of mean : %f\n", mean );
}

• Output:

Value of mean : 3.400000


Type Casting
• It should be noted that:

the cast operator has precedence over division,

so the value of sum is first converted to type double


and finally it gets divided by integer count yielding a
double value.

#include <stdio.h>
main()
{
int sum = 17, count = 5;
double mean;
mean = (double) sum / count;
printf("Value of mean : %f\n", mean );
}
Type Casting
• Type conversions can be implicit which is performed
by the compiler automatically, or it can be specified
explicitly through the use of the cast operator. It is
considered good programming practice to use the cast
operator whenever type conversions are necessary.

Explicit Implicit

#include <stdio.h> #include <stdio.h>


main() main()
{ {
int sum = 17, count = 5; int sum = 17;
double mean; double count = 5, mean;
mean = (double) sum / count; mean = sum / count;
printf("Value of mean : %f\n", mean ); printf("Value of mean : %f\n", mean );
} }
Type Casting
• Type conversions can be implicit or explicit through the use of the cast
operator.

Explicit Implicit

# include <stdio.h> # include <stdio.h>


main() main()
{ {
Bothint
outputs are the same: WHY?
i = 17; int i = 17;
char c = 'c'; /* ascii value is 99 */ char c = 'c'; /* ascii value is 99 */
float sum; float sum;
sum = (float) i + (int) c; sum = i + c;
printf("Value of sum : %f\n", sum ); printf("Value of sum : %f\n", sum );
} }

Value of sum : 116.000000


Implicit Type Casting
How it works:
• first c gets converted to integer,
• but as the final value is double, usual arithmetic
conversion applies and
• the compiler converts i and c into 'float' and
• adds them yielding a 'float' result.

# include <stdio.h>
main()
{
int i = 17;
char c = 'c'; /* ascii value is 99 */
float sum;
sum = i + c;
printf("Value of sum : %f\n", sum );
} Value of sum : 116.000000

You might also like