






Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
An overview of a c program that calculates the square root of numbers from 1 to 10. It explains the use of functions, comments, header files, and variable declarations in c. Students will learn about the main() function, function definitions, formal parameters, and local variables. The document also covers the inclusion of header files and the use of comments in c.
Typology: Assignments
1 / 10
This page cannot be seen from the preview
Don't miss anything!







#include <stdio.h>
#include <math.h>
#include is somewhat like Python's **from … import ***
The most commonly included files are header files, whose names end with .h
angle brackets mean that it is a standard C header
If we include a file from our own project, surround it's name with quotes, as in #include "myFile.h"
A header file usually contains definitions of constants, and function signatures (without their bodies)
Two lines from math.h (we'll explain later): #define M_PI 3. double sqrt (double);
Other headers: http://www.utas.edu.au/infosys/info/documentation/C/CStdLib.html
#include <stdio.h> #include <math.h>
void printRootTable(int n) {
int main() { printRootTable(10); return 0;
}
What is the name of the "return type" of the printRootTable() function? What does that mean?
The formal parameter is called n , its type is int
The type of every formal parameter must be declared
As in Python, if there are multiple formal parameters, they are separated by commas
Note that this function has no return statement. In that case, the return type must be declared to be void
Notice that we do not provide the type of the actual parameter. Its type is the type of whatever value we pass in. It must "match" the type of the formal parameter
As in Python, when printRootTable is called, the value of the actual parameter (10) is used to initialize the formal parameter (n)
#include <stdio.h> #include <math.h>
void printRootTable(int n) { int i;
int main() { printRootTable(10); return 0;
}
i is a local (to the function) variable of the printRootTable function
Its type is int
Variable declarations must include a type. An optional initialization is allowed, such as int i = 17; or int i = n + 5;
A local variable cannot have the same name as a formal parameter of the same function
Unlike in Python, each C variable's and formal parameter's type must be declared before the variable can be used
Because the variables i and n are local to printRootTable, you cannot refer to them from anywhere else in the program
#include <stdio.h> #include <math.h>
void printRootTable(int n) { int i; for (i=1; i<=n; i++) { printf(" %2d %7.3f\n", i, sqrt(i)); } }
Basic syntax is
for (