We've covered quite a few topics in these first few weeks. It's time to pause and discuss some of the differences between what we've learned with C++ and how the C language differs. Many of these differences are quite small, so you just need to be aware of them and remember which to use depending on the language that you are utilizing.
To keep things simple, we'll cover what we've learned so far and how it differs.
| file.cpp | file.c |
|
|
g++ file.cpp | gcc file.c |
cout vs printfPrinting is different in C because the shorthand operators like >> are not available and neither are the handy variables cin and cout. Those are handy things we get from C++ and its available libraries. Let's continue our example and print a greeting:
|
|
The new function is printf and it can take a single string argument. This is then printed the terminal. Notice that endl is no longer available, so you use must explicitly use the newline char '\n' when you want a new line.
What about variables? The good news is that C and C++ have roughly the same primitive types so not much changes there. The one difference is that bool is not a variable type in C. There are no true/false constant values and there is no bool type. Instead, the int value 0 corresponds to false and all other values true.
Let's put these differences together and print out some numbers:
|
|
The C printf function can take any number of arguments, and the first argument must be a string which is the template for what you print. If you want to print variable values, then you must use format specifiers like %d and %lf in my example. Specifiers tell the program how to display the values amongst the rest of the string. All the format specifiers can be seen here - as you can see,
ints.
doubles.
chars.
cin vs scanfInput is one of the bigger differences between the languages. The next thing we'd like to do is read values in from the user, cin-style.
scanf
|
|
Go ahead and compile and run that program, to see that indeed, an input integer reads in to x. The scanf line is saying, read in an integer ("%d", as with printf), and store it in x. Like with printf, a single scanf command can read in multiple values, as long as the number of specifiers and number of pointers is the same.
cin skips leading spaces. How about scanf?
%d, %lf, %s: yes, it skips the leading spaces.
|
$ ./a.out
10 20
10
20
|
%c: no, unless it is explicitly told
so. Check out the following example.
|
$ ./a.out
a b
(32)
a (97)
|
scanf to skip the leading spaces then?. Well,
put a whitespace in front of %c!
| $ ./a.out a b a (97) b (98) $ ./a.out ab a (97) b (98) |
| ex1.c | compile & run |
| $ gcc -o ex1 ex1.c -lm $ ./ex1 command: dist (2,3) 3.605551 command: dist (-3,9.2) 9.676776 command: dist ( 1, 1.2) 1.562050 command: dist(2.2,9.8) 10.043904 command: x bye. |
dist (42,7.5)... you could use the call
scanf("dist (%lf,%lf)",&x,&y);,
and the "dist (" would literally have to appear in the input,
followed by a floating point value, and so on.
%lf is skipped, whitespace in front of the
literal text, like "dist (" is not.
However, a literal space in the format string matches any number of spaces, including 0. So by putting a space before "dist", we skip over any leading whitespace, including newlines. It also means that the space between "dist" and "(" is optional in the input: there can be no spaces in between, one space, or many spaces.
scanfscanf amounts
to the number of successful readings. In the above, the code checks if
scanf returns 2. This means it checks if it reads the input into
both x and y (i.e., two of them), successfully.
string in C. So, we have to use a static array of
char to represent a string.
|
|
fopen(), fclose(), fscanf(), fprintf()fopen, which accepts two C strings as arguments, the first of
which is a filename, and the second is a mode (detailed
here). After doing this, you can use fscanf or
fprintf just like you would scanf and
printf. So, to open a file and read an integer:
|
|
$ echo "1 3 2 4 10 8 7 5" > data.txt $ cat data.txt 1 3 2 4 10 8 7 5 $ ./a.out Filename? data.txt $ cat even.txt 2 4 10 8 $ cat odd.txt 1 3 7 5Solution: here.
$ ./product Enter two integers: 2 5 Product of all integers between 2 and 5 is 120 $ ./product Enter two integers: 10 13 Product of all integers between 10 and 13 is 17160 $ ./product Enter two integers: 1 12 The product of all integers between 1 and 12 is 479001600Solution: here.