Hello Earth
Introduction​
You can print an output or statement in C. To enable this #include <stdio.h>
header file that is specific for input and output behavior in C. The printf() can be used as below:
// Hello Earth program
// C library that includes printf() function
#include <stdio.h>
// Main function
int main() {
printf("Hello Earth");
return 0;
}
Output:
Hello Earth
Variables​
C is statically-typed which means the programmer does have to declare the type of the variable when defining the variable. To declare a integer variable:
int variable = 7;
Programs do have variables as the name to the storage area. These can be manipulated, has the size and layout of the variable’s memory. The names are a combination of letters, digits, and underscore characters. The compiler is specific of upper and lower characters and the name generally starts with underscore or letter.
// Declaring a variable
#include <stdio.h>
int main() {
// Declaring a integer variable which holds the number 7
int variable = 7;
return 0;
}
Format specifier​
The format specifiers in C are used to define the datatype for input and output.
- %d - is used for integers
- %c - is used for characters
// Printing the variable
#include <stdio.h>
int main() {
// Declaring a integer variable which holds the number 7
int variable = 7;
// Printing the variable using integer format specifier
printf("%d", variable);
return 0;
}
Output:
7
New Line​
To print a output in multiple lines, use \n:
printf("Hello Earth\nYou are not as green as I expected\n");
Output:
Hello Earth
You are not as green as I expected
Arrays​
A fixed data structure that stores the same type of elements is called arrays. It is also the collection of the same elements. There is a single and multi-dimensional array. Arrays are widely used during programming for its easy access to elements across the dimension. The index of an array starts with 0, so the first element is accessible using [0].
To create an array without defining the size:
int arr[] = {1, 2, 3, 4};
To define the size of the array:
int arr[5] = {1, 2, 3, 4, 5};
To print the first element of this array:
printf("%d", arr[0]);
Strings​
A string is an array of characters. To define a string variable:
char message[] = "Humans are smart";
note
The datatype here is a string of characters. Therefore, char is the datatype.