C ProgrammingOverView

Embed Size (px)

Citation preview

  • 8/2/2019 C ProgrammingOverView

    1/8

    C Programming - An Overview

    C Programming - An Overview

    Hello World

    Let us look at a very basic C program. We will write the canonical "Hello World" program in afile called hello.c. Here are the contents of file hello.c:

    Sample Code

    1. #include 2.3. int main(void)4. {5. printf("Hello Worldn");6. return0;7. }

    I have added line numbers here to make it easier to refer to specific lines; the actual C sourcecode does not have line numbers. We can run the C compiler on this program (the $ is thecommand prompt, what I type is in red):

    Sample Code

    1. $ gcc hello.c

    The compiler does not give any errors and has created an executable file called "a.out". Asmentioned, how you run the compiler on your system and what executable file it creates dependson your system and compiler or IDE. We run the program and get the output:

    Sample Code

    1. $ ./a.out Hello World!

    Let us take a look at this example in detail, starting with how the file is compiled.

    A C compiler translates source files into machine instructions and then links them with anylibraries needed to run the program, creating an executable file. When a compiler processes a

  • 8/2/2019 C ProgrammingOverView

    2/8

    source file, one of the first things it does is carry out the preprocessor directives. These arevarious commands that control how the compiler processes the source file.

    All preprocessor commands start with the hash (#) mark. We will take a more detailed look atpreprocessor commands in another tutorial, but for now we will talk about the include directive

    seen on the first line of hello.c. This directive gives the name of another source file to thecompiler to include in this file. The file name is surrounded by double quotes or angle brackets.

    When the compiler sees this command it switches processing to the named file, then back to theoriginal file. It is like saying to the compiler "include the contents of the file stdio.h here".Typically the include directives are put near the top of a source file and are used to includeheader files.

    The stdio.h file is a standard header file included with all C compilers. We will talk about headerfiles more a little later.

    C is a procedural language. A procedural language can break a program up into severalprocedures (also called subroutines), and each procedure can issue commands and invoke otherprocedures. Though C's procedures are called "functions", that does not make C a "functional"programming language that term is used for another type of programming paradigm. In thistutorial we will use C's terminology and call procedures "functions".

    A C program should have one function called "main". When the program is run this is thefunction that gets executed first. On line 3 of hello.c we have our main function. C functions cantake parameters (also called arguments) and return values, similar to functions in math. On line 3the int signifies that the main function returns an integer value. The void indicates that main doesnot take any parameters.

    There are two classic ways to declare the main function. One is as seen on line 3, the other is likethis:

    Sample Code

    1. int main(int argc, char*argv[])

    This form is used when you wish to pass parameters to the main function. In addition to these, aC compiler may allow other forms of main(). A couple of common ones are:

    Sample Code

    1. void main(void)2. void main(int ac, char*av[])

  • 8/2/2019 C ProgrammingOverView

    3/8

    These are the same as the two main() declarations seen before except they return a void that is,they do not return any value.

    The body of a function is placed between curly braces { } like on lines 4 and 7 in hello.c. In thisprogram the body of the main() function only calls function printf() to print the string "Hello

    World!" and a new line (the n at the end of the string) to the output screen on line 5, and returnsthe value 0 on line 6. printf() is a standard function used to write formatted output to the screenor whatever output device you have. This function is in the standard libraries that the C compilerlinks with programs. We will talk more about the printf() function below.

    In C, every statement is terminated by a semicolon (;) character. A compound statement (alsocalled a block) is a sequence of statements inside curly braces { }. As you have seen, the body ofa function is a compound statement. In addition, most places that accept a single statement canalso accept a compound statement, such as after the if, while, case statements.

    In a C source file, by convention the statements in between curly braces are indented by a certain

    amount of space. You can see that lines 5 and 6 of hello.c above have been indented with a tab.This is only to make the code easier to read for people, it is not required by the compiler. Thecompiler ignores white-space (spaces, tabs, new lines) unless it occurs in a character or string.For example the statement:

    Sample Code

    1. sum = a+b;

    could have written like this:

    Sample Code

    1. sum2. =3. a +4. b ;

    The amount to indent statements inside a block is up to you; some people indent a tabstop, others indent 2 or 4 spaces, it is just whatever you prefer.

  • 8/2/2019 C ProgrammingOverView

    4/8

    Contract No : 3111C154684-001 Client Name : Lokesh LTD

    Contract Type : SPOT Contract Status : CONFIRMED

    Trans Charges : 0.00

    Late Payment Forfeit : 0.00

    Other Charges :

    Net Total : 100,000.00

    Purpose : Salary

    Due Date :13/01/2012

    INR

    Maturity Date : 13/01/2012

    Branch MoorgateHO

    SellingCurrency

    BuyingCurrency

    Currency Code : GBP INR

    CurrencyDescription :

    STERLINGPOUND

    INDIAN RUPEE

    Rate Quoted : 82.16000000 0.01217137

    Amount : 100,000.00 8,216,000.00

    Pay Country : UK

    Contract Date : 11/01/2012 TTD

    Value Date : 13/01/2012

    Quick Note

    Client Payment Details

    Beneficiary Name -- A/c No -- BankName Add Bank

    Amount Mode of Payment

    0.00

    Bank Name AmountMode ofPayment

    Direct DebitDate

    1

    1

    3238247

    100000.00

    243

    100000.00

    N

    Abbey Busine

    100000.00

    BACS / CHAP

    Beneficiary Funds Out Details

    http://172.31.1.116/cdis.ui/Deals/DealEdit.aspx?DealId=3264218&fromPage=Home+http://172.31.1.116/cdis.ui/Deals/DealEdit.aspx?DealId=3264218&fromPage=Home+http://172.31.1.116/cdis.ui/Deals/DealEdit.aspx?DealId=3264218&fromPage=Home+
  • 8/2/2019 C ProgrammingOverView

    5/8

    Beneficiary Name -- A/c No -- BankName Add Bank

    Amount Mode of PaymentReferenceNumber

    Ganesh Pal-xx35343453-SBI COMMERCIAL

    0.00

    Prepaid Card Load

    Bank Name AmountMode ofPayment

    ReferenceNumber

    SerialNumbers

    1

    1

    3238246

    8216000.00

    93

    8216000.00

    N

    N

    0

    N

    Axis--INR--xx

    8216000.00

    TT

    0

    Memos

    ADMIN Contract Number : 3111C154684-001; ; Bank Name -HSBC Bank

    GBP

    Estimated Profit 0.00

    Actual 901.01

    Charges 0.00

    Total Profit / Loss 901.01

    3264218 7 1 1 171724

    s11

    1

    8216000.00100000

    1 5000.00

    15.00 GBP 1 0

    http://172.31.1.116/cdis.ui/Deals/DealEdit.aspx?DealId=3264218&fromPage=Home+http://172.31.1.116/cdis.ui/Deals/DealEdit.aspx?DealId=3264218&fromPage=Home+http://172.31.1.116/cdis.ui/Deals/DealEdit.aspx?DealId=3264218&fromPage=Home+
  • 8/2/2019 C ProgrammingOverView

    6/8

    Y 25/01/2012

  • 8/2/2019 C ProgrammingOverView

    7/8

    Oppor No.

    Client NameCreated

    Date

    Reminder

    Date

    Expected Closure

    Date

    O060387 David J Roland 08/01/2008 08/01/2008 15/01/2008

    O060399 Oleg Kutsevich 08/01/2008 08/01/2008 15/01/2008

    O060408 Robert Van Dongen 08/01/2008 09/01/2008 16/01/2008

    O060422 Barry Lawson 08/01/2008 11/01/2008 18/01/2008

    O060448 Guy Whittaker Williams 09/01/2008 15/01/2008 22/01/2008

    O060430 Terts Mark Van Den Berg 09/01/2008 15/01/2008 22/01/2008

    O060472 Terence John Arnold 09/01/2008 15/01/2008 22/01/2008

    O060483 Stanley Alwyn Barnett 09/01/2008 10/01/2008 17/01/2008

    O060486 Tarique Mohammed Ghaffur 09/01/2008 11/01/2008 18/01/2008

    O060510Carol Anne Pepper & John Richard

    Pepper10/01/2008 16/01/2008 23/01/2008

    O060494 Eduardo Aguilar-Samitier 10/01/2008 16/01/2008 23/01/2008

    O060581 Michael Alan Evens & Elaine Evens 14/01/2008 21/01/2008 28/01/2008

    O060636 Caroline Angit 14/01/2008 21/01/2008 28/01/2008

    O060553 Jean Marie Millard 11/01/2008 18/01/2008 25/01/2008

    O060739 Peter William Bashforth 16/01/2008 22/01/2008 29/01/2008

    O060812 Latif Mohammed Hussain 17/01/2008 24/01/2008 31/01/2008

    O060852 Anthony James Croydon 18/01/2008 24/01/2008 31/01/2008

    O079878 Christian Manuel Rodriguez Martin 29/12/2008 05/01/2009 12/01/2009

    O079899 Dale Matthew Francis 29/12/2008 05/01/2009 12/01/2009

    O079953 Eric Berg & Janet Shirley Berg 02/01/2009 09/01/2009 16/01/2009

    O079976 Richard Jeffery 02/01/2009 09/01/2009 16/01/2009

    O079893 Michael John Hughes 29/12/2008 05/01/2009 12/01/2009

    http://172.31.1.116/cdis.ui/LookUpPages/OpporUserAlerts.aspxhttp://172.31.1.116/cdis.ui/LookUpPages/OpporUserAlerts.aspxhttp://172.31.1.116/cdis.ui/LookUpPages/OpporUserAlerts.aspxhttp://172.31.1.116/cdis.ui/LookUpPages/OpporUserAlerts.aspxhttp://172.31.1.116/cdis.ui/LookUpPages/OpporUserAlerts.aspxhttp://172.31.1.116/cdis.ui/LookUpPages/OpporUserAlerts.aspxhttp://172.31.1.116/cdis.ui/LookUpPages/OpporUserAlerts.aspxhttp://172.31.1.116/cdis.ui/LookUpPages/OpporUserAlerts.aspxhttp://172.31.1.116/cdis.ui/LookUpPages/OpporUserAlerts.aspxhttp://172.31.1.116/cdis.ui/LookUpPages/OpporUserAlerts.aspxhttp://172.31.1.116/cdis.ui/LookUpPages/OpporUserAlerts.aspxhttp://172.31.1.116/cdis.ui/LookUpPages/OpporUserAlerts.aspxhttp://172.31.1.116/cdis.ui/LookUpPages/OpporUserAlerts.aspxhttp://172.31.1.116/cdis.ui/LookUpPages/OpporUserAlerts.aspxhttp://172.31.1.116/cdis.ui/LookUpPages/OpporUserAlerts.aspxhttp://172.31.1.116/cdis.ui/LookUpPages/OpporUserAlerts.aspxhttp://172.31.1.116/cdis.ui/LookUpPages/OpporUserAlerts.aspxhttp://172.31.1.116/cdis.ui/LookUpPages/OpporUserAlerts.aspxhttp://172.31.1.116/cdis.ui/LookUpPages/OpporUserAlerts.aspx
  • 8/2/2019 C ProgrammingOverView

    8/8

    O079995 Adrian John Arnold Williams 05/01/2009 12/01/2009 19/01/2009

    O060401 Gary Raymond Workman 08/01/2008 09/01/2008 15/01/2008

    O060402 Brendan Gerard Murphy 08/01/2008 09/01/2008 15/01/2008