Вы находитесь на странице: 1из 3

C Programming for Java Programmers

Motivation: why you want to know C


Gives better performance for systems programming
Relationship between C, C++, and Java
nline resources:s
C++ notes !you already know most of this"
C Reference: standard library
C Reference: C for Java #rogrammers at Cornell
!includes link to other C tutorials"
$ownloads !on the C++ page"
%utorials !on the C++ page"
Hello World in C !store the following lines in file prog1.c":
#include <stdio.h>
int main()
{
printf(A man, a plan, a canal, panama. \n);
}
&nd then from the command line, compile and run:
ernie! "#$%&#temp> 'cc pro'$.c
some warnings which you can ignore
ernie! "#$%&#temp> .#a.out
A man, a plan, a canal, panama.
ernie! "#$%&#temp>
Hello World in C++ !store the following lines in file prog2.cpp":
#include <iostream>
usin' namespace std;
int main()
{
cout << All 'enerali(ations are false. \n;
return %;
}
&nd then from the command line, compile and run:
ernie! "#$%&#temp> ')) pro'*.cpp
ernie! "#$%&#temp> .#a.out
All 'enerali(ations are false.
ernie! "#$%&#temp>
r to compile with a resulting e'ecutable called prog2run rather than a.out:
ernie! "#$%&#temp> ')) +o pro'*run pro'*.cpp
Pausing Ouput Window in DevCpp
(hen running a program using the $evCpp )$* the output appears on a terminal
window which +uickly then disappears, %o pause the output, consider adding this
line at the end of your program:
s,stem(-pause-);
%he system command in allows you to run from within your program any
command that normally would be typed at the command line,
Input & Output
)n C++ we use cout and cin and cout for output and input, respectively,
)n C we use printf and scanf for output and input, respectively,
Classes, Objets
-, C does not have classes or ob.ects, however C++ does, /arge C programs can be
broken up into multiple files, though we will not do this during the current
semester,
0, Java subroutines are called methods, )n C1C++ subroutines are called functions,
*verything must be called from an ob.ect in Java !e'cept static methods", %here
are no ob.ects in C, so functions are called by name, without a calling ob.ect,
2, )n Java we e'tensively reuse ob.ects that already e'ist, )n C, we more often must
write our own functions to do things,
3, b.ect4oriented programming !#" in Java focuses on what something is, along
with the associated actions for a class, #rocedural code in C focuses on the order
in which things happen, %o draw an analogy to grammar, creating # classes
are like nouns, while writing functions for #rocedural programming focuses on
verbs, or the actions of what happens when,
Control !trutures
%hese are pretty much the same in C and Java !if, while, do, switch, for", )n Java
it is possible to confuse the assignment operator !5" and the e+uality operator !55"
inside an if statement, but only for boolean types, )n C this confusion can e'ist
for any type,
Parameters
)n Java, parameter values can be changed within a method, but these
changes will not be reflected back, )n C, we can put an ampersand !6" between
the parameter type and the parameter name, which makes it a reference
parameter, which means its changes are reflected back to the caller,
"untion Definition #oation
)n Java a method can be defined either before or after its invocation, )n C the
compiler must know about a function before it can be called, %his can be satisfied
by declaring a function in a program above the point where it is first called,
7ometimes this is not possible, such as when two functions call each other
recursively, 8ou may also provide only the function declaration !return type,
name, and types of parameters" at the top of the program, and then supply the
body of the function !the function definition" anywhere in the file, without regard
to order, %hese function declarations are also called prototypes or forward
declarations,
$rra% Delarations
&rrays in Java are declared as:
int9: the&rray 5 new int9 si;e:<
%his can be done anywhere in a program, and size can be a variable,
)n contrast, arrays in C are declared as:
int the&rray9 si;e:<
where size must be a constant,
Data &%pes and 'ariables
-, %he =oolean type is called .ool in C++, %his type does not e'ist in C, where we
must instead use int, where > is false and any non4;ero value is true,
0, %he C++ string type is called strin', however there is no e'plicit string type in
C, (e will be restricting ourselves to arrays of characters that are ?@//
terminated, !)n C we use the constant NULL, as opposed to JavaAs null",
'ariables, Constants, !ope
-, %he C++ compiler !unlike Java" does not insist on variables being initiali;ed, so it
is easy to miss this,
0, Bariables can be declared globally, outside any functions or classes, but still
within the program, &void global variablesC
2, Remember, constants in Java were declared as:
final int %D*E7)F* 5 G<
)n C they are declared as:
const int %D*E7)F* 5 G<
3, &s in Java, you can declare a variable within a for loop, though some compilers
will not want you to redeclare the same variable in a subse+uent for loop,
Pointers
-, )n C we can e'plicitly manipulate the address of an entity in memory, %his
address is a pointer,
0, (e use the address operator 6 to get the address of something, and the
dereference operator H to go to a supplied address,
2, %he IarrowJ operator is the same as dereference and take member of,
!truts vs( Classes
-, )n C we group elements of different types using a struct, %his is similar to a
Java class with only instance variables !members", and no methods,
0, 7tructure elements are referenced using the dot !," operator,
2, %he IarrowJ operator is the same as dereference and take member of,
3, %ypedef allows creating synonyms for a type,
D%nami )emor% $lloation
-, %his re+uires a bit more Ibook4keepingJ in C than it did in Java,
0, Memory is allocated as:
void* malloc(int) //similar to new in Java
2, Memory is deallocated as:
void free(void*)
3, @nlike JavaAs automatic garbage collection, in C memory must be e'plicitly
deallocated,

Вам также может понравиться