C and C++ Interview Tips 1 - C and C++ Programming Fundamentals

1. C++ domain operators

First look at the code below

#include <stdio.h>

int value = 0;

void printvalue()
{
	printf("value  = %d\n", value);
}

intmain()
{
	int value = 0;
	value = 1;
	printf("value = %d\n", value);

	::value = 2;
	printvalue ();

	return 0;
}

If we compile directly, the compiler will report an error, because this is a c file, there is no domain operator (::) in c language yet, but as long as we change the header file. Write it as a c++ file, and then compile it and output it normally. The result of the output is

value = 1
value = 2

The main points of this question are:

1. Domain operators, domain operators are added only by C++.

2, the scope of the variable.

The first value is a global variable, and its scope is the entire program, and then a value is defined in the main function, which is a local variable, and this variable will shield the global variable with the same name. So what is printed in the main function is value = 1. If we want to use the value of the global variable, we have to use the domain operator to access the value of the global variable.

Second, the difference between i++ and ++i

i++ : use the value of i first, and then increment i

++i : i is incremented first, and then the value after i is incremented is used.

Efficiency issues for both versions

Both are equally efficient if using primitive data types.

But if you use your own data types, such as classes, then the pre-version is more efficient. Because the pre-version can directly return the reference of the object , but the post-version needs to oppose the value of the object, and a temporary variable will be generated during the return process, which needs to be copied , which will generate a large overhead.

3. Conversion between signed and unsigned numbers

When performing operations, we need to pay attention to the data type of the operand. If we do not pay attention, unexpected troubles may occur.

Let's look at the following example

#include <stdio.h>

char getChar(int x, int y)
{
	char c;
	unsigned int a = x;
	
	(a + y > 10) ? (c = 1) : (c = 2);
	return c;
}

int main(void)
{
	char c1 = getChar(7,4);
	char c2 = getChar(7,3);
	char c3 = getChar(7,-7);
	char c4 = getChar(7,-8);
	
	printf("c1 = %d\n", c1);
	printf("c2 = %d\n", c2);
	printf("c3 = %d\n", c3);
	printf("c4 = %d\n", c4);
	return 0;
}
The function of the above function determines whether the sum of the two numbers passed in is greater than 10, but at this time we need to pay attention to the two operands of the plus operator. Reading the above code, we can see that a is an unsigned int type, but the type of y is indeed an int type. At this time, a type conversion occurs, and the signed type is converted to an unsigned type.


Convert signed number to unsigned number

If the first bit is not 1, then the unsigned number is the same as the original signed number.

If the first bit is 1, then the signed number is complemented, and the resulting number is an unsigned number.

Convert unsigned number to signed number

If the first bit is not 1, then the signed number is the same as the original unsigned number.

If the first bit is 1, then the unsigned number is complemented, and the first bit of the final number is the sign bit, and the remaining bits are the value of the signed number.

Four, three different methods of exchanging variables

1. Use an intermediate variable. This is also the commonly used method

void swap1(int &a, int &b)
{
	int x;
	x = a;
	a = b;
	b = x;
}

2. Use addition and subtraction to exchange the values ​​of two variables, which do not rely on intermediate variables

void swap2(int &a, int &b)
{
	a = a + b;
	b = a - b; //b is equal to a at this time;
	a = a - b; //At this time, subtracting the above b is equivalent to subtracting the original a, leaving the original b.
}

3. Use bitwise operators to swap two variables, this method does not require the help of intermediate variables

void swap3(int &a, int &b)
{
	a = a ^ b;
	b = a ^ b;
	a = a ^ b;
}
[Note]: The above parameters are passed by reference .


Five, the difference and connection between C and C++

the difference

The main difference between c and c++ is that the thinking of programming is different

c is procedural programming

c++ is object oriented programming

connect

We can also write procedure-oriented programs in C++, so C++ is a superset of C. C++ introduces classes, overloading, virtual functions, templates and containers on the basis of c.

6. The difference between #include <head.h> and #include "head.h"

#include<head.h> indicates that this header file is a standard header file, and the compiler will look for this file in the standard location. If it is not found, the compiler will report an error.

#include"head.h" indicates that this header file is a header file defined by ourselves. The compiler will first look for this header file in our project directory or the location we have specified. If it is not found, it will go to the standard location to find this header. If the file is still not found, the compiler will also report an error.


Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325948839&siteId=291194637