Hello. I'm new and I hope my question is not too naïve.
Years ago I studied Pascal, Visual Basic, and a little bit of a couple of other languages in school. Now, I'm thinking of picking up programming again for personal interest. I've learnt some Python and like it but I've concluded that I won't be comfortable until I'm decent at coding in c.
Now, I'm reading the Kernighan-Ritchie book, and one thing has struck me especially: it seems to me that the idea of a "string" type (in Pascal, if I remember rightly, and other languages) is misleading. Apparently in c you can put text either in an array of chars or in an int, because at bottom there's no such thing as "text" as opposed to numbers. Data types are only concerned with containing a certain numbers of binary digits. Correct?
I'm getting to the question. When I read in the K-R book there are examples where it's necessary for a variable to be able to contain the value of EOF in addition to legitimate chars, it struck me that putting text in an int or an array of chars is just a matter of convenience (e.g. dealing in characters instead of bits) and economy. Correct?
I then figured: what if I assign a text string to an int and then printf the variable? It works, as it should. But an int is not infinite, so I used a web-app to convert a string to binary to see that it was larger than an integer. Would I get an error if I assigned it to the int variable? I didn't. I know next to nothing about promotion now, but I supposed the int had been promoted to a float or another larger type??
Then I assigned a number like "999999999999", which turned out to be significantly shorter in binary than the text string I'd tried before. Now I got an error, SIGSEGV, which I suppose is just because an integer can't hold such a number.
So what is it that I don't get? I hope I get most of it... Thanks!
Oh, another thing. Not sure if it's on topic, though:
Code:
int blah="Hello";
printf(blah);
prints blah as text by default, that is "Hello".
Code:
int blah="Hello";
printf("%d", blah);
will print 69600. I assumed, in my ignorance, that this was ascii to binary back to decimal. Except that I've tried several web text-to-binary and binary-to-decimal converters, and I'm mistaken. I suspect I'm under some huge misconception about how these things work. What do you think?
Also, more on topic, while I was still thinking that 69600 was what I thought it was, I tried:
Code:
int blah=69600;
printf("%s", blah);
Never mind the maths for a moment. Why doesn't c even try to do what I want? Because it's useless (but so is printing text as "%d", right?) and wasn't provided for or because it doesn't make any sense?
Briangriffin