Sie sind auf Seite 1von 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 a file called hello.c. Here are the contents of file hello.c: Sample Code
1. 2. 3. 4. 5. 6. 7. #include <stdio.h> int main(void) { printf("Hello Worldn"); return 0; }

I have added line numbers here to make it easier to refer to specific lines; the actual C source code does not have line numbers. We can run the C compiler on this program (the $ is the command 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". As mentioned, how you run the compiler on your system and what executable file it creates depends on 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 any libraries needed to run the program, creating an executable file. When a compiler processes a

source file, one of the first things it does is carry out the preprocessor directives. These are various 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 at preprocessor 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 the compiler 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 the original 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 include header files. The stdio.h file is a standard header file included with all C compilers. We will talk about header files more a little later. C is a procedural language. A procedural language can break a program up into several procedures (also called subroutines), and each procedure can issue commands and invoke other procedures. 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 this tutorial 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 the function that gets executed first. On line 3 of hello.c we have our main function. C functions can take parameters (also called arguments) and return values, similar to functions in math. On line 3 the int signifies that the main function returns an integer value. The void indicates that main does not take any parameters. There are two classic ways to declare the main function. One is as seen on line 3, the other is like this: 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, a C 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[] )

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 this program 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 returns the value 0 on line 6. printf() is a standard function used to write formatted output to the screen or whatever output device you have. This function is in the standard libraries that the C compiler links with programs. We will talk more about the printf() function below. In C, every statement is terminated by a semicolon (;) character. A compound statement (also called a block) is a sequence of statements inside curly braces { }. As you have seen, the body of a function is a compound statement. In addition, most places that accept a single statement can also 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. The compiler 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. 2. 3. 4. sum = a + b ;

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

Contract No : Contract Type :

3111C154684-001 SPOT

Client Name : Contract Status :

Lokesh LTD CONFIRMED

Selling Currency Currency Code : Currency Description : Rate Quoted : Amount : Pay Country : Contract Date : Value Date : GBP STERLING POUND 82.16000000 100,000.00 UK 11/01/2012 13/01/2012

Buying Currency INR INDIAN RUPEE 0.01217137 8,216,000.00 Trans Charges : Late Payment Forfeit : Other Charges : Net Total : Purpose : 100,000.00 Salary 13/01/2012 13/01/2012 MoorgateHO 0.00 0.00

TTD

Due Date : Maturity Date : Branch

INR

Quick Note

Client Payment Details Beneficiary Name -- A/c No -- Bank Name Add Bank Amount Mode of Payment

<-Select->
Bank Name Amount

0.00

<-Select->
Mode of Payment Direct Debit Date

1
3238247

100000.00 Abbey Busine


243

100000.00

BACS / CHAPS

100000.00

Beneficiary Funds Out Details

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

Mode of Payment

Reference Number

0.00 Ganesh Pal-xx35343453-SBI COMMERCIAL


Bank Name Amount

Prepaid Card Load


Mode of Payment Reference Number Serial Numbers

1
1 3238246 8216000.00

93 Axis--INR--xx5 8216000.00
8216000.00 N

TT

0 N

Memos ADMIN Contract Number : 3111C154684-001; ; Bank Name -HSBC Bank GBP Estimated Profit Actual Charges Total Profit / Loss 0.00 901.01 0.00 901.01

3264218

7
s11

171724

1
8216000.00

100000 GBP

1 1

5000.00 0

15.00

25/01/2012

Oppor No.

Client Name

Created Date

Reminder Date

Expected Closure Date

O060387 O060399 O060408 O060422 O060448 O060430 O060472 O060483 O060486 O060510 O060494 O060581 O060636 O060553 O060739 O060812 O060852 O079878 O079899 O079953 O079976 O079893

David J Roland Oleg Kutsevich Robert Van Dongen Barry Lawson Guy Whittaker Williams Terts Mark Van Den Berg Terence John Arnold Stanley Alwyn Barnett Tarique Mohammed Ghaffur

08/01/2008 08/01/2008 08/01/2008 08/01/2008 08/01/2008 09/01/2008 08/01/2008 11/01/2008 09/01/2008 15/01/2008 09/01/2008 15/01/2008 09/01/2008 15/01/2008 09/01/2008 10/01/2008 09/01/2008 11/01/2008

15/01/2008 15/01/2008 16/01/2008 18/01/2008 22/01/2008 22/01/2008 22/01/2008 17/01/2008 18/01/2008 23/01/2008 23/01/2008 28/01/2008 28/01/2008 25/01/2008 29/01/2008 31/01/2008 31/01/2008 12/01/2009 12/01/2009 16/01/2009 16/01/2009 12/01/2009

Carol Anne Pepper & John Richard 10/01/2008 16/01/2008 Pepper Eduardo Aguilar-Samitier 10/01/2008 16/01/2008

Michael Alan Evens & Elaine Evens 14/01/2008 21/01/2008 Caroline Angit Jean Marie Millard Peter William Bashforth Latif Mohammed Hussain Anthony James Croydon 14/01/2008 21/01/2008 11/01/2008 18/01/2008 16/01/2008 22/01/2008 17/01/2008 24/01/2008 18/01/2008 24/01/2008

Christian Manuel Rodriguez Martin 29/12/2008 05/01/2009 Dale Matthew Francis Eric Berg & Janet Shirley Berg Richard Jeffery Michael John Hughes 29/12/2008 05/01/2009 02/01/2009 09/01/2009 02/01/2009 09/01/2009 29/12/2008 05/01/2009

O079995 O060401 O060402

Adrian John Arnold Williams Gary Raymond Workman Brendan Gerard Murphy

05/01/2009 12/01/2009 08/01/2008 09/01/2008 08/01/2008 09/01/2008

19/01/2009 15/01/2008 15/01/2008

Das könnte Ihnen auch gefallen