02 Writing First C Program
02 Writing First C Program
int main(void)
{
return 0; Notice the statement ends
with a semicolon (;).
}
Statements
int main(void)
{
return 0;
}
int main(void)
{
int x, y;
x = 5;
y = x;
return 0;
}
Complete Program
x = 5; Function Body
y = x;
return 0;
}
Complete Program
int main(void)
{
Declarations
int x, y;
x = 5;
y = x; Statements
return 0;
}
Complete Program
int main(void)
{
int x, y;
x = 5; Assignment statements
y = x;
return statement
return 0;
}
Showing Results
The above program will run but it does not show any results.
Everything happens inside the computer’s main memory only.
Example:
Showing Results
Note that
A manipulator is used to format the output
Example: endl causes insertion point to move to the beginning of the
next line.
int main(void)
{
int x, y;
x = 5;
y = x;
cout << "y = " << y;
return 0;
}
Showing Results
The cout object is same as some other functions available
in standard libraries.
cin and cout are declared in the header file iostream, but
within std namespace
#include <iostream>
using namespace std;
Complete Program
#include <iostream> Memory Cells
using namespace std;
x 5
int main(void)
{
int x, y; y 5
x = 5; Computer Screen
y = x;
cout << "y = " << y; y=5
return 0;
}
Showing Results
How do we make the program display the following?
y is 5
cout << "y is " << y;
5 is the value in y.
cout << y << " is the value in y.";
Computation – Arithmetic Operators
How do we make the program do some computation or
calculation?
Use arithmetic operators.
Operator In Maths In C
Add a+b a+b
Subtract a-b a-b
Multiply ab a*b
a
Divide --- or a / b or a ÷ b a/b
b
Modulus a mod b a%b
Variables
Programs store data in memory cells.
int n;
n = 72;
Example:
If miles is a double variable
cin >> miles;
Causes computer to get a value of type double
Places it in the variable miles
Getting Input Data
Using more than one variable in cin allows more than
one value to be read at a time
int main(void)
{
int num;
return 0;
}
Getting Input Data
Computer Screen
Cursor appears and
_ program waits for the
user to enter a number.
int main(void)
Displays a
{
prompt.
int num;
Prompt displayed by
cout object
Getting Input Data
Computer Screen
User types a number and
Enter a number: 91 presses “Enter” key.
num 91
Getting Input Data
Computer Screen
User types a number and
Enter a number: 91 presses “Enter” key.
int main(void)
{
int num, num_power2;
return 0;
}
What does this program do?
Computer Screen
5 num
Enter n: 5
n x n = 25
25 num_power2
What does this program do?
#include <iostream>
using namespace std;
int main(void)
{
int a, b, sum;
sum = a + b;
cout << "a + b = " << sum << endl;
return 0;
}
What does this program do?
Computer Screen a
531
Enter a: 531
Enter b: 24 24 b
a + b = 555
555 sum
What does this program do?
#include <iostream>
using namespace std;
int main(void)
{
int a, b, sum;
sum = a + b;
cout << a << " + " << b << " = " << sum << endl;
return 0;
}
What does this program do?
a 531 b 24 sum ?
What does this program do?
int main(void)
{
int a, b;
a = 3;
b = 5;
cout << "a = " << a << ", b = " << b << endl;
a = b;
b = a;
cout << "a = " << a << ", b = " << b << endl;
return 0;
}
What does this program do?
a = 3;
a 3 b ?
b = 5;
a = b;
b = a;
What does this program do?
a = 3;
a 3 b 5
b = 5;
a = b;
b = a;
What does this program do?
a = 3;
a 5 b 5
b = 5;
a = b;
b = a;
What does this program do?
a = 3;
a 5 b 5
b = 5;
a = b;
b = a;
What does this program do?
#include <iostream>
using namespace std;
int main(void)
{
int a, b, temp;
a = 3;
b = 5;
return 0;
}
What does this program do?
a = 3;
a 3 b ? temp ?
b = 5;
temp = a;
a = b;
b = temp;
What does this program do?
a = 3;
a 3 b 5 temp ?
b = 5;
temp = a;
a = b;
b = temp;
What does this program do?
a = 3;
a 3 b 5 temp 3
b = 5;
temp = a;
a = b;
b = temp;
What does this program do?
a = 3;
a 5 b 5 temp 3
b = 5;
temp = a;
a = b;
b = temp;
What does this program do?
a = 3;
a 5 b 3 temp 3
b = 5;
temp = a;
a = b;
b = temp;