What is C language?
The programming language C was originally developed by Dennis Ritchie of Bell Laboratories and was designed to run on a PDP-11 with a UNIX operating system. Although it was originally intended to run under UNIX, there has been a great interest in running it under the MS-DOS operating system on the IBM PC and compatibles.
What’s the best way to declare and define global variables?
First, though there can be many “declarations” (and in many translation units) of a single “global” (strictly speaking, “external”) variable or function, there must be exactly one “definition”. (The definition is the declaration that actually allocates space, and provides an initialization value, if any.)
The best arrangement is to place each definition in some relevant .c file, with an external declaration in a header (“.h”) file, which is #included wherever the declaration is needed. The .c file containing the definition should also #include the same header file, so that the compiler can check that the definition matches the declarations. This rule promotes a high degree of portability: it is consistent with the requirements of the ANSI C Standard, and is also consistent with most pre-ANSI compilers and linkers. (Unix compilers and linkers typically use a common model” which allows multiple definitions, as long as at most one is initialized; this behavior is mentioned as a “common extension” by the ANSI Standard, no pun intended. A few very odd systems may require an explicit initializer to distinguish a definition from an external declaration.)
It is possible to use preprocessor tricks to arrange that a line like
DEFINE(int, i); need only be entered once in one header file, and turned into a definition or a declaration depending on the setting of some macro, but it’s not clear if this is worth the trouble.
What’s the best place to declare and define global variables?
It’s especially important to put global declarations in header files if you want the compiler to catch inconsistent declarations for you. In particular, never place a prototype for an external function in a .c file: it wouldn’t generally be checked for consistency with the definition, and an incompatible prototype is worse than useless.
Why can’t you compare structures?
There is no single, good way for a compiler to implement structure comparison which is consistent with C’s low-level flavor. A simple byte-by-byte comparison could founder on random bits present in unused “holes” in the structure (such padding is used to keep the alignment of later fields correct; see question 2.12). A field-by-field comparison might require unacceptable amounts of repetitive code for large structures.
If you need to compare two structures, you’ll have to write your own function to do so, field by field.
How can I read/write structures from/to data files?
It is relatively straightforward to write a structure out using fwrite():
fwrite(&somestruct, sizeof somestruct, 1, fp);
and a corresponding fread invocation can read it back in. (Under pre-ANSI C, a (char *) cast on the first argument is required. What’s important is that fwrite() receive a byte pointer, not a structure pointer.) However, data files so written will *not* be portable (see questions 2.12 and 20.5). Note also that if the structure contains any pointers, only the pointer values will be written, and they are most unlikely to be valid when read back in. Finally, note that for widespread portability you must use the “b” flag when fopening the files;
A more portable solution, though it’s a bit more work initially, is to write a pair of functions for writing and reading a structure, field-by-field, in a portable (perhaps even human- readable) way.
What is a Variable?
A variable is a convenient placeholder that refers to a computer memory location where you can store program information that may change during the execution of your program.
You can declare the variable in C Language as
data_type var_name; i.e. int i;
In C Language every statement ends with a semicolon ;. You can give any name to your variable, but it’s a good programming practice to give the logical names to the variables.
In C Language you can declare as many variable on a single line as you can, but all of them must have the same data type. i.e.
int i, j, l, m; float f1, f2, f3;
What are the data types in C Language?
C Language have five basic data types. They are
3: Floating Point
4: Double Floating Point
5: Value less
Characters stores ASCIII characters or any 8-bit quantity. Float and double data types holds the real numbers.
C Language also support aggregate data types which include unions, structures and enumerations etc…
How to declare a variable?
In C Language every variable must be declared before using it. Standard declaration form of a variable is
Type must be a valid C Language data type and name can be anything except the following few restrictions.
1: Variable name can not start with a numeric value i.e 4myvar is a wrong variable name.
2: Variable name can not be a keyword in C Language i.e int chort is a wrong variable name.
3: int name and int NAME both are different variable names.
What is the difference between a local variable and a global variable?
Local variables are those which are declared within the boundries of a function or a code block. They are also called automatic variables. Local variables can be only referenced by the code within that code block.
Local variables are created when the control enters into the code block where the variable is created and destroyed when the control exits the code block.
Global variables span throughtout the application, i.e. they can be accessed from anywhere in the code. In C Language if we declare a variable outside the function then it will be a global variable.
Global variables take the memory the entire time the program is executing.