Skip to main content

Structure of C Programs

Objectives:

Having completed this section you should know about:

1.C's character set
2.C's keywords
3.the general structure of a C program
4.that all C statement must end in a ;
5.that C is a free format language
6.all C programs us header files that contain standard library functions.


C's Character Set:

C does not use, nor requires the use of, every character found on a modern computer keyboard. The only characters required by the C Programming Language are as follows:

A - Z
a -z
0 - 9
space . , : ; ' $ "
# % & ! _ {} [] () < > |
+ - / * =

The use of most of this set of characters will be dicussed throughout the course.


The form of a C Program:

All C programs will consist of at least one function, but it is usual (when your experience grows) to write a C program that comprises several functions. The only function that has to be
present is the function called main. For more advanced programs the main function will act as a controling function calling other functions in their turn to do the dirty work! The main

function is the first function that is called when your program executes.

C makes use of only 32 keywords which combine with the formal syntax to the form the C programming language. Note that all keywords are written in lower case - C, like UNIX, uses
upper and lowercase text to mean different things. If you are not sure what to use then always use lowercase text in writing your C programs. A keyword may not be used for any other
purposes. For example, you cannot have a variable called auto.

The layout of C Programs:

The general form of a C program is as follows (don't worry about what everything means at the moment - things will be explained later):

preprocessor directives
global declarations
main()
{
local variables to function main ;
statements associated with function main ;
}
f1()
{
local variables to function 1 ;
statements associated with function 1 ;
}
f2()
{
local variables to function f2 ;
statements associated with function 2 ;

}
.
.
.
etc

Note the use of the bracket set () and {}. () are used in conjunction with function names whereas {} are used as to delimit the C statements that are associated with that function. Also note
the semicolon - yes it is there, but you might have missed it! a semicolon (;) is used to terminate C statements. C is a free format language and long statements can be continued, without
truncation, onto the next line. The semicolon informs the C compiler that the end of the statement has been reached. Free format also means that you can add as many spaces as you like to

improve the look of your programs.

A very common mistake made by everyone, who is new to the C programming language, is to miss off the semicolon. The C compiler will concatinate the various lines of the program
together and then tries to understand them - which it will not be able to do. The error message produced by the compiler will relate to a line of you program which could be some distance
from the initial mistake.


Preprocessor Directives:

C is a small language but provides the programmer with all the tools to be able to write powerful programs. Some people don't like C because it is too primitive! Look again at the set of

keywords that comprises the C language and see if you can find a command that allows you to print to the computer's screen the result of, say, a simple calculation. Don't look too hard
because it dosen't exist.

It would be very tedious, for all of us, if everytime we wanted to communicate with the computer we all had to write our own output functions. Fortunately, we do not have to. C uses
libraries of standard functions which are included when we build our programs. For the novice C programmer one of the many questions always asked is does a function already exist for

what I want to do? Only experience will help here but we do include a function listing as part of this course.

All programs you will write will need to communicate to the outside world - I don't think I can think of a program that doesn't need to tell someone an answer. So all our C programs will
need at least one of C's standard libraries which deals with standard inputting and outputting of data. This library is called stdin.h and it is declared in our programs before the main

function. The .h extension indicates that this is a header file.

I have already mentioned that C is a free format language and that you can layout your programs how you want to using as much white space as you like. The only exception are statements
associated with the preprocessor.

All preprocessor directives begin with a # and the must start in the first column. The commonest directive to all C programs is:

#include

Note the use of the angle brackets (< and >) around the header's name. These indicate that the header file is to be looked for on the system disk which stores the rest of the C program

application. Some text books will show the above statement as follows:

#include "stdio.h"

The double quotes indicate that the current working directory should be searched for the required header file. This will be true when you write your own header files but the standard header
files should always have the angle brackets around them.

NOTE: just to keep you on your toes - preprocessor statements, such as include, DO NOT use semi-colons as delimiters! But don't forget the # must be in the first column.

Thats enough background to C programs - lets get on with our first program which will start to bring together some of the ideas outlined above.

Comments

Popular posts from this blog

12 Tips to Maintain a Virus Free Computer

1. Email is one of the common ways by which your computer can catch a virus . So it is always recommended to stay away from SPAM. Open only those emails that has it’s origin from a trusted source such as those which comes from your contact list. If you are using your own private email host (other than gmail, yahoo, hotmail etc.) " then it is highly recommended that you use a good anti-spam software. And finally NEVER click on any links in the emails that comes from untrusted sources. 2. USB thumb/pen drives is another common way by which viruses spread rapidly." So it is always a good habit to perform a virus scan before copying any data onto your computer. NEVER double-click the pen drive to open it. Instead right-click on it and select the option “open”. This is a safe way to open a pen drive. 3. Be careful about using MS Outlook. Outlook is more susceptible to worms than other e-mail programs, unless you have efficient Anti-Virus programs running. Use Pegasus ...

what is LOREM ipsum and why do designers use it

What is Lorem Ipsum? Lorem Ipsum  is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum. Why do we use it? It is a long established fact that a reader will be distracted by the readable content of a page when looking at its layout. The point of using Lorem Ipsum is that it has a more-or-less normal distribution of letters, as opposed to using 'Content here, content here', making it look like readable English. Many desktop publishing packages and web page editors now...

DOWNLOAD CODE BLOCKS 16.01 MINGW.SETUP .EXE 86.3 MB

Code::Blocks for Mac is a free C, C++ and Fortran IDE that has a custom build system and optional Make support. The application has been designed to be very extensible and fully configurable. Code::Blocks is an IDE packed full of all the features you will need. It has a consistent look, feel and operation across its supported platforms. It has been built around a plugin framework, therefore Code::Blocks can be extended with plugins. Support for any kind of functionality can be added by installing/coding a plugin. Key features include: Written in C++. No interpreted languages or proprietary libs needed.. Full plugin support. Multiple compiler support: GCC (MingW / GNU GCC), MSVC++, clang, Digital Mars, Borland C++ 5.5, and Open Watcom etc. Support for parallel builds. Imports Dev-C++ projects. Debugger with full breakpoints support. Cross-platform. Code::Blocks' interface is both customizable and extensible with Syntax highlighting, a tabbed interface, Class Br...

5 Best Popular Posts Widgets For Blogger

Adding the Popular Posts Widget for Blogger Just click on your blog title, access the "Layout" menu, click "Add a Gadget" and choose "Popular Posts". A window will appear asking you to configure the widget by choosing which posts you'll feature (e.g. those that were most viewed in the past 7 days or 30 days or from the beginning of your blog). You'll also be asked to choose how many posts you'll feature in your Popular Posts section and select if you'll show the post title only or along with the image thumbnail and/or the snippet. (Remember that each widget style has different requirements, so follow the styles and instructions carefully to find out if you need the snippet and image thumbnail or not). Popular Posts Style 1 - Box within a box This is an interesting widget style since it uses your snippet and image thumbnail in a unique way. Your snippet is written in opaque text and placed in a small transparent box. This, in turn, ...

How to Put Google Adsense Below Post Title in Blogger?

Adsense is used by majority of expert bloggers for their website monetization because it is a cookie based contextual advertising system that shows targeted ads relevant to the content and reader. As bloggers are paid on per click basis, they try various ad placements on the blog to  increase the revenue  and get maximum clicks on the ad units. Well, on some blogs, you might have seen Adsense ad units placed below the post title. Do you know why? It is because the area just below the post title gets the most exposure and is the best place to put AdSense ad units to increase  Click Through Rate (CTR). Even though ads below post title work like a charm but this doesn’t mean that it will work for you as well. If you want to find out the best AdSense ads placement for your blog, try experimenting by placing ads at various locations such as header, sidebar, footer, etc. You can try other  blog monetization methods  as well to effectively monetize y...

USE any TRIAL SOFTWARE FOREVER WITHOUT SERIAL NUMBER

USE any TRIAL SOFTWARE FOREVER WITHOUT SERIAL NUMBER(most wanted trick) Run a trial software forever now with time stopper you can run a trial software forever no need to fetch for serial numbers,activation codes,patch just DOWNLOAD TIME STOPPER now open it install it click browse select the .exe of the software or file which you want to run forever now simply click create desktop icon and now delete all its existing shortcuts now have fun enjoying software for life time

How to Detect Anonymous IP Addresses

Proxy Detection Serviceshttp = So in order to stop such online frauds, Proxy Detection has become a critical component. Today most companies, credit card merchants and websites that deal with e-commerce transactions make use of Proxy Detection Services like MaxMind and FraudLabs to detect the usage of proxy or spoofed IP from users participating online. Proxy Detection web services allow instant detection of anonymous IP addresses. Even though the use of proxy address by users is not a direct indication of fraudulent behaviour, it can often indicate the intention of the user to hide his or her real IP. In fact, some of the most used ISPs like AOL and MSN are forms of proxies and are used by both good and bad consumers. How Proxy Detection Works? Proxy detection services often rely on IP addresses to determine whether or not the IP is a proxy. Merchants can obtain the IP address of the users from the HTTP header on the order that comes into their website . This IP addr...

CBSE PSA Exam Result 2013 at cbseresults.nic.in

CBSE PSA Results 2013: CBSE PSA Results 2013: CBSE Problem Solving Assessment (CBSE-PSA) for the students of class IX and class XI in CBSE board. Board operate this test to test the general reasoning and aptitude quality of a student. Through this test a student get know about his/her preparation for competitive exams, that is it sufficient or he/she have to prepare hard in advance for his/her future dreams. students can check CBSE PSA Result 2013 through below given link. Although for this test student doesn't require to prepare hard but it is a compulsory test for all, the students of class 9th and 12th. CBSE PSA RESULT 2013 : PSA IX & XI Class Result 2013 As CBSE Problem Solving Assessment test was conducted on 16th February 2013, it is expected that result for PSA will be declare soon. It may declare on first week of March month.Student can check the official site for any latest notification regarding PSA test as http://cbse.nic.in/ . And to get latest result of C...

C++ Program to implement File Handling by using ifstream & ofstream -2

Write a paragraph (At least 2 lines of sentences) in a file. Read the same file in three different ways –   i) Reverse the whole paragraph.  ii) Line by Line Reverses the whole para. iii) Break the paragraph into two equal halves. Reverse the second half of the paragraph. #include #include <fstream.h> #include <conio.h> void main(){     char para[500], ln[80];     int len, i, half;     clrscr();     cout<<"\n Enter a Paragraph (Quit by ^Z):-";     cout<<"\n ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~";     cin.getline(para, 500, '^Z');     len = strlen(para);     ofstream fout;     fout.open("para.txt", ios::out);     fout.write((char *) &para, len);     fout.close();     ifstream fin;     cout<<"\n Reverse the Whole Paragraph:-";     cout<<"\n ~~~~~~~~~~~~~~~~~~~~~~~~~~~";     fin.open...

C++ Program to find out the Prime number's By sieve Method

NOTE : YOU CAN DOWNLOAD EXE FILE HERE: SIEVE.EXE SORUCE CODE (RTF): source.cpp click here   1  #include <iostream>  2  #include <conio.h>  3  using namespace std ;  4  int main ()  5   6  {  7   8  int number , n = 2 ;  9  cout << "Program to find out Prime number by sieve method\n\n\a" ; 10  11  cout << "Enter upto which number you want to find out the prime number\n" ; 12  cin >> number ; 13  int arr [ number ]; 14  for ( int i = 0 ; i < number ; i ++) 15  arr [ i ]= i + 1 ; 16  cout << "  Original number:\n" ; 17  18  for ( int i = 0 ; i < number ; i ++) 19  { 20  21  22      cout << arr [ i ]<< "\t\a" ; 23  24  } 25  while (( n * n )< number )  // while n 2  ...