Skip to article frontmatterSkip to article content
Site not loading correctly?

This may be due to an incorrect BASE_URL configuration. See the MyST Documentation for reference.

Building in C

ssh seawulf
Last failed login: Tue Apr  7 13:50:18 EDT 2026 from 172.20.26.244 on ssh:notty
There were 3 failed login attempts since the last successful login.
Last login: Tue Apr  7 13:39:17 2026 from 172.20.24.214
salloc
salloc: Granted job allocation 28320
salloc: Waiting for resource configuration
salloc: Nodes n005 are ready for job
pwd
/home/brownsarahm
ls
bash-lesson.tar.gz                           SRR307024_1.fastq  SRR307027_2.fastq
demo                                         SRR307024_2.fastq  SRR307028_1.fastq
dmel-all-r6.19.gtf                           SRR307025_1.fastq  SRR307028_2.fastq
dmel_unique_protein_isoforms_fb_2016_01.tsv  SRR307025_2.fastq  SRR307029_1.fastq
gene_association.fb                          SRR307026_1.fastq  SRR307029_2.fastq
SRR307023_1.fastq                            SRR307026_2.fastq  SRR307030_1.fastq
SRR307023_2.fastq                            SRR307027_1.fastq  SRR307030_2.fastq
mkdir compilec
cd compilec/
nano hello.c
ls
hello.c
cat hello.c
#include <stdio.h>
void main () {

 printf("Hello world\n");

}
gcc -E hello.c -o hello.i
ls
hello.c  hello.i
wc -l hello.*
    6 hello.c
  842 hello.i
  848 total
head hello.i -n 5
# 1 "hello.c"
# 1 "<built-in>"
# 1 "<command-line>"
# 1 "/usr/include/stdc-predef.h" 1 3 4
# 1 "<command-line>" 2
tail hello.i
extern void funlockfile (FILE *__stream) __attribute__ ((__nothrow__ , __leaf__));
# 943 "/usr/include/stdio.h" 3 4

# 2 "hello.c" 2
void main () {

 printf("Hello world\n");

}
gcc -S hello.i
ls
hello.c  hello.i  hello.s
wc -l hello.*
    6 hello.c
  842 hello.i
   25 hello.s
  873 total
cat hello.s
	.file	"hello.c"
	.section	.rodata
.LC0:
	.string	"Hello world"
	.text
	.globl	main
	.type	main, @function
main:
.LFB0:
	.cfi_startproc
	pushq	%rbp
	.cfi_def_cfa_offset 16
	.cfi_offset 6, -16
	movq	%rsp, %rbp
	.cfi_def_cfa_register 6
	movl	$.LC0, %edi
	call	puts
	popq	%rbp
	.cfi_def_cfa 7, 8
	ret
	.cfi_endproc
.LFE0:
	.size	main, .-main
	.ident	"GCC: (GNU) 4.8.5 20150623 (Red Hat 4.8.5-44)"
	.section	.note.GNU-stack,"",@progbits
gcc -c hello.s -o hello.o
ls
hello.c  hello.i  hello.o  hello.s
wc -l hello.*
    6 hello.c
  842 hello.i
    5 hello.o
   25 hello.s
  878 total
cat hello.o
ELF>?@@
UH???]?Hello worldGCC: (GNU) 4.8.5 20150623 (Red Hat 4.8.5-44)zRx
K                                                               A?C
??      hello.cmainputs


???????? .symtab.strtab.shstrtab.rela.text.data.bss.rodata.comment.note.GNU-stack.rela.eh_frame @?0
&PP1P
     90\.B?W?R@
wc hello.o
   5   17 1496 hello.o
wc -c hello.o
1496 hello.o
gcc -o hello hello.o -lm
ls
hello  hello.c  hello.i  hello.o  hello.s
./hello
Hello world
ls -a
.  ..  hello  hello.c  hello.i  hello.o  hello.s
ls -lh
total 44K
-rwxr-xr-x. 1 brownsarahm spring2022-csc392 8.2K Apr  9 13:24 hello
-rw-r--r--. 1 brownsarahm spring2022-csc392   64 Apr  9 12:57 hello.c
-rw-r--r--. 1 brownsarahm spring2022-csc392  17K Apr  9 12:59 hello.i
-rw-r--r--. 1 brownsarahm spring2022-csc392 1.5K Apr  9 13:15 hello.o
-rw-r--r--. 1 brownsarahm spring2022-csc392  433 Apr  9 13:03 hello.s
gcc -o demohello hello.o -lm
./demohello
Hello world
gcc -o demohello hello.c -lm
./demohello
Hello world
nano hello.c
cat hello.c
#include <stdio.h>
void main () {

 printf("Hello world!\n");

}
./demohello
Hello world
gcc -o demohello hello.c -lm
./demohello
Hello world!
nano main.c
cat main.c
/* Used to illustrate separate compilation.
Created: Joe Zachary, October 22, 1992
Modified:
*/

