C program (string - binary to decimal conversion)

In summary: Yes, strtoul() is a standard library function for converting strings to unsigned long integers. However, it may not have been introduced yet in the curriculum for the students to use.
  • #1
clook
35
0
here's what i have:

Code:
#include<stdio.h>
#include<string.h>
main() {
char string[32];
int sum = 0;
int a = 1;
int i;
printf("Please enter numbers:\n");
gets(string);
for (i=32;i<0;i--)
{
if(string[i] ==1)
{ sum = sum + a;
}
a = a * 2;
printf("The converted string is: %s \n", string[i]);
}

won't compile.. says "compound" statement is missing at the end.

what am i doing wrong?



Homework Equations





The Attempt at a Solution

 
Physics news on Phys.org
  • #2
Count the number of left and right braces {}. You just spaced the one at the end of the main().
 
  • #3
have you noticed one thing...that 1)in the program you have opened three braces and two closed braces{}, 2)why don't you declare as
int sum=o,a=1,i; 3)why don't you prefer to use getch( ); before the ending brace.
 
  • #4
2)why don't you declare as
int sum=o,a=1,i;
It's a matter of style. Some prefer not to declare many things on one line, especially if there are initializers or modifiers involved.

3)why don't you prefer to use getch( ); before the ending brace.
(A) There's no reason to wait for a keypress
(B) getch is a nonstandard extension to the C language. You can only use it if you happen to be using a compiler or library that defines it. (Why not use getchar() instead?)
 
  • #5
Did your teacher ever mention strtoul() ?
 

FAQ: C program (string - binary to decimal conversion)

What is a C program?

A C program is a computer program written in the C programming language. It is a high-level programming language used for developing operating systems, applications, and device drivers.

What is a string in C?

A string in C is a sequence of characters stored in a contiguous block of memory. It is represented by an array of characters terminated by a null character ('\0'). Strings in C are used for storing and manipulating text data.

What is binary to decimal conversion?

Binary to decimal conversion is the process of converting a binary number (base 2) to its equivalent decimal number (base 10). This conversion involves multiplying each digit of the binary number by the corresponding power of 2 and then adding the results together.

How do I convert a binary number to decimal in a C program?

To convert a binary number to decimal in a C program, you can use the strtol() function from the stdlib.h library. This function takes the binary number as a string and returns its decimal equivalent as a long int data type.

Can I convert a decimal number to binary in a C program?

Yes, you can convert a decimal number to binary in a C program using the itoa() function from the stdlib.h library. This function takes the decimal number as an integer and converts it to a binary string representation, which can then be printed or manipulated as needed.

Similar threads

Replies
7
Views
2K
Replies
12
Views
2K
Replies
3
Views
1K
Replies
2
Views
1K
Replies
11
Views
2K
Replies
12
Views
1K
Replies
2
Views
2K
Back
Top