140 likes | 151 Vues
Learn the basics of C programming including variable types, preprocessing directives, input/output, and common operators. Practice problems included.
E N D
CSCE 206 Lab Structured Programming in C SPRING 2019 Lecture 2 LUNA BACKES
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!!
#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
Variable Types & Operators • Common Variable Types: • int • float • double • char • Common Arithmetic Operators: +, - , *, /, %
#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
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; }
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; }
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; }
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; }
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
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
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