You have already seen the following code a couple of times in the first chapters. Let's break it down to understand it better:
Example
#include <stdio.h>
int main() {
printf("Hello World!");
return 0;
}
Example explained
Don't worry if you don't understand how #include <stdio.h> works. Just think of it as something that (almost) always appears in your program.
Note that: Every C statement ends with a semicolon ;
Note: The body of int main() could also been written as:
int main(){printf("Hello World!");return 0;}
Remember: The compiler ignores white spaces. However, multiple lines makes the code more readable.
The printf() function is used to output values/print text:
Example
#include <stdio.h>
int main() {
printf("Hello World!");
return 0;
}
You can add as many printf() functions as you want. However, note that it does not insert a new line at the end of the output:
Example
#include <stdio.h>
int main() {
printf("Hello World!");
printf("I am learning C.");
return 0;
}
To insert a new line, you can use the \n character:
Example
#include <stdio.h>
int main() {
printf("Hello World!\n");
printf("I am learning C.");
return 0;
}
You can also output multiple lines with a single printf() function. However, be aware that this will make the code harder to read:
Example
#include <stdio.h>
int main() {
printf("Hello World!\nI am learning C.\nAnd it is awesome!");
return 0;
}
Tip: Two \n characters after each other will create a blank line:
Example
#include <stdio.h>
int main() {
printf("Hello World!\n\n");
printf("I am learning C.");
return 0;
}
10 videos|13 docs|15 tests
|
|
Explore Courses for Class 6 exam
|