4

It is a very trivial question but I don't know why I am not getting the correct output. Here is what I am trying to do:

char sendBuffer[1000];
int count=0:
while(count<10)
{
    sendBuffer[0]=count;
    sendBuffer[1]='a';
    sendBuffer[2]='b';
    sendBuffer[3]='c';
    sendBuffer[4]='\0';
    printf(%s,sendBuffer);
    count=count+1;
}

In the output, all buffer is printed correctly except the first index. I want 1,2,3 and so on to be printed at the start of the buffer but it does not work. Please Help

Ajith
  • 613
  • 1
  • 15
  • 32
Ayse
  • 2,676
  • 10
  • 36
  • 61
  • `sendBuffer[1]='a'; sendBuffer[2]='b'; sendBuffer[3]='c'; sendBuffer[4]='\0';` is unnecessary to do inside a loop, since values on the right hand side are constants. Also, the max value of 1-digit number is 9, but not 10. – Display Name Mar 09 '13 at 09:05

7 Answers7

6

You need to convert that number into a character. A cheap way to do it is:

sendBuffer[0] = '0' + count;

is there anyway to display integers greater than 9

If you need that you'll want to shift to more elaborate schemes. For example, if you want to convert an integer 42 into the string "42" you can say:

#define ENOUGH ((CHAR_BIT * sizeof(int) - 1) / 3 + 2)

char str[ENOUGH];
snprint(str, sizeof str, "%d", 42);

Credit for ENOUGH goes to caf.

Community
  • 1
  • 1
cnicutar
  • 178,505
  • 25
  • 365
  • 392
  • Thank you so much :) it worked fine, but the problem is that when I want to display Integers greater than 9, it starts displaying special characters like ':', ';' and so on. is there anyway to display integers greater than 9? – Ayse Mar 09 '13 at 09:17
  • Thank you for replying, but I don't intend to display the integer as a character, rather I want to store the integer in a character array. – Ayse Mar 11 '13 at 04:32
  • @AyeshaHassan So maybe it's a different question ? As it stands your question says *"I want 1,2,3 and so on to be printed ..."*. – cnicutar Mar 11 '13 at 05:05
2

printf(%s,sendBuffer); should be printf("%s",sendBuffer);

Burkhard
  • 14,596
  • 22
  • 87
  • 108
  • 4
    OP's code wouldn't compile if that were the problem. (Most likely, the posted code is not exactly what he has). – nneonneo Mar 09 '13 at 08:35
2

Change

sendBuffer[0]=count;

to

sendBuffer[0]='0' + count;

i.e. convert the integer 0...9 to the characters '0' ... '9'

Also add a quote i.e. printf("%s",sendBuffer);

Ed Heal
  • 59,252
  • 17
  • 87
  • 127
1

To convert an integer value to a char representation, add the value of the character '0':

sendBuffer[0]=count + '0';

Notice that's the character '0', not the number 0. This is because of how ascii values work. The char with a literal value of 0 is \0, the null terminator. Digit '0' has a literal value of 48, '1' 49 and so on.

This works for the digits 0-9. You can't put a char representation of a number bigger than that in a single char, obviously.

CodeClown42
  • 11,194
  • 1
  • 32
  • 67
1

Quoted from the question:

In the output, all buffer is printed correctly except the first index.i want 1,2,3 and so on to be printed at the start of the buffer but it does not work. Please Help

I think her problem is that she does not get any output for the first line. That is because in the first iteration, count is 0, i.e. sendBuffer[0] is 0 which is the '\0' character. Hence the string is treated as empty.

uba
  • 2,012
  • 18
  • 21
1

You are trying to print the ascii characters corresponding to decimal values 0-9 which are non-printable characters. If you need to print decimal 0-9 then initialise count to 48 which is the ascii decimal code for '0' and change the condition in the while block to count < 58; 57 is the ascii deciaml code for '9'. See below:

char sendBuffer[1000];
int count=48:
while(count<58)
{
    sendBuffer[0]=count;
    sendBuffer[1]='a';
    sendBuffer[2]='b';
    sendBuffer[3]='c';
    sendBuffer[4]='\0';
    printf(%s,sendBuffer);
    count=count+1;
}
Drona
  • 6,886
  • 1
  • 29
  • 35
  • Thanks but i want to store integers greater than 9 as well. – Ayse Mar 11 '13 at 04:35
  • A char is 1 byte long while the integer is bigger. Hence, you cannot store an integer in 1 byte. You can keep count and buffer separate but still print them together like printf(%d%s, count, sendbuffer); – Drona Mar 11 '13 at 10:14
0
char sendBuffer[1000];

// take fixed stuff outside the loop
    sendBuffer[1]='a';
    sendBuffer[2]='b';
    sendBuffer[3]='c';
    sendBuffer[4]='\0';

// it's best not to rely on order of ASCII values

for(char* numbers="0123456789"; *numbers!=0; numbers++){// use for loop for numbers
    sendBuffer[0] = *numbers;

    printf("%s",sendBuffer);
}
QuentinUK
  • 2,997
  • 21
  • 20