C Notes till Variables
C Notes till Variables
C Notes till Variables
It is a very popular language, despite being old. The main reason for its
popularity is because it is a fundamental language in the field of computer
science.
Why Learn C?
It is one of the most popular programming languages in the world
If you know C, you will have no problem learning other popular
programming languages such as Java, Python, C++, C#, etc, as the
syntax is similar
C is very fast, compared to other programming languages,
like Java and Python
C is very versatile; it can be used in both applications and technologies
#include <stdio.h>
int main ()
{
// Your code starts here
return 0;
}
/* Function Declarations */
int example(int x);
/* Main Function */
int main ()
{
// Your code starts here
return 0;
}
/* Function Definitions */
int example(int x){
Myfirstprogram.c
// My first program
#include <stdio.h>
int main() {
printf("Hello World!");
return 0;
}
Don't worry if you don't understand the code above - we will discuss it in
detail in later chapters. For now, focus on how to run the code.
Example explained
Line 1: #include <stdio.h> is a header file library that lets us work with
input and output functions, such as printf() (used in line 4). Header files add
functionality to C programs.
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.
Line 2: A blank line. C ignores white space. But we use it to make the code
more readable.
Note: The body of int main() could also been written as:
int main(){printf("Hello World!");return 0;}
Line 6: Do not forget to add the closing curly bracket } to actually end the
main function.
C Statements
A computer program is a list of "instructions" to be "executed" by a
computer.
The following statement "instructs" the compiler to print the text "Hello
World" to the screen:
Example
printf("Hello World!");
It is important that you end the statement with a semicolon ;
If you forget the semicolon (;), an error will occur and the program will not
run:
Example
printf("Hello World!")
Many Statements
Most C programs contain many statements.
The statements are executed, one by one, in the same order as they are
written:
Example
printf("Hello World!");
printf("Have a good day!");
return 0;
Example explained
1. printf("Hello World!");
2. printf("Have a good day!");
3. return 0;
The first statement is executed first (print "Hello World!" to the screen).
Then the second statement is executed (print "Have a good day!" to the
screen).
And at last, the third statement is executed (end the C program
successfully).
You will learn more about statements while reading this tutorial. For now,
just remember to always end them with a semicolon to avoid any errors.
Example
#include <stdio.h>
int main() {
printf("Hello World!");
return 0;
}
Double Quotes
When you are working with text, it must be wrapped inside double quotations
marks "".
Example
printf("This sentence will work!");
int main() {
printf("Hello World!");
printf("I am learning C.");
printf("And it is awesome!");
return 0;
}
C New Lines
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,
this could 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;
}
What is \n exactly?
The newline character (\n) is called an escape sequence, and it forces the
cursor to change its position to the beginning of the next line on the screen.
This results in a new line.
Single-line Comments
Single-line comments start with two forward slashes (//).
Any text between // and the end of the line is ignored by the compiler (will
not be executed).
Example
// This is a comment
printf("Hello World!");
Example
printf("Hello World!"); // This is a comment
C Multi-line Comments
Multi-line comments start with /* and ends with */.
Any text between /* and */ will be ignored by the compiler:
Example
/* The code below will print the words Hello World!
to the screen, and it is amazing */
printf("Hello World!");
Good to know: Before version C99 (released in 1999), you could only use
multi-line comments in C.