Basic Syntax of C programming language

A C program basically consists of the following parts :

  • Preprocessor Commands

  • Functions

  • Variables

  • Statements & Expressions

  • Comments


Let us look at a simple code that would print the words "Hello World":

#include<stdio.h>
int main ()
{
     printf("Hello World");
     /* Comment in C */
     return 0;
}

Let us look various parts of the above program -

1. The first line of the program #include<stdio.h> is a preprocessor    command, which tells a C compiler to include stdio.h file before going to     actual compilation.

2. The next line int main() is the main function where program execution begins.

3. The next line printf(...) is another function available in C which causes the message "Hello, World" to be displayed on the screen. In C program, the semicolon is a statement terminator.

4. The next line /*.....*/ will be ignored by the compiler and it has been put to add additional comments in the program. So such lines are called comments in the program.

5. The next line return 0; terminates main() function and returns the value 0.


Compile & Execute C Program :

1. Open a text editor and add the above mentioned code.

2. Save the file as helloworld.c

3. Open a command prompt ( Terminal for Linux OS ) and go to the directory, where you saved the file.

4. If there are no errors in your code, the command prompt will take you to the next line and would generate a.out executable file.

5. Now, type a.out to execute your program. ( For Linux OS - execute as ./a.out )

6. You will be able to see "Hello World" printed on the screen ( Terminal ).


Identifiers : A C identifier is a name used to identify a variable, function, or any other user-defined item. An identifier starts with a letter A to Z, a to z, or an underscore ‘_’ followed by zero or more letters, underscores, and digits (0 to 9). Example -

ABC            nroy            name            _a

C does not allow punctuation characters such as @, $, and % within identifiers. C is a case-sensitive programming language. Thus, mywordand Mywordare two different identifiers in C.

Keywords : The following list shows the reserved words in C. These reserved words may not be used as constants or variables or any other identifier names.


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

0 Comments