Introduction to C
2025. 9.
EE, Sogang University
Prof. Jaewoo So
1
1. An Overview of C
1.1 What is C?
▪ A general-purpose computer programming language developed
between 1969 and 1973 by Dennis Ritchie at the Bell Telephone
Laboratories for use with the Unix operating system.
▪ Although C was designed for implementing system software, it is
also widely used for developing portable application software.
▪ C is one of the most popular programming languages of all time.
2
1.2 Why C?
▪ C is the native language of UNIX. C is the standard development
language for personal computers.
– Much of MS-DOS and OS/2 is written in C.
▪ C is portable.
– Code written on one machine can be easily moved to another.
▪ C is terse.
– C has a very powerful set of operators, and some of these operators
allow the programmer to access the machine at the bit level.
– The increment operator ++ has a direct analogue in machine
language on many machines, making this an efficient operator.
▪ C is modular.
– C supports one style of routine, the external function, for which
arguments are passed call-by-value.
3
▪ C is the basis for C++ and Java.
– This means that many of the constructs and methodologies that are
routinely used by the C programmer are also used by the C++ and
Java programmer.
▪ C is efficient on most machines.
– Because certain constructs in the language are explicitly machine-
dependent, C can be implemented in a manner that is natural with
respect to the machine’s architecture.
4
Example: LTE base station
▪ Bootloader
▪ DSP
▪ MAC processor
Fig. LTE channel cared design
Fig. 1.2GHz TCI6487 block diagram
utilizing TCI6487 1.2GHz DSPs for a
3 sector, 10 MHz, 2 antenna
deployment 5
Example: FPGA Programming in C
Microprocessor FPGA
Architectural design Architectural design
Choice of language (C, C++, JAVA) Choice of language (Verilog, VHDL)
Editing programs Editing programs
Compiling programs (.DLL, .OBJ) Compiling programs
Synthesizing programs (.EDIF)
Linking programs (.EXE) Placing and routing programs
(.VO, .SDF, .TTF)
Loading programs to ROM Loading programs to FPGA
Debugging programs Debugging FPGA programs
Documenting programs Documenting programs
Delivering programs Delivering programs
FPGA: field programmable gate arrays.
6
1.3 History
▪ Evolved over the years:
1972 C invented at AT&T Bell Labs.
1978 Brian W. Kernighan and Dennis M. Ritchie, "The C programming
language” published; first specification of language
1989 C89 standard (known as ANSI C or Standard C)
1990 ANSI C adopted by ISO, known as C90
1999 C99 standard
. Mostly backward-compatible
. Not completely implemented in many compilers
C99 introduced several new features, including inline functions, several
new data types (including long long int and a complex type to
represent complex numbers), variable-length arrays, support for
variadic macros (macros of variable arity) and support for one-line
comments beginning with //, as in BCPL or C++. Many of these had
already been implemented as extensions in several C compilers.
2008 Work on new C standard C1X announced
▪ In this course: ANSI/ISO C (C89/C90)
7
2. Getting Started
▪ The only way to learn a new programming language is by writing
programs in it.
▪ Example: hello.c
8
2.1 Building a C Program
▪ Compilation can involve up to four stages: preprocessing, compilation
proper, assembly and linking, always in that order. The first three
stages apply to an individual source file, and end by producing an
object file; linking combines all the object files into an executable file.
– The C compiler automatically compiles preprocessing, C compiling, and
assembling at once.
9
There are different files:
▪ Regular source code files.
– These files contain function definitions, and have names which end
in ".c" by convention.
▪ Header files.
– These files contain function declarations (also known as function
prototypes) and various preprocessor statements. They are used to
allow source code files to access externally-defined functions.
Header files end in ".h" by convention.
▪ Object files.
– These files are produced as the output of the compiler. They consist
of function definitions in binary form, but they are not executable by
themselves. Object files end in ".o" by convention, although on
some operating systems (e.g. Windows, MS-DOS), they often end in
".obj“.
10
▪ Library files.
– Static library: A set of object files which are resolved in a caller at
compile-time. The static libraries end in “.a” on Unix operating
systems although they end in “.lib” on Windows.
– Dynamic library: The dynamic libraries end in “.so” on Unix
operating systems, although they end in “.dll” on Windows. Dynamic
libraries provide a mechanism for shared code and data, allowing a
developer of shared code/data to upgrade functionality without
requiring applications to be re-linked or re-compiled.
▪ Binary executables.
– These are produced as the output of a program called a "linker".
The linker links together a number of object files to produce a binary
file which can be directly executed. Binary executables have no
special suffix on Unix operating systems, although they generally
end in ".exe" on Windows.
11
2.2 Building a C Program on Windows
2.2.1 Building in Visual Studio
▪ STEP 1: [Build]-Compile [Ctrl-F7]
▪ STEP 2: [Build]-Build [F7]
12
2.2.2 Building on the Command Line
▪ Syntax
CL [option...] file...
▪ Options are specified by either a forward slash (/) or a dash (–).
-c
Compile the source files, but do not link. By default, the object file
name for a source file is made by replacing the suffix with `.o'.
-o
Place output in file. If ‘-o' is not specified, the default is made by
replacing the suffix with ‘.exe’.
▪ file: The name of one or more source files, .obj files, or libraries.
cl –c hello.c // hello.obj
cl hello.c // hello.exe
cl –o helloworld hello.c // helloworld.exe
cl –o a.exe file1.c file2.c file3.obj
13
▪ Setting the Path and Environment Variables for Command-Line
Builds
– Path
⚫ Run vcvars32.bat by typing “vcvars32”.
The vcvars32.bat file sets the appropriate environment
variables to enable 32-bit command-line builds.
⚫ Set the PATH to use “CL” command.
⚫ Need mspdbXX.dll which is the Microsoft program database
library used by Microsoft Visual Studio.
– CL Environment Variables
⚫ The CL tool processes options and arguments defined in the CL
environment variable before processing the command line.
SET CL=[ [option] ... [file] ...] [/link link-opt ...]
14
vcvars32
path %PATH%;”C:\Program Files (x86)\Microsoft Visual Studio\VC98\Bin”
SET CL=/I”C:\Program Files (x86)\Microsoft Visual Studio\VC98\Include”
cl –o helloworld hello.c
15
2.3 Building a C Program on Linux
▪ Syntax
CC [option] filename // ‘gcc’ is identical to ‘cc’
-c
Compile or assemble the source files, but do not link. The ultimate
output is in the form of an object file for each source file. By default,
the object file name for a source file is made by replacing the suffix
`.c', `.i', `.s', etc., with `.o'.
-o
Place output in file. If ‘-o' is not specified, the default is to put an
executable file in ‘a.out’.
-Wall
Enable all the warnings which the authors of cc believe are
worthwhile. Despite the name, it will not enable all the warnings cc
is capable of.
-O
Create an optimized version of the executable.
16
▪ Example
#cc hello.c // generate a.out
#cc –o helloword hello.c
#cc –c hello.c // generate hello.o
#cc –o helloworld hello.o // generate helloworld
#cc –O –o helloworld hello.c
17
2.4 Building a C Program for a Embedded Linux
▪ Cross compiler
– A cross compiler is a compiler capable of creating executable code
for a platform other than the one on which the compiler is run.
hello
# arm-linux-gcc –o hello hello.c
hello
# ppc_8xx-gcc –o hello hello.c
18
2.5 Variables and Arithmetic Expressions
▪ Example
19
▪ Variables
– In ANSI C, all variables must be declared before they are used,
usually at the beginning of the function before any executable
statements.
– The size of these objects is also machine/compiler dependent.
data type size description
(Intel Windows 7
64 bits)
char 1 character – a single byte
short 2 short integer
int 4 integer
long 4 long integer
float 4 float
double 8 double-precision float point
▪ Q: Determine the size of datatypes on our machine/compiler.
20
▪ Arithmetic operations
C operation Arithmetic operator
Addition +
Subtraction -
Multiplication *
Division /
Modulus %
21
2.6 Input and Output
▪ The standard output and input functions are printf() and scanf().
Conversion How the corresponding argument is printed or
character How characters in the input stream are converted
c as a character
d as a decimal integer
e as a floating-point number in scientific notitation
f as a floating-point number (float)
lf as a floating-point number (double)
g in the e-format or f-format, whichever is shorter
s as a string
22
Summary
▪ C Programming Language
– C is a fast, small, general-purpose, platform independent
programming language.
– C is used for systems programming (e.g., compilers and
interpreters, operating systems, database systems, microcontrollers
etc.)
– C is static (compiled), typed, structured and imperative.
– "C is quirky, flawed, and an enormous success."–Ritchie
▪ Basic
– Variable declarations: int i; float f;
– Intialization: char c=’A’; int x=y=10;
– Operators: +,−,∗,/,%
– Expressions: int x,y,z; x=y∗2+z∗3;
– Function: int factorial (int n); /∗function takes int , returns int ∗/
23