C Basic Syntax

A simple C program's basic syntax consists of header files, the main() function, and then program code. The basic syntax of the simplest and smallest C program is as follows:

header_files
return_type main()
{
   // program codes
}

Every C program must contain a function called main(), because program execution starts from main(). For example, here is the simplest and smallest C program, which would print "Hello Compiler" on the output screen:

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

Here is the sample output of the above C program:

Hello there

Let me explain the above code. The first statement is:

#include <stdio.h>

includes a header file named "stdio.h" in the program so that we can use the functions like "printf()" defined in it. The second line of code, which is:

int main()

creates a "main()" function. Let me tell you that the program starts its execution from the "main()" function. Therefore, each and every C program must contain a "main()" function where the program starts its execution. The "int" before the "main()" method is its return type, therefore, at the end of the function definition, I need to return an integer value, so I used "return 0;" as the last statement of the function definition.

Now inside the "main()" function, I used the "printf()" function, which is defined in the "stdio.h" header file and is used to print the formatted text on the output console.

Tokens in C

Tokens in C can be either keywords, constants, string constants, symbols, or identifiers. And, depending on the length of the program, a C program is made up of various tokens. Consider the following example, in which the statement consists of five total tokens:

printf("Hello there");

Here, the individual tokens from the above statements are:

Semicolons in C

In C programming, a semicolon is used to terminate a statement. The semicolon in C is also called a "statement terminator" because each individual statement must be ended with a statement terminator or semicolon. This tells the compiler that this statement is complete. And it indicates the end of one logical entity or set of statements. If a semicolon is not placed at the end of any statements, then the program will not compile and generate an error message on compilation. Let's look at the following example. Here are two different statements, each terminated by a semicolon:

printf("Hello there");
printf("Sum = %d", sum);

However, a semicolon is only required in cases of "expression," "jump statements,"  and "declaration statements." Using a semicolon to terminate iteration statements, selection statements, compound statements, labeled statements, etc. is not allowed.

Comments in C

Comments in C programming are similar to help text in your C program. The compiler disregards them. Comments are extremely helpful when reviewing code. Because given comments greatly assist a programmer when reviewing code. Programmers can use comments to add useful information, a reminder about a line of code, or anything else for other purposes.

Following are the two types of comments available in the C language.

Single-line comment in C

Anything that starts with "//" is a single-line comment. For example:

// This is a single-line comment
#include <stdio.h>
int main()
{
   // The following line of code prints the text
   // "Seattle is a lovely city in the United States."
   // on the output console.
   printf("Seattle is a lovely city in the United States.");

   return 0;        // The program ends here.
}

The output should be:

Seattle is a lovely city in the United States.

Multi-line comment in C

Anything written in between "/*" and "*/" is considered a multi-line comment in the C programming language. A multi-line comment is text that spans multiple lines. However, we can also use multi-line comments to write a comment that spans a single line. For example:

/* This is a single-line comment */
#include <stdio.h>
int main()
{
   /* The following line of code prints the text
      "Seattle is a lovely city in the United States."
      on the output console.
   */
   printf("Seattle is a lovely city in the United States.");

   return 0;        /* The program ends here. */
}

The output will be identical to that of the previous program.

But it will be a better choice to use "//" to write a comment that spans only one line, because we only need to write "//," the two characters, whereas in the case of a multi-line comment, we need to write "/*," two characters at the start, and "*/," two characters at the end of the comment.

Identifiers in C

An identifier is a name in C programming that is used to identify variables, functions, or any other user-defined terms. An identifier can start with letters (A to Z) or (a to z), or it can start with an underscore (_), and then it can contain zero or more letters, underscores, or digits (0 to 9).

Within identifiers, C does not allow punctuation characters (special characters) such as @, $, and %. C is a case-sensitive programming language. As a result, "total", "Total", and "TOTAL" are three distinct identifiers in C.

Following is a list of some valid identifiers in C.

C Keywords

Keywords are words that have a specific meaning. Following is a list of keywords available in the C programming language:

For example, the "if" keyword is used to only run a block of code if the condition given evaluates to true.

#include <stdio.h>
int main()
{
   int a, b;

   printf("Enter the two numbers: ");
   scanf("%d %d", &a, &b);

   if(a>b)
   {
      printf("The first number is greater than the second.\n");
   }

   return 0;
}

The following snapshot shows the initial output produced by the above program.

c basic syntax example

Now type a number, say 20, and hit the ENTER key. Then type another number, say 10, and hit the ENTER key to produce the following output:

c basic syntax program

The "%d" is a format specifier for the integer-type value. The "scanf()" function is used when we need to scan values at program runtime. Therefore, the following statement:

scanf("%d %d", &a, &b);

receives two numbers from the user at program run time in such a way that the first number is initialized to "a" and the second number is initialized to "b."

And since the condition "a>b" or "20>10" (on putting the values of a and b) evaluates to true, therefore the block of code written in the "if"'s body will be executed.

Examples

C Online Test


« Previous Tutorial Next Tutorial »