Sei sulla pagina 1di 19

UFCETS-20-1 Programming in C

Introduction to C Programming

Chulantha Kulasekere chulantha@northshore.lk

Northshore College of Business and Technology

May 8, 2012

ECK/2012 (NCBT)

Programming in C

May 8, 2012

1 / 16

A good reason to learn programming

ECK/2012 (NCBT)

Programming in C

May 8, 2012

2 / 16

What is C?

A small language that: makes extensive use of function calls has loose typing (cf. Pascal) [implicit type conversions] is structured (a.k.a module programming: subset of procedural programming enables low level (bit level) programming easily supports pointers - for memory, array, structures and functions. is ecient is portable (C can be compiled on a variety of computers)

ECK/2012 (NCBT)

Programming in C

May 8, 2012

3 / 16

What is C?

A small language that: makes extensive use of function calls has loose typing (cf. Pascal) [implicit type conversions] is structured (a.k.a module programming: subset of procedural programming enables low level (bit level) programming easily supports pointers - for memory, array, structures and functions. is ecient is portable (C can be compiled on a variety of computers)

ECK/2012 (NCBT)

Programming in C

May 8, 2012

3 / 16

What is C?

A small language that: makes extensive use of function calls has loose typing (cf. Pascal) [implicit type conversions] is structured (a.k.a module programming: subset of procedural programming enables low level (bit level) programming easily supports pointers - for memory, array, structures and functions. is ecient is portable (C can be compiled on a variety of computers)

ECK/2012 (NCBT)

Programming in C

May 8, 2012

3 / 16

What is C?

A small language that: makes extensive use of function calls has loose typing (cf. Pascal) [implicit type conversions] is structured (a.k.a module programming: subset of procedural programming enables low level (bit level) programming easily supports pointers - for memory, array, structures and functions. is ecient is portable (C can be compiled on a variety of computers)

ECK/2012 (NCBT)

Programming in C

May 8, 2012

3 / 16

And the downside ...

It has the following drawbacks: It has poor error detection which can scare the beginner. (This can be improved by setting compiler options, and using lint) It assumes the programmer knows what he/she is doing! It can easily be badly written we will see in the future. Also what C is NOT: C is not an object-orientated programming language C is not C++ or Java (although both are based on C)

ECK/2012 (NCBT)

Programming in C

May 8, 2012

4 / 16

First advice

Learn to use good practices in programming. The annual obfuscated C competition (http://www.ioccc.org) is a contest to see who can write the worst C. An example will be shown in class. Use a style guide like the one given in the UWE local resources.

So what is next?
THE ONLY WAY TO LEARN TO PROGRAM IS TO PROGRAM! To get the most from C you will need to know what functions are available in the standard libraries

ECK/2012 (NCBT)

Programming in C

May 8, 2012

5 / 16

Starting at the beginning ...


... or rather main

All C programs consist of functions. Execution of a C program begins with the function main() It therefore follows that all C programs must have at least one function, and one & only one called main().

A function is declared in C as:


Return_Type Function_Name(Arguments) { /* function contents */ }

ECK/2012 (NCBT)

Programming in C

May 8, 2012

6 / 16

Basics continued ..

Functions can call other functions and make use of their return value, or pass then data to work with as arguments. All C compilers depend on libraries of standard functions to be useful, since the language itself is very small. All libraries (.lib) have an associated header le (.h) which contains prototypes for the functions, (so the compiler can check that youre using them correctly), together with data structure denitions, and other useful denitions. The keyword void can be used if you are not interested in passing variables too and from a function

ECK/2012 (NCBT)

Programming in C

May 8, 2012

7 / 16

Program Syntax: Comments


Comments should add meaning to a program, and aid understanding. /* this is a comment */ Bad eg. day = day + 3 % 7 /* add 3 to day modulo 7 */; useless comment

Nested comments are not acceptable


/* Comment 1 /* Comment 2 End of comment 2 */ End of Comment 1 */

is illegal, although many compilers will not be concerned.

ECK/2012 (NCBT)

Programming in C

May 8, 2012

8 / 16

Program Syntax: Types

All functions return a value of a specied type. If none is stated, then you have implicitly declared that function to return an int The type of a variable tells the compiler how much memory to allocate to hold the variable, and what it will be used for. Older C compilers allowed assignment between incompatible types. Modern compilers do not! Type checking allows the compiler to more carefully evaluate the correctness of your code. You can make the compiler stricter through using optional switches

ECK/2012 (NCBT)

Programming in C

May 8, 2012

9 / 16

Program Syntax: Types


Basic Types

int: An integer value (i.e. a whole number). ints in C are of the most convenient size for the CPU architecture, typically today 32 bit. char: a byte (8 bit) sized integer, convenient for holding ASCII characters (which require 7 bits). float: a single precision oating point number. double: a double precision oating point number (more accurate but requiring usually twice the memory). Unlike some programming languages C does not dene the exact size of the variable. This loose typing can sometimes lead to trouble also. we can also have short int or short and long int or long All integer types can be . Eg. Unsigned Char 0255, on the other hand Char -128127. We also have register, static & volatile type modiers The limits are dened in the standard header le <limits.h>
ECK/2012 (NCBT) Programming in C May 8, 2012 10 / 16

Standard header (or include les)

#include <headerfile.h>: says include headerfile.h from the system location of header les. The system location can vary for dierent OSs. #include \myheader.h": This is how you would reference a header le created by you, if its in the current folder. If its not in the current directory: #include "../include/ myheader.h" Try locating the standard header les in the system you are using.

ECK/2012 (NCBT)

Programming in C

May 8, 2012

11 / 16

Variables

Variable names in C start with a letter, and can contain upper and lower case letters, digits and the underscore character.Note that C is case sensitive. e.g. int my integer variable; Convention in C is that lower case names are used for variables, all upper case names for dened constants: int my integer variable = INT MAX; If the underscore is not used, using camel case is acceptable e.g. intVariable77

ECK/2012 (NCBT)

Programming in C

May 8, 2012

12 / 16

Constants

A constant can be dened with the preprocessor directive #define PI 3.14159. A less frequently used method is const double pi = 31.4159; Constants have a default type, without a decimal point 1000 is an int, 1000L (or l) is a long, and 1000U (or u) is an unsigned int Integers can also be specied in octal (base 8) or hexadecimal (base 16). A leading 0 implies octal, a leading 0x implies hexadecimal. With a decimal point, constants are double precision oating point e.g. 3.14159 is a double and 3.14159F (or f) is a oat doubles can also be represented in exponent form, e.g. 1e3 is 1000 stored as a double.

ECK/2012 (NCBT)

Programming in C

May 8, 2012

13 / 16

Characters
Character constants are written as a. We can also specify a character escape as: \ooo (3 octal digits) or \xhh (2 hex digits) Remember, characters are treated as numbers!, For char x;, x = 48, x = \x30, x = 0x30, and x = 0 all give x the same value. C characters are 8 bit (1 byte) integers, usually signed, which are generally used to store ASCII characters. There are also some pre dened characters:

ECK/2012 (NCBT)

Programming in C

May 8, 2012

14 / 16

Strings

Example of a string: "Hello World", specied in double quotes. It is vitally important to remember that "a" and a are NOT the same. "a" requires 2 bytes of memory, one for the ASCII code for a, one for a null character. a requires 1 byte, to just hold the ASCII code for a. Strings are arrays of characters with an ASCII NULL (value 0) as the last character. Characters are single byte values.

ECK/2012 (NCBT)

Programming in C

May 8, 2012

15 / 16

The rst program


Hello World

Put this in a le called myprogram.cc


#include <stdio.h> /*include standard C I-O header*/ int main(void) /* declare a function called main, */ { /* taking no arguments returning no value*/ printf("Hello World\n"); /* use the library function printf to output our string */ return 0; /* return from the function*/ }

Compile it using gcc -pedantic -Wall {o myprogram myprogram.c

ECK/2012 (NCBT)

Programming in C

May 8, 2012

16 / 16

Potrebbero piacerti anche