Constants, Input and Output Commands
Constants, Input and Output Commands
Constants, Input and Output Commands
Constants, Input
and Output
Commands
Format: #include<filename>
Example: #include<stdio.h>
Example:
#define PI 3.1416
#define NAME “Julio Jose”
Constants, Input
and Output
Commands
Sample program on input and output
#include<stdio.h>
void main()
{ int age;
age = 0;
age = age + 5;
printf(“\nAge is %d”,age);
}
Constants, Input
and Output
Commands
#include<stdio.h>
void main()
int age;
Constants, Input
and Output
Commands
scanf(“%d”,&age);
age = age +
printf(“\nAge is
Constants, Input
and Output
Commands
Format specifiers in
C
int age;
age = 5;
printf(“\nAge is %d”, age);
Age is
New line
Constants, Input
and Output
Commands
sample = ‘B’;
printf (“The letter is %c.”, sample);
}
OUTPUT:
The letter is M.
A = 14.75;
B = 5.30;
sum = x + y;
printf(“SUM: %f.\n”, sum);
}
OUTPUT:
SUM: 20.05
Constants, Input
and Output
Commands
SAMPLE PROGRAMS
#include <stdio.h>
void main()
{
int X;
int Y;
X = 2
Y = 5;
printf (“Result of 2 * 5 is %d.”, X *
Y);
OUTPUT:
Result of 2 * 5 is
Constants, Input
and Output
Commands
Example:
scanf(“%d”, &num);
General Syntax:
scanf(“Format_specifier”,&variable);
Constants, Input
and Output
Commands
scanf(“Format_specifier”,&variable);
#include
<stdio.h> void
main()
{
float dollars, pesos;
#include <stdio.h>
void main()
{
float radius, area, pi = 3.14;
OUTPUT:
Enter the radius of the circle: 2.0
(enter)
#include <stdio.h>
main()
{
char first, middle, last;
int age;
OUTPUT:
24.
Constants, Input
and Output
Commands
Example:
+ 0 1 0 1
0 0 0 1 1
- 1 0 1 0
Examples:
Assume that x = 5, y = 3, z = 10
x+y<=z true
z/x>y false
x + y >= z - y true
y * z / x <= x + 1 true
z>x *y false
Logical and
Relational
Operators in C
EQUALITY OPERATORS AND EXPRESSIONS
= = equal
! = not equal
Examples:
Examples:
p*q==0 is equivalent to (p * q) = =
0 m != n - 7 is equivalent to m != (n - 7)
&& and
| or (double
| pipe) not
Logical and
Relational
Operators in C
LOGICAL OPERATORS AND EXPRESSIONS
The operands of logical operators are usually relational
or equality expressions.
Examples:
(w = = x) || (y <= z)
checks if w is equal to x OR if y
is less than or equal to z
! (p < = q)
0 0 1 1 0 0
0 1 1 0 0 1
1 0 0 1 0 1
1 1 0 0 1 1
Logical and
Relational
Operators in C
Example:
Example:
Assume that x = 6, y = 2, z = 20
y = = z || x ! = z tru
Examples:
b = y = = z || x ! = z
Then:
a = 0 b = 1
c = 1 d = 0
e = 1 f=0
IF st at ement
is: if (expression)
statement
Example:
Example:
if (expression)
statement1;
else
statement2;
IF st at ement
Example:
Example:
Nested if
Example
if (x > 5)
if (x < 10)
printf (“x is between 5 and
IF st at ement
Nested if statement
Nested if statement
Example:
DANGLING else
if (x = = 2)
if (y = = 3)
print
(“Julio\n”);
else
DANGLING else
if (x = = 2)
if (y = = 3)
print (“Julio\n”);
else
print (“Jose\n”);
IF st at ement
PROGRAMMING EXERCISE
Page 39 of 98
Computer Programming 1
Introduction to C Programming
Page 40 of 98
IF st at ement
Solution:
void main()
{
float num1, num2, quotient;
if (num2 != 0) {
quotient = num1 / num2;
printf(“\n%.2f divided by %.2f is %.2f.”, num1, num2,
quotient);
}
else {
printf (“\nThe division cannot take place!”);
printf (“\nThe divisor is 0.”);
}