Search Docs by Keyword

Table of Contents

C Programming Language

Description

C is a general-purpose, procedural computer programming language supporting structured programming, lexical variable scope, and recursion, while a static type system prevents unintended operations. By design, C provides constructs that map efficiently to typical machine instructions and has found lasting use in applications previously coded in assembly language. Such applications include operating systems and various application software for computers, from supercomputers to embedded systems. Wikipedia

Best Practice

We recommend jumping to a compute node for compiling a C program as the compilation could take up to few seconds to a minute depending on the complexity of the code. Additionally, it is best to utilize the test partition to compile and test a program before executing its production run on the cluster as a batch-job.

It is best practice to compile a C code separately and then use the executable, generated during compilation, in the production run using the sbatch script. If possible, avoid including the compilation command in the sbatch script, which will recompile the program every time the job is submitted. If any changes are made to the source code, compile the source code separately, and then submit the production run as a batch-job.

Compilers

You can compile a C code using either a GNU or an Intel compiler.

GNU gcc compiler

To get a list of currently available GNU compilers on the cluster, execute: module spider gcc

The default GNU compiler is typically the latest compiler version on the cluster and can be loaded using module load gcc

To compile a code using a specific version of the GNU compiler and the O2 optimization flag, you can do the following:

module load gcc/9.5.0-fasrc01
gcc -O2 -o sum.x sum.c

Intel icc compiler

To get a list of currently available Intel compilers on the cluster, execute: module spider intel

To compile using a specific version of the Intel compiler, execute:

module load intel/23.0.0-fasrc01
icc -O2 -o sum.x sum.c

Note: If loading an intel module version, refer to the following table for compiling your C code

Intel Comiler Version C Fortran C++
Below 24.0.0 icc ifortran icpc
24.0.0 and above icx ifx icpx

If you load an Intel compiler that is lower than version 24.0.0, you might get this remark

icc: remark #10441: The Intel(R) C++ Compiler Classic (ICC) is deprecated and will be removed from product release in the second half of 2023. The Intel(R) oneAPI DPC++/C++ Compiler (ICX) is the recommended compiler moving forward. Please transition to use this compiler. Use '-diag-disable=10441' to disable this message.

This is just a warning that implies that the user of icc will be deprecated in the second half of 2023. You can quiet this warning by compiling your code using icc in the following manner:

module load intel/23.0.0-fasrc01
icc -O2 -diag-disable=10441 -o sum.x sum.c

Examples

To get started with C on the Harvard University FAS cluster you can try the example shown on our User Codes repository.

Resources

To learn and practice more in C, see the following:

© The President and Fellows of Harvard College
Except where otherwise noted, this content is licensed under Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International license.