C Programming Common Errors

EMMANUEL MATEMBU DOMINIC
3 min readDec 2, 2021

As to every programming language, we ought to learn how to debug errors or bugs which are actually inevitable but can be avoided once you understand and have the consciousness about language. In my experience learning C programming, I have encountered errors like:-

  • Scanf or Printf function being skipped.
  • gets skipped when proceeded with scanf function.
  • gets or puts or Printf functions makes pointer from integer without a cast.

NB: “Debugging is the process to trace the error in the application.”

1. Scanf function being skipped.

/* Sample code of scanf function skipped */#include <stdio.h>;int main(){
int age;
char name;
printf("How old are you?: ");
scanf("%d", &age);
printf("What is your name?: ");
scanf("%c", &name);
printf("%c is %d years old.", name, age);
}

Here, you will realize that the problem is with the trailing newline characters after the second call to scanf() which is of the%d type specifier, when you press Enter on your keyboard a newline character "\n" is left in the stream and the next scanf() tries to read that newline character thus it seems to have just skipped the input, but it read the newline character. So, unless you are interested in whitespace like newlines, do not use %c but simply use the string conversion %s. In other words, the newline character is stored in the variable namethus skipping to ask you for input for that variable.

By debugging the error, change scanf("%c", &name);toscanf(" %c", &name); , however, notice the space between the " %c" .

2. gets skipped when proceeded with scanf function.

You realize this error is quite similar to the above error of scanf function being skipped because these two are both affected by the whitespace "\n" which isn’t stored thus used by the gets function.

/* Sample code of gets function skipped when proceeded with scanf function */#include <stdio.h>;int main(){
int age;
char name[8];
printf("How old are you?: ");
scanf("%d", &age);
printf("What is your name?: ");
gets(name);
printf("%c is %d years old.", name, age);
}

Once again here, you should always be mindful of the overflows. As you can choose to use scanf or fgetsinstead of gets since all scanf conversion specifiers except %s ignore white space including newlines, which is designed to read sequences of input tokens “numbers” or “words” known as the array of characters where the amount and nature of white space are irrelevant. So, words can all be on the same line or each word on a different line scanf wouldn't care unless you force single-character read with %c which is almost never necessary.

Else, another simple solution for the problem would be adding fflush(stdin); between scanf(); and gets(); to flush the input buffer of a stream.

printf("How old are you?: ");
scanf("%d", &age);
fflush(stdin);printf("What is your name?: ");
gets(name);

Therefore, for output streams, fflush(); forces a write of all user-space buffered data for the given output or update stream via the stream’s underlying write function while for input streams, fflush(); discards any buffered data that has been fetched from the underlying file, but has not been consumed by the application.

3. gets or puts or Printf functions makes pointer from integer without a cast.

The gets();, puts(); and printf(); are declared in the header file stdio.h. So, all these functions are involved in the input/output operations of the strings.

/* Sample code of gets, puts, printf function makes pointer from integer without a cast */#include <stdio.h>;int main(){
int age;
char name[8] = "Dominic";
age = 18;
puts(age);
printf(name); printf("How old are you?: ");
gets(age);
printf("%c is %d years old.", name, age);
}

Something to note is that puts stands for put string therefore it's not a good idea to pass an integer to a string because this cause the program to crash.

And printf(name);fails because a string literal is just a pointer to an array of char, this can only be represented by printf("%c", name); or puts(name);.

Similarly, to gets(); accepts strings yet and integer is passed to it, therefore this can be modified to scanf("%d", &age);

Making the program:

/* Sample code that solve the error of gets, puts, printf function makes pointer from integer without a cast */#include <stdio.h>;int main(){
int age;
char name[8] = "Dominic";
age = 18;
printf("%d", age);
printf("\n");
puts(name); printf("How old are you?: ");
scanf("%d", &age);
printf("%c is %d years old.", name, age);
}

Conclusion:

While programming in C, take note of whitespace and different pointers (data types) passed to puts, gets, print functions. Because these can keep you and your code messed up.

  • gets skipped when proceeded with scanf function.

--

--

EMMANUEL MATEMBU DOMINIC

I love brainstorming and believe to be an excellent collaborator and a communicator whilst maintaining Integrity and confidentiality at all levels.