CSCE 206 Lab Structured Programming in C Fall 2018 Lecture 2 Ehsanul Haque Nirjhar
General Structure of a C program Header files and other preprocessing directions are included 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 line of code in declaration and statements!! preprocessing directives int main(void) { declarations 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_character is 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
Highly used conversion characters How the corresponding argument is printed c character d decimal integer f floating-point number s string
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) { int n,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; }
Now it’s your turn!
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