PDF

Introduction C Programming

Leave a Comment
Every full C program begins inside a function call "main". A function is simply a collection of commands that do "something". The main function is always called when the program first executes. From main, we can call other functions, whether they be written by us or by others or built-in language features. To access the standard functions that comes with your compiler, you need to include a header with #include directive. What this dose is effectively take everything in the header and paste it into your program.

Let's look at a working program:

#include<stdio.h>
int main()       /* this is a comment tag */
{
printf("Hello World ! I am Abhi");
return 0;       // it is a comment
}

Let's look at the elements of the program. The #include is a "preprocessor" directive thta tells the compiler to put code from the header called stdio.h into our program before actually creating the executable . By including header files, you can gain access to many different functions - both the printf and getchar functions are included in stdio.h

The next important line is int main(). This line tells the compiler that there is a function named main, and that the function returns as integer, hence int. The "curly braces" { and } signal the beginning and end of functions and other code blocks.

The printf function is the standard C way of displaying output on the screen. The quotes tell the compiler that you want to output the literal string as it is (almost).

Finally, at the end of the program, we return a value from main to operating system by using the return statement . This return value is important as it can be used to tell the operating system whether our program succeeded (without any error or bug) or not. A return value of '0' means success.

The final brace closes off the function. You should try compiling this program and running it. You can cut paste the code(but I personally prefer to type it ) into a notepad file, save it as a .c file extension and compile it

Comments In C Program

Everything that is inside /* and */ is considered as a comment in c program and will be ignored by the compiler. You must not include comments within other comments.e.g.

/* this is a /* comment */ inside a comment, which is completely wrong!! */

Your compiler may allow this, but it's not standard C programming.

You may also encounter comments after //. These are C++ comments and while they will be allowed in the next revision of C standard, you shouldn't use them yet. They are more limited that the flexible /* */ comments anyway.

0 comments:

Post a Comment