Advertising (This ad goes away for registered users. You can Login or Register)

[Tutorial] Introduction to programming using C

Discuss about your favorite (gaming...or not) devices here. The most popular ones will end up getting their own categories
Programming discussions for your favorite Device
Forum rules
Forum rule Nº 15 is strictly enforced in this subforum.
m0skit0
Guru
Posts: 3817
Joined: Mon Sep 27, 2010 6:01 pm

Re: [Tutorial] Introduction to programming using C (I)

Post by m0skit0 »

No, it's not relative. "Levelness" is always relative to machine code.
Advertising
I wanna lots of mov al,0xb
Image
"just not into this RA stuffz"
Terbaddo
Posts: 116
Joined: Mon Feb 14, 2011 3:29 pm
Location: Paris, France
Contact:

Re: [Tutorial] Introduction to programming using C (I)

Post by Terbaddo »

C (compiled) is not lower-level than LUA (scripted) ?
Advertising
That is not Unix. That is OSX a proprietary branch of BSD.
MixShark NGPToolChain Dev Repository Homebrew World
ultimakillz
Retired Mod
Posts: 805
Joined: Mon Sep 27, 2010 6:55 pm

Re: [Tutorial] Introduction to programming using C (I)

Post by ultimakillz »

Terbaddo wrote:C (compiled) is not lower-level than LUA (scripted) ?
if you are implying that scripting languages are higher level programming languages than compiled languages, then that is not necessarily the case. levelness of a programming language depends on the level of abstraction between said language & machine language. whether its compiled or interpreted has nothing to do with it, but rather how much the language hides the specifics of whats really going on in the machine code is the real determining factor.

if thats not what you're implying, then imho c and lua are on a similar level. to me neither abstracts more than the other, for example printf is not any more abstract to me then screen:print.
m0skit0
Guru
Posts: 3817
Joined: Mon Sep 27, 2010 6:01 pm

Re: [Tutorial] Introduction to programming using C (I)

Post by m0skit0 »

Terbaddo wrote:C (compiled) is not lower-level than LUA (scripted) ?
Yes it is. Let's end this... :roll:
I wanna lots of mov al,0xb
Image
"just not into this RA stuffz"
asgard20032
Posts: 186
Joined: Thu Jan 20, 2011 1:16 pm
Location: Milky Way, Solar system, Earth, North America, Canada, Québec..... In front of my computer

Re: [Tutorial] Introduction to programming using C (I)

Post by asgard20032 »

ultimakillz wrote:
Terbaddo wrote:C (compiled) is not lower-level than LUA (scripted) ?
if you are implying that scripting languages are higher level programming languages than compiled languages, then that is not necessarily the case. levelness of a programming language depends on the level of abstraction between said language & machine language. whether its compiled or interpreted has nothing to do with it, but rather how much the language hides the specifics of whats really going on in the machine code is the real determining factor.

if thats not what you're implying, then imho c and lua are on a similar level. to me neither abstracts more than the other, for example printf is not any more abstract to me then screen:print.

levelness of a programming language != abstraction level, but more how close the language is with the hardware... normally, a high level language will also need an interpreter, so there is more "thing" between the hardware and the program.
Image
xist
Posts: 41
Joined: Thu May 19, 2011 9:19 am

Re: [Tutorial] Introduction to programming using C

Post by xist »

I got a teach yourself C++ book the other day because i feel like my mind is dying a bit so please bear in mind i know nothing and i'm starting from first principles.

Take this -

Code: Select all

#include "../../../std_lib_facilities.h"
int main()
{
	cout << "Please input a floating point value: ";
	double n;
	cin >> n;
	cout << "n == " << n
			<< "\nn+1 == " << n+1
			<< "\nthree times n == " << 3*n
			<< "\ntwice n == " << n+n
			<< "\nn squared == " << n*n
			<< "\nhalf of n == " << n/2
			<< "\nsquare root of n == " << sqrt(n)
			<< endl;
	keep_window_open();
}
The exercise i'm looking at says "Modify the program you just made to read an int rather than a double. Note that sqrt is not defined for int values so assign n to a double and take sqrt of that.

So expecting it to fail i just changed double to int to see what happened like this -

Code: Select all

#include "../../../std_lib_facilities.h"
int main()
{
	cout << "Please input a number: ";
	int n;
	cin >> n;
	cout << "n == " << n
			<< "\nn+1 == " << n+1
			<< "\nthree times n == " << 3*n
			<< "\ntwice n == " << n+n
			<< "\nn squared == " << n*n
			<< "\nhalf of n == " << n/2
			<< "\nsquare root of n == " << sqrt(n)
			<< endl;
	keep_window_open();
}
But that works fine, and when i use 9 as my integer it gives back 3 for a sqrt.

Why's that work ok if int shouldn't work with the sqrt function?
m0skit0
Guru
Posts: 3817
Joined: Mon Sep 27, 2010 6:01 pm

Re: [Tutorial] Introduction to programming using C

Post by m0skit0 »

First, this thread is not about C++ but about C. Second, you're asking something totally off-topic for this thread and should have created a new topic to ask that. Third, it's kind of weird mixing C++ and C like you do. If you're going to learn C++, learn it object-oriented. If you don't want to learn object-oriented programming, go with plain C. Mixing those will result in a disaster, specially when learning.

That said, I'll answer you anyway. The compiler is most likely giving you a warning about that (if it doesn't, make sure you're using a recent decent compiler and correct flags are set (-Wall for GCC/G++)). The reason it's working is because the compiler is smart and it automatically converts your int into double before passing it to the sqrt() function, because conversion from int to double is straightforward. Anyway, for sake of readability (and getting rid of the warning) you should explicitly cast that int into double:

Code: Select all

<< "\nsquare root of n == " << sqrt( (double) n)
I wanna lots of mov al,0xb
Image
"just not into this RA stuffz"
xist
Posts: 41
Joined: Thu May 19, 2011 9:19 am

Re: [Tutorial] Introduction to programming using C

Post by xist »

Sorry, i made a noobish error posting here but didn't want to start a new topic for something trivial.

Regarding your concern over mixing stuff up...i'm working directly from Bjarne Stroustrup's Programming Principles and Practice Using C++, which i figured was a good starter.

So basically putting (double) prior to my integer allows a disallowed function by putting the value into a double read space?

Thanks and sorry i won't do it again.
m0skit0
Guru
Posts: 3817
Joined: Mon Sep 27, 2010 6:01 pm

Re: [Tutorial] Introduction to programming using C

Post by m0skit0 »

xist wrote:i'm working directly from Bjarne Stroustrup's Programming Principles and Practice Using C++, which i figured was a good starter.
Never read it, but it depends to whom that book is intented and what's your programming background.
xist wrote:So basically putting (double) prior to my integer allows a disallowed function by putting the value into a double read space?
Yes, that's called casting. But beware with that because compiler will only convert the data format correctly (double format is not int format) if he knows how to. Otherwise the data format will remain the same, but compiler won't raise any error (unlike Java for example where unknown castings are not allowed).
I wanna lots of mov al,0xb
Image
"just not into this RA stuffz"
xist
Posts: 41
Joined: Thu May 19, 2011 9:19 am

Re: [Tutorial] Introduction to programming using C

Post by xist »

Thank you. I'll start a new topic in future.

As for the book, it's a beginners book and i know nothing.... :roll:

Thanks again.

(didn't get any errors btw...)
Locked

Return to “Programming and Security”