How to decrease a string number by one in C?


 
Thread Tools Search this Thread
Top Forums Programming How to decrease a string number by one in C?
# 1  
Old 04-06-2011
Error How to decrease a string number by one in C?

If i have a macro called NUM which is "8" (string) for example
How do I make it into say "7", well i just want to lower it by one, also there will never be the case where its "0" and i have to decrease it.
there is no guarantee how many digits NUM could have, but will always be an integer.
How do I do the conversion?
# 2  
Old 04-06-2011
# 3  
Old 04-08-2011
Quote:
Originally Posted by ctsgnb
Maybe the following link can help
atoi
strtol
And sprintf() to save the number back to a string.
# 4  
Old 04-08-2011
use the code..... but if u need a variable why use macro..... ?

Code:
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>

#define NUM "8"
int main(int argc, char ** argv)
{
char s1[10];
itoa((atoi(NUM)-1),s1,10);
puts(s1);

getch();
return 0;
}



Last edited by Franklin52; 04-08-2011 at 09:41 AM.. Reason: Please use code tags, thank you
# 5  
Old 04-08-2011
Quote:
Originally Posted by vishnulinux
Code:
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>

#define NUM "8"
int main(int argc, char ** argv)
{
char s1[10];
itoa((atoi(NUM)-1),s1,10);
puts(s1);

getch();
return 0;
}


conio.h is a C header file for old MS-DOS compilers.....Smilie and itoa is not a portable C function.

Last edited by Franklin52; 04-08-2011 at 09:50 AM..
# 6  
Old 04-08-2011
Quote:
Originally Posted by omega666
If i have a macro called NUM which is "8" (string) for example
How do I make it into say "7", well i just want to lower it by one, also there will never be the case where its "0" and i have to decrease it.
there is no guarantee how many digits NUM could have, but will always be an integer.
How do I do the conversion?
Only with preprocessor directives...and each time you undefine and redefine NUM so its one less than last.
# 7  
Old 04-08-2011
Maybe if you told us what you were actually trying to do, we could help you figure out a solution? Because doing math on integer strings isn't usually a good idea unless you're in a shell.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Concatenate a string and number and compare that with another string in awk script

I have below code inside my awk script if ( $0 ~ /SVC IN:/ ) { svc_in=substr( $0,23 , 3); if (msg_start == 1 && msg_end == 0) { msg_arr=$0; } } else if ( $0 ~ /^SVC OUT:/ ) { svc_out=substr( $0, 9, 3); if (msg_start == 1 && msg_end == 0) ... (6 Replies)
Discussion started by: bhagya123
6 Replies

2. AIX

[ASK] decrease/shrink the size of filesystem

Hello, I would like to reduce the size of filesystem online. We can do online for increase without any problem. So any risk can be occurred with the decrease? This is not an issue, just a discussion for decrease/shrink space with chfs command. My AIX system is version 6.1 and the filesystem... (2 Replies)
Discussion started by: Phat
2 Replies

3. Shell Programming and Scripting

increase/decrease multiple numbers in a filename.

I got a game that output map tiles of the session with the 0,0 position at the place you login/spawn. That makes making a map somewhat troublesome since the 0,0 will move. So I've been looking for a way to change the numbers in the filenames of all files in a folder by a certain value. The... (5 Replies)
Discussion started by: Ravenholdt
5 Replies

4. UNIX for Dummies Questions & Answers

Decrease buffer size

Hi, I am using the below command to get the output in a file called "Logs.txt" tail -f filename | egrep -i "cpu | hung " >> Logs.txt The problem is the Logs.txt file gets updated only after the buffer is 8Kb, but i want to update the file immediately and not wait for the buffer to get 8kb. Is... (8 Replies)
Discussion started by: @bhi
8 Replies

5. Shell Programming and Scripting

changing number in bash (number is in form of string)

I have a txt file as database. when i run my program what it does is it ask me for 3 name and stored in the file as name1:name2:name3:1 when u enter 3 name it add those in file as above format and add 1 at the end. I what i want is if i enter same names again it changes that 1 to 2 and so... (3 Replies)
Discussion started by: Learnerabc
3 Replies

6. Shell Programming and Scripting

decrease number by 1 in text file

i have text file which contains number like 234565 i need a shell script or command which decrease number by 1 in text file. pls (2 Replies)
Discussion started by: reyazan
2 Replies

7. Solaris

How to increase or decrease inode number of the particular UFS filesystem

Hi Gurus I want to know the command & tips regarding, how to increase or decrease inode number of the particular ufs filesystem. Is it possible to do it in a live/production environment. Regards (3 Replies)
Discussion started by: girish.batra
3 Replies

8. Linux

Decrease Mouse Sensitivities

Hey guys, I have a HP Pavilion laptop, and in order to preserve it's lifespan; I decided to use an external mouse and keyboard whilst at my desk. It worked right away - I was really impressed. I expected to have to recompile the kernel. Anyway, the only thing is that the mouse is... (1 Reply)
Discussion started by: newbie sarah
1 Replies

9. Shell Programming and Scripting

How to decrease the font size in Mailx

Hi all, I am using echo "$EMAILMESSAGE" | mailx -s "$SUBJECT" -b $CC "$TO" I am receiving the mail but seems to big in font size. Is there any option mailx to decrease the size of the mail generated. Thanks, (1 Reply)
Discussion started by: shellscripter
1 Replies

10. Solaris

increase/decrease filesystem

Hi All, I need to increase the filesystem of / and /var (two different slices)? Space will be coming from /home slice so I need to decrease it. Is that possible without reinstallation or in a single-user-mode? Any idea or link please. Thanks in advance. (5 Replies)
Discussion started by: itik
5 Replies
Login or Register to Ask a Question

Featured Tech Videos