C Programming Tutorials – Basic structure, syntax

c-programming-tutorial

This is the second post in the series of C programming tutorials. In the first post we gave a brief summary on the basics of C programming and how to set the environment. In this post we will take you through the basic C program structure. We will also explain the basic syntax, different data types, variables  and constants and the syntax for declarations.

So to start with this post we will start form the basic programming structure and so on.


#include <stdio.h> /* Preprocessor command */

int main() {       /* Main function */
   /* Program in C which prints Hello World to the console */
   printf("Hello, World! \n");
   
   return 0;       /* Return statement */
}
                                 (Prog. 1)

Prog. 1 shows the basic program in C which is use to print Hello World on the console. We will use this program to explain the structure of program in C programming.

C program basically consists of the following structure blocks

  • Pre-processor Commands
  • Functions
  • Variables
  • Statements & Expressions
  • Comments

Now with the help of the above program we will explain each block.

  • #include <stdio.h> is a pre-processor command which is used by the compiler to include the file stdio.h before actually going into compilation.
  • int main() is the main function in the C program. C program works w.r.t. the functions and this main function is the start point of the actual C Program.
  • /*….*/ are the comments in the program which are ignored by the compiler while compiling the program. /*….*/ is use for a multi line comments.
  • printf(“…..”) function is used for printing a message on the screen.
  • return 0 final statement in the main function which is used to return a value 0

Once the basic program is written, we will learn how to save this file and how to compile and execute this program. The following steps are used to save and compile a C program.

  • Open a text editor and add the above-mentioned code.
  • Save the file as hello.c
  • Open a command prompt and go to the directory where you have saved the file.
  • Type gcc hello.c and press enter to compile your code.
  • 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.
  • Now, type a.out to execute your program. Refer to the bellow commands.
  • You will see the output “Hello World” printed on the screen.


$ gcc hello.c
$ ./a.out
Hello, World!

So this is how a C program is written, saved and compiled in a command prompt. We can also use Turbo C++ to write and compile this C program. Turbo C++ is a IDE which is used to write C and C++ programs. You can refer to our post on how to install Turbo C++ for more information.

After the basic building structure of C now is the time to learn basic syntax and terminology use in C.

Tokens In C:

Program consists of various tokens which can either be a keyword, an identifier, a constant, a string literal, or a symbol.
For example, the following C statement consists of following tokens −


printf("Hello, World! \n");

  1. printf
  2. (
  3. Hello, World! \n”
  4. )
  5. ;

Semicolons:

In C program ;(semicolons) is used as a statement terminator. I.e. ; is use to terminate a statement. That is, each individual statement must be ended with a semicolon. It states the end of one logical statement.

Comments:

Comments are use to write a message in a C program and this comments  are ignored by the compiler. They start with /* and terminate with the characters */  as shown in the prog. 1.

Identifier:

Identifier in a C program  is a name used to identify a variable, function, or any other user-defined item. An identifier can start with a letter A to Z, a to z, or an underscore ‘_’ followed by zero or more letters, underscores, and digits (0 to 9).  Some of the punctuation characters such as @, $, and % are not allowed in the identifier in C. C is a case-sensitive programming language. Thus, Total and total are 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

 

White Spaces:

A line containing only whitespace, possibly with a comment, is known as a blank line, and a C compiler totally ignores it.

Whitespace is the term used in C to describe blanks, tabs, newline characters and comments. Whitespace separates one part of a statement from another and enables the compiler to identify where one element in a statement, such as int, ends and the next element begins.


mm

About Shrikrishna Parab

Shrikrishna Parab is Co-founder at TechNirmiti Solutions. He is a researcher in the field of Computer Science and Technology. He is 25 and lives in Goa. You can Follow him on Twitter, Facebook and Google +.

Leave a Reply

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