I am learning C and this is an exercise from The C Programmnig Languange by Brian and Ritchie.

Write a program "detab" that replaces tabs in the input with the proper number of blanks to space to the next tab stop. Assume a fixed set of tab stops, say every \$n\$ columns. Should \$n\$ be a variable or a symbolic parameter?

Here's my try, and I've made \$n\$ a parameter. I would appreciate any feedback and explanation on symbolic parameter.

#include <stdio.h>

#define TABLENGTH 8

void detab(int n);

int main () {

  int c, noOfTabs;
  noOfTabs = 0;

  while ((c = getchar()) != EOF) {

    if (c == '\t') {
    } else if (noOfTabs > 0) {
      noOfTabs = 0;


  return 0;

void detab (int n) {

  while (n > 0) {
    int i;
    for (i = 0; i < TABLENGTH; ++i) {
      putchar(' ');


The code is not ready for review, because it does not solve the exercise. It blindly replaces each tab with TABLENGTH spaces. This is not detabbing; the amount of blanks must give you a next tab stop. For example, for


the output should be

a       b
aa      b
aaa     b

(number of blanks is 7, 6, and 5 respectively, to align b at the tab stop, the 8th column). Your code produces

a        b
aa        b
aaa        b



Not the answer you're looking for? Browse other questions tagged or ask your own question.