23
C Tutorial http://www.its.strath.ac.uk/courses/c/ FAQ http://www.eskimo.com/~scs/C-faq/top.html - well established and trusted - low level - compiled and linked - statement grouping is done by bracketing - variable and argument declarations are necessary - few built in compound data types available - comments: /*hello*/ or // hello

Tutorial FAQ http ... · There are five basic data types associated with variables: •int - integer: a whole number. •float - floating point value: ie a number with a fractional

Embed Size (px)

Citation preview

CTutorial http://www.its.strath.ac.uk/courses/c/FAQ http://www.eskimo.com/~scs/C-faq/top.html

- well established and trusted- low level- compiled and linked- statement grouping is done by bracketing- variable and argument declarations are necessary- few built in compound data types available - comments: /*hello*/ or // hello

There are five basic data types associated with variables:

•int - integer: a whole number.

•float - floating point value: ie a number with a fractional part.

•double - a double-precision floating point value.

•char - a single character.

•void - valueless special purpose type

DATA TYPES

int integer 34 (8 bit)float floating point 34.567 (32 bit)char character, letter ‘m’ (8 bit)

short reduced range integer (1 bit)long expanded range integer (16 bit)unsigned int with no negative rangeunsigned long unsigned int with increased rangedouble double precision floating point

VARIABLE TYPESlocalglobalexternalstaticconstants

EXPANDED TYPESint this_is_an_array[100]; 1inear array of integerschar characters[10][10][10]; 3d array of characters

typedef struct structure with collection of types{

char name[32];int weight;

}cosmonaut;

cosmonaut first_cosmonaut; first_cosmonaut.name = “Juri”;

EXPRESSIONS and OPERATORSassignment int y = 5;arithmetic +, -, *, /, %, ++, --comparison ==, >, <, >=, <=, !=logical &&, ||, !bitwise &, ^, |shift <<, >>

CONTROL STATEMENTSif/else

if(result == true)gohome();

elsestayhere();

switchestimate(number)int number;{ switch(number)

{ case 0 :printf("None\n"); break; case 1 : printf("One\n"); break; case 2 : printf("Two\n"); break;

default : printf("Many\n"); break; }}

whileint string_length(char string[]){ int i = 0; while (string[i] != '\0') i++;

return(i);}

do/whiledo{ printf("Enter 1 for yes, 0 for no:"); scanf("%d", &input_value);} while(input_value != 1 && input_value != 0)

FUNCTIONSdouble power(double val, unsigned pow){ double ret_val = 1.0; short i; for(i = 0; i < pow; i++)

{ret_val *= val;} return(ret_val);}

double result = power(val, pow);

I/Oputchar()getchar()puts()gets()kbhit()printf()scanf()

#include <ctype.h> /* For toupper */#include <stdio.h> /* For getchar, putchar, EOF */

main(){ int ch; while((ch = getchar()) != EOF) putchar(toupper(ch));}

printf("Continue (Y,N)?");{answer = getch();}while(answer!='Y' && answer!='N');

EXAMPLE

#include <stdio.h>

main(){ printf("Hello World\n");}

The layout of a C Program

The general form of a C program is as follows

#include <a header.h>

#define world = mine;

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 ;}.

#include <stdio.h>

main() { printf("Hello World\n"); }

The Edit-Compile-Link-Execute Process

Developing a program in a compiled language such as C requires at least four steps:

editing (or writing) the program

compiling it

linking it

executing it

Compiling

You cannot directly execute the source file. To run on any computer system, the source file must be translated into binary numbers understandable to the computer's Central Processing Unit (for example, the 80*87 microprocessor). This process produces an intermediate object file - with the extension .obj, the .obj stands for Object.

Linking

Why is linking necessary? The main reason is that many compiled languages come with library routines which can be added to your program. Theses routines are written by the manufacturer of the compiler to perform a variety of tasks, from input/output to complicated mathematical functions. In the case of C the standard input and output functions are contained in a library (stdio.h) so even the most basic program will require a library function. After linking the file extension is .exe which are executable files.

stdio.h: I/O functions: string.h: String functions ctype.h: Character functions math.h: Mathematics functions time.h: Time and Date functions stdlib.h:Miscellaneous functions

#include <stdio.h> main() { int a,b,c;printf("\nThe first number is "); scanf("%d",&a);printf("The second number is ");scanf("%d",&b); c=a+b; printf("The answer is %d \n",c);

}

#include <stdio.h> main() { while (1 == 1) printf("Hello World!\n"); }

#include <stdio.h> main() { do printf("Hello World!\n"); while (1 == 1) }

#include <stdio.h> main() {

int count; count=0; while (count < 100) {

++count; printf("Hello World!\n");

}

}

for ( i=l; i <= 100; ++i ) printf("%d \n",i);

#include <stdio.h>

main()

{

int a , b;

do

{

printf("\nEnter first number: ");

scanf("%d" , &a);

printf("\nEnter second number: ");

scanf("%d" , &b);

if (a<b)

{

printf("\n\nFirst number is less than second\n");

printf("Their difference is : %d\n" , b-a);

printf("\n");

}

printf("\n");

} while (a < 999);

}

< (less than)

>= (for greater than or equal to )

<= (for less than or equal to)

== (to test for equality)

#include <stdio.h>

main()

{

int t ;

for ( ; ; )

{

scanf("%d" , &t) ;

if ( t==10 )

break ;

}

printf("End of an infinite loop...\n");

}

main() { int i;

printf("Enter a number between 1 and 4"); scanf("%d",&i);

switch (i) { case 1: printf("one"); break; case 2: printf("two"); break; case 3: printf("three"); break; case 4: printf("four"); break; default: printf("unrecognized number"); } /* end of switch */

}

demo() { printf("Hello"); int total; total = total + 1; }

int sum( int a, int b) { int result; result=a + b; return (result); }

main() { int result; demo(); result = sum (5, 7); printf(“the result is: %d”, result); }

A pointer to any variable type is an address in memory -- which is an integer address. A pointer is NOT an integer.

POINTERS

int a[10], x;int *pa; pa = &a[0]; /* pa pointer to address of a[0] */ x = *pa; /* x = contents of pa (a[0] in this case) */

char ch; /* a character */char *pch; /* a pointer to a character */char **ppch; /* a pointer to a pointer to a character */

swap(a, b) WON'T WORK. ->> Pass the address of the variables to the functions

->> swap(&a, &b)

void swap(int *px, int *py) { int temp; temp = *px; /* contents of pointer */

*px = *py;*py = temp;

}

C Preprocessor#define MAXSIZE 256 //defines substitutions#include “myfile.h” //reads in contents of another file