Open In App

C Basic Syntax

Improve
Improve
Like Article
Like
Save
Share
Report

C is a procedural programming language. It was developed by Dennis Ritchie at the Bell Laboratories in the year 1972. Despite being old C is a very popular language among programmers. It is a very fast language compared to other languages like Python, Java, etc.

Below is the basic syntax structure of the C program:

structure of c program

The basic syntax of the C program consists of the header, main() function, variable declaration, body, and return type of the program.

  • The header is the first line in the C program with extension .h which contains macro definitions and C functions. 
  • Programs must contain the main() function because execution in C programming starts from the main().
  • Variable declaration in C is done inside the main function and can be used in the body anywhere but before the main, we can also declare variables which are known as Global variables. 
  • In the body of the function, we perform operations required inside the function like printing, sum, average, sorting, searching, etc.
  • The last part of the C program is the return statement which refers to returning values of the program. If the return type is void then there will be no return statement.

C




// Basic Syntax of C Program
#include <stdio.h>
 
// main function
int main()
{
 
    // body
    printf("Hi! This is a basic C program.");
 
    // return statement
    return 0;
}


Output

Hi! This is a basic C program.

Tokens

A token in C programming is either a keyword, an identifier, a constant, a string literal, or a symbol. Let’s get a good understanding of tokens through a print statement in C language.

printf("GeeksforGeeks\n");

Individual tokens from the above syntax are:

printf
(
"GeeksforGeeks\n"
)
;

Semicolons

In C programming Semicolon is used to show the termination of Instruction. It is also called a statement terminator since every single statement should be ended with a semicolon. Semicolons are used to end statements in C.

The Semicolon tells the compiler that the current statement has been terminated. If any statement in the Program ends without a semicolon, then the program will not compile and will generate an error message.

C




// C program to demonstrate use of Semicolon
#include <stdio.h>
 
int main() { printf("GeeksforGeeks") return 0; }


Error

./253df686-61b0-495f-98fe-46dfeb318172.c: In function 'main':
./253df686-61b0-495f-98fe-46dfeb318172.c:7:5: error: expected ';' before 'return'
     return 0;
     ^

Preprocessor Directives

In C language, a program should begin with preprocessor directives since they contain multiple files that contain specific functions. Preprocessors in C are used to process our source code before compilation.

There are 4 main types of Preprocessor directives in C:

  1. Macros
  2. File inclusion
  3. Conditional Compilation
  4. Other directives

While executing a program in C multiple steps are involved as mentioned below:

Preprocessor Directives in C

 

Identifiers in C

In C programming, the identifier is used to identify a variable, function, or any other user-defined data type. C programming language does not allow special characters such as $, @, or % within the identifier.

C is a case-sensitive programming language which means that “geeksforgeeks” and “Geeksforgeeks” are treated as two different identifiers in C. Identifiers should begin with a letter Uppercase letters (A to Z), lowercase letters (a-z), digits (0-9) or underscores ( _ ).

Some examples of identifiers in C are:

geeks Geek geek12 _geek
g_f_g G_f_2 _geek89 geeksforgeeks
GEEKSFORGEEKS GEEKS_for_9081 gfg_69 g23gf9

C Keywords

In the C programming language, keywords are reserved words that have special meanings. These reserved words can’t be used as variables or constants or any other identifier name. C contains a total of 32 keywords that are reserved and have special meanings.

Keywords in C programming are mentioned below:

int long short signed
unsigned void char break
register structure class volatile
while for switch typedef
union static auto return
case const continue default
do double else if
enum extern float goto

Comments in C

In C programming Comments are used to make any program more readable and understandable. Comments are generally used by programmers to add explanations or descriptive text in the code without any interference from the compiler or the programming structure.

Comments are not programming statements and they are ignored by the compiler. We can’t have comments inside comments since they will interfere within themselves.

In C language there are two types of programming comments:

  • Single-line Comments: Single-line comments begin with // and they are only used for a single line and end in the next line.
  • Multiline Comments: Multi-line comments start with /* and end with */. They can be a single line or multiline.

Example

C




// C Program to demonstrate the comments
#include <stdio.h>
 
int main()
{
 
    // This is a single line comment
 
    /*
        This is a multiline comment
        used in this program
    */
 
    printf("GeeksforGeeks");
 
    return 0;
}


Output

GeeksforGeeks

In the above C program, comments are ignored by the compiler and they can be read only by the programmer.

Whitespaces in C

In C programming lines containing white spaces, blank lines, and comments are ignored by the compiler. Whitespace in C is used to describe blanks, newline characters, comments, and tabs.

Whitespace is used to separate parts of a statement from another and it helps the compiler to distinguish the keywords, identifiers, and elements in a statement. It allows us to format our code in a way that makes it easier to understand by programmers and others. In C we are free to use the whitespaces to increase user readability.

Example of Whitespace in C:

int a;
// whitespace used to increase readablity and to distinguish elements.
string s = "GeeksforGeeks";

Functions

In C programming Function is a set of statements that performs specific computations. Functions help us to reduce our code redundancy. Instead of writing multiple lines of code again and again we use functions to reduce code redundancy.

Functions in C provide abstractions. In C “main” is also a function that has its own return type defined in the program. This function serves as the starting point for program execution.

Syntax

return_type function_name(parameter_list) {
    // Function body (code goes here)

}

Example

int mul(int Full_marks,int Full_marks2);

Below is the representation of the above C function:

function syntax in C

 



Last Updated : 16 Jun, 2023
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads