C language syntax specify rules for sequence of characters to be written in C language. In simple language it states how to form statements in a C language program - How should the line of code start, how it should end, where to use double quotes, where to use curly brackets etc.
The rule specify how the character sequence will be grouped together, to form tokens. A smallest individual unit in C program is known as C Token. Tokens are either keywords, identifiers, constants, variables or any symbol which has some meaning in C language. A C program can also be called as a collection of various tokens.
In the following program,
#include
int main()
{
printf("Hello,World");
return 0;
}
if we take any one statement:
printf("Hello,World");
Then the tokens in this statement are→
printf, (, "Hello,World", ) and ;.
So C tokens are basically the building blocks of a C program.
Semicolon ;
Semicolon
; is used to mark the end of a statement and beginning of another statement. Absence of semicolon at the end of any statement, will mislead the compiler to think that this statement is not yet finished and it will add the next consecutive statement after it, which may lead to compilation(syntax) error.#include
int main()
{
printf("Hello,World")
return 0;
}
In the above program, we have omitted the semicolon from the
printf("...") statement, hence the compiler will think that starting from printf uptill the semicolon after return 0 statement, is a single statement and this will lead to compilation error.Comments
Comments are plain simple text in a C program that are not compiled by the compiler. We write comments for better understanding of the program. Though writing comments is not compulsory, but it is recommended to make your program more descriptive. It make the code more readable.
There are two ways in which we can write comments.
- Using
//This is used to write a single line comment. - Using
/* */: The statements enclosed within/*and*/, are used to write multi-line comments.
Example of comments :
// This is a comment
/* This is a comment */
/* This is a long
and valid comment */
// this is not
a valid comment
Some basic syntax rule for C program
- C is a case sensitive language so all C instructions must be written in lower case letter.
- All C statement must end with a semicolon.
- Whitespace is used in C to describe blanks and tabs.
- Whitespace is required between keywords and identifiers. We will learn about keywords and identifiers in the next tutorial.
Here is the code explanation:
Pre-processor directive
#include is a pre-processor directive in 'C.'
#include <stdio.h>, stdio is the library where the function printf is defined. printf is used for generating output. Before using this function, we have to first include the required file, also known as a header file (.h).
You can also create your own functions, group them in header files and declare them at the top of the program to use them. To include a file in a program, use pre-processor directive
#include <file-name>.h
File-name is the name of a file in which the functions are stored. Pre-processor directives are always placed at the beginning of the program.
The main function
The main function is a part of every 'C' program. We can represent the main function in various forms, such as:
- main()
- int main()
- void main()
- main(void)
- void main(void)
- int main(void)
The empty parentheses indicate that this function does not take any argument, value or a parameter. You can also represent this explicitly by placing the keyword void inside the parentheses. The keyword void means the function does not return any value, in this case, the last statement is always getch ().
#include<stdio.h> //Pre-processor directive
int main() //main function declaration
{
printf("Hello World"); //to output the string on a display
return 0; //terminating function
}
In the above example, the keyword int means the function will return an integer value. In this case, the last statement should always return 0.
The source code
After the main function has been declared, we have to specify the opening and closing parentheses. Curly brackets { }, indicate the starting and end of a program. These brackets must be always put after the main function. All the program code is written inside these brackets, such as declarative and executable part.
The printf function generates the output by passing the text "Hello World!"
The semicolon ; determines the end of the statement. In C, each statement must end with a semicolon.
So we have successfully installed the compiler and now can begin working in 'C.' We will write a simple program that will say hello to us. Let's start.
1. C PROGRAMMING BASICS TO WRITE A C PROGRAM:
Below are few commands and syntax used in C programming to write a simple C program. Let’s see all the sections of a simple C program line by line.
C Basic commands Explanation
#include <stdio.h> This is a preprocessor command that includes standard input output header file(stdio.h) from the C library before compiling a C program
int main() This is the main function from where execution of any C program begins.
{ This indicates the beginning of the main function.
/*_some_comments_*/ whatever is given inside the command “/* */” in any C program, won’t be considered for compilation and execution.
printf(“Hello_World! “); printf command prints the output onto the screen.
getch(); This command waits for any character input from keyboard.
return 0;
This command terminates C program (main function) and returns 0.
}
This indicates the end of the main function.
Below are few commands and syntax used in C programming to write a simple C program. Let’s see all the sections of a simple C program line by line.
| C Basic commands | Explanation |
| #include <stdio.h> | This is a preprocessor command that includes standard input output header file(stdio.h) from the C library before compiling a C program |
| int main() | This is the main function from where execution of any C program begins. |
| { | This indicates the beginning of the main function. |
| /*_some_comments_*/ | whatever is given inside the command “/* */” in any C program, won’t be considered for compilation and execution. |
| printf(“Hello_World! “); | printf command prints the output onto the screen. |
| getch(); | This command waits for any character input from keyboard. |
| return 0; |
This command terminates C program (main function) and returns 0.
|
| } |
This indicates the end of the main function.
|
2. A SIMPLE C PROGRAM:
Below C program is a very simple and basic program in C programming language. This C program displays “Hello World!” in the output window. And, all syntax and commands in C programming are case sensitive. Also, each statement should be ended with semicolon (;) which is a statement terminator.
1
2
3
4
5
6
7
8
#include <stdio.h>
int main()
{
/* Our first simple C basic program */
printf("Hello World! ");
getch();
return 0;
}
Below C program is a very simple and basic program in C programming language. This C program displays “Hello World!” in the output window. And, all syntax and commands in C programming are case sensitive. Also, each statement should be ended with semicolon (;) which is a statement terminator.
1
2
3
4
5
6
7
8
|
#include <stdio.h>
int main()
{
/* Our first simple C basic program */
printf("Hello World! ");
getch();
return 0;
}
|
OUTPUT:
Hello World!
#include<stdio.h> //Pre-processor directive
void main() //main function declaration
{
printf("Hello World"); //to output the string on a display
getch (); //terminating function
}
- Hello World!
3. STEPS TO WRITE C PROGRAMS AND GET THE OUTPUT:
Below are the steps to be followed for any C program to create and get the output. This is common to all C program and there is no exception whether its a very small C program or very large C program.
- Create
- Compile
- Execute or Run
- Get the Output
Below are the steps to be followed for any C program to create and get the output. This is common to all C program and there is no exception whether its a very small C program or very large C program.
- Create
- Compile
- Execute or Run
- Get the Output
4. CREATION, COMPILATION AND EXECUTION OF A C PROGRAM:
Prerequisite:
- If you want to create, compile and execute C programs by your own, you have to install C compiler in your machine. Then, you can start to execute your own C programs in your machine.
Prerequisite:
- If you want to create, compile and execute C programs by your own, you have to install C compiler in your machine. Then, you can start to execute your own C programs in your machine.
5. BASIC STRUCTURE OF A C PROGRAM:
Structure of C program is defined by set of rules called protocol, to be followed by programmer while writing C program. All C programs are having sections/parts which are mentioned below.
- Documentation section
- Link Section
- Definition Section
- Global declaration section
- Function prototype declaration section
- Main function
- User defined function definition section
Structure of C program is defined by set of rules called protocol, to be followed by programmer while writing C program. All C programs are having sections/parts which are mentioned below.
- Documentation section
- Link Section
- Definition Section
- Global declaration section
- Function prototype declaration section
- Main function
- User defined function definition section
EXAMPLE C PROGRAM TO COMPARE ALL THE SECTIONS:
You can compare all the sections of a C program with the below C program.
You can compare all the sections of a C program with the below C program.
OUTPUT:
This is a C basic program
Sum of two numbers : 2
This is a C basic program
Sum of two numbers : 2 |
DESCRIPTION FOR EACH SECTION OF THE C PROGRAM:
- Let us see about each section of a C basic program in detail below.
- Please note that a C program mayn’t have all below mentioned sections except main function and link sections.
- Also, a C program structure mayn’t be in below mentioned order.
Sections Description
Documentation section We can give comments about the program, creation or modified date, author name etc in this section. The characters or words or anything which are given between “/*” and “*/”, won’t be considered by C compiler for compilation process.These will be ignored by C compiler during compilation.
Example : /* comment line1 comment line2 comment 3 */
Link Section Header files that are required to execute a C program are included in this section
Definition Section In this section, variables are defined and values are set to these variables.
Global declaration section Global variables are defined in this section. When a variable is to be used throughout the program, can be defined in this section.
Function prototype declaration section Function prototype gives many information about a function like return type, parameter names used inside the function.
Main function Every C program is started from main function and this function contains two major sections called declaration section and executable section.
User defined function section User can define their own functions in this section which perform particular task as per the user requirement.
- Let us see about each section of a C basic program in detail below.
- Please note that a C program mayn’t have all below mentioned sections except main function and link sections.
- Also, a C program structure mayn’t be in below mentioned order.
| Sections | Description |
| Documentation section | We can give comments about the program, creation or modified date, author name etc in this section. The characters or words or anything which are given between “/*” and “*/”, won’t be considered by C compiler for compilation process.These will be ignored by C compiler during compilation. Example : /* comment line1 comment line2 comment 3 */ |
| Link Section | Header files that are required to execute a C program are included in this section |
| Definition Section | In this section, variables are defined and values are set to these variables. |
| Global declaration section | Global variables are defined in this section. When a variable is to be used throughout the program, can be defined in this section. |
| Function prototype declaration section | Function prototype gives many information about a function like return type, parameter names used inside the function. |
| Main function | Every C program is started from main function and this function contains two major sections called declaration section and executable section. |
| User defined function section | User can define their own functions in this section which perform particular task as per the user requirement. |
6. EXAMPLE C PROGRAMS WITH DEFINITION, EXAMPLE PROGRAM AND OUTPUT:
If you have enough basic knowledge on C programming language and all concepts, you can refer following C programs.
Please click here “C programs” for referring below programs.
- C program for Prime number
- C program for Factorial
- C program for Fibonacci series
- C program for Palindrome
- C program for Swapping 2 numbers with and without temp variable
- Sample calculator program and bank application program
- etc.
If you have enough basic knowledge on C programming language and all concepts, you can refer following C programs.
Please click here “C programs” for referring below programs.
- C program for Prime number
- C program for Factorial
- C program for Fibonacci series
- C program for Palindrome
- C program for Swapping 2 numbers with and without temp variable
- Sample calculator program and bank application program
- etc.
KEY POINTS TO REMEMBER IN C PROGRAMMING BASICS:
- C programming is a case sensitive programming language.
- Each C programming statement is ended with semicolon (;) which are referred as statement terminator.
- printf() command is used to print the output onto the screen.
- C programs are compiled using C compilers and displays output when executed.
How to run C Program
- C programming is a case sensitive programming language.
- Each C programming statement is ended with semicolon (;) which are referred as statement terminator.
- printf() command is used to print the output onto the screen.
- C programs are compiled using C compilers and displays output when executed.
How to run C Program
Step 1) Create a new Project
Step 3) Continue, by clicking on "Next."
Step 4) To create the new file ,select a "C" file then click on "Next" button to continue.
Step 5) Set the file path by clicking the "..." button, the explorer window permits to create the C file.
Step 6) Select the path of your new C File then its name which has .c extension and save it.
Step 7) Finally, to confirm the C file creation click "Finish."
Step 8) Enter the code, save it and compile it by clicking on the "Build & Run "button.
Here is the result:
Hello, World!
Summary
- The main function is a mandatory part of every 'C' program.
- To use the functionality of a header file, we have to include the file at the beginning of our program.
- Every 'C' program follows a basic structure.
#include<stdio.h>
With this line of code we include a file called stdio.h. (Standard Input/Output header file). This file lets us use certain commands for input or output which we can use in our program. (Look at it as lines of code commands) that have been written for us by someone else). For instance it has commands for input like reading from the keyboard and output commands like printing things on the screen.
int main()
The int is what is called the return value (in this case of the type integer). Where it used for will be explained further down. Every program must have a main(). It is the starting point of every program. The round brackets are there for a
reason, in a later tutorial it will be explained, but for now it is enough to know that they have to be there.
reason, in a later tutorial it will be explained, but for now it is enough to know that they have to be there.
{}
The two curly brackets (one in the beginning and one at the end) are used to group all commands together. In this case all the commands between the two curly brackets belong to main(). The curly brackets are often used in the C language to group commands together. (To mark the beginning and end of a
group or function.).
group or function.).
printf(“Hello World\n”);
The printf is used for printing things on the screen, in this case the words: Hello World. As you can see the data that is to be printed is put inside round brackets.
The words Hello World are inside inverted commas, because they are what is called a string. (A single letter is called a character and a series of characters is called a string). Strings must always be put between inverted commas.
The \n is called an escape sequence. In this case \n represents a newline character. After printing something to the screen you usually want to print something on the next line. If there is no \n then a next printf command will print the string on the same line.
Commonly used escape sequences are:
- \n (newline)
- \t (tab)
- \v (vertical tab)
- \f (new page)
- \b (backspace)
- \r (carriage return)
After the last round bracket there must be a semi-colon. The semi-colon shows that it is the end of the command. (So in the future, don’t forget to put a semi-colon if a command ended).
return 0;
When we wrote the first line “int main()”, we declared that main must return an integer int main(). (int is short for integer which is another word for number). With the command return 0; we can return the value null to the operating system. When you return with a zero you tell the operating system that there were no errors while running the program. (Take a look at the C tutorial – variables and constants for more information on integer variables).
Compile
If you have typed everything correct there should be no problem to compile your program. After compilation you now should have a hello.exe (Windows) or hello binary (UNIX/Linux). You can now run this program by typing hello.exe (Windows) or ./hello (UNIX/Linux).
If everything was done correct, you should now see the words “Hello World” printed on the screen.
Congratulations!!!!!
You have just made your first program in C.
You have just made your first program in C.
Comments in your program
The Hello World program is a small program that is easy to understand. But a program can contain thousands of lines of code and can be so complex that it is hard for us to understand. To make our lives easier it is possible to write an explanation or comment in a program. This makes it much easier to understand the code. (Even if you did not look at the code for years).These comments will be ignored by the compiler at compilation time.
Comments have to be put after // or be placed between /* */ .
Here is an example of how to comment the Hello World source code :
Is it fine to write “void main()” or “main()” in C/C++?
The definition
#include<stdio.h>int main() { printf(“Hello World\n”); return 0; }









No comments:
Post a Comment