For 2023 (and 2024 ...) - we are now fully retired from IT training. We have made many, many friends over 25 years of teaching about Python, Tcl, Perl, PHP, Lua, Java, C and C++ - and MySQL, Linux and Solaris/SunOS too. Our training notes are now very much out of date, but due to upward compatability most of our examples remain operational and even relevant ad you are welcome to make us if them "as seen" and at your own risk.
Lisa and I (Graham) now live in what was our training centre in Melksham - happy to meet with former delegates here - but do check ahead before coming round. We are far from inactive - rather, enjoying the times that we are retired but still healthy enough in mind and body to be active!
I am also active in many other area and still look after a lot of web sites - you can find an index ((here)) |
Defining, declaring and initialising variables in C
When you declare a variable in C, you're instructing the compiler to set memory aside for it, and you're also telling the compiler how to handle any references to that memory.
• C does NOT spend time working out what sort of data you're storing and how many bytes are needed - it's up to you to tell it the right data sort and to make sure you handle it in the right way (though some checks and warnings are made for you at compile time)
• C does NOT spend time setting initial values into all the variables that you declare - it's up to you to initialise variables if it's needed within your application.
If you get these things wrong, you'll end up treating the bit pattern of an integer as if it's a float, or making use of whatever happened to be left in my ory from the last program during the running of your program, with results which will be unpredictable.
Example ( [full source]) - with declarations without initialisation, I may get:
int fred;
float boat;
munchkin:c12 grahamellis$ ./vars
Integer is 32767; float is 27679303629726023680.000000
munchkin:c12 grahamellis$
But with declarations with initialisations from the same program ( [full source]) , I will get:
int fred = 15;
float boat = 17.778;
munchkin:c12 grahamellis$ ./vars2
Integer is 15; float is 17.778000
munchkin:c12 grahamellis$
It is features like this, of C that make it a very fast language to run, but a very much longer-winded language in which to design and write code as there's so much more work you have to do to make sure things that other languages would check don't get overlooked. Just one of the reasons that C is great for the really heavy compute jobs, and for code that's copied millions of times like operating systems, but is a poor choice for lighter work and occasional code!
(written 2012-01-24, updated 2012-01-28)
Associated topics are indexed as below, or enter http://melksh.am/nnnn for individual articles C202 - C and C based languages - A first practical program [888] Turning C from source to a running program - (2006-10-06) [2580] C course inspires new teaching examples - (2010-01-16) [3121] New year, new C Course - (2011-01-05) [3236] C - a first program that does something useful for you - (2011-04-09) [4337] Learning to program sample program - past its prime, but still useful - (2014-12-02) [4632] Remember to ask the question before you listen for the answer - (2016-01-26)
Some other Articles
Back to UniChars, char arrays and strings in C. Some early cautions and pitfalls.Are we one man, one vote? No, and we never have been. Integer types, and integer overflows, in CDefining, declaring and initialising variables in CPromoting a single one of your domains on the search enginesLearning about how to help people learning - and retaining C++ Courses - do I get official certification at the end of my Well House course?Emergency First Aid Provision at Well House Manor - part of the serviceThe changing Hotel and Bed and Breakfast business in Melksham
|
4759 posts, page by page
Link to page ... 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96 at 50 posts per page
This is a page archived from The Horse's Mouth at
http://www.wellho.net/horse/ -
the diary and writings of Graham Ellis.
Every attempt was made to provide current information at the time the
page was written, but things do move forward in our business - new software
releases, price changes, new techniques. Please check back via
our main site for current courses,
prices, versions, etc - any mention of a price in "The Horse's Mouth"
cannot be taken as an offer to supply at that price.
Link to Ezine home page (for reading).
Link to Blogging home page (to add comments).
|
|