Computer Systems Organization

Start Lecture #3

1.8: Arguments—Call by Value

Remark: All the online material is now associated with V22.0202-002. You should now sign up for the mailing list as shown in lecture 1.

1.9: Character Arrays

Unlike Java, C does not have a string datatype. A string in C is an array of chars. String operations like concatenate and copy (assignment) become functions in C. Indeed there are a number of standard library routines for string.

The most common implementation of strings in C is null terminated. That is, a string of length 5 has 6 characters, the 5 characters in the string and a sixth character = '\0'.

Print Longest Line

#include <stdio.h>
#define MAXLINE 1000
int getLine(char line[], int maxline);
void copy(char to[], char from[]);
int main() {
  int len;
  int max;
  char line[MAXLINE];
  char longest[MAXLINE];
  max = 0;
  while ((len=getLine(line,MAXLINE))>0)
    if (len > max) {
      max = len;
  if (max>0)
    printf("%s", longest);
  return 0;
int getLine(char s[], int lim) {
  int c, i;
  for (i=0; i<lim-1 && (c=getchar())!=EOF && c!='\n'; ++i)
    s[i] = c;
  if (c=='\n') {
    s[i]= c;
  s[i] = '\0';
  return i;
void copy(char to[], char from[]) {
  int i;
  while ((to[i] = from[i]) != '\0')

This program reads lines from the terminal, converts them to C strings by appending '\0', and prints the longest one found. Pseudo code would be

    while (more lines)
      if (longest)
        save it and its length

Thus we need the ability to read in a line and save it and write two functions getLine and copy for these tasks (the book uses getline, but that doesn't compile for me since there is a library routine in stdio.h with the same name and different signature). Given these two routines, main is fairly simple, needing only a few small comments.

  1. Note that getLine and copy are declared before main. They are defined later in the file but C requires declare before use so either main would have to come last or the declarations are needed. Since only main uses the routines, the declarations could have been in main but it is common practice to put them outside as shown.
  2. Note %s inside printf. This is used for (null-terminated) strings.
  3. Note that main is declared to return an integer and it does return 0. In unix at least, this is the indication of a successful run.

The for condition in getLine is rather complex. Perhaps it would be clearer if the test were simply i<lim-1 and the rest was done with if-break statments inside the loop.

copy is declared and defined to return void.

Homework: Simplify the for condition as just indicated.

1.10: External Variables and Scope

Solving Quadratic Equations

#include <stdio.h>
#include <math.h>
#define A +1.0   // should read
#define B -3.0   // A,B,C
#define C +2.0   // using scanf()
void solve (float a, float b, float c);
int main() {
  return 0;
void solve (float a, float b, float c) {
  float d;
  d = b*b - 4*a*c;
  if (d < 0)
    printf("No real roots\n");
  else if (d == 0)
    printf("Double root is %f\n", -b/(2*a));
    printf("Roots are %f and %f\n",

#include <stdio.h> #include <math.h> #define A +1.0 // should read #define B -3.0 // A,B,C #define C +2.0 // using scanf() void solve(); float a, b, c; // definition int main() { extern float a, b, c; // declaration a=A; b=B; c=C; solve(); return 0; } void solve () { extern float a, b, c; // declaration float d; d = b*b - 4*a*c; if (d < 0) printf("No real roots\n"); else if (d == 0) printf("Double root is %f\n", -b/(2*a)); else printf("Roots are %f and %f\n", ((-b)+sqrt(d))/(2*a), ((-b)-sqrt(d))/(2*a)); }

The two programs on the right find the real roots (no complex numbers) of the quadratic equation

using the standard technique of first calculating the discriminant
    d = b2-4ac
These programs deal only with real roots, i.e., when d≥0.

The first program calls a function solve() passing it as arguments the three coeficients a,b,c.

The second program communicates with solve using external (global) variables rather than arguments/parameters.

Remark: I am not assigning lab 1 yet because we haven't resolved the situation with But when it is resolved lab1 will include some of problems 20-24 on page 34.