Wednesday, November 22, 2017

Basic Structure of a C program


In this section we will try to understand basic structure of a simple C program.


Let’s consider we don’t know anything about C programming if we know already. So we have no idea about its syntax or anything. Before introducing the basic terminology i would like to show you the basic C code first.


Let’s look at the code below. It is a simple C program.


#include <stdio.h>
int main()
{
    return 0;
}

So what this code means? Does it print anything? NO, it’s not going to print anything because we didn’t use any command which can print.

Wait a minute! Let’s add another line to our previous code before going to the first question.


#include <stdio.h>
int main()
{
    printf(“Hello World”);
    return 0;
}

OUTPUT :



Let’s try to understand the above program line by line.

#include is a preprocessor command and <stdio.h> is a header. By including this, we get the access of using standard input/output form the C library before executing the code.


This line tells the compiler that there is a function called main, and this function returns an integer. Every C program must have exactly one main function. From the main function or this line, every C program starts execution.


Opening curly brace { indicates the beginning of all functions in C. In this case it is indicating the beginning of the main function.


This whole line is a statement and printf(“ ”) is a function which prints the output on the screen. Whatever you write inside the quotes “” it will show that on screen after executing the program. What about the semicolon? It indicates the end of the statement. In C programming you have to put a semicolon after every statement.


It indicates the END of the program. We return a value from main to the operating system (we discussed before that main function returns an integer). A return value 0 means your program executed successfully.


Closing curly brace } indicates the end of the function.


Challenge :



Previous Topic

Next Topic


No comments:

Post a Comment