c language elements
play

C Language Elements CSCI 112: Programming in C A simple program to - PowerPoint PPT Presentation

C Language Elements CSCI 112: Programming in C A simple program to convert miles to kilometers Ask the user for a number of miles Convert that number to kilometers Display the result to the user #include <stdio.h> #define


  1. C Language Elements CSCI 112: Programming in C

  2. – A simple program to convert miles to kilometers – Ask the user for a number of miles – Convert that number to kilometers – Display the result to the user #include <stdio.h> #define KMS_PER_MILE 1.609 int Example main(void) { Program double miles, kms; printf("Enter the distance in miles: "); scanf("%lf", &miles); kms = KMS_PER_MILE * miles; printf("That is %f kilometers. \n", kms); return 0; }

  3. #include <stdio.h> #define KMS_PER_MILE 1.609 int – These are instructions that main(void) { are given to the double miles, kms; C preprocessor . The printf("Enter the distance in miles: preprocessor's job is to "); scanf("%lf", &miles); modify the text of the C Preprocessor program before it is compiled kms = KMS_PER_MILE * miles; – Preprocessor directives start Directives printf("That is %f kilometers. \n", with the # sign. The two kms); most common ones are: – #include <header file> return 0; } – #define NAME <value

  4. #include – This allows the programmer to include definitions from a header file into the current source file. – Header files typically end with .h, and contain additional definitions that the program can use – When a program contains multiple source files, header files are Preprocessor how a programmer can share functions between files – C does not have a lot of functionality directly. Instead, it comes Directives with several 'standard libraries' which may be included in this manner. – For example, we included stdio.h in the miles to kiometers rpgoram – If the name is in quotes, it’s treated as a relative path to that file. – If it’s in <>, the preprocessor looks for that file in the system’s C direcotry.

  5. #define NAME <value> – This is a 'constant macro' Preprocessor – It instructs the preprocessor to replace every occurrence Directives of NAME in the code with the defined value. – NAME is a user-defined identifier

  6. #include <stdio.h> – Comments are important, as #define KMS_PER_MILE 1.609 they help the programmer understand what the code is /* * The main function (runs first) supposed to be doing. */ int – Ignored by preprocessor and main(void) { compiler. double miles, kms; printf("Enter the distance in miles: – In C, there are two styles "); Comments of comments : block scanf("%lf", &miles); comments and line // Convert the distance to kilometers kms = KMS_PER_MILE * miles; comments. – A block comment starts printf("That is %f kilometers. \n", with the characters /* and kms); ends with the characters */. – A line comment starts with return 0; } the characters //, and ends at the end of the line.

  7. #include <stdio.h> – The entry point into a C program— #define KMS_PER_MILE 1.609 the first thing t be run. int – First line declares return type of main(void) function, in this case int. { double miles, kms; – Next line indicates function name printf("Enter the distance in miles: (main) and parameters (void, "); indicating no parameters) scanf("%lf", &miles); – Between the {} brackets is the function body, containing kms = KMS_PER_MILE * miles; Function main declarations and executable statements . printf("That is %f kilometers. \n", kms); – The final line is the return statement, the value the function return 0; gives back to the caller (the } operating system). – 0 indicates success. Any other number indicates some type of error.

  8. #include <stdio.h> #define KMS_PER_MILE 1.609 int main(void) { – Some words have special double miles, kms; meaning in C, and cannot be printf("Enter the distance in miles: used for other purposes. "); scanf("%lf", &miles); – These include Reserved – Variable types kms = KMS_PER_MILE * miles; – If/else Words printf("That is %f kilometers. \n", – For/while/do kms); – Return return 0; – sizeof }

  9. #include <stdio.h> #define KMS_PER_MILE 1.609 int main(void) { double miles, kms; – These are words with a printf("Enter the distance in miles: special meaning, like "); scanf("%lf", &miles); reserved words. Standard – The difference is that they’re kms = KMS_PER_MILE * miles; defined in a C standard Identifiers library. printf("That is %f kilometers. \n", kms); – Printf, scanf in our example return 0; }

  10. #include <stdio.h> – Programmers choose names of #define KMS_PER_MILE 1.609 functions, variable and constans int main(void) { – An identifier must consist double miles, kms; only of letters, digits, and underscores printf("Enter the distance in miles: "); – An identifier cannot begin scanf("%lf", &miles); with a digit User-defined – A C reserved word cannot be kms = KMS_PER_MILE * miles; used as an identifier Identifiers printf("That is %f kilometers. \n", – An identifier defined in a C kms); standard library should not be redefined return 0; – Try to keep identifiers concise } and meaningful! – All identifiers are CASE SENSITIVE

  11. – C defines a few different data types, for storing different types of content. – int - whole numbers – double – A float with twice the precision – char - individual characters (letter, digit, punctuation, etc...) – Notice anything missing? Data Types – The char data type actually represents characters as integers. The value stored for a certain character depends upon the system your C compiler uses. – The most common version of this system is the ASCII code (American Standard Code for Information Interchange).

  12. – The int data type stores integers, but there are a few different qualifiers depending on what range of values need to be stored – Why might we want to use long instead of int ? Storing – Say we need to store positive integers up to 60,000. Which type would Integers be best? Why? – C does not guarantee sizes of data types, only inequalities between them: sizeof(short) <= sizeof(int) <= sizeof(long) – On modern macOS systems, for example: – A char is 1 byte – A short is 2 bytes – An int is 4 bytes – A long is 8 bytes

  13. – Like with the integers, there are various types that can store reals. – These numbers are stored in memory in three parts: the exponent, sign and mantissa. Storing Real Numbers – The actual value is equal to mantisaa * 2 exponent – Why we care—for now, because the storage space affects both the range and accuracy of stored numbers

  14. – A function consists of variable declarations and executable statements – A declaration tells the compiler that a variable exists, and that it needs to set aside memory cells to store it, but it does not initialize it to any value What’s inside a char myChar; function? – An executable statement performs some sort of computation or action with variables. It’s what gets translated into machine language and run by the CPU.

  15. – You use a function call n order to activate a function in C. – This is done by simply typing it as an instruction in our program. Executable – A function performs some work for you, without you having to know the details of what it is doing. statements: – Function name, in this case, printf Function Calls – Function arguments. This includes everything in between the ( ) parenthesis characters. – Example: printf

  16. – The arguments for printf consist of two main things: – The format string , which starts and ends with the " quote character. – The print list , which is a comma-separated list of variable names that app – The format string contains the placeholder %f. When the string is printed to the screen, the value stored in kms will be substituted Printf and into that text as it is printed out.ear after the format string. Placeholders – The number of placeholders in the format string should match the number of variables passed to printf in the print list. – Placeholders must also match the data type of the variable passed in. – Below, %lf is placeholder and kms is the only member of the value list. printf("That equals %lf kilometers.\n", kms);

  17. printf(”%d, %lf, %f, %c", anInt, aDouble, aFloat, aChar); Printf with lots of variables

  18. – Data type..............Placeholder Common int.......................... %d char....................... %c Placeholders double................... %lf float....................... %f

  19. – The \n character is the 'newline' character. It causes the cursor to What is \n? move down to the beginning of the next line, similar to how pressing 'enter' in a text editor adds a new line to a text document.

  20. – This function copies the data from the standard input device (the keyboard) into the variable(s) specified. Getting user – The first argument is what input to look for—same as printf – The & is the address-of operator: it provides the memory address input: scanf in which the variable resides. – In this case, the compiler doesn’t care what value is in miles, it needs to know where miles is so that it can place the user input into it. printf("Enter the distance in miles > "); scanf("%lf", &miles);

Recommend


More recommend