#1
  1. Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2002
    Posts
    421
    Rep Power
    13

    how would code look without libraries/header files?


    How would lets say a hello world program look without iostream? or a program that prints a string without string.h? It seems C++ has alot to do with already made libraries rather then hard coding.
    hmmm...
  2. #2
  3. /(bb|[^b]{2})/

    Join Date
    Nov 2001
    Location
    Somewhere in the great unknown
    Posts
    5,163
    Rep Power
    793
    if you strip all of the headers and libraries away from it then you would have c with a few more rules.

    some headers like iostream are as much a part of c++ as the base language is.
  4. #3
  5. Contributing User
    Devshed Supreme Being (6500+ posts)

    Join Date
    Jan 2003
    Location
    USA
    Posts
    7,255
    Rep Power
    2222

    Re: how would code look without libraries/header files?


    Originally posted by andy3109
    How would lets say a hello world program look without iostream? or a program that prints a string without string.h? It seems C++ has alot to do with already made libraries rather then hard coding.
    Code:
    #include <stdlib.h>
    #include <stdio.h>
    
    int main(void)
    {
        printf("Hello, world.\n");
        return 0;
    }
    Of course, printf() also uses a library, stdio, which requires that stdio.h gets included. I don't think stdlib.h contributes anything in this case, but I've learned to always include it.

    BTW, this works just fine in C++, too.
  6. #4
  7. Banned ;)
    Devshed Supreme Being (6500+ posts)

    Join Date
    Nov 2001
    Location
    Woodland Hills, Los Angeles County, California, USA
    Posts
    9,645
    Rep Power
    4248
    >> Of course, printf() also uses a library, stdio, which requires that stdio.h gets included.

    Actually, the code will compile just fine without <stdio.h> and <stdlib.h> included, on a C compiler (Note: I said C Compiler, not C++ compiler which is a different matter). The code for the printf() function is in the standard library, which the linker will link to by default. All that the stdio.h file contains is a function prototype for the printf() function, so that the compiler will know ahead of time what printf() is expecting for arguments. If a C compiler cannot find a function prototype before it finds the function call, it will assume that you are calling the function correctly with the correct number/types of arguments and keep going (they may issue a warning, but will still compile the program). A C++ compiler on the other hand will consider this an error instead of a warning (this is per the C++ definition). However, most C++ compilers have a switch or two, to allow them to compile C code. So if you flip the correct switches, then the C++ compiler won't treat it as an error either.

    For more on this issue, I discussed it here: Linakge of Standard Library Functions

    Cheers :)
    Last edited by Scorpions4ever; March 19th, 2003 at 12:22 PM.
  8. #5
  9. No Profile Picture
    Contributing User
    Devshed Beginner (1000 - 1499 posts)

    Join Date
    Feb 2001
    Posts
    1,481
    Rep Power
    15
    "How would lets say a hello world program look without iostream?"

    A lot longer. An include statement replaces the include line with the contents of the file. If you want to write all the code for outputting to your screen every time, feel free to do that--don't forget to make your code device independent. :)

    Here is some of what I see when I step through a Hello World program with my debugger(and there is a lot more than what I'm posting):

    Code:
    #if     _MSC_VER > 1000
    #pragma once
    #endif
    
    #ifndef _OSTREAM_
    #define _OSTREAM_
    #include <ios>
    
    #ifdef  _MSC_VER
    #pragma pack(push,8)
    #endif  /* _MSC_VER */
    		// I/O exception macros
     #define _TRY_IO_BEGIN	_TRY_BEGIN
     #define _CATCH_IO_END	_CATCH_ALL \
    	setstate(ios_base::badbit, true); _CATCH_END
     #define _CATCH_IO_(x)	_CATCH_ALL \
    	(x).setstate(ios_base::badbit, true); _CATCH_END
    _STD_BEGIN
    		// TEMPLATE CLASS basic_ostream
    template<class _E, class _Tr = char_traits<_E> >
    	class basic_ostream : virtual public basic_ios<_E, _Tr> {
    public:
    	typedef basic_ostream<_E, _Tr> _Myt;
    	typedef basic_ios<_E, _Tr> _Myios;
    	typedef basic_streambuf<_E, _Tr> _Mysb;
    	typedef ostreambuf_iterator<_E, _Tr> _Iter;
    	typedef num_put<_E, _Iter> _Nput;
    	explicit basic_ostream(basic_streambuf<_E, _Tr> *_S,
    		bool _Isstd = false, bool _Doinit = true)
    		{if (_Doinit)
    		    init(_S, _Isstd); }
    	basic_ostream(_Uninitialized)
    		{_Addstd(); }
    	virtual ~basic_ostream()
    		{}
    	class sentry {
    	public:
    		explicit sentry(_Myt& _Os)
    			: _Ok(_Os.opfx()), _Ostr(_Os) {}
    		~sentry()
    			{if (!uncaught_exception())
    				_Ostr.osfx(); }
    		operator bool() const
    			{return (_Ok); }
    	private:
    		bool _Ok;
    		_Myt& _Ostr;
    		};
    	bool opfx()
    		{if (good() && tie() != 0)
    			tie()->flush();
    		return (good()); }
    	void osfx()
    		{if (flags() & unitbuf)
    			flush(); }
    	_Myt& operator<<(_Myt& (__cdecl *_F)(_Myt&))
    		{return ((*_F)(*this)); }
    	_Myt& operator<<(_Myios& (__cdecl *_F)(_Myios&))
    		{(*_F)(*(_Myios *)this);
    		return (*this); }
    	_Myt& operator<<(ios_base& (__cdecl *_F)(ios_base&))
    		{(*_F)(*(ios_base *)this);
    		return (*this); }
    	_Myt& operator<<(_Bool _X)
    		{iostate _St = goodbit;
    		const sentry _Ok(*this);
    		if (_Ok)
    			{const _Nput& _Fac = _USE(getloc(), _Nput);
    			_TRY_IO_BEGIN
    			if (_Fac.put(_Iter(rdbuf()), *this,
    				fill(), _X).failed())
    				_St |= badbit;
    			_CATCH_IO_END }
    		setstate(_St);
    		return (*this); }
    	_Myt& operator<<(short _X)
    		{iostate _St = goodbit;
    		const sentry _Ok(*this);
    		if (_Ok)
    			{const _Nput& _Fac = _USE(getloc(), _Nput);
    			fmtflags _Bfl = flags() & basefield;
    			long _Y = (_Bfl == oct || _Bfl == hex)
    				? (long)(unsigned short)_X : (long)_X;
    			_TRY_IO_BEGIN
    			if (_Fac.put(_Iter(rdbuf()), *this,
    				fill(), _Y).failed())
    				_St |= badbit;
    			_CATCH_IO_END }
    		setstate(_St);
    		return (*this); }
    	_Myt& operator<<(unsigned short _X)
    		{iostate _St = goodbit;
    		const sentry _Ok(*this);
    		if (_Ok)
    			{const _Nput& _Fac = _USE(getloc(), _Nput);
    			_TRY_IO_BEGIN
    			if (_Fac.put(_Iter(rdbuf()), *this,
    				fill(), (unsigned long)_X).failed())
    				_St |= badbit;
    			_CATCH_IO_END }
    		setstate(_St);
    		return (*this); }
    	_Myt& operator<<(int _X)
    		{iostate _St = goodbit;
    		const sentry _Ok(*this);
    		if (_Ok)
    			{const _Nput& _Fac = _USE(getloc(), _Nput);
    			fmtflags _Bfl = flags() & basefield;
    			long _Y = (_Bfl == oct || _Bfl == hex)
    				? (long)(unsigned int)_X : (long)_X;
    			_TRY_IO_BEGIN
    			if (_Fac.put(_Iter(rdbuf()), *this,
    				fill(), _Y).failed())
    				_St |= badbit;
    			_CATCH_IO_END }
    		setstate(_St);
    		return (*this); }
    	_Myt& operator<<(unsigned int _X)
    		{iostate _St = goodbit;
    		const sentry _Ok(*this);
    		if (_Ok)
    			{const _Nput& _Fac = _USE(getloc(), _Nput);
    			_TRY_IO_BEGIN
    			if (_Fac.put(_Iter(rdbuf()), *this,
    				fill(), (unsigned long)_X).failed())
    				_St |= badbit;
    			_CATCH_IO_END }
    		setstate(_St);
    		return (*this); }
    	_Myt& operator<<(long _X)
    		{iostate _St = goodbit;
    		const sentry _Ok(*this);
    		if (_Ok)
    			{const _Nput& _Fac = _USE(getloc(), _Nput);
    			_TRY_IO_BEGIN
    			if (_Fac.put(_Iter(rdbuf()), *this,
    				fill(), _X).failed())
    				_St |= badbit;
    			_CATCH_IO_END }
    		setstate(_St);
    		return (*this); }
    	_Myt& operator<<(unsigned long _X)
    		{iostate _St = goodbit;
    		const sentry _Ok(*this);
    		if (_Ok)
    			{const _Nput& _Fac = _USE(getloc(), _Nput);
    			_TRY_IO_BEGIN
    			if (_Fac.put(_Iter(rdbuf()), *this,
    				fill(), _X).failed())
    				_St |= badbit;
    			_CATCH_IO_END }
    		setstate(_St);
    		return (*this); }
    	_Myt& operator<<(float _X)
    		{iostate _St = goodbit;
    		const sentry _Ok(*this);
    		if (_Ok)
    			{const _Nput& _Fac = _USE(getloc(), _Nput);
    			_TRY_IO_BEGIN
    			if (_Fac.put(_Iter(rdbuf()), *this,
    				fill(), (double)_X).failed())
    				_St |= badbit;
    			_CATCH_IO_END }
    		setstate(_St);
    		return (*this); }
    	_Myt& operator<<(double _X)
    		{iostate _St = goodbit;
    		const sentry _Ok(*this);
    		if (_Ok)
    			{const _Nput& _Fac = _USE(getloc(), _Nput);
    			_TRY_IO_BEGIN
    			if (_Fac.put(_Iter(rdbuf()), *this,
    				fill(), _X).failed())
    				_St |= badbit;
    			_CATCH_IO_END }
    		setstate(_St);
    		return (*this); }
    	_Myt& operator<<(long double _X)
    		{iostate _St = goodbit;
    		const sentry _Ok(*this);
    		if (_Ok)
    			{const _Nput& _Fac = _USE(getloc(), _Nput);
    			_TRY_IO_BEGIN
    			if (_Fac.put(_Iter(rdbuf()), *this,
    				fill(), _X).failed())
    				_St |= badbit;
    			_CATCH_IO_END }
    		setstate(_St);
    		return (*this); }
    	_Myt& operator<<(const void *_X)
    		{iostate _St = goodbit;
    		const sentry _Ok(*this);
    		if (_Ok)
    			{const _Nput& _Fac = _USE(getloc(), _Nput);
    			_TRY_IO_BEGIN
    			if (_Fac.put(_Iter(rdbuf()), *this,
    				fill(), _X).failed())
    				_St |= badbit;
    			_CATCH_IO_END }
    		setstate(_St);
    		return (*this); }
    	_Myt& operator<<(_Mysb *_Pb)
    		{iostate _St = goodbit;
    		bool _Copied = false;
    		const sentry _Ok(*this);
    		if (_Ok && _Pb != 0)
    			for (int_type _C = _Tr::eof(); ; _Copied = true)
    				{_TRY_BEGIN
    				_C = _Tr::eq_int_type(_Tr::eof(), _C)
    					? _Pb->sgetc() : _Pb->snextc();
    				_CATCH_ALL
    					setstate(failbit);
    					_RERAISE;
    				_CATCH_END
    				if (_Tr::eq_int_type(_Tr::eof(),_C))
    					break;
    				_TRY_IO_BEGIN
    					if (_Tr::eq_int_type(_Tr::eof(),
    						rdbuf()->sputc(_Tr::to_char_type(_C))))
    						{_St |= badbit;
    						break; }
    				_CATCH_IO_END }
    		width(0);
    		setstate(!_Copied ? _St | failbit : _St);
    		return (*this); }
    	_Myt& put(_E _X)
    		{iostate _St = goodbit;
    		const sentry _Ok(*this);
    		if (!_Ok)
    			_St |= badbit;
    		else
    			{_TRY_IO_BEGIN
    			 if (_Tr::eq_int_type(_Tr::eof(),
    				rdbuf()->sputc(_X)))
    				_St |= badbit;
    			_CATCH_IO_END }
    		setstate(_St);
    		return (*this); }
    	_Myt& write(const _E *_S, streamsize _N)
    		{iostate _St = goodbit;
    		const sentry _Ok(*this);
    		if (!_Ok)
    			_St |= badbit;
    		else
    			{_TRY_IO_BEGIN
    			if (rdbuf()->sputn(_S, _N) != _N)
    				_St |= badbit;
    			_CATCH_IO_END }
    		setstate(_St);
    		return (*this); }
    	_Myt& flush()
    		{iostate _St = goodbit;
    		if (!fail() && rdbuf()->pubsync() == -1)
    			_St |= badbit;
    		setstate(_St);
    		return (*this); }
    	_Myt& seekp(pos_type _P)
    		{if (!fail())
    			rdbuf()->pubseekpos(_P, out);
    		return (*this); }
    	_Myt& seekp(off_type _O, ios_base::seekdir _W)
    		{if (!fail())
    			rdbuf()->pubseekoff(_O, _W, out);
    		return (*this); }
    	pos_type tellp()
    		{if (!fail())
    			return (rdbuf()->pubseekoff(0, cur, out));
    		else
    			return (streampos(_BADOFF)); }
    	};
    
    
    #ifdef _DLL
    #pragma warning(disable:4231) /* the extern before template is a non-standard extension */
    extern template class _CRTIMP basic_ostream<char, char_traits<char> >;
    extern template class _CRTIMP basic_ostream<wchar_t, char_traits<wchar_t> >;
    #pragma warning(default:4231) /* restore previous warning */
    #endif		// _DLL
    
    
    		// INSERTERS
    template<class _E, class _Tr> inline
    	basic_ostream<_E, _Tr>& __cdecl operator<<(
    		basic_ostream<_E, _Tr>& _O, const _E *_X)
    	{typedef basic_ostream<_E, _Tr> _Myos;
    	ios_base::iostate _St = ios_base::goodbit;
    	size_t _N = _Tr::length(_X);
    	size_t _M = _O.width() <= 0 || _O.width() <= _N
    		? 0 : _O.width() - _N;
    	const _Myos::sentry _Ok(_O);
    	if (!_Ok)
    		_St |= ios_base::badbit;
    	else
    		{_TRY_IO_BEGIN
    		if ((_O.flags() & ios_base::adjustfield)
    			!= ios_base::left)
    			for (; 0 < _M; --_M)
    				if (_Tr::eq_int_type(_Tr::eof(),
    					_O.rdbuf()->sputc(_O.fill())))
    					{_St |= ios_base::badbit;
    					break; }
    		if (_St == ios_base::goodbit
    			&& _O.rdbuf()->sputn(_X, _N) != _N)
    			_St |= ios_base::badbit;
    		if (_St == ios_base::goodbit)
    			for (; 0 < _M; --_M)
    				if (_Tr::eq_int_type(_Tr::eof(),
    					_O.rdbuf()->sputc(_O.fill())))
    					{_St |= ios_base::badbit;
    					break; }
    		_O.width(0);
    		_CATCH_IO_(_O) }
    	_O.setstate(_St);
    	return (_O); }
    template<class _E, class _Tr> inline
    	basic_ostream<_E, _Tr>& __cdecl operator<<(
    		basic_ostream<_E, _Tr>& _O, _E _C)
    	{typedef basic_ostream<_E, _Tr> _Myos;
    	ios_base::iostate _St = ios_base::goodbit;
    	const _Myos::sentry _Ok(_O);
    	if (_Ok)
    		{size_t _M = _O.width() <= 1 ? 0 : _O.width() - 1;
    		_TRY_IO_BEGIN
    		if ((_O.flags() & ios_base::adjustfield)
    			!= ios_base::left)
    			for (; _St == ios_base::goodbit && 0 < _M; --_M)
    				if (_Tr::eq_int_type(_Tr::eof(),
    					_O.rdbuf()->sputc(_O.fill())))
    					_St |= ios_base::badbit;
    		if (_St == ios_base::goodbit
    			&& _Tr::eq_int_type(_Tr::eof(),
    				_O.rdbuf()->sputc(_C)))
    			_St |= ios_base::badbit;
    		for (; _St == ios_base::goodbit && 0 < _M; --_M)
    			if (_Tr::eq_int_type(_Tr::eof(),
    				_O.rdbuf()->sputc(_O.fill())))
    				_St |= ios_base::badbit;
    		_CATCH_IO_(_O) }
    	_O.width(0);
    	_O.setstate(_St);
    	return (_O); }
    Last edited by 7stud; March 19th, 2003 at 03:56 PM.
  10. #6
  11. No Profile Picture
    Contributing User
    Devshed God 1st Plane (5500 - 5999 posts)

    Join Date
    Oct 2000
    Location
    Back in the real world.
    Posts
    5,966
    Rep Power
    191
    Without any libraries or headers, your C program AFAIK would be assembler...
  12. #7
  13. Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2002
    Posts
    421
    Rep Power
    13
    yea..that helloworld program without iostream scares me :eek:
    hmmm...
  14. #8
  15. No Profile Picture
    Contributing User
    Devshed Beginner (1000 - 1499 posts)

    Join Date
    Feb 2001
    Posts
    1,481
    Rep Power
    15
    Wait till you get to the MFC and Visual C++--those are some serious wheels they invented.

IMN logo majestic logo threadwatch logo seochat tools logo