Building C Programs
Building C Programs
•#include <stdio.h>
int main() {
printf("Hello World!");
return 0;
}
C Variables
Variables are containers for storing data values, like numbers and characters.
In C, there are different types of variables (defined with different keywords), for
example:
1. int - stores integers (whole numbers), without decimals, such as 123 or -123
2. float - stores floating point numbers, with decimals, such as 19.99 or -19.99
3. char - stores single characters, such as 'a' or 'B'. Characters are surrounded
by single quotes
Declaring (Creating) Variables
•A declaration in C introduces a
variable to the program and specifies its
type, but it does not allocate memory
for the variable. It tells the compiler
what type of data the variable will hold.
Syntax
int num;
Variable Definition
%f or %F float
%lf double
%c char
1. #include <stdio.h>
2. int main() {
3. int myNum = 5;
4. float myFloatNum = 5.99;
5. char myLetter = 'D';
6. printf("%d\n", myNum);
7. printf("%f\n", myFloatNum);
8. printf("%c\n", myLetter);
9. return 0;
10.}
Simple Calculator in C to Calculate the
Sum of Two Numbers
#include <stdio.h>
int main() {
float num1, num2, sum;
printf("Enter the first number: ");
scanf("%f", &num1);
printf("The sum of %.2f and %.2f is: %.2f\n", num1, num2, sum);
return 0;
}