This is mostly out of curiosity to why this is happening as it doesn't matter in my case. If I type in an invalid number it properly goes to the repeat label and asks me to enter a number again, but if i enter in a character like 'f' it will loop endlessly and not stop. Why is this?
The array and all variables here are of type int.
repeat:
printf("Enter number of available space, you are %c: ", userXO);
scanf("%d", user);
switch (*user)
{
case 1: if (spaces[0][0] == 49){ spaces[0][0] = userXO;}else goto repeat; break;
case 2: if (spaces[0][1] == 50){ spaces[0][1] = userXO;}else goto repeat; break;
case 3: if (spaces[0][2] == 51){ spaces[0][2] = userXO;}else goto repeat; break;
case 4: if (spaces[1][0] == 52){ spaces[1][0] = userXO;}else goto repeat; break;
case 5: if (spaces[1][1] == 53){ spaces[1][1] = userXO;}else goto repeat; break;
case 6: if (spaces[1][2] == 54){ spaces[1][2] = userXO;}else goto repeat; break;
case 7: if (spaces[2][0] == 55){ spaces[2][0] = userXO;}else goto repeat; break;
case 8: if (spaces[2][1] == 56){ spaces[2][1] = userXO;}else goto repeat; break;
case 9: if (spaces[2][2] == 57){ spaces[2][2] = userXO;}else goto repeat; break;
default: goto repeat; break;
}
scanf("%d", user); tries to read a number, finds a char(f), leaves it in the buffer and ends. The loop then loops around and executes scanf("%d", user); again. And again...
Here is how I would have written what you did:
int rc, user;
char buf[100];
for (;;) // repeat until explicitly broken out of
{
printf ("Enter number of available space; you are %c: ", userXO);
if (!fgets (buf, sizeof buf, stdin)) /* end of file or i/o error? */
break;
rc = sscanf(buf, "%d", &user);
if (rc != 1) /* other than one parsed input item is an error */
{
printf ("invalid number; try again\n");
continue;
}
/*
* this switch has the odd property of potentially
* doing all 9 cases for case 1, 8 cases for case 2, etc.
* Maybe explicit breaks for success are needed?
*/
switch (user)
{
case 1: if (spaces[0][0] == 49) spaces[0][0] = userX0; else continue;
case 2: if (spaces[0][1] == 50) spaces[0][1] = userX0; else continue;
case 3: if (spaces[0][2] == 51) spaces[0][2] = userX0; else continue;
case 4: if (spaces[1][0] == 52) spaces[1][0] = userX0; else continue;
case 5: if (spaces[1][1] == 53) spaces[1][1] = userX0; else continue;
case 6: if (spaces[1][2] == 54) spaces[1][2] = userX0; else continue;
case 7: if (spaces[2][0] == 55) spaces[2][0] = userX0; else continue;
case 8: if (spaces[2][1] == 56) spaces[2][1] = userX0; else continue;
case 9: if (spaces[2][2] == 57) spaces[2][2] = userX0; else continue;
default: continue;
}
break; /* if valid case(s) taken, exits loop */
}
As you can see, there is no need for a label or a goto. Also the code is more compact.
Related
I have this bit of code where I need to enter specific course CRN numbers, but if I enter anything but that number I want it to give me an error saying that isnt a possible answer choice. I thought this was how the != worked but when I type in the right answers I think get the "Sorry that is not an option"
if(amount == 1){
printf("Enter the course number:\n");
scanf("%f", &course);
if(course != 4587){
printf("Sorry invalid entry!\n");
}
else if(course != 4599){
printf("Sorry invalid entry!\n");
}
else if(course != 8997){
printf("Sorry invalid entry!\n");
}
else if(course != 9696){
printf("Sorry invalid entry!\n");
}
else{
switch(course)
{
case '4587':
credit1 = 4;
break;
case '4599':
credit2 = 3;
break;
case '8997':
credit3 = 1;
break;
case '9696':
credit4 = 3;
break;
}
}
}
It does not do what you think. Your else is only if the value == so checking against any other values does not make sense
switch(course)
{
case 4587:
credit1 = 4;
break;
case 4599:
credit2 = 3;
break;
case 8997:
credit3 = 1;
break;
case 9696:
credit4 = 3;
break;
default:
printf("Sorry invalid entry!\n");
break;
}
Currently you are doing the following:
If course isn't 4587, print an error.
If it is 4587, check if it is also 4599. If it isn't, print an error... and so on.
As you see, even If the first case is met, it's impossible for it to meet all cases as an integer can only have one value.
Try doing something like this:
if(course == 4587){
credit1 = 4;
} else if(course == 4599){
credit2 = 3;
} else if(course == 8997){
credit3 = 1;
} else if(course == 9696){
credit4 = 3;
} else {
printf("Sorry invalid entry!\n");
}
When passing %f to scanf, it will return a float type, which stands for a fraction. From the man page for scanf:
f
Matches an optionally signed floating-point number; the next pointer
must be a pointer to float.
Later on, when it is being compared to a number of integer type, it fails.
The variable course should probably be declared as int.
Later on, in the switch case you use strings instead of integers.
The cases such as case '4587': should be case 4587: instead, and it will work.
I am trying to create a tic-tac-toe game in C.
My instructions are as follows:
Write a C program that lets two people play tic-tac-toe. Use a global array: char ttt[3][3]; to represent the board state, and globals for the player (a char to hold X or O or whatever you characters you want), i and j integers for indexes, and an integer to count the number of moves completed. Show the game board for each move two characters for the players with a key for the user to type one character or digit to say what spot to take in the board. At the end of a game, say who won or that it is a tie. Have at least the functions to print the board, get a valid move, and check for a win; do use printf, scanf, if-else, switch (to find indexes into ttt) while, for.
My code is as follows:
#include <stdio.h>
#include <conio.h>
// Global array for the board
char ttt[3][3] = {
{'1', '2', '3'},
{'4', '5', '6'},
{'7', '8', '9'}
};
void PrintTheBoard() { // Print the board as it currently is
printf("| %c | | %c | | %c |\n", ttt[0][0], ttt[0][1], ttt[0][2]);
printf("| %c | | %c | | %c |\n", ttt[1][0], ttt[1][1], ttt[1][2]);
printf("| %c | | %c | | %c |\n", ttt[2][0], ttt[2][1], ttt[2][2]);
}
// Check for a win
int CheckForWin() {
// Checks for horizontal win conditions
if (ttt[0][0] == ttt[0][1] && ttt[0][1] == ttt[0][2])
return 1;
else if (ttt[1][0] == ttt[1][1] && ttt[1][1] == ttt[1][2])
return 1;
else if (ttt[2][0] == ttt[2][1] && ttt[2][1] == ttt[2][2])
return 1;
// Checks for vertical wins
else if (ttt[0][0] == ttt[1][0] && ttt[1][0] == ttt[2][0])
return 1;
else if (ttt[0][1] == ttt[1][1] && ttt[1][1] == ttt[2][1])
return 1;
else if (ttt[0][2] == ttt[1][2] && ttt[1][2] == ttt[2][2])
return 1;
// Checks for diagonal wins
else if (ttt[0][0] == ttt[1][1] && ttt[1][1] == ttt[2][2])
return 1;
else if (ttt[0][2] == ttt[1][1] && ttt[1][1] == ttt[2][0])
return 1;
else if (ttt[0][0] != '1' && ttt[0][1] != '2' && ttt[0][2] != '3' &&
ttt[1][0] != '4' && ttt[1][1] != '5' && ttt[1][2] != '6' &&
ttt[2][0] != '7' && ttt[2][1] != '8' && ttt[2][2] != '9')
return 2;
else
return 0;
}
int main() { // The function below gets a move, validates it, and keep tracks of the # of moves made.
int choice;
int player = 1;
int i;
int counter = 0;
char mark;
int isValid;
// Gets user input
do {
PrintTheBoard();
player = (player % 2) ? 1 : 2;
printf("Player %d, enter a number: ", player);
scanf("%d", &choice);
// Determines what mark to make, depending on the current player by way of "if current player is player 1, use X, otherwise, use O"
mark = (player == 1) ? 'X' : 'O';
/*
The below switch function is a bit convoluted. Depending on the value of the "choice" variable (1-9, chosen by the active player
and corresponding to a position on the board), the value is checked for position validity by checking if the position in the
array corresponding to the choice still has its original numeral value, which indicates the spot is not taken.
If it still has that original value, the position is assigned an X or an O depending on the current player.
If the spot is taken, indicated by the value not being equal to its original numeral, the player is told that the position is
invalid.
If the player does not choose a valid case, the player is informed of this and told what to do.
*/
switch(choice) {
case 1:
if (ttt[0][0] == '1') {
ttt[0][0] = mark;
}
break;
case 2:
if (ttt[0][1] == '2') {
ttt[0][1] = mark;
}
break;
case 3:
if (ttt[0][2] == '3') {
ttt[0][2] = mark;
}
break;
case 4:
if (ttt[1][0] == '4') {
ttt[1][0] = mark;
}
break;
case 5:
if (ttt[1][1] == '5') {
ttt[1][1] = mark;
}
break;
case 6:
if (ttt[1][2] == '6') {
ttt[1][2] = mark;
}
break;
case 7:
if (ttt[2][0] == '7') {
ttt[2][0] = mark;
}
break;
case 8:
if (ttt[2][1] == '8') {
ttt[2][1] = mark;
}
break;
case 9:
if (ttt[2][2] == '9') {
ttt[2][2] = mark;
}
break;
default:
printf("Invalid input. Please choose and type a number 1-9 corresponding to a position that is not already taken.\n");
printf("Press any key to continue.\n");
player--;
getch();
}
i = CheckForWin();
if (i != 1 && i != 2)
player++;
} while (i == 0);
PrintTheBoard();
if (i == 1)
printf("Player %d wins!", player);
else
printf("The game is a draw!");
}
My issue is that the error message is not printed when the two players choose the same spot. The program then goes to the next players turn, effectively skipping the player who chose an occupied spot. The player who occupied the spot in question keeps the spot.
Furthermore, I don't really understand how to use i and j integers to fill out the indexes. When I try code blocks previously used for this, tons of errors show up.
Thanks for any help.
Try adding an extra boolean variable and moving the error message outside of the switch:
int okay = 0;
switch (choice) {
case 1:
if (ttt[0][0] == '1') {
ttt[0][0] = mark;
okay = 1;
}
break;
case 2:
if (ttt[0][1] == '2') {
ttt[0][1] = mark;
okay = 1;
}
break;
case 3:
if (ttt[0][2] == '3') {
ttt[0][2] = mark;
okay = 1;
}
break;
case 4:
if (ttt[1][0] == '4') {
ttt[1][0] = mark;
okay = 1;
}
break;
case 5:
if (ttt[1][1] == '5') {
ttt[1][1] = mark;
okay = 1;
}
break;
case 6:
if (ttt[1][2] == '6') {
ttt[1][2] = mark;
okay = 1;
}
break;
case 7:
if (ttt[2][0] == '7') {
ttt[2][0] = mark;
okay = 1;
}
break;
case 8:
if (ttt[2][1] == '8') {
ttt[2][1] = mark;
okay = 1;
}
break;
case 9:
if (ttt[2][2] == '9') {
ttt[2][2] = mark;
okay = 1;
}
break;
}
if (! okay) {
printf("Invalid input. Please choose and type a number 1-9 corresponding to a position that is not already taken.\n");
printf("Press any key to continue.\n");
player--;
getch();
}
With a bit of trickery, we can make this a bit more compact:
int savemark = mark;
switch (choice) {
case 1:
if (ttt[0][0] == '1') {
ttt[0][0] = mark++;
}
break;
case 2:
if (ttt[0][1] == '2') {
ttt[0][1] = mark++;
}
break;
case 3:
if (ttt[0][2] == '3') {
ttt[0][2] = mark++;
}
break;
case 4:
if (ttt[1][0] == '4') {
ttt[1][0] = mark++;
}
break;
case 5:
if (ttt[1][1] == '5') {
ttt[1][1] = mark++;
}
break;
case 6:
if (ttt[1][2] == '6') {
ttt[1][2] = mark++;
}
break;
case 7:
if (ttt[2][0] == '7') {
ttt[2][0] = mark++;
}
break;
case 8:
if (ttt[2][1] == '8') {
ttt[2][1] = mark++;
}
break;
case 9:
if (ttt[2][2] == '9') {
ttt[2][2] = mark++;
}
break;
}
if (mark == savemark) {
printf("Invalid input. Please choose and type a number 1-9 corresponding to a position that is not already taken.\n");
printf("Press any key to continue.\n");
player--;
getch();
}
You mentioned that you must use a switch but expressed the desire to see the more compact solution. So, here's a way:
int okay = 0;
do {
if (choice < 1)
break;
if (choice > 9)
break;
choice -= 1;
if (ttt[choice / 3][choice % 3] == ('1' + choice)) {
ttt[choice / 3][choice % 3] = mark;
okay = 1;
}
} while (0);
if (! okay) {
printf("Invalid input. Please choose and type a number 1-9 corresponding to a position that is not already taken.\n");
printf("Press any key to continue.\n");
player--;
getch();
}
This program takes the input
1283
5105
and prints out the following, I was wondering how to do it the opposite way around so to take the LC-3 instruction as the input and display it in Hexadecimal as I've been stuck on that for quite awhile. Any help would be great !
`add r1,r2,r3
and r0,r4,r5`
#include<stdio.h>
#include<string.h>
#include<ctype.h>
int main(int arga, char *argb[]){
int str; //string
int firstReg=0;
int secondReg=0;
int opCode=0;
int reg=0; //register
char hexa[7]; //hexadecimal value
FILE *fp;
fp = fopen(argb[1],"r");
// Statement 1
while((fscanf(fp,"%s",hexa))!=EOF){
int x =0;
str = 0;
// Statement 2
for (;x<=3;x++){
switch(hexa[x]){
case '0': str = str|0;
break;
case '1': str = str|1;
break;
case '2': str = str|2;
break;
case '3': str = str|3;
break;
case '4': str = str|4;
break;
case '5': str = str|5;
break;
case '6': str = str|6;
break;
case '7': str = str|7;
break;
case '8': str = str|8;
break;
case '9': str = str|9;
break;
case 'a': str = str|10;
break;
case 'b': str = str|11;
break;
case 'c': str = str|12;
break;
case 'd': str = str|13;
break;
case 'e': str = str|14;
break;
case 'f': str = str|15;
break;
default: printf("\nInvalid hexadecimal digit %c ",hexa[x]); return 0;
}
if(x != 3){
str = str <<4;
}
}
opCode = str&(15<<12);
opCode = opCode>>12;
// Statement 3
if(opCode == 5){
printf("and");
reg= str&(7<<9);
reg= reg>>9;
printf(" r%d",reg);
firstReg =str & (7<<6);
firstReg= firstReg>>6;
printf(", r%d",firstReg);
secondReg = str &(7);
printf(", r%d",secondReg);
printf("\n");
}
// Statement 4
if(opCode == 1) {
printf("add");
reg= str&(7<<9);
reg= reg>>9;
printf(" r%d",reg);
firstReg =str & (7<<6);
firstReg= firstReg>>6;
printf(", r%d",firstReg);
secondReg = str &(7);
printf(", r%d",secondReg);
printf("\n");
}
}
fclose(fp);
return 0;
}
I have written one C application contains switch statements.When I tried to run the application,it will prompt for options,sometimes when i tried to enter one option the system is not responding,only after 2 or more time I am forced to enter the same option with enter.
Is there any problem in my code?
Am executing the application in my development board ,through Terminal.
See below my code snippet
#include "TestApp.h"
int main(void) {
BTON = "ON"; // To hold Bluetooth status as ON
BTOFF = "OFF"; // To hold Bluetooth status as OFF
int mainChoice, btChoice, zigChoice; // To select choices
char key, keyMain, c; // To handle continuity of the program
int flag = 0; // To handle continuity of the program
btstatus = "OFF";
puts("!!! Test App!!!\n");
do {
printf("\nMain Menu: Enter your choice\n"
"1.Bluetooth Test\n"
"2.Zigbee Test\n"
"3.Brightness Test\n");
scanf("%d", &mainChoice);
switch (mainChoice) {
case 1:
flag = 1;
do {
printf("Enter your choice for Bluetooth :\n"
"1. Turn on bluetooth\n"
"2. Turn on visibility\n"
"3. Set passkey\n"
"4. Scan bluetooth device\n"
"5. Display bluetooth configuration\n"
"6. Reset Passkey\n"
"7. Turn off visibility\n"
"8. Turn off bluetooth\n");
scanf("%d", &btChoice);
bluetoothTest(btChoice);
do {
printf(
"Do u want to continue with bluetooth test? (y/n) \n");
while (((c = getchar()) != '\n') && (c != EOF))
;
key = getchar();
if (key != 'y' && key != 'n') {
printf("Invalid choice\n");
}
} while (key != 'y' && key != 'n');
} while (key == 'y');
break;
case 2:
flag = 1;
do {
printf("Enter your choice for Zigbee :\n"
"1. To Enable Zigbee Module\n"
"2. Display Firmware version\n"
"3. Display network information\n"
"4. Disassociate from PAN\n"
"5. Establish PAN\n"
"6. Scan for PAN\n"
"7. Join network\n"
"8. Scan Network \n"
"9. Broadcast message\n");
scanf("%d", &zigChoice);
zigbeeTest(zigChoice);
do {
printf("Do u want to continue with zigbee test? (y/n) \n");
while (((c = getchar()) != '\n') && (c != EOF))
;
key = getchar();
if (key != 'y' && key != 'n') {
printf("Invalid choice\n");
}
} while (key != 'y' && key != 'n');
} while (key == 'y');
break;
case 3:
flag = 1;
do {
brightnessTest();
do {
printf(
"Do u want to continue with brightness test? (y/n) \n");
while (((c = getchar()) != '\n') && (c != EOF))
;
key = getchar();
if (key != 'y' && key != 'n') {
printf("Invalid choice\n");
}
} while (key != 'y' && key != 'n');
} while (key == 'y');
break;
default:
printf("\n Invalid choice\n");
break;
}
do {
printf("Do u want to continue Test App? (y/n) \n");
if (flag == 1) {
while (((c = getchar()) != '\n') && (c != EOF))
;
}
keyMain = getchar();
if (keyMain != 'y' && keyMain != 'n') {
printf("Invalid choice\n");
}
} while (keyMain != 'y' && keyMain != 'n');
if (keyMain == 'n') {
break;
}
} while (keyMain == 'y');
return EXIT_SUCCESS;
}
void bluetoothTest(int option) {
switch (option) {
case 1:
turnBluetooth(BTON);
break;
case 2:
setVisibility(BTON);
break;
case 3:
setPasskey();
break;
case 4:
scanBTDevice();
break;
case 5:
displayBTConfiguration();
break;
case 6:
resetPasskey();
break;
case 7:
setVisibility(BTOFF);
break;
case 8:
turnBluetooth(BTOFF);
break;
default:
printf("\n Invalid choice\n");
break;
}
}
void zigbeeTest(int option) {
switch (option) {
case 1:
printf("\n Zigbee Module Enabled!!!! \n");
zigbeeEnable();
break;
case 2:
displayFirmwareVersion();
break;
case 3:
displayNetworkInfo();
break;
case 4:
disAssociateFromPan();
break;
case 5:
establishPan();
break;
case 6:
scanForPan();
break;
case 7:
joinNetwork();
break;
case 8:
scanNetwork();
break;
case 9:
broadcastMessage();
break;
default:
printf("\n Invalid choice\n");
break;
}
}
void brightnessTest() {
float brightval;
printf("Enter brightness value from 0 to 10 :\n ");
scanf("%f", &brightval);
setBrightness(brightval);
}
I'm new to C and I've been working on this homework problem for about 2 hours to no avail. I'm attempting to create a program that takes an alphabetic phone number (ie; CALLATT or 1-800-COL-LECT) and turns it into the number form (2255288 or 1-800-265-5328). No matter what I put for input, though, I always get -4197680 for my output.
int main(void){
int c=0, len, a[len];
char n[len];
printf("Enter phone number: \n");
scanf("%c", n);
len = sizeof(n) / sizeof(n[0]);
while (len > c){
if (n[c] == 'A' || n[c] == 'B' || n[c] == 'C'){
a[c] = 2;
c++;
}
else if (n[c] == 'D' || n[c] == 'E' || n[c] == 'F'){
a[c] = 3;
c++;
}
else if (n[c] == 'G' || n[c] == 'H' || n[c] == 'I'){
a[c] = 4;
c++;
}
else if (n[c] == 'J' || n[c] == 'L' || n[c] == 'L'){
a[c] = 5;
c++;
}
else if (n[c] == 'M' || n[c] == 'N' || n[c] == 'O'){
a[c] = 6;
c++;
}
else if (n[c] == 'P' || n[c] == 'Q' || n[c] == 'R' || n[c] == 'S'){
a[c] = 7;
c++;
}
else if (n[c] == 'T' || n[c] == 'U' || n[c] == 'V'){
a[c] = 8;
c++;
}
else if (n[c] == 'W' || n[c] == 'X' || n[c] == 'Y' || n[c] == 'Z'){
a[c] = 9;
c++;
}
else {
a[c] = n[c];
c++;
}
}
printf("%d\n", a);
return 0;
}
EDIT: Revised. There were many comments pointing out problems, here is my answer which works with a reasonable length phone number. It skips any non-dialing characters, such as '-' which is not part of a phone number.
#include <stdio.h>
#include <string.h>
#include <ctype.h>
int main(void){
int k, d, e, len;
char dial[20], entry[20] = {0};
printf("Enter phone number: ");
fgets(entry, 19, stdin);
len = strlen(entry);
d = 0; // dial string index of output
for (e=0; e<len; e++) { // entry string index of input
k = entry[e];
switch (toupper(k)) {
case 'A': case 'B': case 'C': dial[d++] = '2'; break;
case 'D': case 'E': case 'F': dial[d++] = '3'; break;
case 'G': case 'H': case 'I': dial[d++] = '4'; break;
case 'J': case 'K': case 'L': dial[d++] = '5'; break;
case 'M': case 'N': case 'O': dial[d++] = '6'; break;
case 'P': case 'Q': case 'R': case 'S': dial[d++] = '7'; break;
case 'T': case 'U': case 'V': dial[d++] = '8'; break;
case 'W': case 'X': case 'Y': case 'Z': dial[d++] = '9'; break;
default:
if (isdigit(k) || k=='*' || k=='#') dial[d++] = k;
}
}
dial[d] = 0; // terminate string
printf("Dial %s\n", dial);
return 0;
}
Here is some code:
char buf[32];
sscanf("%31s", buf);
size_t i;
for (i = 0; i < sizeof(buf) && buf[i]; ++i)
{
switch (buf[i])
{
case 'A': case 'B': case 'C':
buf[i] = '2'; break; // Note: character literal, not integer
case 'D': case 'E': case 'F':
buf[i] = '3'; break;
....
}
}
printf("%s", buf);
If you have a Posix-compliant library, you can use dynamic allocation:
char *buf;
scanf("%ms", &buf); //scanf would allocate memory
for (i = 0; buf[i]; ++i)
{
.....
}
printf("%s", buf);
free(buf);
There are so many problems in your code, it will almost need a re-write to make it work. I think you should start something small. Make sure it works before adding more functionality. I would suggest dividing the code in main into three sections -- reading the phone number, converting phone number and printing the converted phone number.
Here's a skeletal program that captures those three steps.
#define SIZE 50
void readPhoneNumber(char phoneNumber[])
{
}
void convertTextToNumber(char phoneNumber[], char dialedNumber[])
{
}
void printPhoneNumber(char phoneNumber[])
{
}
int main(void)
{
char phoneNumber[SIZE];
char dialedNumber[SIZE];
readPhoneNumber(phoneNumber);
convertTextToNumber(phoneNumber, dialedNumber);
printPhoneNumber(dialedNumber);
}
Now, you can start fleshing out the functions. For example, readPhoneNumber can be implemented as:
void readPhoneNumber(char phoneNumber[])
{
printf("Enter phone number: \n");
fgets(phoneNumber, SIZE, stdin);
}
printPhoneNumber can be implemented as:
void printPhoneNumber(char phoneNumber[])
{
printf("%s\n", phoneNumber);
}
I'll leave you to work out the implementation of convertTextToNumber.
Here you have undefined behavior, len is not initialized.
int c=0, len, a[len];
char n[len];
Use instead a constant value instead, i bet the phone number in your country has some kind of maximum length.
This way to read from the keyboard is not recommended, scanf does not check for length of string so you can do a faceroll on the keyboard and your program will crash. Instead use fgets( ) to read from stdin then go through the string char by char skipping the included \n
printf("Enter phone number: \n");
scanf("%c", n);
This makes no sense, you calculate the sizeof n i.e. of the integer that holds n. If you want the length of the string use strlen( n ); btw try to use more descriptive variable names.
len = sizeof(n) / sizeof(n[0]);
Instead of
while (len > c){
why not use a normal for-loop ? you seem to increment c++ everywhere.
this here will not do what you expect it to do
printf("%d\n", a);
but you assign 'a' integers e.g.
a[c] = 2;
printf can not magically print a number of your array, instead you want to print out is a string with the numbers. the ascii value of a digit is 48 + digit. e.g. '0' is 48, by knowing this have a character buffer and add the ascii values to it. make sure it ends with \0 which is end of string. then print out the string
buf[c++] = 48 + digit;
...
buf[c] = '\0';
puts( buf );