1 / 14

CSCE 206 Lab Structured Programming in C

Learn the basics of C programming including variable types, preprocessing directives, input/output, and common operators. Practice problems included.

carlc
Download Presentation

CSCE 206 Lab Structured Programming in C

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. CSCE 206 Lab Structured Programming in C SPRING 2019 Lecture 2 LUNA BACKES

  2. General Structure of a C program preprocessing directives int main(void) { declarations statements } • Header files and other preprocessing directions areincluded in the directive • Variables are declared inside the main function.They can be integer, float, string, character etc. • Calculations and instructions are placed in the statements portion of the code. • Never ever forget to put semicolon(;) after each lineof code in declaration and statements!!

  3. #include and #define • These are basic preprocessing directives • #include <filename.h> looks for filename.h header file in standard places. • We will use #include <stdio.h> that is required for working with standard input and output processes. • #define NAME VALUE makes sure that all occurrences of NAME corresponds to same VALUE • #define GRADE 4 makes sure that any time GRADE is used in the code, it has a value 4

  4. Variable Types & Operators • Common Variable Types: • int • float • double • char • Common Arithmetic Operators: +, - , *, /, %

  5. #printf() and #scanf() • Used for formatted printing output and reading input respectively • printf("%conversion_character",variable_name) prints the value storedin the variable. • scanf("%conversion_character",&variable_name) reads the value from user prompt to store in the variable • conversion_characteris for specifying the data type • & used in scanf(), specifies the memory location of the variable • You can write anything inside “” of the printf() function to show that on screen

  6. Highly used conversion characters

  7. Sample Code-1 • /*Lab2 sample code-1 */ • #include <stdio.h> • int main(void) • { int n; n=5; printf("%d",n); // prints the value of n on screen return 0; }

  8. Sample Code-2 • /*Lab2 sample code-2 */ • #include <stdio.h> • int main(void) • { float n; n=5.0; printf("Value stored in n:%f",n); // Difference?? return 0; }

  9. Sample Code-3 • /*Lab2 sample code-3 */ • #include <stdio.h> • int main(void) • { int n; printf("Enter a number:"); // Asks user for input scanf("%d",&n); printf("You have entered:%d",n); // Shows the number return 0; }

  10. Sample Code-4 • /*Lab2 sample code-4 */ • #include <stdio.h> • #define VAL 2 // defining a constant • int main(void) • { intn,x; printf("Enter a number:"); // Asks user for input scanf("%d",&n); x=n*VAL; printf("Double of your input:%d",x); // Shows the number return 0; }

  11. Now it’s your turn!

  12. Practice Problem-1 • Write a program with a char variable and an int variable. Store user input in the variables using scanf(). Print them on a line with a message saying what each one is. • Example: If the user gives input 5 and E for int and char respectively, the program will print • character: E integer: 5

  13. Practice Problem-2 • Write a program that calculates the area of circle of a given radius. First, the program will ask user to give a input for radius. Then it will print the area of the circle. • Example: If the user gives 2 as the input of radius, the program should print • Area of Circle: 12.5663

  14. Practice Problem-3 • Write a program that takes two integers as input in two variables a and b. It prints out the following: • Sum of the 2 integers • Result of the operation where a is divided by b • Remainder of the operation where a is divided by b

More Related