This question was asked by my professor in an exam and I couldn’t find an answer on Google, so here I am.
I don't understand what objects are in C at all.
This question was asked by my professor in an exam and I couldn’t find an answer on Google, so here I am.
I don't understand what objects are in C at all.
C has objects, just not in the OO sense of an object. Basically, an object in C is something that takes up memory:
3.15
1 object region of data storage in the execution environment, the contents of which can represent values
2 NOTE When referenced, an object may be interpreted as having a particular type; see 6.3.2.1.
Visibility refers more to identifiers than object instances:
6.2.1 Scopes of identifiers
...
2 For each different entity that an identifier designates, the identifier is visible (i.e., can be used) only within a region of program text called its scope. Different entities designated by the same identifier either have different scopes, or are in different name spaces. There are four kinds of scopes: function, file, block, and function prototype. (A function prototype is a declaration of a function that declares the types of its parameters.)
...
4 Every other identifier has scope determined by the placement of its declaration (in a declarator or type specifier). If the declarator or type specifier that declares the identifier appears outside of any block or list of parameters, the identifier has file scope, which terminates at the end of the translation unit. If the declarator or type specifier that declares the identifier appears inside a block or within the list of parameter declarations in a function definition, the identifier has block scope, which terminates at the end of the associated block. If the declarator or type specifier that declares the identifier appears within the list of parameter declarations in a function prototype (not part of a function definition), the identifier has function prototype scope, which terminates at the end of the function declarator. If an identifier designates two different entities in the same name space, the scopes might overlap. If so, the scope of one entity (the inner scope) will end strictly before the scope of the other entity (the outer scope). Within the inner scope, the identifier designates the entity declared in the inner scope; the entity declared in the outer scope is hidden (and not visible) within the inner scope.
Examples:
int foo; // file scope; visible over entire translation unit. By default,
// the name will be exported (visible to) other translation units.
void bar( int bletch ) // bletch has block scope
{
int blurga; // as does blurga; neither are visible
... // outside of bar
if ( condition() )
{
char blurga[N]; // this version of blurga also has block scope,
... // and "shadows" or "hides" the instance declared
// outside of the scope of the if statement, and is
// not visible outside of the if statement
}
}
The identifiers foo
and bar
are visible to other translation units; to prevent this, you would declare them with the static
keyword.
static int foo;
static void bar( ... )
"Objects" is probably too string a term for 'C', but replace it with "Stuff" and we have something to work with.
At it's most basic (no pun intended) anything is "visible" after its been declared.
Within a single source file, that means listing your function prototypes at the top of the file. Any code after that "knows" what that function looks like and can compile against it.
int doSomething( char* name, char* DoB, char* petName );
int rc = doSomething( myName, myDoB, "" );
If you omit this declaration, your compiler may "assume" a standard "shape" of function declaration, something like "int functionName();". Your code will fail to compile because the signature you intended doesn't match the [wrongly] assumed one.
Across multiple source files, you move that declaration into a header file and include that into any source file that needs to use the function.
[file.h]
int doSomething( char* name, char* DoB, char* petName );
[file.c]
#include file.h;
int rc = doSomething( myName, myDoB, "" );
[someOtherFile.c]
#include file.h;
int rc = doSomething( yourName, yourDoB, yourPetName );