#include <stdio.h>

void main () {
 int n;
 printf("Please enter a small positive integer: ");
 scanf("%d", &n);
 printf("The sum of the first n integers is %d\n", sum(n));
 printf("The product of the first n integers is %d\n", product(n));
}
nano help.c
cat hel
cat: hel: No such file or directory
cat help.c
/* Used to illustrate separate compilation

Created: Joe Zachary, October 22, 1992
Modified:

*/

/* Requires that "n" be positive. Returns the sum of the
  first "n" integers. */

int sum (int n) {
 int i;
 int total = 0;
 for (i = 1; i <= n; i++)
  total += i;
 return(total);
}


/* Requires that "n" be positive. Returns the product of the
  first "n" integers. */

int product (int n) {
 int i;
 int total = 1;
 for (i = 1; i <= n; i++)
  total *= i;
 return(total);
}
gcc -Wall -g -c main.c
main.c:8:6: warning: return type of ‘main’ is not ‘int’ [-Wmain]
 void main () {
      ^
main.c: In function ‘main’:
main.c:12:2: warning: implicit declaration of function ‘sum’ [-Wimplicit-function-declaration]
  printf("The sum of the first n integers is %d\n", sum(n));
  ^
main.c:13:2: warning: implicit declaration of function ‘product’ [-Wimplicit-function-declaration]
  printf("The product of the first n integers is %d\n", product(n));
  ^
gcc -Wall -g -c help.c
ls
demohello  hello  hello.c  hello.i  hello.o  hello.s  help.c  help.o  main.c  main.o
gcc -o demo main.o help.o -lm
ls
demo  demohello  hello  hello.c  hello.i  hello.o  hello.s  help.c  help.o  main.c  main.o
./demo
Please enter a small positive integer: 8
The sum of the first n integers is 36
The product of the first n integers is 40320
nano main.c
cat main.c
/* Used to illustrate separate compilation.
Created: Joe Zachary, October 22, 1992
Modified:
*/

#include <stdio.h>

void main () {
 int n;
 printf("Please enter a small positive integer: ");
 scanf("%d", &n);
 printf("The sum of the first n integers is: %d\n", sum(n));
 printf("The product of the first n integers is: %d\n", product(n));
}
./demo
Please enter a small positive integer: 6
The sum of the first n integers is 21
The product of the first n integers is 720
gcc -Wall -g -c main.c
main.c:8:6: warning: return type of ‘main’ is not ‘int’ [-Wmain]
 void main () {
      ^
main.c: In function ‘main’:
main.c:12:2: warning: implicit declaration of function ‘sum’ [-Wimplicit-function-declaration]
  printf("The sum of the first n integers is: %d\n", sum(n));
  ^
main.c:13:2: warning: implicit declaration of function ‘product’ [-Wimplicit-function-declaration]
  printf("The product of the first n integers is: %d\n", product(n));
  ^
./demo
Please enter a small positive integer: 7
The sum of the first n integers is 28
The product of the first n integers is 5040
gcc -o demo main.o help.o -lm
./demo
Please enter a small positive integer: 7
The sum of the first n integers is: 28
The product of the first n integers is: 5040
logout
bash: logout: not login shell: use `exit'
exir
bash: exir: command not found
exit
exit
salloc: Relinquishing job allocation 28320
salloc: Job allocation 28320 has been revoked.
logout
Connection to seawulf.uri.edu closed.
(base) ```

+++{"lesson_part": "main"}

```{code-cell} bash
:tags: ["skip-execution"]
## Prepare for Next Class 

```{include} ../_prepare/2026-04-14.md

Badges

Review
Practice
  1. Review the notes from today

  2. Create some variations of the hello.c we made in class. Make hello2.c print twice with 2 print commands. Make hello5.c print 5 times with a for loop and hello7.c print 7 times with a for loop. Build them all on the command line and make sure they run correctly.

  3. Write a bash script, assembly.sh to compile each program to assembly and print the number of lines in each file.

  4. Put the output of your script in hello_assembly_compare.md. Add to the file some notes on how they are similar or different based on your own reading of them.

Experience Report Evidence

Questions After Today’s Class