1Likes -
1 Post By anduril462
1 inside s
This is a discussion on 000
1Likes -
1 Post By anduril462
1 inside s within the
C Programming forums, part of the General Programming Boards category; edit:
gotcha!
Code:
s[i++] = n / 2 + '0';
I'm stuck. I'm writing a function called itob which has ...
-
Tears of the stars
000\001 inside s
edit:
gotcha!
Code:
s[i++] = n / 2 + '0';
I'm stuck. I'm writing a function called itob which has to convert an integer to a value in the chosen base (I'm testing with base 2). But the s array is getting the value 000\001
validation.h
Code:
int isanumber(char*);
isanumber.c
Code:
int isanumber(char* s) {
int i = 0;
while(s[i] != '\n')
{
if(isalpha((unsigned char) s[i]) || isblank( (unsigned char) s[i]) )
break;
i++;
}
if(s[i] == '\n')
return 1;
return 0;
} itob.c
Code:
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include "Validation/isanumber.c"
#include "Headers/validation.h"
#ifndef MAXSIZE
#define MAXSIZE 20
#endif
static char reversed[MAXSIZE];
void itob(int, char*, int);
void reverse(char*);
void itob( int n, char* s, int b)
{
int sign, i;
i = 0;
if( (sign = n) < 0)
n = -n;
if(b == 2)
{
do {
s[i++] = n % 2 + '0';
if(n == 1 || n == 2)
{
s[i++] = n / 2;
break;
}
} while( (n /= 2) > 0);
}
if(sign < 0)
s[i++] = '-';
s[i] = '\0';
reverse(s);
}
void reverse(char* s)
{
int i, j;
for(i = (strlen(s) - 1), j = 0; i >= 0; i--, j++)
{
reversed[j] = s[i];
}
reversed[j] = '\0';
}
int main(void)
{
char *s = malloc(MAXSIZE * sizeof(char));
char *strnum = malloc(MAXSIZE * sizeof(char));
int base;
long long num;
do {
printf("Enter a number: ");
fgets(strnum, MAXSIZE, stdin);
num = strtoll(strnum, NULL, 10);
} while( !(isanumber(strnum)));
do {
printf("Enter a number base: ");
scanf("%d", &base);
} while(base < 2 || base > 16);
itob(num, s, base);
printf("The string representation of the number is %s\n", reversed);
free(s);
free(strnum);
return 0;
} Code:
gdb -q itob
Reading symbols from /home/thames/C/itob...done.
(gdb) break 32
Breakpoint 1 at 0x400830: file itob.c, line 32.
(gdb) break 33
Breakpoint 2 at 0x40084e: file itob.c, line 33.
(gdb) run
Starting program: /home/thames/C/itob
Enter a number: 16
Enter a number base: 2
Breakpoint 1, itob (n=2, s=0x602010 "0000", b=2) at itob.c:32
32 s[i++] = n / 2;
(gdb) cont
Continuing.
Breakpoint 2, itob (n=2, s=0x602010 "0000\001", b=2) at itob.c:33
33 break;
(gdb)
Last edited by thames; 11-15-2012 at 01:53 PM.
Reason: gotcha
-
Not sure if your "edit: gotcha!" means you figured it out, but just in case somebody else finds this:
The "\001" is an octal (base 8) escape sequence. The \ starts the escape sequence, and it includes the next 1-3 digits that are valid octal. That means the "\001" represents a single byte with octal representation 001, which is a byte with value 1 (which is different than a byte with the value of the ASCII digit '1', which has a value of 49). This is valid inside a string literals (double quotes) and char literals (single quotes). The octal sequence ends after either 3 digits, or the first character that is not valid octal (letters, punctuation or 8 or 9), whichever is first. The null terminator character, '\0', is an example of a 1-digit octal escape sequence.
Popular pages Recent additions
Similar Threads
-
By Andr3w in forum C Programming
Replies: 0
Last Post: 02-26-2012, 02:31 AM
-
By nicauron in forum C Programming
Replies: 16
Last Post: 07-06-2011, 02:33 PM
-
By princessre in forum C Programming
Replies: 2
Last Post: 04-13-2010, 03:14 PM
-
By phil in forum C++ Programming
Replies: 0
Last Post: 05-10-2004, 04:16 AM
-
By Magos in forum Windows Programming
Replies: 4
Last Post: 09-29-2003, 04:25 AM
Tags for this Thread