C programming Language – Code snippets
C Programming Language, 2nd Edition
Compiling and running the sample codes using gcc :
gcc sample.c -o sample
./sample
Chapter 1 – Introductory tutorial : Input/output, characters, strings
0. Hello World!
#include<stdio.h>
int main()
{
printf("Hello World\n");
return 0;
}
1. Word counter
/* word counter */
#include<stdio.h>
#define IN 1 //inside a word
#define OUT 0 //outside a word
int main()
{
int c,nl,nw,nc,state;
state=OUT;
nl=nw=nc=0;
while((c=getchar())!=EOF)
{
++nc;
if(c=='\n') ++nl;
if (c==' ' || c == '\n' || c=='\t') state=OUT;
else if (state==OUT)
{
state=IN;
++nw;
}
}
printf("lines=%d words=%d characters=%d\n",nl,nw,nc);
return 0;
}
2. Convert Fahrenheit to Celsius
#include<stdio.h>
#define LOWER 0
#define UPPER 300
#define STEP 20
int main()
{
int fahr;
for (fahr = LOWER; fahr <= UPPER; fahr=fahr + STEP)
printf("%3d %6.1f\n", fahr, (5.0/9.0)*(fahr-32));
return 0;
}
3. Count characters
/* character counter */
#include<stdio.h>
int main()
{
short int nc;
for(nc=0; getchar()!=EOF ; ++nc);
printf("%d\n",nc);
return 0;
}
4. Count characters by type – digits, white spaces etc.
/* count digits, white space, others */
#include<stdio.h>
int main()
{
int c,i,nwhite,nother;
int ndigit[10];
nwhite=nother=0;
for(i=0; i < 10; ++i) ndigit[i]=0;
while((c=getchar())!=EOF)
if(c>='0' && c<='9') ++ndigit[c-'0'];
else if (c==' ' || c=='\n' || c=='\t') ++nwhite;
else ++nother;
printf("digits=");
for(i=0;i<10;++i) printf(" %d",ndigit[i]);
printf(",white space=%d, other=%d\n",nwhite,nother);
return 0;
}
5. Copy input to output terminal
/* copy input to output 1 */
#include<stdio.h>
int main()
{
int c;
c = getchar();
while ( c!=EOF)
{
putchar(c);
c = getchar();
}
return 0;
}
6. Copy input to output terminal (shorter version)
/* copy input to output */
#include<stdio.h>
int main()
{
int c;
while((c=getchar())!=EOF)
putchar(c);
return 0;
}
7. Print longest line
#include<stdio.h>
#define MAXLINE 1000 /* maximum input line length */
int getline(char line[], int maxline);
void copy(char to[], char from[]);
/* print the longest input line */
int main()
{
int len; /* current line length */
int max; /* maximum length seen so far */
char line[MAXLINE]; /* current input line */
char longest[MAXLINE]; /*longest line saved here */
max=0;
while((len=getline(line,MAXLINE))>0)
if(len>max)
{ max=len;
copy(longest,line);
}
if(max>0) /*there was a line*/
printf("Longest Line from input is:\n%s\n",longest);
printf("Line length is: %d characters\n",max);
return 0;
}
/*getline: read a line into s, return length */
int getline(char s[], int lim)
{
int c,i;
for(i=0; i<lim-1 && (c=getchar())!=EOF && c!='\n'; ++i)
s[i]=c;
if(c=='\n') {
s[i]=c;
++i;
}
s[i]='\0';
return i;
}
/*copy: copy 'from' into 'to'; assume 'to' is big enough */
void copy(char to[], char from[])
{
int i;
i=0;
while((to[i]=from[i]) != '\0')
++i;
}
8. Print longest line – improved
#include<stdio.h>
#define MAXLINE 1000 /* maximum input line length */
int max;
char line[MAXLINE];
char longest[MAXLINE];
int getline(char line[], int maxline);
void copy(char to[], char from[]);
/* print the longest input line */
int main()
{
int len; /* current line length */
extern int max; /* maximum length seen so far */
extern char longest[MAXLINE]; /*longest line saved here */
max=0;
while((len=getline(line,MAXLINE))>0)
if(len>max)
{ max=len;
copy(longest,line);
}
if(max>0) /*there was a line*/
printf("Longest Line from input is:\n%s\n",longest);
printf("Line length is: %d characters\n",max);
return 0;
}
/*getline: read a line into s, return length */
int getline(char s[], int lim)
{
int c,i;
extern char line[];
for(i=0; i<lim-1 && (c=getchar())!=EOF && c!='\n'; ++i)
s[i]=c;
if(c=='\n') {
s[i]=c;
++i;
}
s[i]='\0';
return i;
}
/*copy: copy 'from' into 'to'; assume 'to' is big enough */
void copy(char to[], char from[])
{
int i;
extern char line[], longest[];
i=0;
while((to[i]=from[i]) != '\0')
++i;
}
9. Power function
#include<stdio.h>
int power(int m, int n);
/* test power function */
int main()
{
int i;
for(i=0; i<10; ++i)
printf("%d %d %d\n",i,power(2,i),power(-3,i));
return 0;
}
int power(int base, int n)
{
int i,p=1;
for(i=1;i<=n;++i)
p*=base;
return p;
}
10. Reverse string
#include<stdio.h>
#define MAXLINE 1000
int size=0;
void reverse(char line[], int lim);
int main()
{ extern int size;
char line[MAXLINE];
reverse(line,MAXLINE);
while(size) putchar(line[--size]);
printf("\n");
return 0;
}
void reverse(char line[], int lim)
{
extern int size;
int c;
while((c=getchar())!=EOF ) line[size++]=c;
line[size]='\0';
}
11. Get digits only
#include<stdio.h>
int atoi(char s[]);
int main()
{
char str[]="12a4c5 ";
int i=0,n=0;
while(str[i]!='\0')
{
if(str[i]>='0' && str[i]<='9') { n=10*n+(str[i]-'0'); i++; }
else
{ i++; continue; }
}
printf("number=%d\n",n);
return 0;
}
12. Fahrenheit to Celsius table
/* print Fahrenheit-Celsius table for fahr =0,20,40,....,300 */
#include<stdio.h>
int main()
{
int fahr, celsius;
int lower, upper, step;
lower = 0;
upper = 300;
step = 20;
fahr = lower;
while( fahr <= upper)
{
celsius = 5 * (fahr - 32) / 9;
printf("%d\t%d\n", fahr, celsius);
fahr = fahr + step;
}
return 0;
}
Reference –
https://www.amazon.com/Programming-Language-2nd-Brian-Kernighan/dp/0131103628#reader_0131103628