Basic Syntax of C

Syntax of C programming language is a set of rules, principles, and processes that govern the structure. C was the first widely successful high-level language to develop portable operating systems.

C program has the following components (usually found in this order):

  • Preprocessor Commands
  • Type Definitions
  • Function Prototypes
  • Variables Functions
Example of a C Program:
#include<stdio.h>
int main(void)// Every C program must have a main function.
{
int a=1, b=2;
int sum=0;
sum=a+b;
printf("Sum of a & b=%d",sum);
return 0;
}

#include

In C Programming Langauge #include is a preprocessor command. The Symbol # (hash) in C is processed by the preprocessor.

Function of stdio.h in C Programming Language

stdio.h .h file in the above program imports some external libraries/functions which required in our program .file contains functions such as scanf() and print().

Role of the main function in C 

The main function indicates the compiler to tell from where to start the program execution.

Functionality of printf

printf() is a library function to print formatted output to the screen.

Use of the return statement

return 0 the statement is the “Exit status” of the program.

;(semi-colon) 

; semicolon is the terminator it is placed after every single line of code.

Comments in C 

Single line comment

Example:

/*This is an example of single line comment*/

Comments are not read by the compiler it is used by the compiler to keep the track of the flow of the program written by him/her.

Multiline Comment :

Example

//This is an example of a multi-line comment.

Variable Declaration

Variables are usually declared at the top of files and
functions.

#include <stdio.h>
int myglobalinteger;
int main() {
int mylocalinteger;
// do something
}

Constants

  • In C, a variable can be declared as constant.
  • The value of a constant is initialized when the variable is
    declared. That value cannot be changed.
  • An optimizing compiler can use the constant declaration
    to simplify and optimize the code.
    int const a = 1;
    const int a = 2;

Arithmetic Operations

  • C provides the basic arithmetic operations : + – * /
  • For efficiency purposes, it also provides an increment and decrement operator: ++ and —
  • The modulus (%) operator is also provided. Note that / operation for float and integer is very different. Unless both operands are float, the division will be integer-based.
float a, b;
a = 3.0 / 2; // a = 1.0
b = 3.0 / 2.0; // b = 1.5

Comparison Operators

C provides the following comparison operators:

== : equality
!= : not equal
< : smaller than
> : greater than
<= : smaller or equal than
>= : greater or equal than

Logical Operators

C provides the following logical operator:

&& : AND
|| : OR
! : NOT

These can be used with the comparison operators:

if ( (a == 5) || (a == 6) ) // a = 5 or 6
if ( (a == 3) && (b == 4) ) // a = 3 and b = 4
if ( !(a == 5) ) // a is not 5
if ( a != 5 ) // a is not 5

If statement

If statements in C are identical to if statements in Java.

if (expression) {
statement;
} else if (expression) {
statement;
} else {
statement;
}

? operator

The ? operator is a designed to replace small if
statements. Its syntax is as follows:

(expression) ? (statement if true) : (statement if false)

The following example calculates the absolute value of an integer.

int a, b;
a = some random int value;
b= (a > 0) ? a : -a;

Switch statement

A switch statement allows testing of a variable under multiple conditions:

switch(variable) {
case constant1:
statements;
break;
case constant2:
case constant3:
statements;
break;
default:
statements;
}

Leave a Reply

Your email address will not be published. Required fields are marked *

Shopping cart