C programming Language - Code snippets
C Programming Language, 2nd Edition
Compiling and running the sample codes using gcc :
This shell example compiles one saved C source file into an executable named sample, then runs it from the current directory. It is a workflow illustration, not a guarantee that every historical example below compiles unchanged with a modern compiler; inspect the individual caveats first.
gcc sample.c -o sample
./sample
Chapter 2 - Types, Operators and Expressions
1.Convert to lower case.
This argument-processing example prints an ASCII-style lowercase version of the first argument. It advances a pointer through the argument and offsets uppercase letters into the lowercase range. The preserved program does not validate argc before accessing argv[1], and the arithmetic is not a Unicode case conversion.
#include<stdio.h>
int main(int argc, char *argv[])
{
while(*argv[1])
{
if(*argv[1] >='A' && *argv[1] <='Z') { putchar(*argv[1] + 'a' -'A'); *++argv[1]; }
else
{putchar(*argv[1]); *++argv[1]; }
}
printf("\n");
return 0;
}
2. Get bits
This bit-field exercise shifts the requested field toward the low-order end and masks away surrounding bits. It illustrates bitwise operators rather than a portable production implementation. The mask shifts a signed negative value in the preserved code, and unchecked shift counts can also cause undefined behavior.
#include<stdio.h>
unsigned getbits(unsigned x,int p, int n);
int main()
{
int x=16;
printf("%d\n",getbits(x,4,3));
return 0;
}
unsigned getbits(unsigned x, int p, int n)
{
return ( x >> (p+1-n)) & ~(~0 << n);
}
3. Count one bits
This population-count example tests the low bit with bitwise AND, adds to the count when it is set, and shifts right until no bits remain. Using an unsigned input makes the right-shift behavior appropriate for this simple loop. Its running time depends on the position of the highest set bit.
#include<stdio.h>
int bitcount(unsigned x);
int main()
{
unsigned short x=38;
printf("%d has %d 1 bits\n",x,bitcount(x));
return 0;
}
int bitcount(unsigned x)
{
int b;
for(b=0; x!=0; x>>=1)
if (x&1) b++;
return b;
}
4. Remove character from string
This command-line filter prints the first argument while omitting occurrences of the first character of the second argument. Advancing the pointer in both branches ensures progress. It demonstrates character filtering, not substring replacement; an empty second argument is not meaningfully validated.
#include<stdio.h>
int main(int argc, char *argv[])
{
if (argc !=3) { printf("usage: del string char\n"); return -1;}
while(*argv[1])
{
if(*argv[1] != *argv[2]) { putchar(*argv[1]); *++argv[1]; }
else
{ *++argv[1]; continue; }
}
printf("\n");
return 0;
}
5. Convert x to binary
This conversion exercise repeatedly divides an integer by two, saving remainders and printing them backward. That is the usual digit-building strategy for a base conversion. The preserved output loop starts at the next unused array slot, producing an extra leading zero for this fixture, and the fixed array requires bounds review.
#include<stdio.h>
#define LEN 16
int main()
{
int x=112,counter=0;
int binary[LEN]={0};
while(x)
{
binary[counter]=x%2; x/=2; counter++;
}
while(counter>=0) { printf("%d",binary[counter]); counter--; }
printf("\n");
return 0;
}
6. Convert char to integer
This example builds an integer from digit characters in a command-line argument while skipping all other characters. It demonstrates pointer traversal and decimal accumulation. It does not implement signs, overflow checks or strict input validation, so it should not be described as a replacement for a checked numeric parser.
#include<stdio.h>
#define NUM 1
int main(int argc, char *argv[])
{
int counter=1,n=0;
if(argc!=2) { printf("usage: atoi arglist\n"); return -1;}
while(*argv[NUM])
{
if(*argv[NUM]>='0' && *argv[NUM]<='9') { n=10*n+(*argv[NUM]-'0'); *++argv[NUM]; }
else
{ *++argv[NUM]; continue; }
}
printf("number=%d\n",n);
return 0;
}
Reference -
https://www.amazon.com/Programming-Language-2nd-Brian-Kernighan/dp/0131103628#reader_0131103628