How do we communicate? Language. We have a common protocol to communicate. Computer languages like C serve the same purpose – i.e., to connect humans and computers.
The CPU understands binary (0 and 1), so gcc
or g++
converts your C code to a binary executable.
int x = 5;
. The real storage in the computer is 00000101
, since int
s are allocated 8 bits.int x = -5;
. The real storage in the computer is 11111011
, according to two's complement notation.A racing car that goes incredibly fast, but breaks down every fifty miles.
new
and delete
cout
and cin
string
datatype&
)An advanced version of the C racing car, with lots of improvements and dozens of extra features that increase the breaking down limit to 250 miles.
You are the car. It breaks down every few feet.
Hello world:
#include <stdio.h>
int main() {
printf("Hello World");
}
Printing numbers, zero through 9:
#include <stdio.h>
int main() {
int i;
for (int i = 0; i < 10; i++) {
printf("i = %d\n", i);
}
}
There is no string
type in C. Use char *
or char[]
instead.
char *text = "Hello";
char text[] = {'H', 'e', 'l', 'l', 'o', '\0'};
Do not forget the null sentinel, if using list initialization!
What's wrong with this?
char *text = "Hello";
char *copy;
copy = text;
Both text
and copy
are pointing to the same address of the string. If text
is changed, so is copy
, and you might not want that.
What's wrong with this?
char *text = "Hello";
if (text == "Hello") {
// ...
}
It is comparing a char *
of the memory location of the string text
, to the memory address of a local string "Hello"
. This would always return false.
int *foo;
foo = (int *) malloc(sizeof(int)); // Allocates memory for int
*foo = 5;
// ...
free(foo);
Use this instead of new
and delete
.
cout
statements, if you're a noobcout
isn't going to save you.gdb
.General purpose processor is a general processor, in contrast to an ASIC processor which is an Application Specific Integrated Circuit.