C Flashcards

1
Q

What’s the difference regarding to printing, C vs C++?

A
  • 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 well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

How can we design the output?

A
  • %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.*
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

What is the difference between const int x = 5, and #define x 5

A
  • # 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.*
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

What is the return value of (c = getchar())?

A

The value of this assignment is getchar().

Thus the following is legal:

x = y = c = getchar();

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

When using c = getchar() we use define c as int. why?

A

because we stop the execution when c!=EOF. EOF is defined to be -1, which is not in the range for char.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

There’s a new way to initiate objects in C++. Describe how.

A

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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

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?

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

What’s the differences between the functionallity of enum in C and C++?

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

When do we need to only declare a function.

A

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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

What should we do if we want the function to not change the value of its parameters?

A

we define the parameters as const

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

Describe two significant properties of local-static variable.

A
  • 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.*
How well did you know this?
1
Not at all
2
3
4
5
Perfectly