DOC PREVIEW
Rose-Hulman CSSE 432 - Study notes

This preview shows page 1-2-3-4-5-6-7-48-49-50-51-52-53-54-97-98-99-100-101-102-103 out of 103 pages.

Save
View full document
View full document
Premium Document
Do you want full access? Go Premium and unlock all 103 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 103 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 103 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 103 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 103 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 103 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 103 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 103 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 103 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 103 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 103 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 103 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 103 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 103 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 103 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 103 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 103 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 103 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 103 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 103 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 103 pages.
Access to all documents
Download any document
Ad free experience
Premium Document
Do you want full access? Go Premium and unlock all 103 pages.
Access to all documents
Download any document
Ad free experience

Unformatted text preview:

Day 01Introduction to C 1Si Dk tSaving Dyknow noteszSave on the serverzSave on the server.z Can access from anywhere. z Will have to install Dyknow on that machine.z Must connect to the networkz Bring a thumb drive and save it on the drive at the end of class. z Don’t have to connect to the network to access notes.z Will have to install Dyknow on that machine.Do nload fromhttp // d kno com/do nload/zDownload from http://www.dyknow.com/download/z DyKnow Client 5.0 (x86) , Version 5.0.70z The dyknow server is dyknow.cs.rose-hulman.eduyyCt hfilCreate a .cshrc fileprompt> emacs cshrcprompt> emacs .cshrcz Create aliases for common commandszAfter making changes, for the changes to take effect immediately:prompt> source .cshrcWhy learn C (after Java)?Why learn C (after Java)?zBoth high-level and low-level languagezBoth highlevel and lowlevel languagez Better control of low-level mechanismsz Performance better than Java z Java hides many details needed for writing OS codeBtBut,….z Memory management responsibilityzExplicit initialization and error detectionzExplicit initialization and error detectionz More room for mistakes4Goals of this tutorialGoals of this tutorialzTo introduce some basic C concepts to youzTo introduce some basic C concepts to youz so that you can read further details on your ownzTo warn you about common mistakes madezTo warn you about common mistakes made by beginners5Creating an executableCreating an executable6Source: http://www.eng.hawaii.edu/Tutor/Make/1-2.htmlTypes of filesTypes of fileszC source files ( c)zC source files (.c)z C header files (.h)Object files ( o)zObject files (.o)z Executable files (typically no extension – by df lt t)default : a.out)z Library files (.a or .so)7External library fileslibname.a or libname.solibname.a or libname.so z Special functionality is provided in the form of external libraries of readmade f nctionsready-made functions z Ready-compiled code that the compiler merges, or links, with a C program during compilationprogram during compilation z For example, libraries of mathematical functions, string handling functions, and input/output functionsfunctions, and input/output functions z Look for the library files under /usr/lib and header files under /usr/include8Example 1 – What is the output of this program?program?#include <stdio.h> //#include “myheader.h”intmain(){{printf(“Hello World. \n \t and you ! \n ”);/* print out a message */return 0;;}9Summarizing the Examplez #include <stdio.h> = include header file stdio.hz No semicolon at end z Small letters only – C is case-sensitivez int main(){ … } is the only code executedz printf(“ /* message you want printed */ ”);z \n = newline \t = tabz \ in front of other special characters within printf ppcreates “escape sequences”. z printf(“Have you heard of \”The Rock\” ? \n”);10Compiling and runningCompiling and runningz>gcc eg1 c (Creates a out)z>gcc eg1.c (Creates a.out)z >./a.out (Runs the executable)z >gcc eg1.c –o eg1 (Creates eg1 not a.out)z >./eg111To compile, use flag“l”and name i.e.To compile, use flag“l”and name i.e.––lname.lname.To compile, use flag l and name i.e. To compile, use flag l and name i.e. lname.lname.Eg. gcc Eg. gcc ––o test test.c o test test.c ––lm lm where“m”in“lm”comes from libm so i e thewhere“m”in“lm”comes from libm so i e thewhere m in lm comes from libm.so i.e. the where m in lm comes from libm.so i.e. the math library.math library.12Using external library filesUsing external library filesTo use the library files you must:To use the library files, you must:z include the library header files You may also have to:You may also have to:z link the library with a -l option to gcc13Pre-processor directivesPreprocessor directiveszA preprocessor is a program that examines CzA preprocessor is a program that examines C code before it is compiled and manipulates it in various ways. yz Two commonly used pre-processor directivesz#include–to include library filesz#include to include library files.z #define - to define macros (names that are expanded by the preprocessor into pieces of text pypp por C code) 14Example of pre-processor directivesExample of preprocessor directivesExample 2:#include <stdio.h> #define STRING1 "A macro definition\n" #define STRING2 "must be all on one line!\n" #define EXPRESSION1 1 + 2 + 3 + 4 #define EXPRESSION2 EXPRESSION1 + 10 #define ABS(x) ((x) < 0) ? -(x) : (x) #define MAX(a,b) (a < b) ? (b) : (a) #define BIGGEST(a,b,c) (MAX(a,b) < c) ? (c) : (MAX(a,b)) intmain (){ printf (STRING1); printf (STRING2); printf ("%d\n", EXPRESSION1); printf ("%d\n", EXPRESSION2); printf ("%d\n", ABS(-5));f ( f d %d\ IGG S ( )) 15printf ("Biggest of 1, 2, and 3 is %d\n", BIGGEST(1,2,3)); return 0;}What is the output of the ?program?#definez The expression is NOT evaluated when it preplaces the macro in the pre-processing stage.zEvaluation takes place only during thezEvaluation takes place only during the execution phase.17Simple Data TypesShort-hand# of bytes (typical)Data-type%c1char%d %i4int(typical)%lf8double%f4float%c1char%i2short%l4long%lf8double%i2shortString -%s 18gaddress - %p(HEX) or %u (unsigned int)#include <stdio.h>Example 3intmain(){{int nstudents = 0; /* Initialization, required */float age = 21.527;printf(“How many students does RHIT have ?”);scanf (“%d”, &nstudents); /* Read input */printf(“RHIT has %d students.\n”, nstudents); printf(“The average age of the students is %3.1f\n”,age);//3.1 => width.precisionreturn 0;}}$How many students does RHIT have ?:2000 (enter)RHIT has 2000 students19RHIT has 2000 students.The average age of the students is 21.5$Complete the program below:#include < stdio.h>int main() {int nStudents nFaculty;int nStudents, nFaculty;printf(“How many students and faculty does RHIT have?\n”);scanf(___________________________________________________);/* You may use only one scanf statement to accept the two values. Assume that a space will be used by the user to delimit the two input values */printf(_____________________________________________________________________________________________________);/* Y l i tf t t t t i t th/* You may use only one printf statement to print the values. Use a meaningful statement to do so. */return 0;;}Like JavazOperators similar to those in Java:Operators similar to those in Java: z Arithmeticz int i = i+1; i++; i--; i *= 2;z +, -, *, /, %,z Relational and Logicalz <,


View Full Document

Rose-Hulman CSSE 432 - Study notes

Download Study notes
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view Study notes and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view Study notes 2 2 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?