comment include statements functions
/** * Simple program that prints "Hello, World" to * standard output. */ #include <stdio.h> #include <stdlib.h> int main() { printf("Hello, World!\n"); return 0; }
return_type function_name( parameter_list ) { statements }
return_type function_name( parameter_list );
#include header_file
char -128 to 127 short -32768 to 32767 int -2147483648 to 2147483647 long (typically same range as int)
unsigned char 0 to 255 unsigned short 0 to 65535 unsigned int 0 to 4294967295 unsigned long (typically same range as int)
float -3.40282e38 to 3.40282e38 (and also -inf and +inf) double -1.79769e308 to 1.79769e308 (and -inf and +inf)
type variable_list;
int x; int y, z; float f; int n = 5;
type variable_name[constant];
int a[100]; char s[30]; int x[3] = {5, 10, 15};
Declares an array of 100 int's: a[0], a[1], ... a[99]
Declares an array of 30 char's: s[0], s[1], ..., s[29]
Declares an array of 3 int's with initialized values: x[0] = 5, x[1] = 10, and x[2] = 15
char u[6] = {'H', 'e', 'l', 'l', 'o', '\0'}; char u[6] = "Hello"; char u[] = "Hello";
type * variable_name;
int x = 5; int y; int * p = &x; y = *p; *p = 6;
int status; int n; float x; printf("Enter an integer and a float: "); status = scanf("%d%f", &n, &x); printf("n = %d, x = %f\n", n, x); Possible Input: (on one line) 5 3.5 (or on two lines) 5 3.5
char line[30]; printf("Enter a string\n"); fgets(line, 30, stdin); printf("line = %s", line); Possible input: (on one line) Hello World (or on two lines) Hello World
If the input is on one line, the output is
line = Hello World!
If the input is on two lines, the ouput is
line = Hello
void reverse(char s[]); or void reverse(char *s);
With either declaration, s can be treated as an array in the body of reverse. In particular the characters of the string parameter can be referenced using integer subscripts; e.g. as s[i] for i = 0, 1, ..., strlen(s) - 1
/** * Reads one line of input from standard input and prints the characters read * in reverse order. */ #include <stdio.h> #include <stdlib.h> #include <string.h> /* Function declarations (defined after main) */ void chomp(char s[]); void reverse(char s[]); int main() { char line[132]; printf("Enter a line of input\n"); fgets(line, 132, stdin); chomp(line); reverse(line); printf("The input line in reverse order:\n"); printf("%s\n", line); return 0; } /** * removes the last character of string s if it is a newline */ void chomp(char s[]) { /* you write this */ } /** * reverses the order of characters in the string s */ void reverse(char s[]) { /* you write this */ }
string.h
So a program that uses these functions should add the include statement at the beginning of the file:
#include <string.h>
Function declaration | Description |
---|---|
unsigned int strlen(const char s[]); | returns the length of the string stored in s (doesn't count the terminating 0. |
int strcmp(const char s1[], const char s2[]); |
returns negative value if s1 is lexicographically less than
s2 (comes before s2 as strings) returns 0 if s1 and s2 are equal as strings returns positive value if s1 is lexicographically greater than s2 (comes after s2 as strings) |
char * strcpy(char dest[], const char src[]); | copies the characters in src to dest including the terminating 0. The return value is just the value of dest, that is, the beginning address of the dest array. |
/** * Reads words, one per line, from standard input. * Sorts the words in alphabetical order and * prints to standard output. */ #include <stdio.h> #include <stdlib.h> #include <string.h> #define MAXLEN 31 #define MAXCNT 100 void sort(char s[][MAXLEN], int n); void print(char s[][MAXLEN], int n); void chomp(char w[]); int main() { char w[MAXLEN]; char words[MAXCNT][MAXLEN]; int cnt = 0; printf("Enter words, one per line. ctrl-d to finish\n"); while( fgets(w, MAXLEN, stdin) != NULL ) { chomp(w); strcpy(words[cnt], w); cnt++; } sort(words, cnt); printf("\nSorted words\n"); print(words, cnt); return 0; } void sort(char s[][MAXLEN], int n) { char tmp[MAXLEN]; int len, i; int maxpos; for(len = n; len > 1; len--) { maxpos = 0; for(i = 1; i < len; i++) { if (strcmp(s[maxpos], s[i]) < 0) { maxpos = i; } } strcpy(tmp, s[maxpos]); strcpy(s[maxpos], s[len - 1]); strcpy(s[len - 1], tmp); } } void print(char s[][MAXLEN], int n) { int i; for(i = 0; i < n; i++) { printf("%s\n", s[i]); } } /** * remove trailing newline from string w if there is one. */ void chomp(char w[]) { int n = strlen(w); if (w[n-1] == '\n') { w[n-1] = 0; } }