C Tutorial - Sheet1 Page 1 Programming in C _ A Tutorial...

Info iconThis preview shows pages 1–3. Sign up to view the full content.

View Full Document Right Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: Sheet1 Page 1 Programming in C _ A Tutorial Brian W. Kernighan Bell Laboratories, Murray Hill, N. J. 1. Introduction. C is a computer language available on the GCOS and UNIX operating systems at Murray Hill and (in preliminary form) on OS/360 at Holmdel. C lets you write your programs clearly and simply _ it has decent control flow facilities so your code can be read straight down the page, without labels or GOTO's without being too cryptic program organization facilities. This memorandum is a tutorial to make learning C as painless as possible. The first part concentrates on the central features of C of the language which are useful (usually for getting more efficient and smaller code) but which are not necessary for the new user. This is "not" a reference manual. Details and special cases will be skipped ruthlessly, and no attempt will be made to cover every language feature. The order of presentation is hopefully pedagogical instead of logical. Users who would like the full story should consult the 'C Reference Manual' by D. M. Ritchie [1], which should be read for details anyway. Runtime support is described in [2] and [3] pile and run a C program. We will assume that you are familiar with the mysteries of creating files, text editing, and the like in the operat- ing system you run on, and that you have programmed in some language before. 2. A Simple C Program main( ) { printf("hello, world") } A C program consists of one or more functions, which are similar to the functions and subroutines of a Fortran program or the procedures of PL/I, and perhaps some external data definitions. main is such a function, and in fact all C programs must have a main. Execution of the program begins at the first statement of main. main will usually invoke other functions to perform its job, some coming from the same program, and others from libraries. One method of communicating data between functions is Sheet1 Page 2 by arguments. The parentheses following the function name surround the argument list arguments, indicated by ( ). The {} enclose the statements of the function. Individual statements end with a semicolon but are otherwise free-format. printf is a library function which will format and print output on the terminal (unless some other destination is specified). In this case it prints hello, world A function is invoked by naming it, followed by a list of arguments in parentheses. There is no CALL statement as in Fortran or PL/I. 3. A Working C Program Here's a bigger program that adds three integers and prints their sum. main( ) { int a, b, c, sum a = 1 sum = a + b + c printf("sum is %d", sum) } Arithmetic and the assignment statements are much the same as in Fortran (except for the semicolons) or PL/I. The format of C programs is quite free. We can put several statements on a line if we want, or we can split a statement among several lines if it seems desirable. The split may be between any of the operators or variables, but NOT in the middle of a name or operator. As a matter of style, spaces,middle of a name or operator....
View Full Document

This note was uploaded on 06/08/2010 for the course IIM CAT taught by Professor Cat during the Spring '06 term at Indian Institute Of Management, Ahmedabad.

Page1 / 72

C Tutorial - Sheet1 Page 1 Programming in C _ A Tutorial...

This preview shows document pages 1 - 3. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online