C Flashcards
What’s the difference regarding to printing, C vs C++?
- C - printf(“enter two integers\n”)
- C - scanf(“ %d %d”, &num1 , &num2)
- C++ - cout << “Enter two integers:” << endl
- C++ - cin >> num1 >> num2
In c++ we don’t need to mention the type of the variables
How can we design the output?
- %2d - means the integer width is at least 2*
- %5.2f - means the float width(including the point and the fraction) is 5, and there are 2 digits after the point.*
What is the difference between const int x = 5, and #define x 5
- # define X 5- a command to the pre-processor. before the compilation, the pre-processor switches all occurences of X with 5. That is, this constant does not take place in memory, while for const int x = 5 a memory is allocated.*
- Another difference; #define makes the variable defined for any part of the program, right from where it is defined, while const might be bounded.*
What is the return value of (c = getchar())?
The value of this assignment is getchar().
Thus the following is legal:
x = y = c = getchar();
When using c = getchar() we use define c as int. why?
because we stop the execution when c!=EOF. EOF is defined to be -1, which is not in the range for char.
There’s a new way to initiate objects in C++. Describe how.
No need to use the “=” keyboard.
we can simply write:
int a []{1,2,3};
string s{“hello world”};
On Java it is not possible
Can we compare strings or assign one string to another in C?
Can we assing something to a string or some other array after it has been initialized?
No, we have to use the string.h library.
No. In C any kind of array cannot be the LHS of an assignment.
In C++ both are possible.
What’s the differences between the functionallity of enum in C and C++?
Two differences between C and C++:
-
In C we need to use typedef:
- typedef enum {Sun =1 , Mon..} Day in order to declatre: Day today = Mon**
-
Not doing so enforces us to write:
- enum Day today = Mon
- In C++ we don’t need typedef
- In C we can assign any value to the enum variable.
- In C++ we are forbidden to use any value other than the specific values we defined.
With Java we need to call it using “public/private/protected” and the values are not assigned to integers. They are simply the names.
When do we need to only declare a function.
In C function cannot use a function which is defined later on in the program. To let the compiler know this function is ineed exists, we declare it at the the top of the file
What should we do if we want the function to not change the value of its parameters?
we define the parameters as const
Describe two significant properties of local-static variable.
- It lives throgh the lifetime of the program.*
- It is saved in the Data-segment, where the global variables are saved.*
- It can be accessed only via its locality.*