Saturday 23 December 2017

Basic Syntax of C Program - Geeks4Coding

Basic Syntax of C Program

A C program basically consists of the following parts −
  • Preprocessor Commands
  • Functions
  • Variables
  • Keywords
  • Statements & Expressions
  • Comments
Let us take a look at the various parts of the above program

#include <stdio.h>

int main() {
/* multline comment */
printf("Hello, World! \n");//single line comment

return 0;
}
 

Preprocessor Commands

#include is a preprocessor command, which tells a C compiler to include stdio.h file from standard library and there won’t be any line with # symbol in object file after compilation, it will get replace by content of stdio.h header file. Learn more about Preprocessor directives. 
 

Functions

Main() is the function which tell compiler where to start and there should be only one main() function and printf(),scanf() function are used to print on screen and take input from user.  

Learn more about Functions

Variables

A variable in simple terms is a storage place which has some memory allocated to it. So basically a variable used to store some form of data. Different types of variables require different amounts of memory and have some specific set of operations which can be applied on them.

Keywords

Keywords are specific reserved words in C each of which has a specific feature associated with it. Almost all of the words which help us use the functionality of the C language are included in the list of keywords. So you can imagine that the list of keywords is not going to be a small one!
 

There are a total of 32 keywords in C:  
auto             break         case            char            const            continue
default         do              double          else            enum            extern
float             for             goto              if               int                  long
register       return        short             signed        sizeof            static
struct           switch       typedef        union         unsigned        void
volatile       while
 
Learn More about Keywords and Variables 
 

Expressions and Statements

An expression represents a single data item--usually a number. The expression may consist of a single entity, such as a constant or variable, or it may consist of some combination of such entities, interconnected by one or more operators. Expressions can also represent logical conditions which are either true or false. However, in C, the conditions true and false are represented by the integer values 1 and 0, respectively

A statement causes the computer to carry out some definite action. There are three different classes of statements in C: expression statements, compound statements, and control statements.
An expression statement consists of an expression followed by a semicolon. The execution of such a statement causes the associated expression to be evaluated. For example:
 

a = 6;
c = a + b;
++j;

 

The first two expression statements both cause the value of the expression on the right of the equal sign to be assigned to the variable on the left. The third expression statement causes the value of j to be incremented by 1. Again, there is no restriction on the length of an expression statement: such a statement can even be split over many lines, so long as its end is signaled by a semicolon.
 

A compound statement consists of several individual statements enclosed within a pair of braces { }. The individual statements may themselves be expression statements, compound statements, or control statements. Unlike expression statements, compound statements do not end with semicolons. A typical compound statement is shown below:
 

{
pi = 3.141593;
circumference = 2. * pi * radius;
area = pi * radius * radius;
}

 

This particular compound statement consists of three expression statements, but acts like a single entity in the program in which it appears.

Comments

Comments are non-executable code used to provide documentation to programmer. Single Line Comment is used to comment out just Single Line in the Code. It is used to provide One Liner Description of line.

No comments:

Post a Comment

Thank you for your Time. Keep Learning

Geeks4Coding

Contributors

Popular Posts