Sei sulla pagina 1di 76

Topic 2

Writing First C Program

Smallest C Program

A C program consists of one or more blocks of code called functions. Minimum number of functions in a program is one.

The name of one of the functions must be main.


The main function is where program execution begins.

Smallest C Program

When a C program runs, it gives back a result to indicate whether program execution is successful.

0 (zero) shows successful execution. Non-zero shows there was some problem.

Data Type int and Constant

The numbers (0,1,2, etc.) are integers i.e. whole numbers which do not have decimal point. Integer values are one category of constants i.e. values that appear in a program. In C, this type of numbers belong to a group called int i.e. this type of data or this data type is called int type.

The main Function

Smallest C program has these lines:

int main(void) { return 0; }

Function header marks the beginning of a function.


Indicates that the function name is main and the function gives back an integer type value (int) and the function does not need any information (void).

The main Function

Smallest C program has these lines:

int main(void) { return 0; }

Opening curly brace indicates the start of the body of the function. Closing curly brace indicates the end of the body of the function.

The main Function

Smallest C program has these lines:

int main(void) { return 0; }

The return statement terminates the execution of the function and returns control to the operating system. It also gives back the functions result.

Zero indicates the program executed without error.

Statements

A statement causes an action to be performed by the program. It translates directly into one or more machine language instructions.

Statements

int main(void) { return 0; }

Notice the statement ends with a semicolon (;).

Statements

int main(void) { return 0; }

This program can run but it doesnt do anything.

Input-Process-Output

The simplest program that does some useful work should: get some input data, process that data, and produce some output result.

Input

Process

Output

Working with Data

A program usually processes data.

But how do we store the data?

In memory cells

How does the program refer to these memory cells?


By giving names to the memory cells. Example names are x, y, number, n1, n2.

Declaration Statement

Before we can use a memory cell in the program, we need to tell the compiler the name and the type of data it will store. We do this using a declaration.

Example:

int x;

Declaration that tells the compiler the name of the memory cell is x and the type of data it will store is integer.

Declaration Statement

We can declare more than one name in one declaration.

Example:

int x, y, z;

Tells compiler the program uses 3 memory cells with the names x, y and z to store integer values.
Notice the names are separated by commas (,).

Assignment Statement

How do we tell the program to store a value in the memory cell?

By writing an assignment statement. Example: Assignment statement that tells the program to store/assign x = 5; the value 5 in the memory cell with the name x.

= is the assignment operator. It does not mean equal.

Assignment Statement

How do we tell the program to store a value in the memory cell?

By writing an assignment statement. Example: Think of it like this: x 5 x = 5; which you can read as: put the value 5 in the memory cell with the name x or simply: assign 5 to x or x becomes 5.

Assignment Statement

How do we tell the program to store a value in the memory cell?

By writing an assignment statement. Example:

x = 5;

We can describe this in picture form as: x 5

Assignment Statement

How do we tell the program to copy a value from one memory cell to another?

Use assignment statement. Example:

x = 5; y = x;

Assignment Statement

How do we tell the program to copy a value from one memory cell to another?

Use assignment statement Example:

5 Copy value of x into y

x = 5; y = x;
y

Assignment Statement

How do we tell the program to copy a value from one memory cell to another?

Use assignment statement Example:

5 Copy value of x into y

x = 5; y = x;
y 5

Complete Program
int main(void) { int x; int y; x = 5; y = x;

return 0;
}

Complete Program
int main(void) { int x; int y; x = 5; y = x;
Function Header

Function Body

return 0;
}

Complete Program
int main(void) { int x; int y; x = 5; y = x;

Declarations

Statements

return 0;
}

Complete Program
int main(void) { int x; int y; x = 5; y = x;
Assignment statements

return 0;
}

return statement

Showing Results

The above program will run but it does not show any results. Everything happens inside the computers main memory only.

How do we make the program display the values stored in the memory cells?

By asking a special function to do it i.e. calling or activating a function to display the values, just like you call your friend to do something for you. The name of the function is printf.

Showing Results

How do we tell function printf what to display?

By passing it two pieces of information: the value to display the format to display the value

