02 Writing First C Program
02 Writing First C Program
02 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.
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.
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.
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 return statement terminates the execution of the function and returns control to the operating system. It also gives back the functions result.
Statements
A statement causes an action to be performed by the program. It translates directly into one or more machine language instructions.
Statements
Statements
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
In 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
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
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.
Assignment Statement
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
x = 5;
Assignment Statement
How do we tell the program to copy a value from one memory cell to another?
x = 5; y = x;
Assignment Statement
How do we tell the program to copy a value from one memory cell to another?
x = 5; y = x;
y
Assignment Statement
How do we tell the program to copy a value from one memory cell to another?
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
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
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
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
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
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.
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
Showing Results
Y is 5
5 is the value in y.
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:
Showing Results
Showing Results
Can we do this?
printf("a=%d\nb=%d\n", a, b);
Showing Results
printf("a=%d\nb=%d\n", a, b);
Placeholder for a Placeholder for b
Variables
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.
How do we make a program input the data from the program user in order to do computation?
In a variable
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.
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
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.
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;
Better to let the user know that the program is expecting some data. How do we do this?
Complete Program
#include <stdio.h> int main(void) { int num;
Displays a prompt.
Enter a number: _
Enter a number: 91
91
Enter a number: 91
Note: the underline is used to show that this is data entered by the user.
91
printf("Enter n: "); scanf("%d", &num); numPower2 = num * num; printf("n x n = %d\n", numPower2);
return 0; }
Enter n: 5 n x n = 25
25 numPower2
555
sum
531
24
sum
531
24
sum 555
temp = a; a = b; b = temp;
temp = a; a = b; b = temp;
temp = a; a = b; b = temp;
temp = a; a = b; b = temp;
temp = a; a = b; b = temp;