Let’s print “Hi”
In your first computer program, let’s print something on the screen to display. How can we instruct a computer to print “Hi” on the screen in simple English?
Did something similar come to your mind?
print Hi
Now, let’s think from a computer’s perspective and do a few changes to our command.
Round Brackets
Round Brackets: Round brackets will help the computer to separate the print command from the text which we want to print.
Now our command will change to:
print(Hi)
Double Quotes
Double Quotes: In C language, the text to be printed is always enclosed within double quotes. We call this text a string. So our command will change to
print("Hi")
Semicolon
Semicolon: In C, we use semicolon at the end of each command to indicate the computer that it’s the end of a command.
Now our command will change to:
print("Hi");
Wait a minute, there is one more thing. In the C language, we use printf instead of just the print command. So our final code i.e. the command to print the text ‘Hi’ on the computer screen will be:
printf("Hi");
The result of the above command will be:
Hi
Adding changes to our command
Use of \n
Let us learn one more thing. Suppose you want to print your first name in one line and your last name in the next line, you will have to use at the end of your first name.
\n tells the computer to go to the next line before printing the next text. \n is referred to as the newline character. So you can now use the below commands to print your first name and last name:
printf (“Jack\n”);
printf(“Sparrow”);
Output: Jack Sparrow
If we miss \n and write as follows:
printf (“Johny”);
printf(“Depp”);
The Result will be: JackSparrow
Please note
Please note that ‘C’ is a case sensitive language. If you write Printf instead of printf, you will get an error. However, C does not care about extra spacing. Because the meaning of Printf and printf is different for the computer.
Now you know how to tell the computer to print some text on the screen, in ‘C’ language. But our program is still incomplete. To be able to make any program actually work and produce desired results i.e. to ‘Run’ a program, we will have to add few more commands.
Let’s see how to do it.
Let’s print on new line:
Step 1: Add the below commands to your program.
int main () {
return 0;
}
Don’t be scared by looking at the above commands, let’s make it easy for you by dividing it into different parts.
int main(): Every C program requires a main function to be executed.
A function is nothing but a group of commands. Hence, all the commands that you will be learning during our course will be written inside this function.
The int represents the output of the function. The main function always returns an integer, hence it starts with an int. We will discuss the parentheses another time.
{ }: Commands will be written inside the open and closed curly brackets {…}.
And at the end of your commands, you need to return some value, so, we used ‘return 0;’. This tells the computer that you have ended your main function and you want to return 0 as the integer value.
Now you will be able to run the program, click on Build and Run.
After running the above program, the output will be. What happened? You are not able to see anything in the Result box right?
That is because we haven’t written our printf command. You simply told the computer to compute this function but you did not tell it to display the result. Let’s do it.
But before that, you will have to add #include <stdio.h>
command at the very beginning of our program.
#include <stdio.h>
int main () {
printf("Welcome to mrwixXsid")
return 0;
}
Hurrah! you just ran your very first program in C language. Think about why the 0 did not display in your output (pause and think). It is because you did not tell the computer to display it!
0 Comments