LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 03-07-2009, 09:04 AM   #1
SpLaSh212
Member
 
Registered: Mar 2006
Posts: 42

Rep: Reputation: 15
ANSI C decimal to octal


hey guys,
how can I transform a decimal variable to octal variable ?

for example, I have n=9, and I want to save to octal type to variable 'oc_n' ...
thnx !
 
Old 03-07-2009, 09:25 AM   #2
johnsfine
LQ Guru
 
Registered: Dec 2007
Distribution: Centos
Posts: 5,286

Rep: Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197
I see you've been getting pretty good help with your homework from LQ so far. But you would learn more if you listened in class.

I don't think the actual assignment said anything about "octal variable" or "octal type".

I expect most of the programmers here could guess what the assignment really was (despite the bad description you just posted) and do it for you. But I hope no one will.

Representing a number in a specific numeric base means representing it as a series of digits. In C, the data type you would use to store a series of digits is an array of char.

Last edited by johnsfine; 03-07-2009 at 09:28 AM.
 
Old 03-07-2009, 09:29 AM   #3
alex1983-0112
Member
 
Registered: Apr 2008
Location: Russia
Distribution: Fedora Core
Posts: 30

Rep: Reputation: 16
/*To Convert a Decimal number to Octal*/
extern int printf(const char *fmt , ...);
void main()
{
int m,i,n,z,x,b,c,d[100];
printf("Enter a Number : ");
scanf("%d",&n);
z=n;
b=1;
while(n>0)
{
c=n%8;
b=b*10+c;
n=n/8;
}
n=b;
b=0;
while(n>0)
{
c=n%10;
b=b*10+c;
n=n/10;
}
b=b/10;
printf("Octal Value for the given number is : %d ",b);
}

Hope this help.
 
Old 03-07-2009, 09:40 AM   #4
bgeddy
Senior Member
 
Registered: Sep 2006
Location: Liverpool - England
Distribution: slackware64 13.37 and -current, Dragonfly BSD
Posts: 1,810

Rep: Reputation: 232Reputation: 232Reputation: 232
Well the printf function provides octal formats as standard so simply this :

Code:
printf("The number in octal=%o",myint);
Will show the integer in myint in octal. Obviously this :
Code:
printf("The number in decimal=%d",myint);
- will print the integer in decimal form.

These formats may be used with the scanf/printf/sprintf functions to get,output or store in a character string numbers in different representations.
 
Old 03-07-2009, 10:19 AM   #5
SpLaSh212
Member
 
Registered: Mar 2006
Posts: 42

Original Poster
Rep: Reputation: 15
bgeddy: I know that, but I need to save the different representations of the value in a data sheet, not to print it to the screen ...

alex1983-0112: thanks alot ! btw, why did u initialized so many unused variables ?
 
Old 03-07-2009, 10:32 AM   #6
johnsfine
LQ Guru
 
Registered: Dec 2007
Distribution: Centos
Posts: 5,286

Rep: Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197
Quote:
Originally Posted by SpLaSh212 View Post
not to print it to the screen
You can use sprintf instead of printf

If this isn't homework, that is your easiest option. If it is homework, that might depend on details of the assignment.
 
Old 03-07-2009, 11:06 AM   #7
wje_lq
Member
 
Registered: Sep 2007
Location: Mariposa
Distribution: FreeBSD,Debian wheezy
Posts: 811

Rep: Reputation: 179Reputation: 179
Quote:
Originally Posted by SpLaSh212 View Post
alex1983-0112: thanks alot ! btw, why did u initialized so many unused variables ?
In this code?
Code:
/*To Convert a Decimal number to Octal*/
extern int printf(const char *fmt , ...);
void main()
{
 int m,i,n,z,x,b,c,d[100];
 printf("Enter a Number : ");
 scanf("%d",&n);
 z=n;
 b=1;
 while(n>0)
 {
  c=n%8;
  b=b*10+c;
  n=n/8;
 }
 n=b;
 b=0;
 while(n>0)
 {
  c=n%10;
  b=b*10+c;
  n=n/10;
 }
 b=b/10;
 printf("Octal Value for the given number is : %d ",b);
}
There are many more things wrong with this code. Since this is a homework assignment, you'll have plenty of opportunity to clean them up so you can transform this from a C- to an A.

I won't point out the defects, because it's your homework assignment. I don't normally just slam someone's work without being specific, but I felt a warning was necessary.

