Introduction to C:
The C programming language is a general-purpose, high-level language (generally denoted as structured language). C programming language was at first developed by Dennis M. Ritchie at At&T Bell Labs.
C is one of the most commonly used programming languages. It is simple and efficient therefore it become best among all. It is used in all extents of application, mainly in the software development.
Many software's & applications as well as the compilers for other programming languages are written in C also Operating Systems like Unix, DOS and Windows are written in C.
C has many powers, it is simple, stretchy and portable, and it can control System Hardware easily. It is also one of the few languages to have an international standard, ANSI C.
Advantages:
- Fast, Powerful & efficient
- Easy to learn.
- It is portable
- ''Mid-level'' Language
- Widely accepted language
- Supports modular programming style
- Useful for all applications
- C is the native language of UNIX
- Easy to connect with system devices/assembly routines
Structure of Program:
/* This program prints Hello World on screen */
-----------------------------
#include <stdio.h>
Void main()
{
printf(''Hello World\n'');
}
-----------------------------
1 . /* This program ... */
The symbols/* and*/ used for comment. This Comments are ignored by the compiler, and are used to provide useful information about program to humans who use it.
2. #include<stdio.h>
This is a preprocessor command which tells compiler to include stdio.h file.
3. main()
C programs consist of one or more functions. There must be one and only one function called main. The brackets following the word main indicate that it is a function and not a variable.
4. { }
braces surround the body of the function, which may have one or more instructions/statements.
5. printf()
it is a library function that is used to print data on the user screen.
6. ''Hello World\n'' is a string that will be displayed on user screen
\n is the newline character.
; a semicolon ends a statement.
7. return 0; return the value zero to the Operating system.
C is case sensitive language, so the names of the functions must be typed in lower case as above.
we can use white spaces, tabs & new line to make our code easy to read.
Variables:
3.1.1 Variable Declaration
Usually Variables are declared before use either at the start of a block of code after the opening { and before any other statements or outside a function.
--------------------------
int a,b; /* global variables */
main()
{
float a; /* local variables */
}
--------------------------
Local variables can only accessed within that function only whereas Global variables can access in whole program.
3.1.2 Variable Types
There are many 'built-in' data types in C.
short int -128 to 127 (1 byte)
unsigned short int 0 to 255 (1 byte)
char 0 to 255 or -128 to +127 (1 byte)
unsigned char 0 to 255 (1 byte)
signed char -128 to 127 (1 byte)
int -32,768 to +32,767 (2 bytes)
unsigned int 0 to +65,535 (2 bytes)
long int -2,147,483,648 to +2,147,483,647 (4 bytes)
unsigned long int 0 to 4,294,967,295 (4 bytes)
float single precision floating point (4 bytes)
double double precision floating point (8 bytes)
long double extended precision floating point (10 bytes)
3.1.3 Variable Names
we can use any combination of letters and numbers for Variable and function names but it must start with a letter.
We can use Underscore (_) as a letter in variable name and can begin with an underscore But Identifiers beginning with an underscore are reserved, And identifiers beginning with an underscore followed by a lower case letter are reserved for file scope identifiers Therefore using underscore as starting letter is not desirable.
Surya and surya are different identifiers because upper and lower case letters are treated as different identifiers
Operators:
An operator is a symbol. Compiler identifies Operator and performs specific mathematical or logical operation.C provides following operators :
# Arithmetic Operators
# Increment and Decrement Operators
# Relational Operators
# Logical Operators
# Cast Operators
# Bitwise Operators
# Assignment Operators
3.2.1 Arithmetic Operators
* multiplication
/ division
% remainder after division (modulo arithmetic)
+ addition
- subtraction and unary minus
3.2.2 Increment and Decrement Operators
Increment and decrement operators are used to add or subtract 1 from the current value of oprand.
++ increment
-- decrement
Increment and Decrement operators can be prefix or postfix. In the prefix style the value of oprand is changed before the result of expression and in the postfix style the variable is modified after result.
For eg.
a = 9;
b = a++ + 5; /* a=10 b=14 */
a = 3;
b = ++a + 6; /* a=10 b=15 */
3.2.3 Relational Operators
== equal.
!= Not equal.
> < Greater than/less than
>= greater than or equal to
<= less than or equal to
3.2.4 Logical Operators
&& Called Logical AND operator. If both the operands are non-zero, then condition becomes true.
|| Called Logical OR Operator. If any of the two operands is non-zero, then condition becomes true.
! Called Logical NOT Operator. Use to reverses the logical state of its operand. If a condition is true, then Logical NOT operator will make false.
3.2.5 Cast Operators
Cast operators are used to convert a value from one to another type.
(float) sum; converts type to float
(int) fred; converts type to int
3.2.6 Bitwise Operators
Bitwise operators performs operation on actual bits present in each byte of a variable. Each byte contain 8 bits, each bit can store the value 0 or 1
~ one's complement
& bitwise AND
^ bitwise XOR
| bitwise OR
<< left shift (binary multiply by 2)
>> right shift (binary divide by 2)
3.2.7 Assignment Operators
= assign
+= assign with add
-= assign with subtract
*= assign with multiply
/= assign with divide
%= assign with remainder
>>= assign with right shift
<<= assign with left shift
&= assign with bitwise AND
^= assign with bitwise XOR
|= assign with bitwise OR
For example,
a = a + 64; is same as
a += 64;
Printf and Scanf: Displaying Text and Taking Input from user:
4.1.1 Formatted Output - printf
It takes text and values from within the program and sends it out onto the screen.
printf(''%f is your weight\n'', w);
In the above program statement:
''%f is your weight\n'' is the control string
w is the variable to be printed
%f meaning that a floating point value is to be printed.
The number of conversion specifications and the number of variables following the control string must be same, and that the conversion character is correct for the type of the parameter.
4.1.2 Formatted Input - scanf
scanf is used to get input from user and to store it in the specified variable(s).
scanf(''%d'', &x);
read a decimal integer from the keyboard and store the value in the memory address of the variable x.
4.1.3 Character Escape Sequences
There are several character escape sequences which can be used in place of a character constant or within a string.
\a alert (bell)
\b backspace
\f formfeed
\n newline
\r carriage return
\t tab
\v vertical tab
\ backslash
\? question mark
\' quote
\'' double quote
\ooo character specified as an octal number
\xhh character specified in hexadecimal.
Getchar and Putchar:
getchar and putchar are used for the input or output only one character.
getchar()
It returns an int which is either EOF(indicating end-of-file) or the next character in the standard input stream.
putchar(c)
puts the character on the output stream.
int main()
{
int a;
a = getchar();
/* read and assign character to c */
putchar(a);
/* print c on the screen */
return 0;
}
Conditional branches: if ->
5.1.1 if statement
An if statement contains a Boolean expression and block of statements enclosed within braces.
Structure of if statement :
if (boolean expression )
/* if expression is true */
statements... ; /* Execute statements */
If the Boolean expression is true then statement block is executed otherwise (if false) program directly goes to next statement without executing Statement block.
5.1.2 if...else statement
If statement block with else statement is known as as if...else statement. Else portion is non-compulsory.
Structure of if...else
if(condition)
{
statements...
}
else
{
statements...
}
If the condition is true, then compiler will execute the if block of statements, if false then else block of statements will be executed.
We can use multiple if-else for one inside other this is called as Nested if-else.
Conditional Selection: switch ->
A switch statement is used instead of nested if...else statements. It is multiple branch decision statement of C.A switch statement tests a variable with list of values for equivalence. Each value is called a case.
The case value must be a constant integer.
Structure of switch() statement :
switch (expression)
{
case value: statements...
case value: statements...
default : statements...
}
Individual case keyword and a semi-colon (:) is used for each constant. Switch tool is used for skipping to particular case, after jumping to that case it will execute all statements from cases beneath that case this is called as ''Fall Through''.
In the example below, for example, if the value 2 is entered, then the program will print two one something else!
int main()
{
int i;
printf(''Enter an integer: '');
scanf(''%d'',&i); switch(i)
{
case 4: printf(''four ''); break;
case 3: printf(''three ''); break;
case 2: printf(''two '');
case 1: printf(''one '');
default: printf(''something else!'');
}
return 0;
}
To avoid fall through, the break statements are necessary to exit the switch. If value 4 is entered, then in case 4 it will just print four and ends the switch.
The default label is non-compulsory, It is used for cases that are not present.
Loops: for and while loop:
The while loop calculates the expression before every loop. If the expression is true then block of statements is executed, so it will not execute If the condition is initially false. It needs the parenthesis like the if statement.
while ( expression )
/* while expression is true do following*/
statements... ;
Do While loop
This is equivalent to a while loop, but it have test condition at the end of the loop. The Do while loop will always execute at least once.
do
statements ;
while ( expression );
/* while expression is true do...*/
For loop
This is very widely held loop.
For loops work like the corresponding while loop shown in the above example. The first expression is treated as a statement and executed, then the second expression is test or condition which is evaluated to see if the body of the loop should be executed. The third expression is increment or decrement which is performed at the end of every iteration/repetition of the loop.
for (expr1; expr2; expr3)
statements...;
In while loop it can happen that the statement will never execute But In the do-while loop, test condition is based at the end of loop therefore the block of statement will always execute at least once. This is the main difference between the while and the do-while loop.
For example, to execute a statement 5 times:
for (i = 0; i < 5; i++)
printf(''%d\\n'',i);
Another way of doing this is:
i = 5;
while (i--)
statements;
While using this method, make sure that value of i is greater than zero, or make the test i-->0.
Local jumps: goto
We can jump to any statement inside the same function using goto. To spot the end point of the jump a label (tag) is used. goto statement is not to ideal to choose in any programming language because it makes difficult to trace the flow of a program, makes the program hard to understand, and to guess the output.
void any_function(void)
{
for( ... )
if (problem) goto error;
error:
solve problem
}
But in the example above, remember that the code could be rewritten as:
void function1(void)
{
for( ... )
if (problem)
{
solve problem
return;
}
}
Break and Continue:
Break:
Break statement is usually used to terminate a case in the switch statement.
Break statement in loops to instantly terminates the loop and program control goes to the next statement after the loop.
If break statement is used in nested loops (i.e., loop within another loop), the break statement will end the execution of the inner loop and Program control goes back to outer loop.
Syntax :
break;
5.5.2 Continue statement
In C programming language the continue statement works slightly similar to the break statement. The continue restarts the loop with the next value of item. All the line code below continue statement is skips.
Syntax :
continue;
In the for loop, continue statement skips the test condition and increment value of the variable to execute again and In the while and do...while loops, continue skips all the statements and program control goes to at the end of loop for tests condition.
Function Blocks:
6.1.1 Building Blocks of Programs
Functions like main, printf and scanf, We have already come through.
All C programs made up of one or more functions. There must be one and only one main function. All functions are at the same level - there is no nesting.
6.1.2 Return Value
Including main All functions can return a value,.
Void Return type is specified if fuction is returning no value. Functions can return arithmetic values (int, float etc.), pointers structures, unions, or will not return anything (void) But they cannot return an array or a function.
6.1.3 Function Parameters
Any function (as well as main) can receive some values called parameters. While calling a function we must pass values of parameters.
Format of Function :-
< return_type> < function_name>(parameters...)
{
}
Only Values of the parameters to the function at the time of calling it
If, In definition of function contains void as parameter then function will not accept any parameter.
Calling the expressions specified as arguments in a function call and the variables listed as parameters in the function definition is very usual. For example, in the following call of function, the expressions x and y*2 are the arguments passed to the function. The values of the x and y will be copied into the parameters p and q.The variable given in the function definition are called as formal argument and the expression given in the function call are called as the actual argument .
cal_area(a, b*2);
Definition And Declaration:
A function definition contains function name, parameters, its code and return type and A function declaration contains only name and return type.
User can define a function only once but it can be declared several times.
declaration of function
Syntax :
< return_type> < function_name>(arguments... );
/* Declaration of area() */
int area(int x, int y);
int main()
{
int x=10, y=25; printf(''%d\n'',area(x,y)));
return 0;
}
Definition of function
syntax
< return_type> < function_name>(arguments)
{
Body of function;
}
/* Definition of area() */
int area(int x, int y)
{
int z;
z = x*y;
retrun z;}
Standard Header Files:
standard header files contains Prototypes of the library functions. For example, math.h contains prototypes for mathematical operations.
some standard header files are:
stdio.h printf, scanf, getchar, putchat etc.
stdlib.h utility functions; number conversions, memory allocation, exit and system , Quick Sort
float.h system limits for floating point types
math.h mathematical functions
string.h string functions
assert.h assertions
ctype.h character class tests
time.h date and time functions
limits.h system limits for integral types
setjmp.h non-local jumps
signal.h signals and error handling
stdarg.h variable length parameter lists
Use the #include (preprocessor directive) and give angle brackets around the name of the file to include these standard header files in your program.
Blocks and Scope:
C is a block structured language. Blocks are enclosed by { and }. Blocks can be defined wherever a C statement could be used. No semi-colon is required after the closing brace of a block.
Variable Scope
refers to where variables is declared.
Global variables
Global variable are declared outside any functions, usually at top of program. they can be used by later blocks of code:
int g;
int main(void)
{
g = 0;
}
Local variables
Variables that are declared inside a function or block are local variables.The scope of local variables will be within the function only.These variables are declared within the function and can't be accessed outside the function.
void main()
{
int g;
g=2;
printf(''g= %d'',&g);
}
Variables storage classes:
The default class. Automatic variables are local to their block. Their storage space is reclaimed on exit from the block.
register
If possible, the variable will be stored in a processor register. May give faster access to the variable. If register storage is not possible, then the variable will be of automatic class.
Use of the register class is not recommended, as the compiler should be able to make better judgement about which variables to hold in registers, in fact injudicious use of register variables may slow down the program.
static
On exit from block, static variables are not reclaimed. They keep their value. On re-entry to the block the variable will have its old value.
extern
Allows access to external variables. An external variable is either a global variable or a variable defined in another source file. External variables are defined outside of any function. (Note: Variables passed to a function and modified by way of a pointer are not external variables)
static external
External variables can be accessed by any function in any source file which make up the final program. Static external variables can only be accessed by functions in the same file as the variable declaration.
Definition and Decleration:
Definition is the place where variable is created (allocated storage).
Declaration is a place where nature (type) of variable is stated, but no storage is allocated.
Initialization means assigning a value to the variable.
Variables can be declared many times, but defined only once. Memory space is not allocated for a variable while declaration. It happens only on variable definition.
Variable declaration
syntax
data_type variable_name;
example
int a, b, c;
char flag;
Variable initialization
syntax
data_type variable_name = value;
example
int a = 50;
char flag = 't';
Initializing a Variable:
Initialization means assigning a value to the variable.
If variables are not explicitly initialised, then external and static variables are initialised to zero; pointers (see ch 8) are initialised to NULL ; auto and register variables have undefined values.
int x = 1;
char quote = '\'';
long day = 60 * 24;
int len = strlen(s);
external and static
initialisation done once only.
auto and register
initialisation done each time block is entered.
external and static variables cannot be initialised with a value that is not known until run-time; the initialiser must be a constant expression.
Arrays:
Array is a collection of similar data type items. Arrays are used to store group of data of same datatype. Arrays can of any datatype. Arrays must have constant size. Continuous memory locations are used to store array. Array index always starts with 0.
Example for Arrays:
int a[5]; // integer array
char a[5]; // character(string) array
Types of Arrays:
# One Dimensional Array
# Two Dimensional Array
# Multi Dimensional Array
8.1.1 One Dimensional Array
Array declaration
int age [5];
Array initialization
int age[5]={0, 1, 2, 3, 4, 5};
Accessing array
age[0]; /*0_is_accessed*/
age[1]; /*1_is_accessed*/
age[2]; /*2_is_accessed*/
8.1.2 Two Dimensional Array
Two dimensional array is combination of rows n columns.
Array declaration
int arr[2][2];
Array initialization
int arr[2][2] = {{1,2}, {3,4}};
Accessing array
arr [0][0] = 1;
arr [0][1] = 2;
arr [1][0] = 3;
arr [1][1] = 4;
8.1.3 Multi Dimensional Array
C programming language allows programmer to create arrays of arrays known as multidimensional arrays.
For example:
float a[2][4][3];
8.1.4 Passing Array To Function
In C we can pass entire Arrays to functions as an argument.
For eg.
#include <stdio.h>
void display(int a)
{
int i;
for(i=0;i < 4;i++){
printf("%d",a[i]);
}
}
int main(){
int c[]={1,2,3,4};
display(c);
//Passing array to display.
return 0;
}
See Programs section of this app for example of Arrays.
Pointers:
Pointer is a variable that stores the address of another variable. They can make some things much easier, help improve your program's efficiency, and even allow you to handle unlimited amounts of data.
C Pointer is used to allocate memory dynamically i.e. at run time.The variable might be any of the data type such as int, float, char, double, short etc.
Syntax :Pointers require a bit of new syntax because when you have a pointer, you need the ability to both request the memory location it stores and the value stored at that memory location.
data_type *ptr_name;
Example :
int *a; char *a;
Where, * is used to denote that ''a'' is pointer variable and not a normal variable.
Key points to remember about pointers in C:
# Normal variable stores the value whereas pointer variable stores the address of the variable.
# The content of the C pointer always be a whole number i.e. address.
# Always C pointer is initialized to null, i.e. int *p = null.
# The value of null pointer is 0.
# & symbol is used to get the address of the variable.
# * symbol is used to get the value of the variable that the pointer is pointing to.
# If pointer is assigned to NULL, it means it is pointing to nothing.
# Two pointers can be subtracted to know how many elements are available between these two pointers.
# But, Pointer addition, multiplication, division are not allowed.
# The size of any pointer is 2 byte (for 16 bit compiler).
Example program for pointer in C:
#include
int main()
{
int *ptr, q;
q = 50;
/* address of q is assigned to ptr */
ptr = &q;
/* display q's value using ptr variable */
printf("%d", *ptr);
return 0;
}
NULL Pointers
It is always a good practice to assign a NULL value to a pointer variable in case you do not have exact address to be assigned. This is done at the time of variable declaration. A pointer that is assigned NULL is called a null pointer.Eg : int *ptr = NULL;
The value of ptr is 0
Pointer Arithmetic
As you understood pointer is an address which is a numeric value; therefore, you can perform arithmetic operations on a pointer just as you can a numeric value. There are four arithmetic operators that can be used on pointers: ++, --, +, and -.
Example :
ptr++;
ptr--;
ptr+21;
ptr-10;
If a char pointer pointing to address 100 is incremented (ptr++) then it will point to memory address 101
Pointers vs Arrays
Pointers and arrays are strongly related. In fact, pointers and arrays are interchangeable in many cases. For example, a pointer that points to the beginning of an array can access that array by using either pointer arithmetic or array-style indexing.
int main ()
{
int var[3] = {1, 2, 3};
int *ptr;
printf("%d \n",*ptr);
ptr++;
printf("%d \n",*ptr);
return 0;
}
this code will return :
1
2
Pointer to Pointer
A pointer to a pointer is a form of multiple indirection or a chain of pointers. Normally, a pointer contains the address of a variable. When we define a pointer to a pointer, the first pointer contains the address of the second pointer, which points to the location that contains the actual value.int main ()
{
int var;
int *ptr;
int **pptr;
var = 3000;
ptr = &var;
pptr = &ptr;
printf("Value of var :%d \n", var);
printf("Value available at *ptr :%d \n",*ptr);
printf("Value available at **pptr :%d\n",**pptr);
return 0;
}
this code will return
Value of var :3000
Value available at *ptr :3000
Value available at **pptr :3000
Strings:
A string is simply an array of characters which is terminated by a null character '\0' which shows the end of the string.
Strings are always enclosed by double quotes. Whereas, character is enclosed by single quotes in C.
This declaration and initialization create a string with the word "AKKI".
To hold the \0 (null character) at the end of the array, the size of array is one more than the number of characters in the word "AKKI".
char my_name[5] = {'A', 'K', 'K', 'I','\0'};
we can also write the above statement as follows:
char my_name[] = "AKKI";
C String functions:
To use string functions programmer must import String.h header file.
String.h header file supports all the string functions in C language.
All the string functions are given below.
1 strcat ( ) Concatenates str2 at the end of str1.
2 strncat ( ) appends a portion of string to another
3 strcpy ( ) Copies str2 into str1
4 strncpy ( ) copies given number of characters of one string to another
5 strlen ( ) gives the length of str1.
6 strcmp ( ) Returns 0 if str1 is same as str2. Returns 0 if str1 > str2.
7 strcmpi ( ) Same as strcmp() function. But, this function negotiates case. ''A'' and ''a'' are treated as same.
8 strchr ( ) Returns pointer to first occurrence of char in str1.
9 strrchr ( ) last occurrence of given character in a string is found
10 strstr ( ) Returns pointer to first occurrence of str2 in str1.
11 strrstr ( ) Returns pointer to last occurrence of str2 in str1.
12 strdup ( ) duplicates the string
13 strlwr ( ) converts string to lowercase
14 strupr ( ) converts string to uppercase
15 strrev ( ) reverses the given string
16 strset ( ) sets all character in a string to given character
17 strnset ( ) It sets the portion of characters in a string to given character
18 strtok ( ) tokenizing given string using delimiter
Structure:
In Array we can store data of one type only, but structure is a variable that gives facility of storing data of different data type in one variable.
Structures are variables that have several parts; each part of the object can have different types.
Each part of the structure is called a member of the structure.
# Declaration
Consider basic data of student :
roll_no, class, name, age, address.
A structure data type called student can hold all this information:
struct student {
int roll_no
char class
char name[25];
int age;
char address[50];
};
before the final semicolon, At the end of the structure's definition, we can specify one or more structure variables.
There is also another way of declaring variables given below,
struct student s1;
# Initialization
Structure members can be initialized at declaration. This is same as the initialization of arrays; the values are listed inside braces. The structure declaration is preceded by the keyword static.
static struct student surya ={1234,''comp'',''surya'',20,''mumbai''};
# Accessing structure data
To access a given member the dot notation is use. The ''dot'' is called the member access operator
struct student s1;
s1.name = ''Surya'';
s1.roll_no = 1234
# scope
A structure type declaration can be local or global, depending upon where the declaration is made.
Union:
C Union is also like structure, i.e. collection of different data types which are grouped together. Each element in a union is called member.
Union and structure both are same, except allocating memory for their members. Structure allocates storage space for each member separately. Whereas, Union allocates one common storage space for all its members.
Syntax
union tag_name
{
data type var_name1;
data type var_name2;
data type var_name3;
};
Example
union student
{
int id;
char name[10];
char address[50];
};
Initializing and Declaring union variable
union student data;
union student data = {001,''AKKI'', ''mumbai''};
Accessing union members
data.id
data.name
data.address
File operations and Functions:
10.1.1 File Pointers
It is not enough to just display the data on the screen. We need to save it because memory is volatile and its contents would be lost once program terminated, so if we need some data again there are two ways one is retype via keyboard to assign it to particular variable, and other is regenerate it via programmatically both options are tedious. At such time it becomes necessary to store the data in a manner that can be later retrieved and displayed either in part or in whole. This medium is usually a ''file'' on the disk.
Introduction to file
Until now we have been using the functions such as scanf,printf, getch, putch etc to read and write data on the variable and arrays for storing data inside the programs. But this approach poses the following programs.
1. The data is lost when program terminated or variable goes out of scope.
2. Difficulty to use large volume of data.
We can overcome these problems by storing data on secondary devices such as Hard Disk. The data is storage on the devices using the concept of ''file''.
A file is collection of related records, a record is composed of several fields and field is a group of character.
The most straightforward use of files is via a file pointer.
FILE *fp;
fp is a pointer to a file.
The type FILE, is not a basic type, instead it is defined in the header file stdio.h , this file must be included in your program.
10.1.2 File Operations
1. Create a new file.
2. Open an existing file
3. Read from file
4. Write to a file
5. Moving a specific location in a file(Seeking)
6. Closing File
10.1.3 Opening a File
fp = fopen(filename, mode);
The filename and mode are both strings.
Here modes can be
"r" read
"w" write, overwrite file if it exists
"a" write, but append instead of overwrite
"r+" read & write, do not destroy file if it exists
"w+" read & write, but overwrite file if it exists
"a+" read & write, but append instead of overwrite
"b" may be appended to any of the above to force the file to be opened in binary mode rather than text mode.
Eg.
FILE *fp;
fp=fopen(''input.txt'',''r'');
//Opens inputs.txt file in read mode
fclose(fp); //close file
Sequential file access is performed with the following library functions.
1. fopen() - Create a new file
2. fclose() - Close file
3. getc() - Read character from file
4. putc() - Write character to a file
5. getw() - Read Integer from file
6. putw() - Write Integer to a file
7. fprintf() - Write set of data values
8. fscanf() - Read set of data values
C Preprocessor directives:
Before a C program is compiled in a compiler, source code is processed by a program called preprocessor. This process is called preprocessing.
Commands used in preprocessor are called preprocessor directives and they begin with ''#'' symbol. Below is the list of preprocessor directives that C language offers.
1 Macro
syntax:
#define
This macro defines constant value and can be any of the basic data types.
2 Header file inclusion
syntax:
#include <file_name>
The source code of the file ''file_name'' is included in the main program at the specified place
3 Conditional compilation
syntax:
#ifdef, #endif, #if, #else, #ifndef
Set of commands are included or excluded in source program before compilation with respect to the condition
4 Other directives
syntax:
#undef, #pragma
#undef is used to undefine a defined macro variable.
#Pragma is used to call a function before and after main function in a C program.
######## Glossary :
argument: A value passed to a function when it is called.
block: Sequence of statements enclosed in curly braces.
buffer: A region of storage used to hold data. IO facilities often store input (or output) in a buffer and read or write the buffer independently of actions in the program. Output buffers usually must be explicitly flushed to force the buffer to be written. By default, reading cin flushes cout; cout is also flushed when the program ends normally.
built-in type: A type, such as int, defined by the language.
cerr: ostream object tied to the standard error, which is often the same stream as the standard output. By default, writes to cerr are not buffered. Usually used for error messages or other output that is not part of the normal logic of the program.
cin:iistream object used to read from the standard input.
class: C++ mechanism for defining our own data structures. The class is one of the most fundamental features in C++. Library types, such as istream and ostream, are classes.
class type: A type defined by a class. The name of the type is the class name.
clog: ostream object tied to the standard error. By default, writes to clog are buffered. Usually used to report information about program execution to a log file.
comments: Program text that is ignored by the compiler. C++ has two kinds of comments: single-line and paired. Single-line comments start with a //. Everything from the // to the end of the line is a comment. Paired comments begin with a /* and include all text up to the next */.
condition: An expression that is evaluated as true or false. An arithmetic expression that evaluates to zero is false; any other value yields true.
cout: ostream object used to write to the standard output. Ordinarily used to write the output of a program.
curly brace: Curly braces delimit blocks. An open curly ({) starts a block; a close curly (}) ends one.
data structure: A logical grouping of data and operations on that data.
edit-compile-debug: The process of getting a program to execute properly.
end-of-file: System-specific marker in a file that indicates that there is no more input in the file.
expression: The smallest unit of computation. An expression consists of one or more operands and usually an operator. Expressions are evaluated to produce a result.
for statement: Control statement that provides iterative execution. Often used to step through a data structure or to repeat a calculation a fixed number of times.
function: A named unit of computation.
function body: Statement block that defines the actions performed by a function.
function name: Name by which a function is known and can be called.
header: A mechanism whereby the definitions of a class or other names may be made available to multiple programs. A header is included in a program through a #include directive.
if statement: Conditional execution based on the value of a specified condition. If the condition is true, the if body is executed. If not, control flows to the statement following the else if there is one or to the statement following the if if there is no else.
iostream: Library type providing stream-oriented input and output.
istream: Library type providing stream-oriented input.
library type: A type, such as istream, defined by the standard library.
main function: Function called by the operating system when executing a C++ program. Each program must have one and only one function named main.
manipulator: Object, such as std::endl, that when read or written "manipulates" the stream itself.
member function: Operation defined by a class. Member functions ordinarily are called to operate on a specific object.
method: Synonym for member function.
namespace: Mechanism for putting names defined by a library into a single place. Namespaces help avoid inadvertent name clashes. The names defined by the C++ library are in the namespace std.
ostream: Library type providing stream-oriented output.
parameter list: Part of the definition of a function. Possibly empty list that specifies
No comments:
Post a Comment