Example:

printf("Y=%d", y);
The name of function to call Parentheses surrounding the information to pass to the function

Showing Results

How do we tell function printf what to display?

By passing it two pieces of information: the value to display the format to display the value

Example:

printf("Y=%d", y);
Indicates that we want to display the value of y.

Showing Results

How do we tell function printf what to display?

By passing it two pieces of information. the value to display the format to display the value

Example:

printf("Y=%d", y);
This is a string i.e. a sequence of characters. A string is always surrounded by double quotes ().

Showing Results

How do we tell function printf what to display?

By passing it two pieces of information: the value to display the format to display the value

Example:

printf("Y=%d", y);
Here the string is called a format string. It tells the printf function how we want to display the value.

Showing Results

How do we tell function printf what to display?

By passing it two pieces of information: the value to display the format to display the value

Example:

printf("Y=%d", y);
We are saying we want the output to be Y= followed by the value of y. The %d is a placeholder that marks the position to display an integer type value.

Program Now
int main(void) { int x; int y;

x = 5; y = x; printf("Y=%d", y);
return 0; }

Showing Results

The printf function is one of many functions available in standard libraries. A library is a collection of functions. Each library has a standard header file whose name ends with .h which contains information about the functions. The information for the printf function is in a file called stdio.h.

Showing Results

For the program to use printf function, this information must be inserted into the program. To do this , we add this line in our program: #include <stdio.h> This is called a preprocessor directive.

Showing Results

A preprocessor or precompiler processes the text of a program before it is compiled. The #include <stdio.h> directive notifies the preprocessor that some names used in the program (such as printf) are defined in the file stdio.h.

std standard io input/output h header file

Complete Program
#include <stdio.h> int main(void) { int x; int y; x = 5; y = x; printf("Y=%d", y); return 0; }
Memory Cells

x
y

5
5

Computer Screen Y=5

Showing Results

How do we make the program display the following?

Y is 5

printf("Y is %d", y);

5 is the value in y.

printf("%d is the value in y.", y);

What is the output of this program?


#include <stdio.h> int main(void) { int a, b;

a = 475; b = a; printf("a=%d", a); printf(" b=%d", b);


return 0; }

Showing Results
Computer Screen

a=475 b=475

How can we get the two values displayed on two separate lines?

Use a special character sequence (\n) which represents a newline escape sequence to display the output on a new line. This makes the cursor (position) on the screen move down to a new line.

Showing Results

Example:

printf("a=%d\n", a); printf("b=%d\n", b);


Computer Screen
a=475 b=475 The cursor is now on the third line

Showing Results

What is the output of the following?

printf("a=%d", a); printf("\nb=%d\n", b);

Showing Results

What is the output of the following? Same as above

printf("a=%d", a); printf("\nb=%d\n", b);

Can we do this?

printf("a=%d\nb=%d\n", a, b);

Showing Results

What is the output of the following? Same as above

printf("a=%d", a); printf("\nb=%d\n", b);

Can we do this? Yes

printf("a=%d\nb=%d\n", a, b);
Placeholder for a Placeholder for b

Computation Arithmetic Operators

How do we make the program do some computation or calculation?

Use arithmetic operators.

Operator Add Subtract Multiply Divide Modulus

In Maths a+b a-b ab a --- or a / b or a b b a mod b

In C a+b a-b a*b a/b a%b

Variables

Programs store data in memory cells.

A memory cell has a name, a data type, a value, and an address. In programming, these memory cells are called variables because the value in the memory cell can change.

Variable Declaration and Assignment


int n;
n = 72;
Variable declaration tells compiler the name and data type of the variable (i.e. memory cell). Assigns value 72 to variable n (i.e. memory cell with name n).

Variable Declaration and Assignment


int n;
n = 72;

Only variables are allowed on the left-hand side of an assignment statement.

Getting Input Data

How do we make a program input the data from the program user in order to do computation?

By asking a special function to do it. The name of the function is scanf.

After we get the data, where do we store it?

In a variable

Getting Input Data

How do we tell function scanf where to store the data?

By passing it two pieces of information: the address of variable we want to store the data the type of the input data