Three weeks from today (if I remember and if the moderator doesn't lock the thread, please please) I will come back and explain.
 
Old 03-07-2009, 11:52 AM   #8
bgeddy
Senior Member
 
Registered: Sep 2006
Location: Liverpool - England
Distribution: slackware64 13.37 and -current, Dragonfly BSD
Posts: 1,810

Rep: Reputation: 232Reputation: 232Reputation: 232
Quote:
bgeddy: I know that, but I need to save the different representations of the value in a data sheet, not to print it to the screen ...
Well the point is the printf family of functions (fprintf,sprintf,etc) provide ready to use easy format conversions. The integer variable itself I used is constant but it's representation changes via the format specifier as desired.
 
Old 03-07-2009, 12:02 PM   #9
alex1983-0112
Member
 
Registered: Apr 2008
Location: Russia
Distribution: Fedora Core
Posts: 30

Rep: Reputation: 16
Quote:
Originally Posted by SpLaSh212 View Post
alex1983-0112: thanks alot ! btw, why did u initialized so many unused variables ?
I cut out and have inserted this piece of a code from the project which wrote at studying of programming language C. It is simply indicative example. I would consider that he has helped you to understand a principle and on the basis of this principle already to realise that what you want. I have made something not so?
 
Old 03-07-2009, 01:06 PM   #10
wje_lq
Member
 
Registered: Sep 2007
Location: Mariposa
Distribution: FreeBSD,Debian wheezy
Posts: 811

Rep: Reputation: 179Reputation: 179
Quote:
Originally Posted by alex1983-0112 View Post
I have made something not so?
Yes and no. The other things I wish to say about the code are not of vital importance, and I intend to address them in three weeks, past the likely homework due date.
 
Old 03-07-2009, 01:17 PM   #11
alex1983-0112
Member
 
Registered: Apr 2008
Location: Russia
Distribution: Fedora Core
Posts: 30

Rep: Reputation: 16
2wje_lq:

Sorry, maybe be I do not understand that? But what you mean under "homework"? I newbee at a forum also am in easy misunderstanding.
Sorry again.
 
Old 03-07-2009, 01:35 PM   #12
H_TeXMeX_H
LQ Guru
 
Registered: Oct 2005
Location: $RANDOM
Distribution: slackware64
Posts: 12,928
Blog Entries: 2

Rep: Reputation: 1301Reputation: 1301Reputation: 1301Reputation: 1301Reputation: 1301Reputation: 1301Reputation: 1301Reputation: 1301Reputation: 1301Reputation: 1301
Homework is something a teacher gives you, and that YOU should complete by yourself, and not have us do it for you. Why ? Because it's actually bad for you, what are you gonna do on the test ? We're not gonna be there with you.

Either way for this I agree with johnsfine, use printf or sprintf.

Take a look here, it even gives you an example:
http://www.cplusplus.com/reference/c...o/sprintf.html

Last edited by H_TeXMeX_H; 03-07-2009 at 01:36 PM.
 
Old 03-07-2009, 01:52 PM   #13
alex1983-0112
Member
 
Registered: Apr 2008
Location: Russia
Distribution: Fedora Core
Posts: 30

Rep: Reputation: 16
It concerns me? Y/N?

P.S. It seems I have problem with English....:-(
 
Old 03-07-2009, 02:00 PM   #14
wje_lq
Member
 
Registered: Sep 2007
Location: Mariposa
Distribution: FreeBSD,Debian wheezy
Posts: 811

Rep: Reputation: 179Reputation: 179
Quote:
Originally Posted by alex1983-0112 View Post
It concerns me? Y/N?

P.S. It seems I have problem with English....:-(
Actually, your English is pretty good. My Russian is nonexistent, so you're way ahead of me!

And the only way the homework issue affects you is that we try to avoid giving source examples to people like SpLaSh212 who are completing homework assignments, because they should write the code themselves.

Once they've done that, if they have questions about why their code doesn't work, then we jump in and help.

Any other way, and they'd never learn anything.
 
Old 03-07-2009, 02:33 PM   #15
alex1983-0112
Member
 
Registered: Apr 2008
Location: Russia
Distribution: Fedora Core
Posts: 30

Rep: Reputation: 16
Lightbulb

Quote:
Originally Posted by wje_lq View Post
Actually, your English is pretty good. \
Thank you, wje_lq! You have given to my to hope.

I simply did not know that here such rules. In Russia peoples like SpLaSh212 have name "students".

I will be corrected in future!
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
convert decimal to octal using the let command Suinatsa Programming 2 10-24-2006 08:42 AM
Have problem converting a decimal number to octal Linh Programming 4 05-20-2004 03:21 PM
How do I add two octal number ? Linh Programming 3 05-20-2004 03:08 PM
Octal format kawaii Linux - Newbie 2 11-02-2003 08:27 AM
--- --- --- octal 000 permissions Fascistchicken Linux - General 4 09-05-2003 05:44 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 07:33 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration