Class: 06 Introduction to Computer Programming --- Complete Course for BSSE and BSCS Students Specially

 




Whitespaces

When we write a program using any programming language, we use various printable characters to prepare programming statements. These printable characters are a, b, c,......z, A, B, C,.....Z, 1, 2, 3,...... 0, !, @, #, $, %, ^, &, *, (, ), -, _, +, =, \, |, {, }, [, ], :, ;, <, >, ?, /, \, ~. `. ", '. Hope I'm not missing any printable characters from your keyboard.

Apart from these characters, there are some characters which we use very frequently but they are invisible in your program and these characters are spaces, tabs (\t), new lines(\n). These characters are called whitespaces.

These three important whitespace characters are common in all the programming languages and they remain invisible in your text document −

WhitespaceExplanationRepresentation
New LineTo create a new line\n
TabTo create a tab.\t
SpaceTo create a space.empty space

A line containing only whitespace, possibly with a comment, is known as a blank line, and a C compiler totally ignores it. Whitespace is the term used in C to describe blanks, tabs, newline characters, and comments. So you can write printf("Hello, World!" ); as shown below. Here all the created spaces around "Hello, World!" are useless and the compiler will ignore them at the time of compilation.

#include <stdio.h>

int main() {

   /* printf() function to write Hello, World! */
   
   printf(    "Hello, World!"      );
   
}

which produces the following result −

Hello, World!

If we make all these whitespace characters visible, then the above program will look like this and you will not be able to compile it −

#include <stdio.h>\n
\n
int main()\n
{
   \n
   \t/* printf() function to write Hello, World! */
   \n 
   \tprintf(\t"Hello, World!"\t);\n
   \n
}\n

Semicolons

Every individual statement in a C Program must be ended with a semicolon (;), for example, if you want to write "Hello, World!" twice, then it will be written as follows −

#include <stdio.h>

int main() {
   /* printf() function to write Hello, World! */
   printf( "Hello, World!\n" );
   printf( "Hello, World!" );
}

This program will produce the following result −

Hello, World! 
Hello, World!

Here, we are using a new line character \n in the first printf() function to create a new line. Let us see what happens if we do not use this new line character −

#include <stdio.h>

int main() {
   /* printf() function to write Hello, World! */
   printf( "Hello, World!" );
   printf( "Hello, World!" );
}

This program will produce the following result −

Hello, World! Hello, World!

We will learn identifiers and keywords in next few chapters.

Program Explanation

Let us understand how the above C program works. First of all, the above program is converted into a binary format using C compiler. So let’s put this code in test.c file and compile it as follows −

$gcc test.c -o demo

If there is any grammatical error (Syntax errors in computer terminologies), then we fix it before converting it into binary format. If everything goes fine, then it produces a binary file called demo. Finally, we execute the produced binary demo as follows −

$./demo

which produces the following result −

Hello, World!

Here, when we execute the binary a.out file, the computer enters inside the program starting from main() and encounters a printf() statement. Keep a note that the line inside /*....*/ is a comment and it is filtered at the time of compilation. So printf() function instructs the computer to print the given line at the computer screen. Finally, it encounters a right curly brace which indicates the end of main() function and exits the program.


Research Resources: Internet -- I am not owner of any thing!

Post a Comment

Previous Post Next Post