- Forum
- Beginners
- How to convert data type int to char?
How to convert data type int to char?
I would like to convert a data type int to char.
For example, i would like to convert int a = 100 into char a[0] = ‘1’, a[1] =’0′ , a[2] =’0′.
i had tried itoa function which gave me the error message of «error: use of undeclared identifier ‘itoa'».
AnyOne pls help, thank you.
You need to include the stdlib header in your program:
|
|
From http://www.cplusplus.com/reference/cstdlib/itoa/
This function is not defined in ANSI-C and is not part of C++, but is supported by some compilers.
ajh32’s code above does not compile on C++ Shell (including either <stdlib.h> or <cstdlib>)
In function 'int main()': 7:24: error: 'itoa' was not declared in this scope
But if you’re compiler does support itoa (or ltoa)…
You need to include the stdlib header in your program:
If compiling as .cpp could include <cstdio> instead.
And stack space isn’t that limited, so no need to use as tight a buffer as possible.
|
|
An alternative, pre-C++11, ANSI compliant solution is to use std::ostringstream (this does work with C++ Shell)
http://www.cplusplus.com/reference/sstream/ostringstream/
|
|
Andy
PS The maximum decimal value of a 64-bit int is -9223372036854775807 (21 chars)
Last edited on
Look like i have a long way to go…. there are a lot of function and code which i never see before. Anyway, Thank you everyone!
Topic archived. No new replies allowed.
Hello,
Let’s take iostream.h for example. Our objective is to narrow out all *.h header files.
Typically if you use depreciated headers, e.g. iostream.h instead of iostream, you do not need the directive. This is because compilers that support depreciated headers for backward compatibility declare the «
using namespace std;» directive prior to including the ANSI header. However this mechanism is supported for backward compatibility, and you should avoid relying upon it in new code.
Keep in mind iostream is part of the C++ standard, iostream.h isn’t. iostream is somewhat more restrictive than the older iostream.h. One would possibly avoid problems by careful use of namespaces, but it would be a lot smarter to stick with one or the other.
You should avoid using the *.h version as much as possible, because some implementations have bugs in their *.h version. Moreover, some of them support non-standard code that is not portable, and fail to support some of the standard code of the STL.
Furthermore, the *.h version puts everything in the global namespace. The extension-less version is more stable and it’s more portable. It also places everything in the std namespace.
iostream.h is an old style method of including std C++ headers, and in opposite to iostream you don’t have to declare that you’re using the std namespace.
For example the string class which needs string, rather than string.h is a C header, and is not the same. It is equivilent to cstring which should be used instead.
In VC++ 6 iostream.h works, which is different than iostream. The *.h version was assembled before there was a standard for C++.
P.S. This pertains to most, if not all, *.h header files.
—
Stack Overflow
Segmentation Fault: I am an error in which a running program attempts to access memory not allocated to it and core dumps with a segmentation violation error. This is often caused by improper usage of pointers, attempts to access a non-existent or read-only physical memory address, re-use of memory if freed within the same scope, de-referencing a null pointer, or (in C) inadvertently using a non-pointer variable as a pointer.
AKruglyak 0 / 0 / 0 Регистрация: 18.02.2012 Сообщений: 24 |
||||
1 |
||||
27.03.2012, 21:41. Показов 12975. Ответов 8 Метки нет (Все метки)
Здравствуйте, при решении задачи потребовалось перевести число в строку. Сначала я не знал, как это сделать, но потом наткнулся на волшебную функцию itoa, которая отказалась у меня работать.
В чем проблема?
__________________
0 |
347 / 292 / 37 Регистрация: 23.03.2012 Сообщений: 838 |
|
27.03.2012, 21:49 |
2 |
В чем проблема? второй параметр должен быть char *
0 |
0 / 0 / 0 Регистрация: 18.02.2012 Сообщений: 24 |
|
27.03.2012, 21:56 [ТС] |
3 |
Это ничего не поменяло, как выдавало `itoa’ undeclared (first use this function), так и продолжает.
0 |
soon 2554 / 1319 / 178 Регистрация: 09.05.2011 Сообщений: 3,086 Записей в блоге: 1 |
||||
27.03.2012, 21:57 |
4 |
|||
1 |
347 / 292 / 37 Регистрация: 23.03.2012 Сообщений: 838 |
|
27.03.2012, 22:04 |
5 |
Это ничего не поменяло, как выдавало `itoa’ undeclared (first use this function), так и продолжает. Какой компилятор? itoa в stdlib.h находится. В Dev-C++ выдаёт ошибку о чар* только.
0 |
0 / 0 / 0 Регистрация: 18.02.2012 Сообщений: 24 |
|
27.03.2012, 22:11 [ТС] |
6 |
У меня тоже Dev-C++. Вот скриншот:
0 |
347 / 292 / 37 Регистрация: 23.03.2012 Сообщений: 838 |
|
27.03.2012, 22:16 |
7 |
У меня тоже Dev-C++. Вот скриншот:
Хз если честно
0 |
7175 / 3234 / 80 Регистрация: 17.06.2009 Сообщений: 14,164 |
|
28.03.2012, 08:04 |
8 |
Компилятор какой ?
0 |
alex_x_x бжни 2473 / 1684 / 135 Регистрация: 14.05.2009 Сообщений: 7,162 |
||||
28.03.2012, 09:05 |
9 |
|||
ideone.com/KmdGz
0 |
IT_Exp Эксперт 87844 / 49110 / 22898 Регистрация: 17.06.2006 Сообщений: 92,604 |
28.03.2012, 09:05 |
Помогаю со студенческими работами здесь Не работает прога. error C4996: ‘itoa’ #include "stdafx.h" Itoa itoa(); itoa Искать еще темы с ответами Или воспользуйтесь поиском по форуму: 9 |
14 Years Ago
String functions like strcpy,strcat,itoa…shows warning as «declared deprecated» in Visual Studio 8.0 instead it suggests to use functions which are enhanced secure versions of the same such as strcpy_s,strcat_s,_itoa_s…..using this in my file removes those warnings but i am using this same file in visual studio 6.0 here it gives compile time error as «undeclared identifier» which is correct as this old version of visual studio is not provided with the new added versions of above functions….so what is the solution for this as i want to use this same file in my VC6 as well as VC8 projects….plz help
Recommended Answers
All 5 Replies
14 Years Ago
#define strcpy_s strcpy
risa
0
Light Poster
14 Years Ago
#define strcpy_s strcpy
![]()
…..thanks for this quick help..,,,, but both of these funtions have different parameters,,,strcpy_s has a additional parameter to strcpy which is the size of buffer,,,..hence #define strcpy_s strcpy gives compile time error in Visual Studio 8 itself,,,,,so nw wht to do??
Nick Evan
4,005
Industrious Poster
Team Colleague
Featured Poster
14 Years Ago
I agree, using strings would probably solve your problem. But since that wasn’t your question, you could try:
#define _CRT_SECURE_NO_DEPRECATE
#define _CRT_NONSTDC_NO_DEPRECATE
#include <......> etc
which would take care of the «declared deprecated» warnings.
Or you could add #pragma warning (disable:4996)
to your code
Here’s a list of all the functions that are «deprecated»
risa
0
Light Poster
14 Years Ago
I agree, using strings would probably solve your problem. But since that wasn’t your question, you could try:
#define _CRT_SECURE_NO_DEPRECATE #define _CRT_NONSTDC_NO_DEPRECATE #include <......> etc
which would take care of the «declared deprecated» warnings.
Or you could add
#pragma warning (disable:4996)
to your codeHere’s a list of all the functions that are «deprecated»
Thanks a lot ….#pragma warning(disable:4996) has solved my problem temporarily…..but later it may give problem as suppressing warning will give compile time errors for these functions as the newer version will remove these functions….but its ok!! for time being the problem is solved.
Reply to this topic
Be a part of the DaniWeb community
We’re a friendly, industry-focused community of developers, IT pros, digital marketers,
and technology enthusiasts meeting, networking, learning, and sharing knowledge.
Эксперт
Профиль
Группа: Завсегдатай
Сообщений: 1650
Регистрация: 25.12.2004
Репутация: нет
Всего: 23
В Visual C++ компилятор ругается:
Цитата |
1.cpp(74) : error C2065: ‘itoa’ : undeclared identifier 1.cpp(112) : warning C4018: ‘<=’ : signed/unsigned mismatch 1.cpp(121) : error C2065: ‘atoi’ : undeclared identifier |
Код |
|
А компилятор C++ Builder — проходит без ошибок.
Код |
// 1.cpp : Defines the entry point for the application. |
Добавлено через 1 минуту и 12 секунд
Что не нравится компилятору Visual C++ ?
———————
i_i
(‘;’)
(V)
C++ — ITOA FUNCTION PROBLEM — STACK OVERFLOW
Web Sep 12, 2011 itoa () is not part of any standard so you shouldn’t use it. There’s better ways, i.e… C: int main () { char n_str [10]; int n = 25; sprintf (n_str, «%d», n); return 0; } …
From stackoverflow.com
Reviews 2
C++ — ITOA REPLACE TO STD::TO_STRING HOW TO — STACK OVERFLOW
Web Dec 10, 2012 To use itoa () function, you need to include stdlib.h file, then you will not see the error while using itoa (). Share Improve this answer Follow answered Dec 10, 2012 …
From stackoverflow.com
Reviews 4
_ITOA_S UNDECLARED IDENTIFIER — FOR BEGINNERS — GAMEDEV.NET
Web Dec 17, 2007 _itoa_s () is a non-standard function. It could be that your compiler doesn’t support it. Kai001 Author 122 December 15, 2007 11:51 PM I’m using VC 6.0. I thought …
From gamedev.net
C++ — COMPILE TIME ERROR «UNDECLARED IDENTIFIER»FOR … — DANIWEB
Web String functions like strcpy,strcat,itoa…shows warning as «declared deprecated» in Visual Studio 8.0 instead it suggests to use functions which are enhanced secure versions of …
From daniweb.com
13 USE OF UNDECLARED IDENTIFIERS ERRORS, PROBLEM WITH THE SCOPE OF …
Web 13 use of undeclared identifiers errors, problem with the scope of variables. So I am getting some errors and I don’t know why because I already defined those functions in this …
From reddit.com
HOW DO I FIX A «USE OF UNDECLARED IDENTIFIER» COMPILATION ERROR?
Web To declare a variable one must write its type followed by its name (also known as identifier). Once you’ve declared it, only then you are allowed to use it in expressions or …
From cs50.stackexchange.com
HOW TO CONVERT DATA TYPE INT TO CHAR? — C++ FORUM — CPLUSPLUS.COM
Web Jul 13, 2015 ajh32’s code above does not compile on C++ Shell (including either <stdlib.h> or <cstdlib>) In function ‘int main ()’: 7:24: error: ‘itoa’ was not declared in this scope But …
From cplusplus.com
STD::IOTA — CPPREFERENCE.COM
Web May 30, 2022 std::iota — cppreference.com std:: iota C++ Algorithm library Fills the range [first, last) with sequentially increasing values, starting with value and repetitively …
From en.cppreference.com
‘ITOA’ : FUNCTION DOES NOT TAKE 1 ARGUMENTS & ‘C’ : UNDECLARED …
Web ‘itoa’ : function does not take 1 arguments & ‘c’ : undeclared identifierHelpful? Please use the *Thanks* button above! Or, thank me via Patreon: https://www…
From youtube.com
ITOA UNDECLARED? — C++ PROGRAMMING
Web The function itoa () is not part of standard C or C++. If using C, you can use sprintf; if using C++, you can use an ostringstream. C++ Example: Example 1: Using ostringstream …
From cboard.cprogramming.com
`ITOA’ UNDECLARED (FIRST USE THIS FUNCTION) — EXPERTS EXCHANGE
Web Aug 22, 2022 itoa () is not supported on all UNix platforms natively. Example above by rstaveley is good to do this function,. I always use the following — #include <iostream> …
From experts-exchange.com
GCC ERROR : UNDEFINED REFERENCE TO `ITOA’ — STACK OVERFLOW
Web Nov 9, 2012 itoa is a non-standard function which is supported by some compilers. Going by the error, it’s not supported by your compiler. Your best bet is to use snprintf () …
From stackoverflow.com
FIX A “USE OF UNDECLARED IDENTIFIER” COMPILATION ERROR IN C++
Web The word identifier is used for the names we give to our variable. This type of error deals with the cases where we make some mistakes while dealing with the identifiers i.e, this …
From codespeedy.com
ITOA — C++ REFERENCE — CPLUSPLUS.COM
Web itoa char * itoa ( int value, char * str, int base ); Convert integer to string (non-standard function) Converts an integer value to a null-terminated string using the specified base …
From cplusplus.com
HOW TO AVOID COMPILE ERROR WHILE DEFINING VARIABLES
Web Apr 5, 2019 prog.c: In function ‘main’: prog.c:5:18: error: ‘x’ undeclared (first use in this function) printf(«%d», x); ^ prog.c:5:18: note: each undeclared identifier is reported only …
From geeksforgeeks.org
MYSQL2 0.4.6 ERROR: USE OF UNDECLARED IDENTIFIER …
Web Oct 1, 2018 $ bundle install Warning: the running version of Bundler (1.16.2) is older than the version that created the lockfile (1.16.3). We suggest you upgrade to the latest …
From github.com
MAKE ERROR: USE OF UNDECLARED IDENTIFIER ‘IOTA’ #1 — GITHUB
Web Jan 9, 2017 Pull requests Projects Insights New issue make error: use of undeclared identifier ‘iota’ #1 Closed cn0xroot opened this issue on Jan 9, 2017 · 2 comments on …
From github.com