Example:

scanf("%d", &num);
Indicates we want to store the data in variable num.

Getting Input Data

How do we tell function scanf where to store the data?

By passing it two pieces of information: the address of variable we want to store the data the type of the input data

Example:

scanf("%d", &num);
& (ampersand character) is the address-of operator that gives the address of the variable

Getting Input Data

How do we tell function scanf where to store the data?

By passing it two pieces of information: the address of variable we want to store the data the type of the input data

Example:

scanf("%d", &num);
The format string indicates that the data type is integer.

Getting Input Data

Just like for the function printf, the stdio.h file provides additional information about the function scanf . We need to add the preprocessor directive in our program: #include <stdio.h>

Complete Program
#include <stdio.h> int main(void) { int num;

scanf("%d", &num);
return 0;

Getting Input Data


Computer Screen Cursor appears and program waits for the user to enter a number.

Better to let the user know that the program is expecting some data. How do we do this?

We display a prompt using printf function

Complete Program
#include <stdio.h> int main(void) { int num;

Displays a prompt.

printf("Enter a number: "); scanf("%d", &num); return 0;


}
Only format string in parentheses

Getting Input Data


Computer Screen Cursor appears and program waits for the user to enter a number

Enter a number: _

Prompt displayed by printf function

Getting Input Data


Computer Screen User types a number and presses Enter key. The input data is stored as an integer in the variable num. num

Enter a number: 91

91

Getting Input Data


Computer Screen User types a number and presses Enter key. The input data is stored as an integer in the variable num. num

Enter a number: 91

Note: the underline is used to show that this is data entered by the user.

91

What does this program do?


#include <stdio.h> int main(void) { int num, numPower2;

printf("Enter n: "); scanf("%d", &num); numPower2 = num * num; printf("n x n = %d\n", numPower2);
return 0; }

What does this program do?


Computer Screen
5
num

Enter n: 5 n x n = 25
25 numPower2

What does this program do?


#include <stdio.h> int main(void) { int a, b, sum; printf("Enter a: "); scanf("%d", &a); printf("Enter b: "); scanf("%d", &b); sum = a + b; printf("a + b = %d\n", sum); return 0; }

What does this program do?


Computer Screen 531 24 a b

Enter a: 531 Enter b: 24 a + b = 555

555

sum

What does this program do?


#include <stdio.h> int main(void) { int a, b, sum; printf("Enter a and b: "); scanf("%d%d", &a, &b); sum = a + b; printf("%d + %d = %d\n", a, b, sum); return 0; }

What does this program do?

Enter a and b: 531 24

531

24

sum

What does this program do?

Enter a and b: 531 24 531 + 24 = 555

531

24

sum 555

What does this program do?


#include <stdio.h> int main(void) { int a, b; a = 3; b = 5; printf("a=%d, b=%d\n", a, b); a = b; b = a; printf("a=%d, b=%d\n", a, b); return 0; }

What does this program do?


a = 3; b = 5; a = b; b = a; a 3 b ?

What does this program do?


a = 3; b = 5; a = b; b = a; a 3 b 5

What does this program do?


a = 3; b = 5; a = b; b = a; a 5 b 5

What does this program do?


a = 3; b = 5; a = b; b = a; a 5 b 5

What does this program do?


#include <stdio.h> int main(void) { int a, b, temp;

a = 3; b = 5; printf("a=%d, b=%d\n", a, b);

What does this program do?


temp = a; a = b; b = temp; printf("a=%d, b=%d\n", a, b); return 0; }

What does this program do?


a = 3; b = 5; a 3 b ? temp ?

temp = a; a = b; b = temp;

What does this program do?


a = 3; b = 5; a 3 b 5 temp ?

temp = a; a = b; b = temp;

What does this program do?


a = 3; b = 5; a 3 b 5 temp 3

temp = a; a = b; b = temp;

What does this program do?


a = 3; b = 5; a 5 b 5 temp 3

temp = a; a = b; b = temp;

What does this program do?


a = 3; b = 5; a 5 b 3 temp 3

temp = a; a = b; b = temp;

Potrebbero piacerti anche