Saturday 3 December 2016

C Language Syntax Rules

C Language Syntax Rules

C language syntax specify rules for sequence of characters to be written in C language. 
The rule specify how character sequence will be grouped together to form tokens. 
A smallest individual unit in c program is known as C Tokens. Tokens are either keyword, identifier, constant, variable or any symbol 
which has some meaning in C language. A C program can also be called as collection of various tokens.

Example of C tokens,


1 int
2 Semicolon (;)





Comments


Comments are simple text in your C program that increases readability of programs. Compiler ignore anything written as comment in your program.

Example of comments :

Single line Comment
//This is a comment

Single line Comment
/*This is a comment*/

Multi line Comment
/*This is a long 
and valid comment*/

Wrong Syntax
//this is not
  a valid comment


Some basic syntax rule for C program


  1. C is a case sensitive language so all C instructions must be written in lower case letter.
  2. All C statement must be end with a semicolon.
  3. Whitespace is used in C to describe blanks and tabs.
  4. Whitespace is required between keywords and identifiers

C Input output function

C programming language provides many of the built-in functions to read given input and write data on screen, printer or in any file.


1 scanf() and printf() functions


#include<stdio.h>
#include<conio.h>
void main()
{
 int i;
 printf("Enter any value");
 scanf("%d",&i);
 printf( "\nYou entered:= %d",i);
 getch();
}

When you will compile the above code, it will ask you to enter a value. When you will enter the value  , it will display the value you have entered.

2 getchar() & putchar() functions


#include <stdio.h>
#include <conio.h>
void main( )
{
 int m;
 printf("Enter a character");
 m=getchar();
 putchar(m);
 getch();
}

The getchar() function reads a character from the terminal and returns it as an integer. This function reads only single character at a time. You can use this method in the loop in case you want to read more than one characters. The putchar() function prints the character passed to it on the screen and returns the same character. This function puts only single character at a time. In case you want to display more than one characters, use putchar() method in the loop.
When you will compile the above code,it will ask you to enter a value. When you will enter the value, it will display the value you have entered.

3 gets() & puts() functions


#include<stdio.h>
#include<conio.h>
void main()
{
 char s[100];
 printf("Enter any string");
 gets(s);
 puts(s);
 getch();
}

The gets() function reads a line from stdin into the buffer pointed to by s until either a terminating newline or EOF (end of file). The puts() function writes the string s and a trailing newline to stdout.
When you will compile the above code,it will ask you to enter a string. When you will enter the string, it will display the value you have entered.