Enter Ur Email ID To Get Study Material & Latest Job Updates In Mail Inbox:
(Please activate subscription from your mail Inbox)  FeedBurner

Home » » STRUCTURE OF C WITH EXAMPLES & PROGRAM

STRUCTURE OF C WITH EXAMPLES & PROGRAM

Written By Unknown on Saturday, 20 April 2013 | 8:54 pm

Explain Structure Of C Language With Program And Examples



Structure of C Language

The first thing we must do is open a text editor. Windows users can use Notepad and UNIX/Linux users can use emacs or vi. Now type the following lines of code and then I will explain it. Make sure that you type it exactly as I have or else you will have problems. Also don't be scared if you think it is too complicated because it is all very easy once you understand it.
Code:

#include<stdio.h>
int main()
{
printf("Hello World\n");
return 0;
}


#include<stdio.h>
This includes a file called stdio.h which lets us use certain commands. stdio is short for Standard Input/Output which means it has commands for input like reading from the keyboard and output like printing things on the screen.

int main()
int is what is called the return value which will be explained in a while. main is the name of the point where the program starts and the brackets are there for a reason that you will learn in the future but they have to be there.

{}
The 2 curly brackets are used to group all the commands together so it is known that the commands belong to main. These curly brackets are used very often in C to group things together.

printf("Hello World\n");
This is the printf command and it prints text on the screen. The data that is to be printed is put inside brackets. You will also notice that the words are inside inverted commas because they are what is called a string. Each letter is called a character and a series of characters that is grouped together is called a string. Strings must always be put between inverted commas. The \n is called an escape sequence and represents a newline character and is used because when you press ENTER it doesn't insert a new line character but instead takes you onto the next line in the text editor. You have to put a semi-colon after every command to show that it is the end of the command.

Table of commonly used escape sequences:
\a Audible signal
\b Backspace
\t Tab
\n Newline
\v Vertical tab
\f New page\Clear screen
\r Carriage return

return 0;
The int in int main() is short for integer which is another word for number. We need to use the return command to return the value 0 to the operating system to tell it that there were no errors while the program was running. Notice that it is a command so it also has to have a semi-colon after it.
Save the text file as hello.c and if you are using Notepad make sure you select All Files from the save dialog or else you won't be able to compile your program.
You now need to open a command prompt. If you are using Windows then click Start->Run and type "command" and Click Ok. If you are using UNIX/Linux and are not using a graphical user interface then you will have to exit the text editor.
Using the command prompt, change to the directory that you saved your file in. Windows users must then type:

C:\>bcc32 hello.c
UNIX/Linux users must type:

$cc hello.c -ohello

The -o is for the output file name. If you leave out the -o then the file name a.out is used.
This will compile your C program. If you made any mistakes then it will tell you which line you made it on and you will have to type out the code again and this time make sure you do it exactly as I did it. If you did everything right then you will not see any error messages and your program will have been compiled into an exe. You can now run this program by typing "hello.exe" for Windows and "./hello" for UNIX/Linux.
You should now see the words "Hello World" printed on the screen. Congratulations! You have just made your first program in C.
You will see that the printf and return commands have been indented or moved away from the left side. This is used to make the code more readable. It seems like a stupid thing to do because it just wastes time but when you start writing longer, more complex programs, you will understand why indentation is needed.
Comments are a way of explaining what a program does. They are put after // or between /* */. Comments are ignored by the compiler and are used by you and other people to understand your code. You should always put a comment at the top of a program that tells you what the program does because one day if you come back and look at a program you might not be able to understand what it does but the comment will tell you. You can also use comments in between your code to explain a piece of code that is very complex. Here is an example of how to comment the Hello World program:
Code:


Writes the words "Hello World" on the screen */

#include<stdio.h>
int main()
{

printf("Hello World"); //prints "Hello World"
return 0; // or you can escape this
}

0 comments:

Post a Comment

Thanks For Your Comment. We Get Back To You As Soon As Possible.