Introduction to Programming with C: First Programs and Compilation Reminder

chapter 2 first programs chapter 2 first programs n.w
1 / 13
Embed
Share

Explore Chapter 2 in detail with the first programs in C programming, understand the basics of coding structure, compilation reminders, and adding enhancements to programs. Get insights into developing, compiling, and running programs effectively in C.

  • Programming
  • C Language
  • Compilation
  • Basics
  • Coding Structure

Uploaded on | 0 Views


Download Presentation

Please find below an Image/Link to download the presentation.

The content on the website is provided AS IS for your information and personal use only. It may not be sold, licensed, or shared on other websites without obtaining consent from the author. If you encounter any issues during the download, it is possible that the publisher has removed the file from their server.

You are allowed to download the files provided on this website for personal or commercial use, subject to the condition that they are used lawfully. All files are the property of their respective owners.

The content on the website is provided AS IS for your information and personal use only. It may not be sold, licensed, or shared on other websites without obtaining consent from the author.

E N D

Presentation Transcript


  1. Chapter 2, First Programs Chapter 2, First Programs

  2. /* prog2_1.c this is the very first program in the book Program 2.1, on page 11 which is explained in detail in Chp2 */ Notice the following: Header comment Indentation Semi-colons at end of lines Curly braces Blank lines #include <stdio.h> int main (void) { printf("\nProgramming is fun. \n"); return 0; }

  3. Developing prog2_1.c Developing prog2_1.c Type up code in an editor (e.g. vim, pico, gedit, etc.) Name it with a .c extension, such as prog2_1.c Compile using the gcc compiler: gcc Wall prog2_1.c If no errors, an executable called a.out will have been created Type ls at the Unix prompt to verify that a.out exists To run it, type: ./a.out If there are errors, re-edit to fix the errors and recompile until no more errors; then run it.

  4. Compilation Reminders Compilation Reminders There are several sub-steps within compilation: Preprocessor Assembler To show all warnings when compiling: gcc Wall prog2_1.c To capture all the intermediate files when compiling: gcc -save-temps Wall prog2_1.c To rename the executable to something other than a.out: gcc o prog2_1 Wall prog2_1.c The above will produce an executable called prog2_1 To run that executable, just type ./prog2_1 Be VERY careful not to add the .c to the renamed executable name you will overwrite your source code if you rename the executable prog2_1.c and all your code will be gone.

  5. /* prog2_1.c with a few additions similar to prog2.2 in the book */ Notice the following: Header comment should always have a header comment Additional line of output Another way of commenting (use of // for single line comments) Indentation of comments #include <stdio.h> int main (void) { // print two lines to the screen printf("\nProgramming is fun. \n"); printf("I m going to love this class!! \n\n"); return 0; }

  6. /* prog2_3.c page 15 in book but with print statements from my previous example shows how to print mulitple lines of output with one print statement */ #include <stdio.h> int main (void) { // print two lines to the screen printf("\nProgramming is fun.\nI m going to love this class!!\n\n"); return 0; } Programming is fun. I m going to love this class!

  7. /* ------------------------------------------------ prog2_4.c Displaying Variables Notice the following: Header comment (and other comments) you can develop your own style, whatever you think looks nice and makes your code more readable Declaration of an integer variable called sum Assigning a value to sum using arithmetic operation of addition Print statement with two arguments, the string in quotes, and the value of the variable sum which will replace the %i format specifier when printed to the screen Reusing the variable sum , assigning a new value to it, and printing its new value Use of blank lines to break up logical groups code, for readability Program 2.4, on page 15 with an added line or two of code --------------------------------------------------*/ #include <stdio.h> int main (void) { // declaring an integer variable called sum int sum; sum = 50 + 25; printf( \nThe sum of 50 and 25 is %i.\n , sum); sum = sum + 100; printf( \nNow sum is %i.\n , sum); return 0; }

  8. /* --------------------------------------------- prog2_5.c Displaying Multiple Variables Notice the following: Declaration of 3 integer variables on the same line, which is fine as long as they are the same type Initialization of value1 and value2 before assigning a value to sum Print statement with four arguments: the part in quotes is one argument; each %i format specifier will be replaced with the values of each of the other three arguments that come after the comma Also shows one way of splitting up a print statement onto two lines so it s not too long (no lines of code should exceed 80 characters, including leading spaces) Program 2.5, on page 17 ----------------------------------------------*/ #include <stdio.h> int main (void) { int value1, value2, sum; value1 = 50; value2 = 25; printf( \nThe sum of %i and %i is %i.\n , value1, value2, sum); sum = value1 + value2; return 0; }

  9. /* ------------------------------------------------- prog2_5.c Displaying Multiple Variables Notice the following: Declaration of 3 variables, each on a separate line Initialization of value1 and value2 on the same line where they are being declared Same print statement with four arguments: the part in quotes is one argument; each %i format specifier will be replaced with the values of each of the other three arguments that come after the comma Also shows another way of splitting up a print statement onto more than 1 line so it s not one long line; this example violates the indentation rules slightly, but in this case, it adds to readability, so it s an acceptable exception to the rule same program as previous slide, but with a few variations ---------------------------------------------------*/ #include <stdio.h> int main (void) { int value1 = 50; int value2 = 25; int sum; sum = value1 + value2; printf( \nThe sum of %i and %i is %i.\n , value1, value2, sum); return 0; }

  10. /* -------------------------------------------- prog2_5.c Displaying Multiple Variables Notice the following: Declaration of an 3 integer variables on the same line, including the initialization of 2 of them Everything else is the same as the previous slide another variation of the previous program ----------------------------------------------*/ #include <stdio.h> int main (void) { int value1 = 50, value2 = 25, sum; sum = value1 + value2; printf( \nThe sum of %i and %i is %i.\n , value1, value2, sum); return 0; }

  11. Formatting Style Requirements & Examples Formatting Requirements Formatting Examples

  12. vim Settings If you decide you will use the vi editor for your programs, there are a couple of settings that you can implement that can make your life easier. Setting Automatic Tabs To set automatic tabs, follow the instructions below. This will automate the indentation of your files. At your root directory, open up the file .vimrc for editing. It s a hidden file, so you won t see if when typing ls. If it doesn t already exist, you will create it when opening it up in an editor. Add the following lines: set autoindent set smartindent set tabstop=3 set shiftwidth=3 syntax on log out and log back in for it to take effect You can Google .vimrc settings and probably find other useful settings let us know if you find a setting that you think will be really helpful!!

  13. Color Coding of Your C code If you are using SSH (for Windows machines) and the text in your files is not color coded in vim, try the following. Choose Edit on the menu bar, and then Settings . In the Settings Window, under Profile Settings on the left hand side, click on Connection . Then on the right hand side, in the box next to Terminal Answerback , choose xterm from the drop down list. Choose Edit on the menu bar, and then Settings . From there you can adjust the foreground and background colors. There are also colorscheme settings that you can add in the .vimrc file. Some work and some don t; Google it and try some out. If you add this line to your .vimrc file, it should work: colorscheme evening but it s a black background with bright colors for the text.

More Related Content