Professional Documents
Culture Documents
College 1
College 1
Introduction to C programming
3 lectures
9, 15, 22 september
(wednesday at 11:00-12:45 Q105)
www.cs.vu.nl/~jason
Introduction to C programming 2
• Crash course in C
• Crash course in C
? Empasis on the differences between Java and C.
∗ most of you know Java
? Some practical examples
? Stuff related to C-programming
∗ compiler, preprocessor, debugging, makefiles, etc.
Introduction to C programming 4
Literature
• Books
? The C Programming Language (Kernighan, Ritchie)
? The Complete Reference (Schildt)
? The C Puzzle Book (Feuer)
? Many others available....
Overview
• Very short history of C
• Preprocessor
History of C
History of C
• Result was C
Introduction to C programming 8
History of C
• Compared to assembly, C is
? high level (C has functions, while and for loops, etc.)
? portable (only C compiler must be ported for each
platform, all applications stay the same)
Introduction to C programming 9
• C has pointers
? similar to references but ...
? ... they can be use in calculations (pointer arithmetic)
Keywords
Keywords
Keywords
Keywords
• A C program consists of
? several functions, in one or more files.
? a main function in of these files.
? possibly some header files.
Example program
#include <stdio.h>
double global;
/∗ This is a comment ∗/
int main(int argc, char ∗∗ argv)
{
int local = 0;
global = 0.42;
for (local=0;local<argc;local++)
{
printf("argument[%d] = %s\n", local, argv[local]);
}
printf("global = %f\n", global);
return 0;
}
Introduction to C programming 20
Example program
−> #include <stdio.h>
double global;
/∗ This is a comment ∗/
int main(int argc, char ∗∗ argv)
{
int local = 0;
global = 0.42;
for (local=0;local<argc;local++)
{
printf("argument[%d] = %s\n", local, argv[local]);
}
printf("global = %f\n", global);
return 0;
}
Introduction to C programming 21
Example program
#include <stdio.h>
−> double global;
/∗ This is a comment ∗/
int main(int argc, char ∗∗ argv)
{
int local = 0;
global = 0.42;
for (local=0;local<argc;local++)
{
printf("argument[%d] = %s\n", local, argv[local]);
}
printf("global = %f\n", global);
return 0;
}
Introduction to C programming 22
Example program
#include <stdio.h>
double global;
/∗ This is a comment ∗/
−> int main(int argc, char ∗∗ argv)
{
int local = 0;
global = 0.42;
for (local=0;local<argc;local++)
{
printf("argument[%d] = %s\n", local, argv[local]);
}
printf("global = %f\n", global);
return 0;
}
Introduction to C programming 23
• Syntax:
int main(int argc, char ∗∗ argv) { /∗ body ∗/ }
int main(void) { /∗ body ∗/ }
Introduction to C programming 24
Example program
#include <stdio.h>
double global;
/∗ This is a comment ∗/
int main(int argc, char ∗∗ argv)
{
int local = 0;
global = 0.42;
for (local=0;local<argc;local++)
{
−> printf("argument[%d] = %s\n", local, argv[local]);
}
−> printf("global = %f\n", global);
return 0;
}
Introduction to C programming 26
Compiling
argument[0] = ./a.out
argument[1] = hello
argument[2] = world
global = 0.420000
Introduction to C programming 27
Example program
#include <stdio.h>
−> double global;
/∗ This is a comment ∗/
−> int main(int argc, char ∗∗ argv)
{
−> int local = 0;
global = 0.42;
for (local=0;local<argc;local++)
{
printf("argument[%d] = %s\n", local, argv[local]);
}
printf("global = %f\n", global);
return 0;
}
Introduction to C programming 28
Booleans (1)
Booleans (2)
Mistakes
• Missing an ’=’:
int y = 0;
if (y == 1) { /* not executed */ }
if (y = 1) { /* executed, result is 1 */ }
• Wrong operator:
int x = 0;
int y = 1;
if (x && y) { /* not executed */ }
if (x & y) { /* executed, result is 1 */ }
Introduction to C programming 36
Mistakes
• No initialization:
int y;
if (y) { /* who knows what will happen ??? */ }
while (y--) { /* same here */ }
Introduction to C programming 37
Variables (1)
int global;
void function(void)
{
int local;
}
Introduction to C programming 38
/∗ ok ∗/ | /∗ ok ∗/ | /∗ wrong ∗/
void funct1(void) | void funct2(void) | void funct3(void)
{ | { | {
int a, b; | int a = 0; | int a = 0;
a = 0; | int b = 1; | a++;
b = 1; | } | int b = 1;
} | | }
Introduction to C programming 47
• Modifiers:
auto does nothing
register try to put the variable in a register
volatile never put the variable in a register
static variable exist after function (sort of global)
Introduction to C programming 49
Arrays in Java
int [] a3 = { 1, 2, 3 };
a2 = a3;
a2[10] = 5; /* throws an exception */
Introduction to C programming 50
Arrays in C (1)
Arrays in C (2)
int a1[5]; /∗ OK ∗/
int a2[] = { 1, 2, 3 }; /∗ OK ∗/
int a3[4][5]; /∗ OK ∗/
int [] a4; /∗ PARSE ERROR ∗/
int a5[]; /∗ ERROR ∗/
int [] function(void) { /∗ PARSE ERROR ∗/
int array[5];
return array;
}
void foo(int a[]) { /∗ OK ∗/
a[3] = 0;
}
void bar(void) {
int a[5];
foo(a); /∗ a is passed ’by−reference’ ∗/
a = a5; /∗ ERROR ∗/
}
Introduction to C programming 52
Arrays in C (3)
#include <stdio.h>
int main(void) {
int i;
int a1[] = { 1, 2, 3, 4, 5 };
int a2[] = { 1, 2, 3, 4, 5 };
a2[10] = 42; /∗ BUG ∗/
for (i=0;i<5;i++) {
printf("a1[%d] = %d\n", i, a1[i]);
}
}
Introduction to C programming 53
Strings
Strings Examples
char name0[6];
name0[0] = ’J’;
name0[1] = ’a’;
name0[2] = ’s’;
name0[3] = ’o’;
name0[4] = ’n’;
name0[5] = ’\0’;
char name1[] = { ’J’, ’a’, ’s’, ’o’, ’n’, ’\0’ };
char name2[6] = "Jason";
char name3[] = "Jason";
char name4[100] = "Not 100 characters long!";
Introduction to C programming 55
Strings Examples
char name0[6];
name0[0] = ’J’;
name0[1] = ’a’;
name0[2] = ’s’;
name0[3] = ’o’;
name0[4] = ’n’;
−> name0[5] = ’\0’;
char name1[] = { ’J’, ’a’, ’s’, ’o’, ’n’, ’\0’ };
char name2[6] = "Jason";
char name3[] = "Jason";
char name4[100] = "Not 100 characters long!";
Introduction to C programming 56
Strings Examples
char name0[6];
name0[0] = ’J’;
name0[1] = ’a’;
name0[2] = ’s’;
name0[3] = ’o’;
name0[4] = ’n’;
name0[5] = ’\0’;
−> char name1[] = { ’J’, ’a’, ’s’, ’o’, ’n’, ’\0’ };
char name2[6] = "Jason";
char name3[] = "Jason";
char name4[100] = "Not 100 characters long!";
Introduction to C programming 57
Strings Examples
char name0[6];
name0[0] = ’J’;
name0[1] = ’a’;
name0[2] = ’s’;
name0[3] = ’o’;
name0[4] = ’n’;
name0[5] = ’\0’;
char name1[] = { ’J’, ’a’, ’s’, ’o’, ’n’, ’\0’ };
−> char name2[6] = "Jason";
char name3[] = "Jason";
char name4[100] = "Not 100 characters long!";
Introduction to C programming 58
Strings Examples
char name0[6];
name0[0] = ’J’;
name0[1] = ’a’;
name0[2] = ’s’;
name0[3] = ’o’;
name0[4] = ’n’;
name0[5] = ’\0’;
char name1[] = { ’J’, ’a’, ’s’, ’o’, ’n’, ’\0’ };
char name2[6] = "Jason";
−> char name3[] = "Jason";
char name4[100] = "Not 100 characters long!";
Introduction to C programming 59
Strings Examples
char name0[6];
name0[0] = ’J’;
name0[1] = ’a’;
name0[2] = ’s’;
name0[3] = ’o’;
name0[4] = ’n’;
name0[5] = ’\0’;
char name1[] = { ’J’, ’a’, ’s’, ’o’, ’n’, ’\0’ };
char name2[6] = "Jason";
char name3[] = "Jason";
−> char name4[100] = "Not 100 characters long!";
Introduction to C programming 60
Enumerations
Structures
Unions
Defining Types
Functions (1)
Printf
• Syntax:
int printf(const char ∗format, ...);
Printf Examples
#include <stdio.h> |
|
int main(void) { |
int val = 5; | Hello world
char c = ’a’; | Hello 5 World
char str[] = "world"; | 5 a World
| Hello world
printf("Hello world\n"); |
printf("Hello %d World\n", val); |
printf("%d %c World\n", val, c); |
printf("Hello %s\n", str); |
return 0; |
} |
Introduction to C programming 67
%d signed int
%u unsigned int
%x hexadecimal unsigned int
%c character
%f double and float
%s string
%% to print a %
Introduction to C programming 68
Man pages
NAME
printf, fprintf, sprintf, snprintf, vprintf, vfprintf,
vsprintf, vsnprintf - formatted output conversion
etc.
Introduction to C programming 69
void example(void) {
int result = max(5, 8); /∗ ERROR ∗/
}
int max(int one, int two) {
return (one < two ? two : one);
}
Introduction to C programming 70
• Example:
? application split into 10 files
? each uses 4 functions from every other file.
? need 36 function prototypes per file!
? same for type definitions!
Header files
Preprocessor (1)
Preprocessor (2)
Preprocessor (#include)
• Syntax:
#include <filename> /∗ FOR ’GLOBAL’ INCLUDES ∗/
#include "filename" /∗ FOR ’LOCAL’ INCLUDES ∗/
Introduction to C programming 77
Preprocessor (#define)
Header files
Overview (1)
Overview (2)
More info
• Reader
• Books
? The C Programming Language, by Kernighan and
Ritchie
• Man pages
• Ask amazon or google
Introduction to C programming 85
Next week
jason@cs.vu.nl
www.cs.vu.nl/~jason
www.cs.vu.nl/~cn