Title: Introduction to C
1Introduction to C
- For the participants of
- Practical Course Scientific Computing and
Visualization
2Contents
- Getting Started the first program
- data input and result output
- compilation and execution
- Variables, expressions, types, functions
- Control program flow
- loops, conditional statements
- Arrays and pointers
- Standard C libraries
- Preprocessing
- Makefiles
3Getting started C Program structure
- Any C programm consists of
- functions
- variables
- Functions consist of
- statements (abc, function call, etc.)
- Statements includes expressions builtwith
operations (for example, arithmeticalexpressoins
with , -, \, )
4Getting Started Input and Output of Data
- include ltstdio.hgt
- main()
- int a, b, resultprintf(Input
a,b\t)scanf(d,d,a,b)
resultabprintf(d plus d is equal to d
\n, a,b,result) -
5Getting Started Compilation
- In order to execute the programm, compile it
- with
- gcc filename
- The executable file a.out will be produced,
- start it with
- a.out
6Operators and Expressions in C
- binary (-,,,/,,,)
- xy year 4 b true
- Unary (-,,,) int a2 int pointer_to_a
a int b-a int sum pointer_to_a
b - postfix and prefix operators (,--)
- a2 ba ca
-
7Variables and their Types
- There are following main types in C
- bool (1 byte)
- char (1 byte)
- int (4 bytes)
- long, short, signed, unsigned int (4, 8 bytes)
- float (4 bytes)
- double (8 bytes)
- void
- For example bool btrue long int a char
c a a 232334
Memory
...
...
reserved for a
8Types of Expressions and Casting
- The types of single variables schould be declared
by programmer - float f_number 3.141592 double
d_number 3.14159265358 int i_number3 - Many operations cause automatic conversions of
operands, in order to bring them to common
type - i_numberf_number -gt 6.141592 int -gt
float - sizeof(f_number d_number)8 float -gt
double - sizeof(f_number (float)d_number)4 double
-gt float -
9Functions
- The function definition in Cret_type name
(arg_type1 arg_name1, ...,arg_typeN argnameN)
...double parabola(double x)
//declare the new variable, that will be returned
return xxdouble f0 parabola(0)
double f1 parabola(1)
10Are functions computing arithmetic expressins
powerfull enough ?
- Now let us try to define the well-known factorial
function in C. This
function can be specified as follows
11Flow control Conditional Statement
- To test logical conditions and make decisions use
conditional statement IF-ELSEif (condition)
... Statemen_block1 ...else ...
Statemen_block1 ... For example
int f(int n) if ( ngt0 )
return nf(n-1)
else return 1
12Loops While and For
int i 1int fac 1
for (init cond post) ...
for (i1 iltn i) facfaci
while (cond) ...
while (iltn) facfaci ii1
do ... while (cond)
do facfaci ii1 while (iltn)
13Arrays Vectors and Matrices
float x31,3,5 include ltmath.hgt float
vector_normsqrt(x1x1x2x2x3x3)
float matrix_trace A11A22A33
float A331,2,3, 3,2,1,
5,6,7
14Arrays Strings
- Strings can be represented in C as arrays of
characters (type char) - For example
-
Memory
char strhelloint i0 while(stri!\0)
printf(c\n, stri) i
15Arrays Pointers
char strhelloint i0 while(stri!\0)
printf(c\n, stri) i
...
str
16Arrays Pointers
...
str
- while(str!\0)printf(c\n,(str))
17Indirection and Address Operators (, ) Examples
- int x1, y2, z10
- int ipx
- yip
- ip0
- ip z3
18Indirection and Address Operators (, ) Examples
- void swap (int a, int b)
- int tmpa
- ab
- btmp
-
- swap(a,b) // wrong
- Swap(a,b) // correct
19Multidimensional Arrays Pointers to Pointers
float A331,2,3, 3,2,1,
5,6,7
A2 ...
A1
20Multidimensional arrays
A2 ...
A1
for(i0ilt3i) for(j0jlt3j)
printf(f , yij) printf(\n)
21Arrays Dynamic Programming
As another example, let us implement the
factorial calculation in a more efficient way.
The main idea is to reuse once calculated
results.We store them in an array.
22Arrays Dynamic Programming
int factorial(int n)int res1 for(in igt0
i--) // has the result been //
already computed ? if(resultsi0)
resresi resultsires else
return resultsi return fac
23Memory allocation
- C provides a number of functionsto allocate
memory to store data instdlib.h - void malloc(unsigned int nbytes)
- void free(void ap)
24Memory allocation
- includeltstdlib.hgt
- int results
- main() resultsmalloc(100sizeof(integer))
factorial(3)factorial(44) - factorial(100)free(results)
25Preprocessing
- File inclusion
- include ltfilenamegt
- include filename
- Macro substitutions
- define max(A,B) (A)gt(B) ? (A)(B)
- define CONSTANT 25
- Conditional inclusion
- ifndef __HELPER_H__define __HELPER_H__...en
dif
26Standard libraries
- stdio.h
- standard input and output
- printf, scanf, fprintf, fscanf, fopen, flcose,
... - string.h
- string processing
- Strlen, strcpy, strcat, strcmp, ...
27Standard libraries
- stlib.h
- memory allocation, random numbers,type
conversion - atof, atoi, rand, alloc, malloc, free
- math.h
- mathematical functions
- sin, cos, sqrt, strcmp, remainder,...
28Compilation with makefiles
- Makefiles consist of
- macro definitions
- rules of compilation
- dependences declaration
29Compilation with makefiles
CCgcc CFLAGS -Wall pedantic OBJ helper.o
init.o boundary.o uvp.o main.o all (OBJ)
(CC) (CFLAGS) o sim (OBJ) lm clean rm
(OBJ) helper.o helper.h init.o helper.h
init.h ....