C Programming: Understanding Functions, Variables, and Header Files in CSSE 120, Assignments of Software Engineering

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

Pre 2010

Uploaded on 08/18/2009

koofers-user-gzb-1
koofers-user-gzb-1 🇺🇸

9 documents

1 / 10

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
FIRST C PROGRAM
CSSE 120Rose Hulman Institute of Technology
pf3
pf4
pf5
pf8
pf9
pfa

Partial preview of the text

Download C Programming: Understanding Functions, Variables, and Header Files in CSSE 120 and more Assignments Software Engineering in PDF only on Docsity!

FIRST C PROGRAM

CSSE 120—Rose Hulman Institute of Technology

Announcements

 No class Thursday/Friday

 Homework due Friday 5:00 pm:

 Fill out team evaluation survey in Angel

 Homework due Session 22 (Tuesday)

 Reading in C textbook

Comments in C

 Python comments begin with # and continue until the

end of the line.

 C comments begin with /* and end with */.

 They can span any number of lines.

 Some C compilers (including the one we are using)

also allow single-line comments that begin with //.

The inclusion of header files

#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

printRootTable()'s interface

#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)

(local) variable declaration

#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

C's for loop

 init : usually initializes variables used by the loop

 test : if the value of the test is true, the loop body

executes

 update : After execution of the loop body, this code

is executed. Then the test code is evaluated again,

and if true …

#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 (; < test>; ) { body }