


Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
The concepts of external and static variables in c programming, the use of typedef for defining new types, and the importance of header files for organizing and compiling larger c programs. It includes examples and explanations of the file scope, static storage duration, and the effects of typedef.
Typology: Summaries
1 / 4
This page cannot be seen from the preview
Don't miss anything!



1
2
#include <stdio.h>
int nextvalue() { static int i = 0; i++; return i; }
int main() { int i; for(i = 10; i > 0; i--) { printf("%d\n", nextvalue()); } return 0; }
output: 1 2 3 4 5 6 7 8 9
extern int i;
void f(void) { i++; }
int i = 0; extern void f(void); void g(void) { f(); printf("%d\n", i); }
5
typedef unsigned int size_t;
struct personrec {
char name[20];
int age;
};
typedef struct personrec Person;
Person *p = malloc(sizeof(Person)); 6
typedef unsigned int size_t;
typedef struct {
char name[20];
int age;
} Person;
Person *p = malloc(sizeof(Person));
void add(int); int isEmpty(); extern List *head; int main() { add(10); isEmpty(); head = NULL; }
List *head = NULL; int isEmpty() {...} void add(int v) {...} void remove(int v) {...}
struct node { int value; struct node * next; } ; typedef struct node List; extern List *head; int isEmpty(int); void add(int); void remove(int)
#include "list.h"
int main() { add(10); isEmpty(); head = NULL; }
#include "list.h" List *head = NULL; int isEmpty() {...} void add(int v) {...} void remove(int v) {...}
13
14
int main(int argc, char **argv) { char *result; if (argc != 3) { fprintf(stderr, "Usage: %s string1 string2\n", argv[0]); exit(1); } switch (mystrcmp(argv[1], argv[2])) { case -1: result = "less than"; break; case 0: result = "equal to"; break; case 1: result = "greater than"; break; default: result = "causing a problem comparing to"; break; } printf("%s is %s %s\n", argv[1], result, argv[2]); return 0; }
int mystrcmp (const char *a, const char b) { while (a && *b && *a == b) { a++; b++; } return (a - *b); }
May return other than -1, 0 and 1
int mystrcmp (const char *a, const char b) { while (a && *b && *a == b) { a++; b++; } if (a < b) return -1; else if (a > b) return 1; else / a same as b */ return 0; }
returns sign of a -b