|
C Programming Tutorials
Basics of C:
Facts about C
Why to Use C
C Program File
C Compilers
Program Structure:
Simple C Program
C
Program Compilation
Basic DataTypes:
DataTypes
Modifiers
Qualifiers
Arrays
Variable Types:
Local Variable
Global
Variable
Storage Classes:
auto storage class
register storage
class
static storage
class
extern storage
class
Using Constants:
Defining Constants
The enum Data Types
Operator Types:
Arithmetic Operators
Logical Operators
Bitwise Operators
Assignment Operators
Misc
Operators
Control Statements:
Branching
Looping
Input and Output:
printf() function
scanf() function
Pointing to Data:
Pointers and Arrays
Pointer
Arithmetic
Pointer Arithmetic
with arrays
Functions:
Using functions
Declaration and
Definition
Strings:
Reading and Writing
Strings
String Manipulation Function
Structured DataTypes:
Structure
Pointer to Structure
Working with Files:
Files
Basic I/O
Bits:
Bits Manipulation
Bits Field
Pre-Processors:
Pre-Processors Examples
Parameterized Macros
Macro
Caveats
Useful Concepts
Built-in Library Functions:
String Manipulation
Function
Memory Management
Function
Buffer
Manipulation
Character
Functions
Error Handling
Functions
Soft Skills
Communication Skills
Leadership Skills
.........More
|
|
C Programming Tutorials
Qualifiers
A type qualifier is used to refine the declaration of a variable, a function,
and parameters, by specifying whether:
Standard C language recognizes the following two qualifiers:
The const qualifier is used to tell C that the variable value
can not change after initialization.
const float pi=3.14159;
Now pi cannot be changed at a later time within the program.
Another way to define constants is with the #define preprocessor which has the
advantage that it does not use any storage
The volatile qualifier declares a data type that can have its value changed in
ways outside the control or detection of the compiler (such as a variable
updated by the system clock or by another program). This prevents the compiler
from optimizing code referring to the object by storing the object's value in a
register and re-reading it from there, rather than from memory, where it may
have changed. You will use this qualifier once you will become expert in "C". So
for now just proceed.
What are Arrays:
We have seen all basic data types. In C language it is possible to make arrays
whose elements are basic types. Thus we can make an array of 10 integers with
the declaration.
The square brackets mean subscripting; parentheses are used
only for function references. Array indexes begin at zero, so the elements of x
are:
Thus Array are special type of variables which can be used to store multiple
values of same data type. Those values are stored and accessed using subscript
or index.
Arrays occupy consecutive memory slots in the computer's memory.
| x[0], x[1], x[2], ..., x[9] |
If an array has n elements, the largest subscript is n-1.
Multiple-dimension arrays are provided. The declaration and use look like:
int name[10] [20];
n = name[i+j] [1] + name[k] [2]; |
Subscripts can be arbitrary integer expressions.
Multi-dimension arrays are stored by row so the rightmost subscript varies
fastest. In above example name has 10 rows and 20 columns.
Same way, arrays can be defined for any data type. Text is usually kept as an
array of characters. By convention in C, the last character in a character array
should be a `\0' because most programs that manipulate character arrays expect
it. For example, printf uses the `\0' to detect the end of a character array
when printing it out with a `%s'.
Here is a program which reads a line, stores it in a buffer, and prints its
length (excluding the newline at the end).
main( )
{
int n, c;
char line[100];
n = 0;
while( (c=getchar( )) != '\n' )
{
if( n < 100 )
line[n] = c;
n++;
}
printf("length = %d\n", n);
} |
Array Initialization
-
As with other declarations, array declarations can
include an optional initialization
-
Scalar variables are initialized with a single value
-
Arrays are initialized with a list of values
-
The list is enclosed in curly braces
| int array [8] = {2, 4, 6, 8, 10, 12, 14, 16}; |
The number of initializes cannot be more than the number of
elements in the array but it can be less in which case, the remaining elements
are initialized to 0.if you like, the array size can be inferred from the number
of initializes by leaving the square brackets empty so these are identical
declarations:
int array1 [8] = {2, 4, 6, 8, 10, 12, 14, 16};
int array2 [] = {2, 4, 6, 8, 10, 12, 14, 16}; |
An array of characters ie string can be initialized as
follows:
| char string[10] = "Hello"; |
NEXT >>
Variables Type
Have a Question ?
post your questions here. It
will be answered as soon as possible.
Check
C Aptitude Questions
for more C Aptitude Interview Questions with Answers
Check
C Interview Questions
for more C Interview Questions with Answers
|