CS125 - Lab 01 - Programing Basics
CS125 - Lab 01 - Programing Basics
Programming Practices
1. 2. 3. 4. 5. 6. 7. 8. 9. 10. 11. 12. 13. 14. 15. 16. 17. 18. 19. 20. Keep scopes small. Dont use the same name in both a scope and an enclosing scope. Declare one name (only) per declaration. Keep common and local names short, and keep uncommon and nonlocal names longer. Avoid similar-looking names. Maintain a consistent naming style. Choose names carefully to reflect meaning rather than implementation. Remember that every declaration must specify a type (there is no implicit int) Avoid unnecessary assumptions about the numeric value of characters. Avoid unnecessary assumptions about the size of integers. Avoid unnecessary assumptions about the range of floating-point types. Prefer a plain int over a short int or short or a long int or a long. Prefer a double over a float or a long double. Prefer plain char over signed char and unsigned char. Avoid making unnecessary assumptions about the sizes of objects. Avoid unsigned arithmetic. View signed to unsigned and unsigned to signed conversions with suspicion. View floating-point to integer conversions with suspicion. View conversions to a smaller type, such as int to char, with suspicion. Develop habit to declare and initialize the variable at the same time.
IDE
Exercise
Write the simplest C++ program using Visual C++ IDE.
Hello World
#include <iostream> int main() { std::cout << "Hello World!" <<endl; }
Exercise
1. Line by line dissection of the above programs 2. Usage of header file and namespaces
a b c d
= = = =
3; 5; a + b; a / b;
Exercise
Check the output of variables c and d in each of the above program by debugging.
Variable Scope
1. Below is a sample program. Use it to answer the following question: What happens if we declare the same name twice within a block, giving it two different meanings?
#include <iostream > using namespace std; int main () { int arg1; arg1 = -1; int x, y, z; char my_double = '5'; char arg1 = 'A'; cout << arg1 <<"\n"; return 0; }
2. Below is a sample program. Use it to answer the following question: Suppose an identifier has two different declarations, one in an outer block and one in a nested inner block. If the name is accessed within the inner block, which declaration is used?
#include <iostream > using namespace std; int main () { int arg1; arg1 = -1; { char arg1 = 'A';
3. Below is a sample program. Use it to answer the following question: Suppose an identifier has two different declarations, one in an outer block and one in a nested inner block. If the name is accessed within the outer block, but after the inner block, which declaration is used?
#include <iostream > using namespace std; int main () { int arg1; arg1 = -1; { char arg1 = 'A'; } cout << arg1 << endl; return 0; }
4. Below is a sample program that will not compile. Why not? By moving which line can we get the code to compile?
using namespace std; int main () {
Date: