C Programming Basics: Variables, Data Types, and Printf
1.
2. Before I explain any int main()
{
code to you, I int i;
would like you to int sum = 0;
execute your first for(i = 0; i<10; i++){
sum += i;
program by }
copying and
printf("Sum = %dn", sum);
pasting my code. //system("pause");
return 0;
}
3. Locate the compile button and run
buttons in your program.
You may alternatively find the compile
and run button that executes both at
once.
You should notice a black box
appearing and disappearing very
quickly. That is because I have allowed
this code to execute without any pause
statements.
4. Programs typically automatically close
once the final line of code is reached.
The line system(“pause”); needs to be
added to “pause” the program in place
The system pause should be placed right
before the return 0; line of any program.
Return 0 is typically used at the end of a
program to tell the computer the program
has ended.
5. Code is normally presented omitting the
system("pause"); statement which
confuses new programmers. Always
make a mental note to add it in if you
are testing new code.
6. When you place // before a line of
code, you comment the code out and
cause the compiler to skip over the
code.
Now you can remove the // before the
system pause line in the example
program and run the program again to
see output. If all is correct, you should
see the output being Sum = 45.
7.
8. I want to draw your attention to the first line:
int main(){
This is the beginning of your main function. The
function ends with the use of }.
Any time you open a { bracket, you must close it with
a } bracket.
As you can see with the main function, you can see
the entire main function enclosed within the { }
brackets.
Additional code is allowed within the brackets. This
includes code that uses another set of { } as well.
The last { bracket to be opened is the first one to be
closed when a } bracket is used.
9. The main function is the starting point of
a program and where a compiler first
starts to execute your code.
It is required in order to compile your
code, therefore it would be a good
practice to use the following template.
10. int main(){
//your new written code goes here
system("pause");
return 0;
}
11. Our program is going to need a way to hold
our values
These values are stored in different types of
variables.
Integers, floats, and doubles are the main
“number” holders of a program.
A variable in C programming must always be
declared at the top of the program right under
int main() { if you want to use the variable in
your program.
Unlike some programming languages, you may
not locally declare a variable anywhere.
12. An integer is a number only
representable in a whole number form.
Integers: 1,2,3,4,5..
Not Integers: 1.5, 1.7, 2.5, 3.9, 1003.4...
Integer math is the calculation between
two integers. The math is calculated as
you would normally between any two
numbers except the decimal value is
dropped. Ex. 1+3=4, 1+.5=1 , 5/4 = 1
13. Double and Float variables allow the use of
decimal numbers. A float has a smaller
default precision than a double.
If you declare a float and then try to store
the number within the float into a double,
you may be transferring only part of the
number.
A float allows at most about 7 decimal
places precision.
A double allows at least 7 places.
To accurately work beyond 7 decimals you
may require addition resources.
14. I typically use only Doubles when working
with decimals and will work with only
Doubles within these tutorials.
The math calculated with a double or
float does not use integer math. It would
be safe to assume the calculated
answer would be similar to the answer
given in a math class.
15. An integer is declared by typing
int variablename;
A float is declared by typing
float variablename;
A double is declared by typing
double variablename;
The variablename is changeable to
anything starting with a letter and
anything that has not been assigned to
a variable yet.
16. Multiple variables of the same type are
declarable by adding a comma after
the first variable and then typing the
second.
int one, two, three;
17. The point of a variable is to eventually
hold data.
The variable could be initialized
immediately with a value.
int one = 1;
double duck = 12.5;
18. To print out the value a variable is holding
you use the printf statement
printf(“The variable one = %d”, one);
As shown above, the text you want printed
out is held within the quotation marks. A
%variabletype is added to signal to the
computer you want to print out a variable.
int -> %d float -> %fl double -> %lf
After the comma, you list the variables in
order that match up with the %variables
19. A double could be printed out but it may
contain too many decimal places.
Limit a double by using %.#lf for the
number of decimal places you want to
be printed out.
The key “n” to a computer is
interpreted as a line break.
When used in a printf statement, it will
add a line break into the output.
20. int main(){
int one = 5.5;
float two = 7.5;
double three = 10.5;
printf("One equals: %d n", one);
printf("Two equals: %f n", two);
printf("Three equals: %lf n ", three);
three = 15;
printf("Three equals: %lf ", three);
system("pause");
return 0;
}