0

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.

8
  • 2
    Technically, C doesn't have objects. It has structs, but not objects. Perhaps your professor is referring to variables. "Visibility" is actually called "scope;" you can read about C's scope rules here: tutorialspoint.com/cprogramming/c_scope_rules.htm Commented Sep 16, 2016 at 0:04
  • 2
    (Your professor needs to brush up on his programming vocabulary). Commented Sep 16, 2016 at 0:09
  • C Doesnt have objects really. There are structs , and enums but no objects. I think this is a C++ Question maybe by any chance a mistake tag?
    – amanuel2
    Commented Sep 16, 2016 at 0:09
  • 1
    Then I guess you'd better get clarification from your instructor. Commented Sep 16, 2016 at 0:17
  • 13
    My PDF search engine tells me, there are over a 1000 mentions of the word "object" in the ISO C Spec, too many even to display them all. Here's the definition: "object: region of data storage in the execution environment, the contents of which can represent values". So, there most definitely are objects in C. They, of course have absolutely nothing whatsoever to do with the term "object" in OOP. They refer to the English dictionary definition of the word. Commented Sep 16, 2016 at 1:15

2 Answers 2

6

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( ... )
0

"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 ); 

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.