Lecture #4
Lecture #4
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
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;
}
C Programming
13
Example #2: C Integer Output
#include <stdio.h>
int main()
{
int testInteger = 5;
printf("Number = %d", testInteger);
return 0;
}
Number = 5
14
How this program works?
#include <stdio.h>
int main()
{
int testInteger = 5;
printf("Number = %d", testInteger);
return 0;
}
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);
Exercise
Exercise
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.
• (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:
#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
Explicit Implicit
# 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