Sample program 1: Printing a message in c

5 minute read
0

Printing a message



A program to print one line of text.

Input 

1    #include<stdio.h>
2    main()
3    {
4
5    /*.....printing begins......*/
6    
7        printf("Hello World");
8
9    /*.....printing ends.......*/
10
11    }

After execution of the above program will produce the output:

Hello World


The first line in the above program, that is:
                
           #include<stdio.h>

Here #include <stdio.h> is a preprocessor directive(we'll discuss this topic in detail later) in the C programming languages. It is used to include the standard input/output library in your program.

Here's what each part of this directive means:

  • #include: This is a preprocessor directive that tells the C compiler to include the content of a file in your source code before the actual compilation process begins. It's a way to bring in external code or libraries into your program.


  • <stdio.h>: This is the name of the header file you want to include. In this case, it's stdio.h, which stands for "standard input/output." This header file contains declarations for various input and output functions, such as printf and scanf, as well as data types like FILE that are used for file operations.

When you include stdio.h in your C or C++ program, you gain access to the functions and definitions it provides, allowing you to perform input and output operations in your code. For example, you can use printf to display text on the console or scanf to read input from the user.

The main function

In the 2nd line, there is a word with two brackets main()is a special function used by the system to tell the computer where the program starts. Every program has exactly one main function. And if we use more than one main function, the compiler can't understand which one is the beginning of the program.
The empty pair of parentheses immediately following the main indicates that the main function has no arguments(or parameters). This concept will be discussed in detail later when we discuss functions.
In the 3rd line, one opening brace "{" is the beginning of the function main. And the closing brace "}" in line 11 is the end of the function. In this case, the closing brace is also the end of the program. All statements between these two brace form a function body. The function body contains a set of instructions to perform the given task.
C language permits different forms of the main statement. The following forms are allowed:
  • main()
  • int main()
  • void main()
  • main(void)
  • void main(void)
  • int main(void)
The use of the keyword void inside the parentheses means that the function has no arguments. We can also use the keyword int or void before the word main. The keyword void means that the function doesn't return any information to the operating system and int means the function returns an integer value to the operating system. When int is used, the last statement in the program must be "return 0". This program will be :

#include<stdio.h>

int main()
{
    printf("Hello World");
    return 0;
}


So, for the sake of simplicity, we use void main.


Comment line

In lines 5 and 9, we can see two lines are known be comment lines. The line begins with /* and ends with */.
These are used to enhance, the readability and understanding of the program.

Comments lines are not executable statements, anything between /* and */ is ignored by the
compiler.

A comment line can be inserted whenever black space occurs --- at the beginning, middle
or end of line --- "but never in the of the word."

This technique is called multiple-lined comment. We can write multiple lines using this technique.

There is another technique to write comments " //*****comment***** ", This technique is only used for single
lines. The following words come after these "//" lines are ignored by the compiler.
For example: // Printing will occure here

The comment line can't be nested in C. It means we can't have a comments line inside the
comments.
                /*....................../*....................*/.....................*/

It will result in an error.


The printf function

Let us now look at the printf() function in the line 7. It is the only executable statement of the program.
                
    printf("Hello World");

printf is a predefined standard c function for printing or displaying output. Predefined means that, it is a function
that has already been written complied, and linked together with our program at the time of linking. This compilation
and linking concept will be discussed later.

The printf function causes everything between the starting and ending quotation marks to be printed out. Here in this
case:

    Hello World

★ Noted that Every statement in c should end with a semicolon (;) mark.

The information contained between the parentheses is called the argument of the function. Here the argument of the printf function is " Hello world ".

Suppose we want to print the above quotation in two line as
        
        Hello
    World

This can be done by adding another printf function as shown below:

        printf("Hello\n");
    printf("World");

Here you have noticed in the first printf function, that there are two new characters '\' and 'n' at the end of the string. This combination is called the newline character. It instructs the computer to go to the next line(new) line. So, we got in the first line Hello and in the 2nd line World as an output.
If we don't use the newline character in the first printf function, then the output will again be a single line.

    HelloWorld

Remember there is no space between Hello and World.


There is also a possible way to produce two or more lines of output by one printf function statement with the use of a newline character at the appropriate place.
The statement:
    
    printf("Hello\nworld");

will output:

    Hello
    Wolrd

while the statement: 
    
    printf("Hello\n......\n....\nto\nworld");

will print out:

    Hello
    ......
    ....
    to
    world


Congratulations you have completed our very first class of C programming language. You learn a few things here, and you will learn many more concepts in this journey of yours. We wish you a great journey ahead and we make sure you will learn and enjoy your journey. In no time you will be a pro in C language.

----------------------------------------------------------------------------------------------------------------------------

Explore our website for valuable C programming resources and enhance your coding skills today!.


Learn C programming language from basic to advanced here (Click here or on the image)


Stay Strong, keep learning, and keep growing.




Post a Comment

0Comments
Post a Comment (0)