Introduction to C
-First Some Useful Hints: - Try to organize your directories Ex: mkdir cs352 cd cs352 mkdir assign1 ... - You can open more than one window(x-terminal) - To save unnecessary typing: -->You can use wildcards(like ? or *) in file names Ex:rm -i prog1* -->You can use the history mechanism Ex: !! (for the last command) !prefix (last command starting with prefix) OR the up-arrow key for teh previous comands. -->Sometimes tab will work too. - How to abort a command? -->Ctrl-C: interrupt character Ctrl-S: stop scrolling Ctrl-Q: resume Ctrl-O: discard the rest of the output - How to see the exit status of a command? --> echo $status - How to get printouts --> lpr -Plw13 file1 lpq -Plw13 (to see the print status) INTRODUCTION TO C /***********************************************/ /* helloworld.c */ /* Our first program. Prints out the sentence: */ /* merhaba dunya */ /***********************************************/ #includemain() { printf("merhaba dunya\n"); } /***********************************************/ Explanations: - compile:gcc helloworld.c the executable is in a.out - #include is for communication with the preprocessor. - is provided by the C system, contains info about the printf() function. - main() is the main function. Every program has a main function where execution begins. Parantheses are to indicate that it's a function. - { is to surround the body of a function - printf() is a function provided by the C system prints on the screen - "merhaba dunya\n" is a string, ends with newline. - ; is to end a statement. /***********************************************/ /*marathon.c */ /*The Greek guy "Lightheos" runs as fastas light*/ /*How long does it take him to complete the */ /*marathon? */ /*a marathon is 26 miles and 385 yards */ /*speed of "Lightheos" is 299792.458 km/sec */ /***********************************************/ #include main() { int miles, yards; float lightheos_speed, kilometers, time; miles = 26; yards = 385; /*distance of marathon in kilometers*/ kilometers = 1.609*(miles+yards/1760.0); printf("Marathon is %f kilometers\n", kilometers); /*marathon time*/ time = kilometers/299792.458; printf("Marathon takes %f seconds\n", time); } /***********************************************/ EXPLANATIONS: - Reserved words int, float are for declaration - Why 1760.0 instead of 1760? int/int=int - Print in another format, change the line to: printf("Marathon is %5.2f kilometers\n", kilometers); - What if Lightheos starts to run faster after some practice. Use #define /***********************************************/ /*minmaxavg.c */ /*compute the min, max, and average */ /*of 3 given numbers */ /*file using redirection. */ /***********************************************/ #include void info(void); float min(float,float); float max(float,float); main() { float num1, num2, num3; info(); scanf("%f%f%f", &num1, &num2, &num3); if ((min(num1,num2)==num1)){ printf("min:%f\n",min(num1,num3)); printf("max:%f\n",max(num2,num3)); } else{ printf("min:%f\n",min(num2,num3)); printf("max:%f\n",max(num1,num3)); } printf("avg:%f\n", (num1+num2+num3)/3); } void info(void) { printf("This program computes min, max, avg of 3 numbers\n"); printf("Enter three numbers:\n"); } float min(float x, float y) { if (x < y) return x; else return y; } float max(float x, float y) { if (x > y) return x; else return y; } /***********************************************/ EXPLANATIONS: - Right after inclide we put the function prototypes. - Notice the keyword void void as return-type->nothing is returned void as input-parameter-type->no input /***********************************************/ /*salary.c */ /*It finds out the min/max/avg salary of the */ /*people in the cs department. (Probably min */ /*will be me!! */ /*See how you can make use of redirection to */ /*read the data from a file */ /***********************************************/ #include #include main() { int count; float salary,min,max,sum,avg; if (scanf("%f",&salary)!=1){ printf("No data.\n"); exit(1); } min=max=sum=avg=salary; count=2; while (scanf("%f",&salary)==1){ if (salary < min) min = salary; else if (salary > max) max = salary; sum += salary; avg = sum / count; count ++; } printf("%d people, min:%f, max:%f, avg:%f\n", --count, min, max, avg); exit(0); } /***********************************************/ EXPLANATION: - Construct a file called data, redirect the input. - Note how we use scanf() - exit() is in stdlib.h - Check exit status of the program.