First up for this blog is how to create a constant in C.
Open a new project and at the top under the #include we are going to create a constant. In order to do so write #define CONSTANT_NAME 5. the structure is the name of the constant in all caps then it's value is right after it. Constants can contain both string and numbers.
Also a very important part of these printf() functions is the use of conversion characters. For integers we want to put %d in the string, for float use %f, or for a string, use %s. These help the program read what is being entered in from the constant or later the variable.
And the end code should look like this:
Next will be variables.
The way variables are instantiated in C is very similar to Java.
int x = 5;
or
double large = 56789123;
It is the type of variables followed by a name, then equals sign and the value.
Running this code should produce the following output:
Here I would like to make note of something I noticed while working on this program. In the run screen there is a \n to signal a newline, but there is no corresponding \n in the code. I ran a previous build and that had the extra \n, but did not re-build the code when I ran it again. Be sure to build the code each time it is changed, the program will not do that automatically.
Here is another program that will go through assigning variables through arithmetic.
Added:
Here is another example for basic arithmetic: volume of a cylinder.
I would have like to have a more complex calculation, like volume of a cylinder.
ReplyDeleteHey Brandon this is Cameron and I will be viewing your C blog. This language is interesting, reminds me of Java.
ReplyDelete