A C program basically has the following form:
- Preprocessor Commands
- Functions
- Variables
- Statements & Expressions
- Comments
#include <stdio.h> int main() { /* My first program */ printf("Hello, World! \n"); return 0; } |
Functions: are main building blocks of any C Program. Every C Programwill have one or more functions and there is one mandatory function which is called main() function. This function is prefixed with keyword int which means this function returns an integer value when it exits. This integer value is retured using return statement.
The C Programming language provides a set of built-in functions. In the above example printf() is a C built-in function which is used to print anything on the screen. Check Builtin function section for more detail.
You will learn how to write your own functions and use them in Using Function session.
Variables: are used to hold numbers, strings and complex data for manipulation. You will learn in detail about variables in C Variable Types.
Statements & Expressions : Expressions combine variables and constants to create new values. Statements are expressions, assignments, function calls, or control flow statements which make up C programs.
Comments: are used to give additional useful information inside a C Program. All the comments will be put inside /*...*/ as given in the example above. A comment can span through multiple lines.
Note the followings
- C is a case sensitive programming language. It means in C printf andPrintf will have different meanings.
- C has a free-form line structure. End of each C statement must be marked with a semicolon.
- Multiple statements can be one the same line.
- White Spaces (ie tab space and space bar ) are ignored.
- Statements can continue over multiple lines.
C Program Compilation
To compile a C program you would have to Compiler name and program files name. Assuming your compiler's name is cc and program file name is hello.c, give following command at Unix prompt.$cc hello.c |
$cc -o hello hello.c |
$chmod 755 hello $./hello This will produce following result Hello, World
No comments:
Post a Comment