#include <stdio.h> main() { printf("Hello, world.\n"); }
int a = 17; char b = 'b'; double pi = 3.14159;C also has the ability to manipulate individual bits, which is one of its primary attractions to systems developers, because it helps access hardware devices. Bitfields are usually stored in unsigned int or unsigned char types.
struct person { char name[50]; char address[200]; int age; }
#include <stdlib.h> #include <time.h> srandom(time(NULL)); if ( i = random() >= RAND_MAX / 2 ) { printf("big: %d\n",i); } else { printf("little: %d\n",i); }
#define RED 1 #define BLUE 2 #define GREEN 3 switch ( color ) { case RED: printf("red\n"); break; case BLUE: printf("blue\n"); break; case GREEN: printf("green\n"); break; default: printf("unknown color\n"); }
#define LOOPS 10 int i, sum = 0; for ( i = 0 ; i < LOOPS ; i++ ) { sum += i; } printf("sum: %d\n",sum);
#define LOOPS 10 int i = 0, sum = 0; while ( i < LOOPS ) { i++; sum += i; } printf("sum: %d\n",sum);
#define LOOPS 10 int i = 0, sum = 0; do { i++; sum += i; } while ( i < LOOPS ); printf("sum: %d\n",sum);
#define PI 3.14159
#include <stdio.h> int a = 3; int *ap = &a; main() { printf("ap: 0x%x &ap: 0x%x\n", ap, &ap); *ap = 5; printf("assigning...\n"); printf("a: %d *ap: %d\n", a, *ap); }
[rdv@localhost network-programming-in-c]$ gcc -o addr addr.c [rdv@localhost network-programming-in-c]$ ./addr a: 3 *ap: 3 ap: 0x80496e4 &ap: 0x80496e8 assigning... a: 5 *ap: 5
unsigned int factorial(unsigned int n) { if (n <= 1) return 1; return n * factorial(n-1); }
#include <errno.h> struct element { struct element *next; int elementNumber; } struct element *MakeElementList(int n) { struct element *tmp = malloc(sizeof(struct element)); if (!tmp) { perror("MakeElementList"); exit(-1); } if (!n) { tmp->next = NULL; else { tmp->next = MakeElementList(n-1); } tmp->elementNumber = n; return tmp; }
Note: I will be in L.A. and will lecture remotely. The room for the lecture has not yet been determined.