0% found this document useful (0 votes)
3 views

Fresh Pap 2 -Programming Languages

Uploaded by

arunaikshiva
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views

Fresh Pap 2 -Programming Languages

Uploaded by

arunaikshiva
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 28

C CHARACTER SET VARIABLES / EXPRESSIONS / STATEMENT / STATEMENT BLOCKS CONSTANTS / DATA TYPES / SCIENTIFIC NOTATIONS / MODIFIERS

Syntax / Commenting Code / Creating Executable Programs / Hello World - First C Program Variables Types of Constants
/ #include Directive / Header Files / Main Functions Variables are like containers in the computer's memory – one can store values in them and retrieve or modify Numbers are considered as LITERAL constants – It is not possible to change the number 20, nor assign

The C character set consists of alphabet, digit or special symbol, that is a to z, A to Z, 0 to 9 and some special them when necessary. something else into 20.
characters like underscore, hash, asterisk etc. Constants are like variables, but once a valued is stored (i.e. the constant is INITIALIZED), its value cannot be On the other hand, SYMBOLIC constants can be assigned a value during initialization and are represented by a

These basic characters are used to form constants, variables and keywords, equivalent to words in English. changed. word.

Keywords are words whose meaning has already been explained to the compiler or the computer. External Variable There are several ways to define constants in C. The const keyword is used to define a constant.

There are about 32 keywords in C and they are: When a variable is declared outside the scope of any function that is outside the main function, then the variable Suppose there is a circle with a fixed radius of 5 units. It is possible to create a symbolic constant like this:

Auto Double If Static Break Else Int is called as a global variable. Such variables are called global variables, and the C language provides storage const int radius = 5;

Struct Case Enum Long Switch Char Extern classes which can meet these requirements; namely, the external and static classes. Since radius is declared using the const keyword, statements like: radius = 12; would be illegal.

Near typedef Const Float Register Union Continue Memory for such variables is allocated when the program begins execution, and remains allocated until the Instructions
Far Return Unsigned Default For Short Void program terminates. The scope of external variables is global, i.e. the entire source code in the file following the Instructions can be classified into four different types based on their purpose:
Do Goto Signed While Sizeof Volatile declarations. All functions following the declaration may access the external variable by using its name. However, 1. Type Declaration 2. Arithmetic instruction
Syntax if a local variable having the same name is declared within a function, references to the name access the local 3. Input / Output instruction 4. Control instruction

The C code written is called the SYNTAX. variable cell. The 4 Data Types
Syntax is a mixture of: Example: Introduction
C keywords like int, for and return. int x,y;
In C, there are four data types: char, int, float, and double. Each one has its own properties. For instance, they
Constants and variables. float f;
all have different sizes. The size of a variable can be pictured as the number of containers / memory slots that
Operators like + (arithmetic "addition"), || (logical "or") and & (the "address of" operator). char c;
are required to store it.
Note that C is CASE SENSITIVE! For example, words like cat, Cat, cAt and CAT are all considered different from main()
The char Data Type
one another. {
Variables of the char data type can store a single character from a set of 256 characters.
Also, the amount of "white space" used in a C program does not affect the way it's compiled. Use extra spaces }
All of the characters on a keyboard have a unique numerical code associated with it, so in reality, numerical
can be used to make the programs more readable - indentation of code is very common. It is not allowed to put We can also pre-initialize the variable as follows,
codes are stored into char variables. The set of codes are known as ASCII codes, where ASCII stands for
spaces or line breaks in the middle of keywords like this: str uct int x=4,y=3;
"American Standard Code for Information Interchange" and is usually pronounced "ask-ee".
Compilers float f=7.23;
Declaring and Initializing Variables
There are many compilers available for C like Borland C++ compiler, Microsoft's Visual C++ 6. 0, which is a char c=’Y’;
To DECLARE a variable, means to reserve memory space for it.
powerful package and is suitable for people who are interested in software engineering. MS VC++ is a little main()
Declaring a variable involves inserting a variable name after a data type. It is also possible to declare many
complex to use. In general, C++ packages are fine for compiling C programs - the compiler executed depends {
variables of the same data type all on one line by separating each one with a comma.
on the extension of the source file(s). There are also UNIX-based compilers. }
INITIALIZING a variable involves assigning (putting in) a value for the first time. This is done using the
Commenting Code Static Variable
ASSIGNMENT OPERATOR, denoted by the equals sign, =.
It is possible to add comments to the code by enclosing the remarks within /* and */. However, nested comments static variable can’t be reinitialized and it is the default storage class for global variables. The two variables below
Declaration and initializing can be done on separate lines, or on one line.
aren't allowed. (x and y) both have a static storage class.
Initializing char Variables
A few properties of comments: static int x;
To store a character into a char variable, it should be enclosed with SINGLE quote marks. Double quote marks
They can be used to inform the person viewing the code what the code does. This is helpful int y;
are reserved for STRINGS (an ARRAY of characters).
when revisiting the code at a later stage. {
Strings and arrays are covered in the later sections. The characters assigned are CHARACTER CONSTANTS.
The compiler ignores all the comments. Hence, commenting does not affect the efficiency of the printf("%d\n", y);
So for the example, 'H' is a character constant.
program. }
It is also possible to assign a char variable an integer, that is, the ASCII code.
One can use /* and */ to comment out sections of code when it comes to finding errors, instead static variables can be 'seen' within all functions in this source file. At link time, the static variables defined here
This example should help clarify the declaration and initialization of char variables. Ignore the printf function for
of deletion. will not be seen by the object modules that are brought in.
now.
Here are examples of commented code: 'static' can also be defined within a function! If this is done the variable is initialized at run time but is not reinitalized
when the function is called.. The int Data Type
/* Comments spanning several */ /* lines can be commented*/
{ Variables of the int data type represent whole numbers. If a fraction is assigned to an int variable, the decimal
/* out like this!*/ /* But this is a simpler way of doing it! */
static x=1; part is ignored and the value assigned is rounded down (or TRUNCATED) from the actual value. Also, assigning
// These are C++ // style comments
} a character constant to an int variable assigns the ASCII value.
// and should NOT // be used with C!!
/* /* NESTED COMMENTS ARE ILLEGAL!! */ */ Local Variable The float Data Type
Creating Executable Programs Local variable is the default storage for a variable in ‘C’ language. Its also called as automatic variables and To store variables correct to six decimal places, the float data type can be used.

represented by the keyword ‘auto’. It should be declared at the start of the block. Floats are relatively easy to use but problems tend to occur when performing division. In general:
There are several tasks that need to be performed before running a program like coding, compiling and linking.
When the program execution enters the block, memory is automatically allocated for this variable and released An int divided by an int returns an int.
1. First thing to do is to write the source code in C. Declare which header files are needed to be included within
automatically upon exit from the block. The scope of automatic variable is local to the block in which they are An int divided by a float returns a float.
the source code. The source code must be saved with the extension .c.
declared. And they are so called local variable. We don’t have direct access to local variables from outside the A float divided by an int returns a float.
2. Then run the compiler, which translates the source code into machine, or binary code, which the computer can
block but can access them indirectly using pointers. A float divided by a float returns a float.
understand. Computers do NOT understand C!
If the result of a division is to be stored as a decimal number, make sure that it is stored in a float declared Naming Variables 3. Sometimes the source code is still lacking some parts, so after going through the compiler, the code is passed
variable. There are several rules that must be followed when naming variables: through a LINKER. This basically "links" the source code to other library or object files so that the final binary
If the decimal result of the division is to be between two integers, it is possible to use a method called COERSION Variable Names Examples code is produced.
(or CASTING). This involves placing (float) before an expression being wished to cast. CANNOT start with a number 2times Hello World - First C Program
The double Data Type CAN contain a number elsewhere times2 Type the following code into any text editor (e.g. Notepad, Emacs, SimpleText etc.) and save it with the .c
It is possible to store decimals correct to ten decimal places using the double data type. However, doubles take CANNOT contain any arithmetic operators... a*b+c extension. These MUST be saved in text only format - try and avoid word processors like MS Word.
up twice as much memory than floats, so doubles should be used when it's really necessary. ... or any other punctuation marks... #@%£!! When saving in Notepad, when entering a filename enclose it in quote marks, for example, "world.c" - this ensures
Once again, care should be taken when it comes to calculations that involve divisions - casting should be used ... but may contain or begin with an underscore _height that the filename will be saved with the correct extension, rather than world.c.txt.
when necessary. CANNOT be a C keyword While Example:
Scientific Notation CANNOT contain a space stupid me #include <stdio.h>

It is possible to express numbers in scientific notation, which is handy if the numbers get very large or small. CAN be of mixed cases HelloWorld int main()

In C, scientific notation is of the form xey - x and y should be replaced with numbers (y must be an integer). EXPRESSIONS {

Basically, xey translates to "x times 10 to the power of y". EXPRESSIONS consist of a mixture of constants, variables and operators. They return values. printf("Hello World!\n");

For example, 1.2e3 is 1.2*1000 = 1200 Here are some examples of expressions: return 0;

1.23e4 is 1.23*10000 = 12300 17 /* a constant */ }

4.5e-2 is 4.5*0.01 = 0.045 X /* a variable */ #include Directive


Type Modifiers x + 17 /* a variable plus a constant */ If a line starts with a hash, denoted by #, it tells the compiler that a command should be sent to the C

The signed and unsigned Keywords STATEMENTS PREPROCESSOR. The C preprocessor is a program that is run when compiled. #include is one of the many C

STATEMENTS are instructions and are terminated with a semicolon';'. Statements consist of a mixture of preprocessor commands.
When a variable of the type, int, by default is declared, its value is SIGNED. In other words, the variable could be
expressions, operators, function calls and various keywords. Here are some examples of statements: Basically, when the preprocessor finds #include it looks for the file specified and replaces #include with the
positive or negative.
x = 1 + 8; contents of that file. In a way, this makes the code more readable and easier to maintain if common library
The minimum value of a signed int is -32768 and the maximum value is 32767 (that is, 2^15 -1).
printf ("We will learn printf soon!\n"); functions need to be used.
An unsigned int on the other hand, can only store positive values, and has the range from 0 to 65535 (that is,
2^16 -1). int x, y, z; /* more on "int" later */ Header Files
The short and long Keywords STATEMENT BLOCKS Header files have the extension .h and the full filename follows from the #include directive. They contain

STATEMENT BLOCKS, on the other hand, can contain a group of statements. The C compiler compiles the declarations to certain functions that may or may not have been used in a program.
The cases above all apply when the integer is of the short type, which takes up less memory than the long type.
statement block as if it was just one statement. To declare a statement block statements should be enclosed For example, the stdio.h file is required if functions like printf and scanf are used in the program.
The range of values that a short int could store is somewhat limited, so if huge numbers are to be stored the long
between curly braces. There are two ways to include a header file:
type should be used. Most of the time, an integer will be of the signed short type by default.
This example has a statement block in an if-else statement. Everything in each statement block is somewhat #include "stdio.h" and

merged into a single statement. There are 2 statement blocks here: #include <stdio.h>

Example: If the double quote marks are used, it means that the directory currently in, will be searched for first for the header

if(x==10) {/* block 1 */ file, before any other directories are searched.

printf ("x equals 10\n"); If the square brackets are used, directories other than the one currently in will be searched for the header file.

x = 11; The main Function


printf ("Now x equals 11\n"); A FUNCTION can be thought of a set of instructions that are carried out when it is CALLED.
x = x + 1; All C programs must have a main function. It is allowed to have only one, but can be placed anywhere within the
printf ("Now x equals 12\n"); code.
} /* end of block 1 The program always starts with the main function and ends when the end of main is reached. Functions return a
*/ value too - this will be explained later. If a function returns nothing, its return type is of type void – that is nothing
else is returned.
{ /* block 2 */ The main function is special, as it returns an integer by default, which is why return 0; is written at the end of the
Print f("x not equal to 10\n"); program. Zero is usually returned to indicate errorfree function termination. Another way to terminate a program
Printf ("Good bye!\n"); is to use the exit.
} /* end of block 2 */
OPERATORS OPERATOR PRECEDENCE / CONTROL STRUCTURES / ELSE IF / NESTING IF
Arithmetic Operators CONDITIONAL BRANCHING / CONDITIONAL OPERATOR ELSE IF statement
Arithmetic operators are commonly used in a variety of programming languages. To test additional conditions, an else if statement can be used, which works in the same way as a normal if
Operator Precedence
In C, there are five of them, and they all take two OPERANDS. Recall that an operand is an expression that is statement. One thing to note, is that one and only one if branch in an entire if block is allowed.
Operator precedence in C is the order in which operators are evaluated in an expression:
required for an operator to work. For example, for 8 + 4, 8 and 4 are considered as the operands. The else branch is entirely optional.After the if branch (but always before the else branch if it exists), as many
Explanation
Operator Name Symbol else if branches can be used, each with their own conditions and statement blocks. Example
Operators with higher precedence are evaluated first. For example, in the expression 50 – 2 * 15, the subtraction
Multiplication * #include <stdio.h>
operator has lower precedence than the multiplication operator, so the expression is evaluated as 50 – (2 * 15).
Division / int main()
Modulus % Precedence order {
Addition + The order of operator precedence in C, from highest to lowest, is: int a;
Subtraction - Parentheses: () printf("Input an integer and push return:\n");
It is explained why the five operators are listed in this particular order... Postfix operators: ++, -- scanf("%d", &a);
The multiplication, division and modulus operators have higher PRECEDENCE over the addition and subtraction Unary operators: +, -, !, ~, ++, --, (type) if(a%2==0 && a<0)
operators. This means that if an expression contains a mixture of arithmetic operators, multiplication, division and Multiplicative operators: *, /, % {
modulus will be carried out first in a LEFT TO RIGHT order, then any addition and subtraction. Additive operators: +, - printf("%d is even and less than zero.\n", a);
Brackets (also known as PARENTHESES) can be used to change precedence, as everything enclosed within Relational operators: <, >, <=, >= }
brackets is always evaluated first. Equality operators: ==, != else if (a%2!=0 && a<0)
Example, 2*4+3 returns 11 because 2*4 is 8, and 8+3 is 11. Logical AND operator: && {
On the other hand, 2*(4+3) returns 14 because 4+3 is 7, and 2*7 is 14. Logical OR operator: || printf("%d is odd and less than zero.\n", a);
Multiplication, addition and subtraction are the simplest to use. Division is also easy, but care should be taken Assignment operators: =, +=, -= }
with the truncation of an int divided by an int. casting if necessary should be used. Now, the one that confuses Importance else if (a%2==0 && a>0)
novices is the modulus operator, sometimes known as the remainder operator. Understanding operator precedence is important for writing accurate expressions and avoiding logical errors. {
Note that anything modulus with zero returns infinity because anything divided by zero is infinite. In JavaScript, Parentheses printf("%d is even and greater than zero.\n", a);
NaN means "Not a Number" - either it's infinity, or something daft is entered. Parentheses can be used to change the order of evaluation }
Anything modulus with itself is always zero because that number goes into itself exactly. else if (a%2!=0 && a>0)
Arithmetic Assignment Operators Here is an example to explain operator precedence. Take: {

Sometimes something like this is to be written: x = x + 2; x+5 == 6 printf("%d is odd and greater than zero.\n", a);

There is a better (and efficient) way of writing expressions like these by Since + has higher precedence than ==, x+5 is evaluated first, then the result is compared with 6. }

combining the operator with an equals sign, as shown in the table. Care should be taken with these though: x *= But what about x<3 && y<7 || y==5? else

y+z is the same as x = x*(y+z) and NOT x = (x*y) + z. Suppose x is 4 and y is 5. Since the relation operators have higher precedence than the logical ones, the line {

Long Hand Short Hand can be rewritten as 0 && 1 || 1 because 4<3, 5<7 and 5==5 return 0, 1 and 1 respectively. printf("You entered zero.\n");

x = x * y; x *= y; Now, && gets evaluated first, then ||. }

x = x / y; x /= y; 0 && 1 returns 0 and 0 || 1, returns 1. return 0;

x = x % y; x %= y; This tends to get confusing, which is why brackets should be used when there are many operators on a single }

x = x + y; x += y; line. The following is easier to understand than the original: The main focus is on the if block, with the solitary if and else statements, as well as the else ifs. Now, the program

x = x – y; x -= y; (x<3 && y<7) || y==5 evaluates the if condition. If it returns a non zero value, the if branch is executed. Once all the statements in that

Increment and Decrement Operators Expressions in brackets are evaluated first. branch have been executed, THE PROGRAM IGNORES THE REST OF THE IF BLOCK. In other words, the
remaining conditions are not evaluated.
Increasing an integer variable by 1 is a common process. There are people who write statements like x+=1; or
If, on the other hand, (a%2==0 && a<0) returns zero, the first else if condition is evaluated and so on. The main
even worse, x=x+1;
CONDITIONAL OPERATOR thing to remember is that once the program chooses a branch in the if block, the remaining branches are totally
There is an easier way: x++; (POST-INCREMENT)
It's possible to say: "If this condition is true, do this.... , otherwise, do this .... " all in one line. ignored. If it is needed to say, "else do nothing", rather than missing the else statement totally, a blank statement
Alternatively, it is also allowed to use ++x; (PRE-INCREMENT)
This can be achieved using the CONDITIONAL OPERATOR, which is represented by ?: block shall be included:
They both increase x by 1 (note that x MUST be an integer), They differ when it comes to statements like these:
It may look a bit odd at first, but it takes 3 expressions as operands, like this: a ? b : c; a should be a condition to if(x==0) {
y = x++;
be tested for. b is an expression that is evaluated if a returns a non zero value (in other words, if a is true), else printf("x is zero\n");
y = ++x;
expression c is evaluated. }
If x was 5, y = x++; would assign x to y, THEN increase x by 1. The end result is that y equals 5 and x equals 6.
For example: else if(x==1){
If x was 5, y = ++x; would increase x by 1, THEN assign x to y. The end result is that y equals 6 and x equals 6.
x<1 ? printf("x<1") : printf("x>=1"); printf("x is 1\n");
Post-incrementing is done AFTER the assignment, pre-incrementing is done
Note the terms used: the 3 separate operands are expressions - the whole line is a statement. }
BEFORE the assignment.
It's a common mistake to put a semi colon in the middle expression like this: x<1 ? printf("x<1"); : printf("x>=1"); else { /* Do nothing */
Similar rules apply for decrementing. If x is to be decreased by 1 it is correct to
This will generate an error when compiled. }
write x--; (POST-DECREMENT) or --x; (PRE-DECREMENT), as opposed to: x-=1; or
Caution: DON'T put a semi colon after if, else if and else statements like this: if (x==1);
x=x-1;
NESTING IF CONTROL STRUCTURE / BRANCHING / CONDITIONAL BRANCHING The sizeof Operator
When statemnet blocaks are placed within other blocks, it is called NESTING blocks. This function enables to find out how many BYTES a variable occupies. A byte is defined as EIGHT BINARY
The same way, it is possible to place if blocks within branches of another if block. This is when the programmer DIGITS (or "8-bits"). Binary numbers are covered later. For now, think of a byte as a container in the computer's
CONDITIONAL BRANCHING - if This Happens, Do This... else Do This...
should indent and use curly brackets, as these will make the code easier to read as this example demonstrates: memory.
CONDITIONAL BRANCHING is a term that is used to describe the way program flow diverges, depending on
Example: The sizeof operator takes one OPERAND. An operand is an expression that is required for an operator to work.
certain condition(s).
#include <stdio.h> A data type can be used as an operand, to find out how much memory is required to store variables of that data
if statements play an important role in conditional branching and their usage is quite easy to understand. After
int main() { type, as demonstrated by this example:
typing if, the condition should be put in a pair of brackets, followed by a statement that the program should read
int a,b,c; Example:
if the condition expression returns a non-zero value. If multiple statements are needed, a statement block should
printf("Input two integers separated with a space and push return:\n"); #include <stdio.h>
be used. If the condition returns zero, sometimes program needs to do something else. In that case, after the if
scanf("%d %d", &a, &b); int main()
statement, an else statement should be used.
c = a + b; {
Here is an example:
if(a==b) { printf("Size of int is %d bytes\n",sizeof(int));
#include <stdio.h>
printf("%d is equal to %d.\n", a, b);
int main()
printf("And their sum is even.\n"); Relational and Logical Operators
{
}
int a; Relational Operators
else if(a<b) {
printf("Input an integer and push return:\n"); Relation operators are used to compare numerical values. Here are the six relation operators:
printf("%d is less than %d.\n", a, b);
scanf("%d", &a); Operator Name Symbol Operator Name Symbol
if(c%2==0) {
if (a%2==0 && a%5==0) Less than < Less than or equal to <=
printf("And their sum is even.\n");
{ /* Start of if block */ Greater than > Greater than or equal to >=
}
printf("%d is a multiple of 2 and 5\n", a); /* This is the if branch */ Equal to == Not equal to !=
else {
} The top four have higher precedence than the "equal to" and "not equal to" operators.
printf("And their sum is odd.\n");
else All arithmetic operators have higher precedence than the relation operators. The "equal to" operator should not
}
{ be confused with the assignment operator.
}
printf("%d is not a multiple of both 2 and 5\n", a); /* This is the else branch */ Logical Operators
else {
} /* End of if block */ Conditions are expressions that can return one of two values: 1 (true) or 0 (false).
printf("%d is greater than %d.\n", a, b);
return 0; Any non-zero value is considered true, where as zero is thought of as false.
if(c%2==0) {
} Experiment with the relation operators:
printf("And their sum is even.\n");
The program output for this example will depend on the value entered. The logical operators can be used to test for more conditions.
}
This example covers recent material, like using the printf and scanf functions, relation and logical operators, and The first one is the NOT operator (some call it negation). It is denoted by the ! symbol and takes one expression
else {
finally the if and else statements. as its operand.
printf("And their sum is odd.\n");
The printf functions are simple enough, but scanf reads in an integer (due to the %d format specifier), and assigns Then there is the AND operator, represented by &&, and the OR operator, denoted by || (double broken vertical
}
it to the int declared variable, a. bar symbols - shift backslash on the keyboard). Both of these operators require two expressions as their
}
a%2==0 && a%5==0 is the condition part of the if statement - it is surrounded by a pair of brackets. This can be operands.
return 0;
made more readable by adding more brackets like this: Now, their return values depend on the TRUTH VALUES of their operands – do they return 1 or 0? Logical
}
if ((a%2==0) && (a%5==0)) operators are commonly summarized using TRUTH
Caution: The extra white space should be used, as indentation will make the code easier to read, especially when
Onto the condition itself... recall that a%2 returns the remainder of the division of a by 2. If zero is returned, then TABLES, which lists all the different truth combinations of the operands and the final
there are lots of if and else statements used. Also, lining up the curly brackets will help when it comes to finding
a must be a multiple of 2. Similarly, a%5 returns zero if a is a multiple of 5. Now, a%2==0 returns 1 (true) if a%2 logical operator truth-value. NOT reverses the truth value of its operand:
and fixing errors (a process known as DEBUGGING).
is equal to 0. Similar story for a%5==0. AND returns 1 if both operands return non zero values (like 1):
a%2==0 && a%5==0 returns 1 if BOTH operands of the && operator return 1. In that case, the if branch is OR only returns 0 if both operands return zero:
executed, and the else branch is ignored. NOT has higher precedence than AND, which has higher precedence than OR.
If a%2==0 && a%5==0 returns 0, the else branch is executed and the if branch is ignored.
Notice how curly brackets are added, despite having only one statement after the if and else statements. This
makes the code more readable in some ways.
The following would've worked fine:
if (a%2==0 && a%5==0)
printf("...");
else
printf("...");
SWITCH STATEMENT LOOPING / WHILE LOOPS / DO..WHILE LOOPS LOOPING / GOTO STATEMENT / FOR LOOP
There are times when need arises to write a huge if block that consists of many else if statements. The Concept of Looping The Concept of Looping
The switch statement can help simplify things a little. It allows to test the value returned by a single expression The term "looping" describes the way in which the program executes statements over and over again, before The term "looping" describes the way in which the program executes statements over and over again, before
and then execute the relevant bit of code. As many cases needed, can be used, including a default case, which exiting the loop and continuing with program flow. exiting the loop and continuing with program flow.
is evaluated if all the cases fail. This the general form... In C, there are three types of loops: for loops, while loops and do while loops. It is possible to simulate each type In C, there are three types of loops: for loops, while loops and do while loops. It is possible to simulate each type
by writing code using other loops. by writing code using other loops.
switch (expression) { There is a fourth kind of loop but has no specific keyword. INFINITE LOOPS can cause the computer to hang. There is a fourth kind of loop but has no specific keyword. INFINITE LOOPS can cause the computer to hang.
case expression1: They sometimes end after a while, but in theory they should go on forever. They sometimes end after a while, but in theory they should go on forever.
/* one or more statements */ while Loops goto Statement
case expression2:
while loops are simpler to use, since they only require one expression in the brackets: It is not advisable to use goto repeatedly and in larger programs.
/* one or more statements */
while (expression) The goto keyword is followed by a label, which is basically some identifier placed elsewhere in the program - like
/* ...more cases if necessary */
{ a hyperlink that points to a place on the same web page.
default:
/* statements */ #include <stdio.h>
/* do this if all other cases fail */
} int main() {
}
The loop body is executed if expression returns a non-zero value. Most of the time, some sort of condition to be int attempt, number = 46;
The program will branch off depending on what is returned by the expression in the parentheses. However, all is
evaluated is used. looping: /* a label */
not what it seems.
It is mentioned earlier on that it is possible simulate one type of loop with another type - there is no unique way printf("Guess a number from 0 to 100\n");
to loop. scanf("%d", &attempt);
Examine the output of this example:
The previous example can be rewritten using a while loop. if(number==attempt) {
#include <stdio.h>
#include <stdio.h> printf("You guessed correctly!\n\n");
int main()
int main() }
{
{ else {
int a;
int i=10; /* initialize variables */ printf("Let me ask again...\n\n");
printf(“Pick a number from 1 to 4\n”);
int j=0; /* part a of a for loop */ goto looping; /* Jump to the label*/
scanf("%d", &a);
while (i!=j) { /* test for condition }
switch (a) {
part b of for loop */ eturn 0;
case 1:
printf("%d - %d = %d\n", i, j, i-j); }
printf("You chose number 1\n");
i--; /* do something to variables */ for Loop
case 2:
j++; /* part c of for loop */ Here's the basic form of for loop:
printf("You chose number 2\n");
} for (a ; b ; c) {
case 3:
return 0; /* statements */
printf("You chose number 3\n");
} }
case 4:
do while Loops Brief example:
printf("You chose number 4\n");
It's almost identical to a while loop, except the loop body is executed at least once - the while (expression) part for(i=10, j=0 ; i!=j ; i--, j++) {
default:
is after the loop body, like this: printf("%d - %d = %d\n", i, j, i-j);
printf("That's not 1,2,3 or 4!\n");
do { }
}
/* loop body */ a, b and c are expressions that are evaluated at different times. a is evaluated once only - before entering the
return 0;
} while (expression); loop. It is a very good place to assign values to variables. If values are to be assigned to multiple variables, each
}
This example prints out the numbers 0-9: assignment can be separated with a comma like this: i=10, j=0. b determines whether or not the program loops.
Don't forget the semi colon after the expression part! If b returns a non-zero value, an iteration of the loop is performed. In other words, the program enters the loop
(Suppose if 2 is entered ...)
If the example is run again, but initializes i with 11 the loop body is still executed. and executes the statements inside. b should therefore be some sort of condition. Logical operators can be used
Pick a number from 1 to 4:
Simulating a do while loop with either a for or while loop is not advisable – most of the time the statements are in the condition as usual.
2
written in the loop body once, before the loop: C is evaluated after an iteration of the loop, i.e. after all the statements inside the loop body have been executed.
You chose number 2
#include <stdio.h> If there are multiple expressions evaluating, each one can be separated with a comma like this: i--, j++
You chose number 3
int main() The round brackets and semi colons are required at all times. The three expressions aren't compulsory, but
You chose number 4
{ omitting b and failing to place a break statement somewhere in the loop can result in an infinite loop!
That's not 1,2,3 or 4!
int I = 0; Here is a proper example:
Here the program will select the correct case but will also run through all the cases below it (including the default)
do #include <stdio.h>
until the switch block's closing bracket is reached. To prevent this from happening, another statement needs to
{ int main()
be inserted into the cases...
printf("%d", i); {
i++; int i,j;
for(i=10, j=0 ; i!=j ; i--, j++) } while (i<10); Keywords
{ return 0;
Keywords – Predefined meaning
printf("%d - %d = %d\n", i, j, i-j); }
Comments -- Ignored by the compiler
} #include <stdio.h>
Variable – Name given to the memory location
return 0; int main()
Expression – Sequence of operands and operators
} {
Statement – Combination of c tokens
Before entering the loop, i is initialized with 10 and j with 0. Here is the step through the loop one iteration at a int I = 0;
Constant – Value which doesn’t change
time: printf("%d", i); /* "loop body" */
Data types – Inbuilt definition inside the c compiler.
At the end of each iteration, i is decremented by 1, j is incremented by 1. I++;
Operators – Symbol which tell compiler to act
The sixth evaluation of i!=j returns 0 for false, because i and j are equal at that while (i<10)
Control structures – Structures which alter the flow of control based on condition or without condition.
time, so the loop body is not executed. {
Function - set of instructions that performs a particular task. It provides the modularity i.e. splitting the entire
printf("%d", i);
task into some smaller sub tasks.
Output: i++;
Array - collection of homogeneous elements stored in contiguous memory locations
10 - 0 = 10 }
preprocessor - activities to be performed before processing
9-1=8 return 0;
Structures - collection of heterogeneous Elements stored in independent Memory locations.
8-2=6 }
Unions - similar to the structures but the Members of union should share the same memory location. It makes
7-3=4
us to save memory.
Pointers - a variable which holds the address of the another identifier rather than storing the value.
scanf() -scan formatted. It means that the way which we follow to get input from the user is in some format.
printf() -print formatted. It means that the output that has to be provide to user are in some format.
getchar() -used to get the single character as input from the user.
putchar() -used to give the single character as output to the user.
gets() -used to get the string as input from the user.
puts() -used to give the string as output to the user.
file -is a group of related data stored in a place on the hard disk.
Basic file operations - naming, opening, reading, writing and closing the file.
Fopen() -is a function helps to open a file in any of the following modes. r - read only, w – write only, a – append
fclose() -is a function used to close the file
getc(),getw(),fscanf() -function used for read operations.
Putc(),putw(),fprintf() -function used for write operations.
Fseek(),ftell(),rewind() – functions used in random file activities.
Ftell() – returns the current position of the file pointer.
Fseek() – used to move the file pointer to the desired locations.
Rewind() – takes the file pointer to the beginning of the file.
THE BREAK STATEMENT / THE CONTINUE STATEMENT FUNCTION / TYPICAL FUNCTION / SCOPE of FUNCTION Variables / RECURSIVE FUNCTIONS - CALL THEMSELVES FUNCTIONS / CALL
The break and continue statements in C are used to alter the flow of a program. The main difference between MODIFYING FUNCTION ARGUMENTS OTHER FUNCTIONS
the two is that a break statement ends a loop or switch, while a continue statement skips an iteration of a loop:
FUNCTION Recursive Functions - Functions That Call Themselves
Break
A function can be thought of as a mini-program, where a group of statements are executed when the function is A function that calls itself is said to be a RECURSIVE function. Recursive function calls can be less efficient than
Ends a loop or switch statement when a specific condition is met. Control moves to the next statement after the
called. A function is CALLED (or INVOKED) when it is needed to branch off from the program flow and execute a loop.
loop or switch.
a group of statements within that function. Once the statements in the function are executed, program flow Example:
Continue
resumes from the place where the function is called. A few functions: main, printf and scanf were already used. #include <stdio.h>
Skips the remaining code within the current iteration of a loop and immediately jumps to the next iteration. It does
The main function is special, in the way that it's called automatically when the program starts. In C, and other int factorial(int x);
not exit the loop entirely.
programming languages, it is possible to create customized functions. int main()
Example:
A Typical Function {
#include <stdio.h>
Functions have 5 main features: int a, b=1, temp;
int main() {
1. The RETURN TYPE is the data type of the RETURN VALUE of the function. printf("Enter an integer: ");
int a;
2. The NAME is required as an identifier to the function, so that the computer knows which scanf("%d", &a);
printf("Pick a number from 1 to 4:\n");
function is called. Naming of functions follows the same set of rules as the naming of variables. printf("\n%d factorial is %d\n\n", a, factorial(a));
scanf("%d", &a);
3. Functions can take ARGUMENTS - a function might need extra information for it to work. printf("Enter another integer: ");
switch (a) {
Arguments are optional. scanf("%d", &a);
case 1:
4. The function BODY is surrounded by curly brackets and contains the statements of the temp = a;
printf("You chose number 1\n");
function. for( ; a>0 ; a--)
break;
5. The RETURN VALUE is the value that is passed back to the main program. Functions exit {
case 2:
whenever a value is returned. b*=a;
printf("You chose number 2\n");
}
break;
printf("\n%d factorial is %d\n", temp, b);
case 3: SCOPE OF FUNCTION VARIABLES return 0;
printf("You chose number 3\n");
Only a limited amount of information is available within each function. Variables declared within the calling }
break;
function can't be accessed unless they are passed to the called function as arguments. The only other contact a int factorial(int x)
case 4:
function might have with the outside world is through global variables. { /* n factorial, (or n!) is 1*2*3* ... *n */
printf("You chose number 4\n");
Local variables are declared within a function. They are created a new each time the function is called, and if(x!=0)
break;
destroyed on return from the function. Values passed to the function as arguments can also be treated like local {
default:
variables. return (x*factorial(x-1));
printf("That's not 1,2,3 or 4!\n");
Static variables are slightly different; they don't die on return from the function. Instead their last value is retained, }
}
and it becomes available when the function is called again. else
return 0;
Global variables don't die on return from a function. Their value is retained, and is available to any other function, {
}
which accesses them. return 1;
seem virtually identical to the last example, except a break statement is inserted at the end of each case to
Function Declaration }
"break" out of the switch block. Now it should work as expected:
This is what a function definition might look like: }
Pick a number from 1 to 4:
int squareNumber(int a) CALL OTHER FUNCTIONS
2
{ To call a function, you simply need to pass the required parameters along with the function name, and if the
You chose number 2
int b = a*a; function returns a value, then you can store the returned value. Control of the program is transferred to the user-
Notice that it is not needed to surround the statement(s) in each case with opening and closing brackets - not like
return b; defined function by calling it.
the "usual" statement blocks seen in previously.
} Example:
The continue statement
squareNumber is the name of this function. Because an integer is returned, the int keyword must be placed before #include <stdio.h>
The continue statement passes control to the next iteration of the nearest enclosing do, for, or while statement in
the function name. If the function does not return a value, the void keyword should be put before the function int triangular(int x);
which it appears, bypassing any remaining statements in the do, for, or while statement body.
name. This function has one argument, which is of the type int. If there are arguments, variable declarations int main()
Syntax jump-statement :
should be in the round brackets. {
continue;
The function body consists of 2 statements. The first, sees an int variable b declared and assigned a*a, i.e. a int x;
The next iteration of a do, for, or while statement is determined as follows:
squared. The second statement uses the return keyword to pass the value of b back into the main program, printf("Enter an integer: ");
Within a do or a while statement, the next iteration starts by reevaluating the expression of the do or while
hence exiting the function. Within the program, it is possible to write: scanf("%d", &x);
statement.
x = squareNumber(5); if(x%10>3 || x==11 || x==12 || x==13)
A continue statement in a for statement causes the first expression of the for statement to be evaluated. Then
This would assign 25 to x. It is said that 5 is passed as an argument to the function squareNumber. {
the compiler reevaluates the conditional expression and, depending on the result, either terminates or iterates
The variables within the squareNumber function are LOCAL VARIABLES - when the function exits, variables a printf("\n%d is the %dth triangular number\n", triangular(x), x);
the statement body. See The for Statement for more information on the for statement and its nonterminals.
and b are deleted from memory. int squareNumber(int a) is also known as the FUNCTION HEADER. }
else Example: Example of the continue statement:
{ #include <stdio.h> while ( i-- > 0 )
switch(x%10) void printAverage(int x, int y, int z); /* the function declaration */ {
{ int main() x = f( i );
case 1: { if ( x == 1 )
printf("\n%d is the %dst triangular number\n", triangular(x), x); int a, b, c; continue;
break; printf("Enter 3 integers separated by spaces: "); y += x * x;
case 2: scanf("%d %d %d", &a, &b, &c); }
printf("\n%d is the %dnd triangular number\n", triangular(x), x); printAverage(a, b, c); /* the function call */ In this example, the statement body is executed while i is greater than 0. First f(i) is assigned to x; then, if x is
break; return 0; /* exit main function */ equal to 1, the continue statement is executed. The rest of the statements in the body are ignored, and execution
case 3: } resumes at the top of the loop with the evaluation of the loops test.
printf("\n%d is the %drd triangular number\n", triangular(x), x);
break; Modifying Function Arguments
} Some functions work by modifying the values of their arguments. This may be done to pass more than one value
} back to the calling routine, or because the return value is already being used in some way. C requires special
printf("You entered: %d\n", x); arrangements for arguments whose values will be changed.
return 0; You can treat the arguments of a function as variables, however direct manipulation of these arguments won't
} change the values of the arguments in the calling function. The value passed to the function is a copy of the
int triangular(int a) calling value. This value is stored like a local variable, it disappears on return from the function.
{ /* the nth triangular number is 1+2+3+ ... +n */ There is a way to change the values of variables declared outside the function. Passing the addresses of variables
int x = (a * (a + 1)) / 2; to the function does it. These addresses, or pointers, behave a bit like integer types, except that only a limited
return x; number of arithmetic operators can be applied to them. They are declared differently to normal types, and we are
} rarely interested in the value of a pointer. It is what lies at the address which the pointer references which interests
Above main is the function declaration. The triangular function takes an integer argument, and returns an integer. us.
Below main is the function definition - it works out and returns the triangular number of the number passed to the To get back to our original function, we pass it the address of a variable whose value we wish to change. The
function. function must now be written to use the value at that address (or at the end of the pointer). On return from the
Inside main, observe that inside printf, the triangular function is called. The if, else and switch blocks determine function, the desired value will have changed. We manipulate the actual value using a copy of the pointer.
how to display the result.
One important thing to remember is that the x in main is totally different to the x in triangular.
The value of x in main remains unchanged after calling triangular.
ARRAYS CHARACTER ARRAYS / INTO THE NEXT DIMENSTION / ARRAY SIZE SINGLE – MULTI DIMENSIONAL ARRAYS - PASSING ARRAYS TO
Introduction So far arrays of integers are used. Arrays for floats and doubles as well as chars can also be used. FUNCTIONS
Arrays allow storing a sequence of variables of the same data type. An array in the computer's memory can be Character arrays have a special property... Each element of the array can hold one character. But if the array is
Single Dimensional Arrays
assumed as a row of consecutive spaces, each of which can store a data item, known as an ELEMENT. ended with the NULL CHARACTER, denoted by \0 (that is, backslash and zero), it is called a STRING
Sometimes it's inconvenient to call a function that requires a long list of arguments. One way around this is to
Declaring Arrays CONSTANT. The null character marks the end of a string - useful for functions like printf.
store the variables into an array, then pass a pointer to the array on the function. This method will be discussed
Example:
A declaration is to be made before using an array. To declare an array, its data type, its name and, in most cases,
in the pointers section, but for now, the array is passed into the function - not the most efficient way, but it'll do
#include <stdio.h>
its size should be specified. Make sure the array has a valid name.
for now.
int main()
Here's an example:
Example:
{
int arrayOfInts[5];
#include <stdio.h>
char charArray[8] = {'F','r','i','e','n','d','s','\0'};
This reserves memory for an array to hold five integer values. The number of elements should be enclosed in the
int addNumbers(int fiveNumbers[]); /* declare function */
int i;
square brackets. If the number of elements is not specified during the declaration, it means that an UNSIZED
int main()
for(i=0 ; i<8 ; i++)
array is declared - the size will be calculated when values are inserted into it.
{
{
Initializing Arrays int array[5];
printf("charArray[%d] has a value of %c\n", i,
Values can be assigned to the array in several ways. for example, if an arrays is to hold the numbers 1 through int i;
charArray[i]);
to 10, this can be done in several ways, as this example demonstrates: printf("Enter 5 integers separated by spaces: ");
}
int main() for(i=0 ; i<5 ; i++)
printf("My favourite comedy is %s\n", charArray); /*
{ {
Alternative way */
int arrayOfInts1[10] = { 1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 , 9 , 10 }; scanf("%d", &array[i]);
return 0;
int arrayOfInts2[10]; }
}
int arrayOfInts3[] = {1,2,3,4,5, 6,7,8,9,10}; /* an unsized array */ printf("\nTheir sum is: %d\n", addNumbers(array));
Output:
int arrayOfInts4[10]; return 0;
charArray[0] has a value of F
int i; }
charArray[1] has a value of r
arrayOfInts2[0] = 1; int addNumbers(int fiveNumbers[]) { /* define function */
charArray[2] has a value of i
arrayOfInts2[1] = 2; int sum = 0;
charArray[3] has a value of e
arrayOfInts2[2] = 3; int i;
charArray[4] has a value of n
arrayOfInts2[3] = 4; for(i=0 ; i<5 ; i++)
charArray[5] has a value of d
arrayOfInts2[4] = 5; {
charArray[6] has a value of s
arrayOfInts2[5] = 6; sum+=fiveNumbers[i]; /* work out the total */
charArray[7] has a value of
arrayOfInts2[6] = 7; }
My favourite comedy is Friends
arrayOfInts2[7] = 8; return sum; /* return the total */
Notice that each of the characters is enclosed with single quote marks - double quote marks are reserved for
arrayOfInts2[8] = 9; }
strings. The character and string format specifiers are also used (%c and %s respectively). Caution: Use the right
arrayOfInts2[9] = 10; Notice that the size of the array is left blank in both the function declaration and definition - the compiler works it
slash symbol for the null character.
for(i=0 ; i<10 ; i++) out for the programmer. Also, when the function is called, the name of the array is passed on.
Into The Next Dimension...
{ Multidimensional Arrays
An array's DIMENSION is the number of indices required to reference an element.
arrayOfInts4[i] = i + 1;
This is similar to passing 1D arrays but in the function declarations it is a must to specify all the dimension sizes
Example,
}
(but the leftmost one is optional).
arrayOfInts[0] is the first element. The index is 0 - there is only 1 index so arrayOfInts is one dimensional.
return 0;
#include <stdio.h>
Now suppose arrayOfInts[0] needs to hold 2 integers. Here comes the next dimension... arrayOfInts[0][0] that
}
could hold the first integer, and arrayOfInts[0][1] could hold the other. 2 indices are needed to reference each
Caution: Notice that the first element of an array is indexed 0 rather than 1. This is a simple, yet important idea
void printArray(int array[][4]); /* declare function */
integer, so arrayOfInts is two-dimensional.
that needs to be taken into account.
int main()
2 dimensions is the mostly used, but 3 dimensions is like arrayOfInts[0][0][0]. 2D arrays are useful for storing
Printing Out Arrays {
grid-base information, like coordinates.
One of the commonest ways to print out the contents of the array is to use a loop. A loop used with scanf to insert int array[3][4] = {0,1,2,3,4,5,6,7,8,9,10,11};
The example given here shows how to declare a 2D array:
elements: printArray(array);
int array2D[3][5];
Example: return 0;
This tells the computer to reserve enough memory space for an array with 15, that is, 3 x 5 elements.
#include <stdio.h> }
One way to picture these 15 elements is an array with 3 rows and 5 columns.
int main()
But there are times when the size is not known. In this case an unsized array can be used. However, every
{ void printArray(int array[][4])
dimension size except the left one should be specified. Like this:
int anotherIntArray[5]; { /* define function */
int array2D[ ] [5];
int i; int i, j;
printf("Enter 5 integers one by one, pressing return after each one:\n"); for(i=0 ; i<3 ; i++)
{
for(j=0 ; j<4 ; j++) ARRAY SIZES for(i=0 ; i<5 ; i++)
{ {
If it is needed to find out how much memory the arrays occupy (1D or multidimensional), the sizeof operator can
printf("%2d ", array[i][j]); scanf("%d", &anotherIntArray[i]);
be used.
} }
Example:
printf("\n"); for(i=0 ; i<5 ; i++)
#include <stdio.h>
} {
int main()
} printf("anotherIntArray[%d] has a value of %d\n", i, anotherIntArray[i]);
{
}
char arrayChar[] = {'A','r','r','a','y','\0'};
return 0;
int arrayInt[5] = {1,2,4,8,16};
}
float arrayFloat[3] = { 1.24 , 2 , 4.68756 };
double arrayDouble[2];
The output might look like:
int arrayInt2D[][4] = {1,6,3,7, 0,3,8,9, 2,5,2,3};
Enter 5 integers one by one, pressing return after each one:
arrayDouble[0] = 23.23456532;
3
arrayDouble[1] = 2.3422267;
7
printf("The size of arrayChar is %d\n", sizeof(arrayChar));
2
printf("The size of arrayInt is %d\n", sizeof(arrayInt));
856
printf("The size of arrayFloat is %d\n", sizeof(arrayFloat));
324
/* Alternative way */
anotherIntArray[0] has a value of 3
printf("The size of arrayDouble is %d\n", sizeof(double) * 2);
anotherIntArray[1] has a value of 7
printf("The size of arrayInt2D is %d\n", sizeof(arrayInt2D));
anotherIntArray[2] has a value of 2
printf("The size of arrayInt2D[0] is %d\n", sizeof(arrayInt2D[0]));
anotherIntArray[3] has a value of 856
return 0;
anotherIntArray[4] has a value of 324
}
Output:
The size of arrayChar is 6
The size of arrayInt is 20
The size of arrayFloat is 12
The size of arrayDouble is 16
The size of arrayInt2D is 48
The size of arrayInt2D[0] is 16
sizeof can be used to find out the maximum of "rows" there are in a 2D array. For example, how many "rows" are
there in arrayInt2D[][4]?
This is given by: sizeof(arrayInt2D) / sizeof(arrayInt2D[0]), which is 48/16 = 3. Think of this calculation as "size
of entire array / size of a column".
So there are 3 "rows" and 4 "columns" to this 2D array.
The "rows and columns" visualization shouldn't be confused with the way the data is stored in memory.
int arrayInt2D[][4] = {1,6,3,7, 0,3,8,9, 2,5,2,3};
This uses 12 consecutive memory slots, with 1 placed in the first slot, 6 in the next one and so on. So 7 is next
to the 0. In fact, arrayInt2D[0][4] returns 0, arrayInt2D[0][5] returns 3 and so on. arrayInt2D[3] returns some
random number because there is no 4th row.
2D ARRAYS / ARRAY SIZES PRE PROCESSOR / DEFINE / UNDEF and MACRO FUNCTIONS IF / ELSE and ELIF / IF DEF, IF NDEF and END IF / The Process of
Methods used are similar to those of the 1D arrays: Introduction Linking /
Example: The C preprocessor is a program that is executed before the source code is compiled.
#if, #else and #elif
#include <stdio.h> C preprocessor commands are called DIRECTIVES, and begin with a pound hash symbol (#). No white space
This set of directives allow to perform conditional branching before the program is compiled, to determine which
int main() should appear before the #, and a semi colon is NOT required at the end. A directive: #include is already
parts to compile - another useful method of debugging.
{ introduced. This takes the specified file, and pastes its contents into the place where #include is placed before
They act just like the C keywords: if, else and else if respectively. No curly brackets are required, but the whole
int first[3][4] = {0,1,2,3,4,5,6,7,8,9,10,11}; the source code is compiled. So when printf is used, stdio.h is required so that the compiler knows what printf is.
conditional block must be terminated by the #endif directive. It is possible to place the condition after the #if or
int second[3][4] = {0, 1, 2, 3, 4, 5, 6, 7,8, 9,10,11}; /* a clearer definition than the first */ #define
#elif directives – round brackets are optional.
int third[][5] = {0,1,2,3,4}; /* third[] only has one index of 1 */
#define allows to make text substitutions before compiling the program. Here's an example:
One important thing to remember is that it is not possible to use any variables declared in main with these
int fourth[][6] = {0,1,2,3,4,5,6,7,8,9,10,11}; /* fourth[] has 2 indices - 0 or 1 */
#define MAX 10
directives, because the directives are read by the C preprocessor - BEFORE main is executed! It is allowed to
int i,j;
Before compilation, if the C preprocessor finds MAX as one word (so words like MAXIMUM will not be affected),
use macro definitions however.
int fifth[5][4];
in the source code, it replaces it with the number 10. If MAX was part of a string (for example, between the quote
#include <stdio.h>
int sixth[2][6];
marks of printf), the preprocessor will leave it alone. The MACRO DEFINITION can be anything that is desired,
#define DEBUG_MODE 1
int seventh[2][3];
as long as it doesn't contain special characters or spaces and it cannot start with a number. It is possible to define
int main()
for(i=0 ; i<5 ; i++)
strings as well:
{
{
Example:
#if DEBUG_MODE==1
for(j=0 ; j<4 ; j++)
#define NAME "Mak" - Every time the preprocessor sees NAME it will replace it with
printf("Debug Mode 1\n");
{
"Mak"
#elif DEBUG_MODE==2
fifth[i][j] = i * 4 + j;
#include <stdio.h>
printf("Debug Mode 2\n");
}
#define MIN 0 /* #defines */
#elif DEBUG_MODE>2
}
#define MAX 10
printf("Debug Mode 3\n");
for(i=0 ; i<2 ; i++)
#define TRUE 1
#else
{
#define FALSE 0
printf("Default Debug Mode\n");
printf("Enter 6 integers separated by spaces: ");
int main() { /* beginning of program */
#endif
for(j=0 ; j<6 ; j++)
int a;
return 0;
{
int okay = FALSE; /* the compiler sees this as int okay = 0; */
}
scanf("%d" , &sixth[i][j]);
while(!okay) {
}
printf("Input an integer between %d and %d: ", MIN, MAX);
Output: Debug Mode 1
printf("\n");
scanf("%d", &a);
Running the example without defining DEBUG_MODE will lead to a compilation error.
}
if(a>MAX) {
printf("You entered:\n");
printf("\nToo large.\n");
for(i=0 ; i<2 ; i++) #ifdef, #ifndef and #endif
}
{ When writing big programs, DEBUGGING should be done, which involves reading through the program, and
else if(a<MIN) {
for(j=0 ; j<6 ; j++) trying to find out what's causing the program to go wrong.
printf("\nToo small.\n");
{ One common method for debugging is to comment out sections of code to find that "bug". But what if there are
}
printf("%d ", sixth[i][j]); comments dotted around the source code? That's when #ifdef and #ifndef come into play. Nesting the comments
else {
} lead to a compilation error.
printf("\nThanks.\n");
printf("\n"); In the case of #ifdef, if the macro definition that follows immediately is defined with #define, the following code
okay = TRUE;
} from that directive to the #endif directive will be considered as part of the program and will be compiled.
}
seventh[0][0] = 0; With #ifndef, it's the other way round, that is, if there isn't a definition, the code up to the #endif directive will be
}
seventh[0][1] = 1; considered as part of the program and will be compiled.
return 0;
seventh[0][2] = 2; Example
}
seventh[1][0] = 3; #include <stdio.h>
The program will loop until a number between 0 and 10 is entered.
seventh[1][1] = 4; #define WORD1 /* No need to put an expression after the defined word */
By the time the compiler receives the program, it won't see words like MIN, MAX, TRUE or FALSE in the source
seventh[1][2] = 5; int main() {
code. Instead, it'll see the numbers 0, 10, 1 and 0 respectively.
return 0; #ifdef WORD1
The Process of Linking
} printf("1: WORD1 is defined so this bit is compiled.\n");
A compiler is a program that translates source code into machine code. But what if the program has more than
#endif
one source file? The LINKER is a program that is run after compilation. Its job is to piece together various parts
For loops are handy. And not just for initialization - they are commonly used for printing out arrays. Here the #ifndef WORD2
of a program, in order to produce the final executable.
"rows" are iterated in the first loop, and then the "columns". printf("2: WORD2 is not defined so this bit is compiled.\n");
The way linker works depends on the compiler. Three compilers were used in the past - gcc on a UNIX platform,
#endif
and Borland C++ and Microsoft Visual C++ on Windows.
#undef WORD1 #undef and Macro Functions ARRAY SIZES
#define WORD2 #undef can be used to remove any already created a macro definition. This means the preprocessor will no longer
If it is needed to find out how much memory the arrays occupy (1D or multidimensional), the sizeof operator can
#ifdef WORD1 make any more text substitutions associated with that word. Also, to change a definition, #undef should be used
be used like this:
printf("3: WORD1 is now undefined so this bit is not compiled.\n"); to undefine it, then #define should be used to redefine it.
Example:
#endif #define can be used to create one’s own MACRO FUNCTIONS. These are useful when the function is relatively
#include <stdio.h>
#ifndef WORD2 small.
int main()
printf("4: WORD2 is now defined so this bit is not compiled.\n"); Example:
{
#endif #define SQR(a) (a*a)
char arrayChar[] = {'A','r','r','a','y','\0'};
return 0; Now if SQR(3) is written in the program, the preprocessor will replace it with (3*3) in the program, NOT 9.
int arrayInt[5] = {1,2,4,8,16};
} Care should be taken with the brackets.
float arrayFloat[3] = { 1.24 , 2 , 4.68756 };
Example :
double arrayDouble[2];
Output: 1: WORD1 is defined so this bit is compiled. #include <stdio.h>
int arrayInt2D[][4] = {1,6,3,7, 0,3,8,9, 2,5,2,3};
Output 2: WORD2 is not defined so this bit is compiled #define MAX(a,b) (a>b ? a : b) /* a "function" */
arrayDouble[0] = 23.23456532;
#define DIFF1 4-7
arrayDouble[1] = 2.3422267;
#define DIFF2 (4-7)
The Process of Linking printf("The size of arrayChar is %d\n", sizeof(arrayChar));
#define NUMBER 10
printf("The size of arrayInt is %d\n", sizeof(arrayInt));
What Does Linking Involve?
int main() {
printf("The size of arrayFloat is %d\n", sizeof(arrayFloat));
A compiler is a program that translates source code into machine code. But what if the program has more than
int a=4, b=7;
/* Alternative way */
one source file?
printf("Out of %d and %d, %d is the bigger number.\n", a, b, MAX(a,b));
printf("The size of arrayDouble is %d\n", sizeof(double) * 2);
The LINKER is a program that is run after compilation. Its job is to piece together various parts of a program, in
printf("DIFF1 = 4-7. DIFF1 times 10 equals %d.\n", DIFF1*10);
printf("The size of arrayInt2D is %d\n", sizeof(arrayInt2D));
order to produce the final executable.
printf("DIFF2 = (4-7). DIFF2 times 10 equals %d.\n", DIFF2*10);
printf("The size of arrayInt2D[0] is %d\n", sizeof(arrayInt2D[0]));
The way linker works depends on the compiler. Three compilers were used in the past - gcc on a UNIX platform,
printf("I live at number %d.\n", NUMBER);
return 0;
and Borland C++ and Microsoft Visual C++ on Windows.
printf("I'm moving soon...");
}
#undef NUMBER /* now undefine NUMBER so that it can be given a different value */
#define NUMBER 7
Output:
rintf(" now I live at number %d.\n", NUMBER);
The size of arrayChar is 6
return 0;
The size of arrayInt is 20
}
The size of arrayFloat is 12
Output:
The size of arrayDouble is 16
Out of 4 and 7, 7 is the bigger number.
The size of arrayInt2D is 48
DIFF1 = 4-7. DIFF1 times 10 equals -66.
The size of arrayInt2D[0] is 16
DIFF2 = (4-7). DIFF2 times 10 equals -30.
I live at number 10.
sizeof can be used to find out the maximum of "rows" there are in a 2D array. For example, how many "rows" are
I'm moving soon... now I live at number 7.
there in arrayInt2D[][4]?
Look at the printf statements:
This is given by: sizeof(arrayInt2D) / sizeof(arrayInt2D[0]), which is 48/16 = 3. Think of this calculation as "size
The compiler will see the first one as:
of entire array / size of a column".
printf("Out of %d and %d, %d is the bigger number.\n", a, b, (a>b ? a : b));
So there are 3 "rows" and 4 "columns" to this 2D array.
Since a and b beforehand were initialized beforehand, it'll work out if a is greater than b, if so, return a from the
The "rows and columns" visualization shouldn't be confused with the way the data is stored in memory.
expression, else return b.
int arrayInt2D[][4] = {1,6,3,7, 0,3,8,9, 2,5,2,3};
The compiler will see the next two printfs as:
This uses 12 consecutive memory slots, with 1 placed in the first slot, 6 in the next one and so on. So 7 is next
printf("DIFF1 = 4-7. DIFF1 times 10 equals %d.\n", 4-7*10);
to the 0. In fact, arrayInt2D[0][4] returns 0, arrayInt2D[0][5] returns 3 and so on. arrayInt2D[3] returns some
printf("DIFF2 = (4-7). DIFF2 times 10 equals %d.\n", (4-7)*10);
random number because there is no 4 th row.
Notice how the preprocessor leaves DIFF1 and DIFF2 alone inside the string of printf. Looking at the above lines,
it will be clear why the top printf prints out -66, where as the bottom did the obvious and printed 30 - macro
The Process of Linking
definitions are just text substitutions! What Does Linking Involve?

The next printf is seen as : printf("I live at number %d.\n", 10); A compiler is a program that translates source code into machine code. But what if the program has more than

After that, NUMBER is undefined, then redefined, so the next printf will look like: one source file?

printf(" now I live at number %d.\n", 7); The LINKER is a program that is run after compilation. Its job is to piece together various parts of a program, in
order to produce the final executable.
The way linker works depends on the compiler. Three compilers were used in the past - gcc on a UNIX platform,
and Borland C++ and Microsoft Visual C++ on Windows.
POINTERS IN C / POINTERS AND STRUCTURES STRUCTURES / USES OF STRUCTURE / DEFINING STRUCTURE ARRAY OF STRUCTURE / NESTED STRUCTURE / ACCESSING and
POINTER The main use of structure is to lump together collections of disparate variable types, so that they can conveniently INITIALIZATION
A pointer in C refers to a variable that holds the address of another variable. In real time, pointers are a quite be treated as a unit. For example, while writing a compiler or assembler, one might need for each identifier
ARRAY OF STRUCTURE
common way to get the contents of a variable. The unary operator `&' is used to return the address of a variable. information like its name (a character array), its source line number (an integer), some type information (a
Just like an array of variable, an array of structure can also be declared. Suppose a symbol table for 100 identifiers
Thus, the following sentence prints the address of a variable called a. character, perhaps) and probably a usage count (another integer).
has to be made. The definitions can be extended like
printf(“The address of the variable is: %u”,&a); char id[10];
char id[100][10];
If the address has to be assigned to a pointer variable, then an appropriate pointer variable has to be declared. int line;
int line[100];
A pointer variable is declared with an asterisk symbol preceding its name. char type;
char type[100];
The following example illustrates this. int usage;
int usage[100];
int a=5, *b, c; In this case, the structure is first defined, that is, what kinds of things it contains; after that one can actually reserve
but a structure lets rearranging this spread-out information so that all the data identifier is collected into one lump:
b = &a; storage for it, either in the same statement or separately. The simplest thing is to define it and allocate storage
struct {
c = *b; all at once.
char id[10];
b contains the address of a and `c = *b' means to use the value in b as an address, that is, as a pointer. The struct {
int line;
effect is that the contents of a is copied to c. char id[10];
char type;
The most frequent use of pointers in C is for walking efficiently along arrays. In fact, in the implementation of an int line;
int usage;
array, the array name represents the address of the zeroth element of the array. char type;
} sym[100];
Consider the following, int usage;
This makes sym an array of structures; each array element has the specified shape. Now, members can be
char *y; } sym;
referred to as
char x[100]; This defines sym to be a structure with the specified shape; id, line, type and usage are the members of the
sym[i].usage++; /* increment usage of i-th identifier */
y is of type pointing to character. The pointer variable y can be made to point to an element of x by either of structure.
for( j=0; sym[i].id[j++] != '\0'; ) ...
y = &x[0];
etc.
y = x;
Uses of Structures Thus, to print a list of all identifiers that have not been used, together with their line number,
Since x is the address of x[0]. This is legal and consistent.
As we have seen, a structure is a good way of storing related data together. It is also a good way of representing for( i=0; i<nsym; i++ )
Now `*y' gives x[0]. More importantly,
certain types of information. Complex numbers in mathematics inhabit a two dimensional plane (stretching in real if( sym[i].usage == 0 )
*(y+1) gives x[1]
and imaginary directions). These could easily be represented here by printf("%d\t%s\n", sym[i].line, sym[i].id);
*(y+i) gives x[i]
typedef struct {
and the sequence
double real;
y = &x[0];
double imag;
y++; Nested Structure
} complex;
leaves y pointing at x[1]. In fact the name of the array is itself a pointer to the first element of the array.
It is possible to declare a structure inside another structure. This means a structure can be made a member of
doubles have been used for each field because their range is greater than floats and because the majority of
The following program accepts elements for an array. The elements of the array are accessed using the name
another structure. The following program uses a structure called date as a member of another structure.
mathematical library functions deal with doubles by default.
of the array.
#include<stdio.h>
In a similar way, structures could be used to hold the locations of points in multidimensional space.
#include<conio.h>
Mathematicians and engineers might see a storage efficient implementation for sparse arrays here.
#include<stdio.h>
struct date
Apart from holding data, structures can be used as members of other structures.
main()
{
Arrays of structures are possible, and are a good way of storing lists of data with regular fields, such as databases.
{
int dd;
Another possibility is a structure whose fields include pointers to its own type.
int x,a[5];
int mm;
These can be used to build chains (programmers call these linked lists), trees or other connected structures.
printf(“Enter 5 elements\n”);
int yy;
These are rather daunting to the new programmer, so we won't deal with them here.
for(x=0;x<5;x++)
};
{
struct stud
scanf(“%d”,&a[x]);
{
}
char name[20];
printf(“The array elements are:\n”);
struct date d;
for(x=0;x<5;x++)
int m1,m2,m3;
{
float per;
printf(“%d\n”,*(a+x));
};
}
main()
}
{
Output:
struct stud s;
Enter 5 elements 1 2 3 4 5
printf("Enter the Details of student\n");
The array elements are 1 2 3 4 5
printf("\nName:");
scanf("%s",s.name); DEFINING A STRUCTURE / UNIONS POINTERS and STRUCTURES
printf("\nDate of Birth(dd mm yy):");
Before a structure is used, it has to be declared. The syntax for structure is as follows: Consider a block of data containing different data types defined by means of a structure. For example, a personal
scanf("%d%d%d",&s.d.dd,&s.d.mm,&s.d.yy);
struct <structure name> file might contain structures, which look something like:
printf("\nMarks in 3 Subject:");
{ struct tag
scanf("%d%d%d",&s.m1,&s.m2,&s.m3);
Data_type structure_element 1; {
s.per=(s.m1+s.m2+s.m3)/3;
Data_type structure_element 2; char lname[20]; /* last name */
printf("\nStudent Result");
Data_type structure _element 3; char fname[20]; /* first name */
printf("\n%s has secured %f%%",s.name,s.per);
Data_type structure_element n; int age; /* age */
getch();
}; };
}
Once, the new structure data type has been defined one or more variable can be declared of that structure type. Just like a variable of type structure, a pointer variable of type structure can also be declared. The following
Consider a structure declared as follows: program declares a pointer variable of type struct tag and uses this pointer variable to access every member of
Output:
struct student { the structure.
Enter the Details of student
char name[10]; Example:
Name:Ashwin
int rollno; #include<stdio.h>
Date of Birth(dd mm yy):12 12 1980
char sex; /* m or f */ struct tag
Marks in 3 Subject:98
int age; {
97
}; char lname[20];
95
This defines a new data type called student to be a structure with the specified shape; name, rollno, sex and age. char fname[20];
Student Result
A variable of type struct student can be declared as follows : int age;
Ashwin has secured 96.000000%
struct student collegestu, schoolstu; }s;
This statement allocates space in memory and makes available space to hold structure elements. A structure main()
ACCESSING and INITIALIZATION of STRUCTURE can be declared in any of the format given below. {
Format one: struct tag *t
Structure variable uses dot (.) operator to access structure element. Syntax to access the structure elements is
struct student { t = &s;
structure_name . structure_element_name
char name[10]; printf(“\nEnter the first name.”);
for example, To set the value to the structure element
int rollno; scanf(“%s”,t-> fname);
collegestu.rollno = 1125;
char sex; printf(“\nEnter the last name:”);
collegestu.age = 21;
int age; scanf(“%s”,t-> lname);
for example, To take the value from the structure element
}; printf(“\nAge:”);
int rnumber = colledestu.rollno;
struct student collegestu,schoolstu; scanf(“%d”,&t->age);
int stuage = collegestu.age;
printf(“\nThe details are:”);
The following program illustrates how structure members can be accessed.
Format two: printf(“Name:%s %s\nAge:%d”, t-> fname, t-> lname,t->age);
#include<stdio.h>
struct }
struct stud
{
{
char name[10]; Output:
int rno;
int rollno; Enter the first name:Mahatma
char name[20];
char sex; /* m or f */ Enter the last name:Gandhi
};
int age; Age:60
main()
} collegestu, schoolstu; The details are:Name:Mahatma Gandhi
{
Age:60
struct stud s;
printf(“Enter the student’s name:”); UNIONS
scanf(“%s”,s.name); A union is similar to a structure, except that it shares storage space between different members.Unlike a struct,

printf(“Enter the student’s roll number:”); the variables a_number.i and a_number.l occupy the same location in memory. Thus, writing into one will

scanf(“%d”,&s.rno); overwrite the other. Elements of a union are accessed in the same manner as a struct.

printf(“The student’s details are:\n”); union union_name {

printf(“Name : %s\n”,s.name); type variable-names ;

printf(“Roll No. : %d\n”,s.rno); }union_variables ;

} Example
union int_or_long {
int i;
long l; } a_number;
STANDARD INPUT OUTPUT FILE / WHOLE LINES INPUT OUTPUT CHATECTER INPUT OUTPUT / FORMATTED INPUT OUTPUT FORMATTED INPUT OUTPUT / WHOLE LINE INPUT OUTPUT
The Standard Input File Stream, stdin The most basic way of reading input is by calling the function getchar. getchar reads one character from the We have been using the I/O built-in functions printf() and scanf() which are the primary routines for formatted

To input a value, the easiest way is to use a library function called scanf. There are other functions like getc and ``standard input,'' which is usually the user's keyboard, but which can sometimes be redirected by the operating output and input in C (the ``f'' stands for formatted).

getchar (both used for inputting characters), but scanf is by far the easiest to use. gets is another one that is system. getchar returns (rather obviously) the character it reads, or, if there are no more characters available, printf() expects arguments giving a format string and values to be printed. The printf() prototype, in stdio.h, is:

covered later. scanf is actually a simplified version of a function called fscanf which requires the programmer to the special value EOF (``end of file''). int printf(char *, ...);

specify an INPUT FILE STREAM. A companion function is putchar, which writes one character to the ``standard output.'' (The standard output is, The first argument of printf() is the format string.

This is basically a series of bytes that is transferred from the input source to the program (recall from the data again not surprisingly, usually the user's screen, although it, too, can be redirected. printf, like putchar, prints to The number of remaining arguments depends on the number of conversion specifiers in the format string.

types section that each character is one byte large, so a stream can be thought of as a series of characters). In the standard output; in fact, you can imagine that printf calls putchar to actually print each of the characters it In C, an ellipsis, i.e. ``...'', is used to indicate an arbitrary number of arguments. The return value of printf() is an

C, stdin is the standard input file stream and links to the keyboard. formats.) int giving the number of bytes output, if successful; otherwise it returns EOF. This information from printf() is not

The Standard Output File Stream, stdout Using these two functions, we can write a very basic program to copy the input, a character at a time, to the generally very useful, and we often simply ignore the return value.
output: The function, printf(), converts, formats, and prints its arguments on the standard output using the conversion
putc, putchar, puts and printf are all used to output values. The latter is the simplest to use.
#include <stdio.h> specifications given in the format string.
Some of these require an OUTPUT FILE STREAM, which is a series of bytes that is transferred from the output
/* copy input to output */ The format string is made up of two kinds of characters: regular characters, which are simply copied to the output,
source to the program. stdout is the standard output file stream and refers to the monitor.
main() and conversion specification characters.
stderr is also an output file stream but makes sure that the output, usually an error message, is seen.
{ A conversion specification indicates how the corresponding argument value is to be converted and formatted
getc() and getchar()
int c; before being printed.
These functions allow the programmer to input a character and assign it to a variable.
c = getchar(); The number of conversion specifications in the format string must match exactly the number of arguments that
getc requires the programmer to specify an input file stream like stdin. getchar is EXACTLY the same as getc
while(c != EOF) follow; otherwise, the results are undefined.
with the input file stream set to stdin.
{ The data type of the argument should also match the data type it will be converted to; for example, integral types
Both functions return integer values, which could be assigned to variables for later use.
putchar(c); for decimal integer formats, float or double types for floating point or exponential formats, and so on.
putc() and putchar()
c = getchar(); If the proper type is not used, the conversion is performed anyway assuming correct data types and the results
To output a character, one of these two functions can be used:
} can be very strange and unexpected. Of course, character values are integral types; so characters can be
putc requires to specify two things - the variable whose character value should be printed out, and the file output
return 0; converted to ASCII integer values for printing, or printed as characters.
stream.
} The syntax of a complete conversion specifier
putchar is exactly the same as putc, but with the output file stream set to stdout. It is only needed to specify the
This code is straightforward, and I encourage you to type it in and try it out. It reads one character, and if it is not %-DD.ddlX
character value to print out.
the EOF code, enters a while loop, printing one character and reading another, as long as the character read is
The other format characters must appear in the order specified above and represent the following formatting
The following example demonstrates the previous four functions:
not EOF. This is a straightforward loop, although there's one mystery surrounding the declaration of the variable
information: (the corresponding characters are shown in parentheses).
Example:
c: if it holds characters, why is it an int?
#include <stdio.h> Justification ( -)
FORMATTED INPUT OUTPUT
int main() The first format character is the minus sign. If present, it specifies left justification of the converted argument in
We have been using the I/O built-in functions printf() and scanf() which are the primary routines for formatted
{ its field. The default is right justification, i.e. padding on the left with blanks if the field specified is wider than the
output and input in C (the ``f'' stands for formatted).
char a,b; converted argument.
printf() expects arguments giving a format string and values to be printed. The printf() prototype, in stdio.h, is:
printf("Enter a 2 letter word "); Field Width ( DD)
int printf(char *, ...);
printf("and press return: "); The field width is the amount of space, in character positions, used to print the data item. The digits, DD, specify
The first argument of printf() is the format string.
a = getc(stdin); the minimum field width. A converted argument will be printed in a field of at least this size, if it fits into it; otherwise,
The number of remaining arguments depends on the number of conversion specifiers in the format string.
b = getchar(); the field width is made large enough to fit the value. If a converted argument has fewer characters than the field
In C, an ellipsis, i.e. ``...'', is used to indicate an arbitrary number of arguments. The return value of printf() is an
printf("The first letter was: "); width, by default it will be padded with blanks to the left, unless left justification is specified, in which case, padding
int giving the number of bytes output, if successful; otherwise it returns EOF. This information from printf() is not
putc(a, stdout); is to the right.
generally very useful, and we often simply ignore the return value.
printf("\nFollowed by: "); Separator ( .)
The function, printf(), converts, formats, and prints its arguments on the standard output using the conversion
putchar(b); A period is used as a separator between the field width and the precision specification.
specifications given in the format string.
putchar(10); Precision ( dd)
The format string is made up of two kinds of characters: regular characters, which are simply copied to the output,
printf("Goodbye!\n");
and conversion specification characters. A conversion specification indicates how the corresponding argument The digits, dd, specify the precision of the argument. If the argument is a float or double, this specifies the number
}
value is to be converted and formatted before being printed. of digits to the right of the decimal point. If an argument is a string, it specifies the maximum number of characters
Suppose YO is entered and pressed return.
The number of conversion specifications in the format string must match exactly the number of arguments that to be printed from the string.
The output generated would be:
follow; otherwise, the results are undefined. Length Modifier ( l)
Enter a 2 letter word and press return: YO
The data type of the argument should also match the data type it will be converted to; for example, integral types The length modifier, l, (ell) indicates that an integer type argument is a long rather than an int type.
The first letter was: Y
for decimal integer formats, float or double types for floating point or exponential formats, and so on.
Followed by: O
If the proper type is not used, the conversion is performed anyway assuming correct data types and the results
Goodbye!
can be very strange and unexpected. Of course, character values are integral types; so characters can be
After declaring two character variables, two printf statements were used to display the first line of text.
converted to ASCII integer values for printing, or printed as characters.
As soon as the user enters a character, it gets assigned to the variable a because the getc function was INVOKED
- a term meaning the calling of a function. Notice how the standard input stream, stdin had to be PASSED into
Character Conversion The syntax of a complete conversion specifier getc. Values passed into a function are called ARGUMENTS - a function sometimes needs extra information for

d The argument is taken to be an integer and converted to decimal integer notation. %-DD.ddlX it to work.

o The argument is taken to be an integer and converted to unsigned octal notation without a As soon as the user enters a second character, it gets assigned to b - notice how getchar requires no arguments.
The other format characters must appear in the order specified above and represent the following formatting
leading zero. The program continues when return is pressed - any additional characters after the second aren't stored.
information: (the corresponding characters are shown in parentheses).
x The argument is taken to be an integer and converted to unsigned hexadecimal notation without The putc function takes a char or int variable as its first argument, followed by the standard output stream, stdout
Justification ( -)
a leading 0x. and prints out the specified character.
The first format character is the minus sign. If present, it specifies left justification of the converted argument in
u The argument is taken to be an unsigned integer and converted to unsigned decimal notation. putchar only requires one argument, and also prints out the specified character.
its field. The default is right justification, i.e. padding on the left with blanks if the field specified is wider than the
c The argument is taken to be an ASCII character value and converted to a character. As a reminder, \n is the NEWLINE character and is the equivalent to a carriage return. It has an ASCII code of
converted argument.
s The argument is taken to be a string pointer. Unless a precision is specified as discussed below, 10, so putchar(10) inserts a newline before printing "Goodbye!".
Field Width ( DD)
characters from the string are pointed out until a NULL character is reached printf and scanf
The field width is the amount of space, in character positions, used to print the data item. The digits, DD, specify
f The argument is taken to be a float or double. It is converted to decimal notation of the form [-] printf
the minimum field width. A converted argument will be printed in a field of at least this size, if it fits into it; otherwise,
ddd.dddddd, where the minus sign shown in square brackets may or may not present. The printf gives the programmer the power to print output onto the screen, and is relatively simple to use.
the field width is made large enough to fit the value. If a converted argument has fewer characters than the field
number of digits, d, after the decimal point is 6 by default if no precision is specified. The number The number of arguments required varies, but the first argument that is passed should be a STRING - a string
width, by default it will be padded with blanks to the left, unless left justification is specified, in which case, padding
of digits, d, before the decimal is as required for the number. can be assumed as a sequence of characters.
is to the right.
e The argument is taken to be a float or double. It is converted to decimal notation of the form Recall that a string must be surrounded by double quote marks. Here's printf in action once again:
Separator ( .)
[-]d.ddddddE[+/-]xxx, where the leading minus sign may be absent. There is one digit before the printf("Hello World!\n");
A period is used as a separator between the field width and the precision specification.
decimal point. The number of digits, Notice how the string, "Hello World!\n" is enclosed in double quote marks.
d, after the decimal point is 6 if no precision is specified. The E signifies the exponent, ten followed Precision ( dd)
by a plus or minus sign, followed by an exponent. The number of digits in the exponent, x, is The digits, dd, specify the precision of the argument. If the argument is a float or double, this specifies the number

implementation dependent, but not less than two. of digits to the right of the decimal point. If an argument is a string, it specifies the maximum number of characters WHOLE LINES INPUT OUTPUT
g The same as %e or %f whichever is shorter and excludes trailing zeros. to be printed from the string. Where we are not too interested in the format of our data, or perhaps we cannot predict its format in advance,
Length Modifier ( l) we can read and write whole lines as character strings. This approach allows us to read in a line of input, and
A complete conversion specification starts with the character % and ends with a conversion character. Between The length modifier, l, (ell) indicates that an integer type argument is a long rather than an int type. then use various string handling functions to analyse it at our leisure.
these two characters, special format characters may be used which can specify justification, field width, field gets
separation, precision, and length modification. Character Conversion gets reads a whole line of input into a string until a newline or EOF is encountered. It is critical to ensure that the
The characters that follow the % character and precede the conversion characters are called format characters. string is large enough to hold any expected input lines. When all input is finished, NULL as defined in stdio.h is
d The argument is taken to be an integer and converted to decimal integer notation.
All format characters are optional, and if they are absent their default values are assumed. returned.
o The argument is taken to be an integer and converted to unsigned octal notation without a
leading zero. puts
x The argument is taken to be an integer and converted to unsigned hexadecimal notation without puts writes a string to the output, and follows it with a newline character.

WHOLE LINES INPUT OUTPUT a leading 0x. Example: Program which uses gets and puts to double space typed input.
u The argument is taken to be an unsigned integer and converted to unsigned decimal notation. #include <stdio.h>
Where we are not too interested in the format of our data, or perhaps we cannot predict its format in advance,
c The argument is taken to be an ASCII character value and converted to a character. main()
we can read and write whole lines as character strings. This approach allows us to read in a line of input, and
s The argument is taken to be a string pointer. Unless a precision is specified as discussed below, { char line[256]; /* Define string sufficiently large to
then use various string handling functions to analyse it at our leisure.
characters from the string are pointed out until a NULL character is reached store a line of input */
gets
f The argument is taken to be a float or double. It is converted to decimal notation of the form [-] while(gets(line) != NULL) /* Read line */
gets reads a whole line of input into a string until a newline or EOF is encountered. It is critical to ensure that the
ddd.dddddd, where the minus sign shown in square brackets may or may not present. The { puts(line); /* Print line */
string is large enough to hold any expected input lines. When all input is finished, NULL as defined in stdio.h is
number of digits, d, after the decimal point is 6 by default if no precision is specified. The number printf("\n"); /* Print blank line */
returned.
of digits, d, before the decimal is as required for the number. }
puts e The argument is taken to be a float or double. It is converted to decimal notation of the form }
puts writes a string to the output, and follows it with a newline character. [-]d.ddddddE[+/-]xxx, where the leading minus sign may be absent. There is one digit before the Note that putchar, printf and puts can be freely used together. So can getchar, scanf and gets.
Example: Program which uses gets and puts to double space typed input. decimal point. The number of digits,
#include <stdio.h> d, after the decimal point is 6 if no precision is specified. The E signifies the exponent, ten followed
main() by a plus or minus sign, followed by an exponent. The number of digits in the exponent, x, is
{ char line[256]; /* Define string sufficiently large to implementation dependent, but not less than two.
store a line of input */ g The same as %e or %f whichever is shorter and excludes trailing zeros.
while(gets(line) != NULL) /* Read line */
{ puts(line); /* Print line */ A complete conversion specification starts with the character % and ends with a conversion character. Between
printf("\n"); /* Print blank line */ these two characters, special format characters may be used which can specify justification, field width, field
} separation, precision, and length modification.
} The characters that follow the % character and precede the conversion characters are called format characters.
Note that putchar, printf and puts can be freely used together. So can getchar, scanf and gets. All format characters are optional, and if they are absent their default values are assumed.
FORMAT SPECIFIERS / INPUTING MULTIPLE VALUES MINIMUM FIELD WIDTH / MAKING IT LOOK NEATER MORE PRECISION
To print the value of variables requires the programmer to embed a format specifier in the text string, and to pass Suppose if need arises for the program to display output that occupies a minimum number of spaces on the More control with the displaying of integers can be gained by placing a dot, followed byMore control with the
extra arguments to the printf function. screen, this can be achieved by adding an integer value after the percent sign of a format specifier. displaying of integers can be gained by placing a dot, followed by an integer, after the minimum field specifier.
example: For example, if an integer is to be displayed using a minimum of 8 spaces, %8d should be written in the printf The dot and this integer are known as a PRECISION SPECIFIER.
printf("x equals %d \n", x); statement. The integer added specifies the maximum field width when displaying an integer or string. If %f is used, the format
This statement prints the value of x out. The value of x is to be passed into the printf function. When arguments This example gives a demonstration: specifier for floating point numbers, it is possible to control the number of decimal places that is displayed (which
are passed to functions, each one should be separated with a comma - here, "x equals %d \n" is an argument, #include <stdio.h> is 6 by default). Using the precision specifier does this. This time, the number after the dot is the number of
so is x. There are several format specifiers - the one that is used should depend on the type of the variable that int main() { decimal places. The number before the dot is still the minimum field width.
is to be printed out. Here are the common ones: int x = 123; This example should help clarify things:
Format Specifier Type printf("Printing 123 using %%0d displays %0d\n", x); #include <stdio.h>
%d (or %i) int printf("Printing 123 using %%1d displays %1d\n", x); int main() {
%c char printf("Printing 123 using %%2d displays %2d\n", x); float x = 3.141592;
%f float / double return 0; printf("Printing 3.141592 using %%f \t displays %f\n", x);
%s string } printf("Printing 3.141592 using %%1.1f \t displays %1.1f\n", x);
To display a number in scientific notation, %e should be used. To display a percent sign, %% should be used. Output: printf("Printing 3.141592 using %%1.2f \t displays %1.2f\n", x);
Caution: Don't try to display a decimal number using the integer format specifier, %d, as this displays unexpected Printing 123 using %0d displays 123 printf("Printing 3.141592 using %%3.3f \t displays %3.3f\n", x);
values. Similarly, don't use %f for displaying integers. Mixing %d with char variables, or %c with int variables is Printing 123 using %1d displays 123 printf("Printing 3.141592 using %%4.4f \t displays %4.4f\n", x);
all right, as shown in this example: Printing 123 using %2d displays 123 printf("Printing 3.141592 using %%4.5f \t displays %4.5f\n", x);
Example: Notice that in the first 4 cases, 123 is displayed in the same way as when %d is normally used. This is because printf("Printing 3.141592 using %%09.3f displays %09.3f\n", x);
#include <stdio.h> the number of spaces on the screen that 123 can be displayed is greater than or equal to 3. printf("Printing 3.141592 using %%-09.3f displays %-09.3f\n", x);
int main() But also, if %09d is written, the program will display zeros before the number itself. In the above example, it will printf("Printing 3.141592 using %%9.3f displays %9.3f\n", x);
{ display: printf("Printing 3.141592 using %%-9.3f displays %-9.3f\n", x);
int a = 72; Printing 123 using %09d displays 000000123 return 0;
char b = 'A'; An advantage of using this is that it is possible to count the minimum field of the number! }
printf("a equals %d \n", a); Making It Look Neater Output:
printf("a equals %c \n", a); The output from the example above doesn't look very neat because the numbersare aligned to the right of the Printing 3.141592 using %f displays 3.141592
printf("b equals %d \n", b); minimum field. In other words, 1,2 and 3 are the digits in the furthest 3 spaces of the minimum field. Printing 3.141592 using %1.1f displays 3.1
printf("b equals %c \n", b); To align the output on the left, a minus sign before the number in the format Printing 3.141592 using %1.2f displays 3.14
} specifier should be inserted. But if this to done to the previous example, all the output Printing 3.141592 using %3.3f displays 3.142
Output: lines will be the same. Printing 3.141592 using %4.4f displays 3.1416
a equals 72 A better example: Printing 3.141592 using %4.5f displays 3.14159
a equals H #include <stdio.h> Printing 3.141592 using %09.3f displays 00003.142
b equals 65 int main() { Printing 3.141592 using %-09.3f displays 3.142
b equals A int x = 12; Printing 3.141592 using %9.3f displays 3.142
The reason why this works is because a character constant is just an integer from 0 to 255. int y = 123; Printing 3.141592 using %-9.3f displays 3.142
Two or More Format Specifiers printf("Printing 12 using %%9d \t\t displays %9d\n", x);
As many format specifiers can be used, as needed with printf - just as long as the correct number of arguments printf("Printing 12 using %%09d \t\t displays %09d\n", x); If a negative value for the minimum width specifier is used, a zero after the minus sign will not affect the output.

are passed. printf("Printing 123 using %%9d \t\t displays %9d\n", y); Also, in the case for decimal numbers, the decimal point occupies a character space on the screen.

The ordering of the arguments matters. The first one should correspond to the first format specifier in the string printf("Printing 123 using %%09d \t displays %09d\n", y);
and so on. Take this example: return 0;
printf("a=%d, b=%d, c=%d\n", a,b,c); }
If a, b and c were integers, this statement will print the values in the correct order. Rewriting the statement as...
printf("a=%d, b=%d, c=%d\n", c,a,b); Output:
... would still cause the program to compile OK, but the values of a,b and c would be displayed in the wrong order! Printing 12 using %9d displays 12
scanf Printing 12 using %09d displays 000000012

scanf is a lot more flexible to use than the getchar and getc functions, simply because it is possible input numbers Printing 123 using %9d displays 123

and strings, as well as characters. Printing 123 using %09d displays 000000123

Here is an example: \t acts like a standard tab. Notice how it begins with a backslash, \ - just like the new line character.

printf("Enter a number and press Enter: ");


scanf("%d", &a);
The & is known as the ADDRESS-OF operator – this will be used a lot in POINTERS.
SUMMARY SUMMARY Back to the example, scanf takes at least two arguments. The first one is a string that can consist of format
specifiers. The rest of the arguments should be variable names preceded with the address-of operator. scanf
C is a high-level programming language that forms the basis to other programming languages such as C++, Perl An array in the computer's memory can be assumed as a row of consecutive spaces, each of which can store a
statement reads an integer from the input string, then goes to the address of the variable called a and puts the
and Java. Variables are like containers in the computer's memory – one can store values in them and retrieve or data item, known as an ELEMENT.
value there.
modify them when necessary The C preprocessor is a program that is executed before the source code is compiled. C preprocessor commands
Remember that a variable is like a container in the computer's memory - each one has a different address.
Local variable is the default storage for a variable in ‘C’ language. Its also called as automatic variables and are called DIRECTIVES, and begin with a pound / hash symbol (#). No white space should appear before the #,
As with printf, the number of arguments after the string argument should match the number of format specifiers
represented by the keyword ‘auto’ and a semi colon is NOT required at the end. A directive: #include is already introduced.
contained in that string.
When a variable is declared outside the scope of any function that is outside the main function, then the variable #undef can be used to remove any already created a macro definition. This means the preprocessor will no longer
Similarly, the type of the format specifier should match the type of the corresponding variable. The ordering of
is called as a global variable. Such variables are called global variables. make any more text substitutions associated with that word. Also, to change a definition, #undef should be used
the variables also matters.
Static variable can’t be reinitialized and it is the default storage class for global variables to undefine it, then #define should be used to redefine it.
Inputting Multiple Values
Arithmetic operators are commonly used for performing arithmetic calculations. Relation operators are used to
If there are multiple format specifiers within the string argument of scanf, it is possible to input multiple values. All
compare numerical values. A pointer in C refers to a variable that holds the address of another variable
that one needs to do is to separate each format specifier with a DELIMITER - a string that separates variables.
CONDITIONAL BRANCHING is a term that is used to describe the way program flow diverges,depending on The main use of structure is to lump together collections of disparate variable types, so that they can conveniently
For convenience, the delimiter should be one character that's a punctuation mark, like a comma or a space. As
certain condition(s) be treated as a unit.
a default, scanf stops reading in a value when space, tab or Enter is pressed.
The switch statement can help simplify things a little. It allows testing the value returned by a single expression A union is similar to a structure, except that it shares storage space between different members.
Consider:
and then executing the relevant bit of code. A pointer in C refers to a variable that holds the address of another variable.
scanf("%d %d", &x, &y);
The term "looping" describes the way in which the program executes statements over and over again, before Structure is a collections of disparate variable types
(Assume that x and y have been declared beforehand).
exiting the loop and continuing with program flow Structure is a good way of storing related data together
If 1 2 is entered and Enter key is pressed, 1 would get assigned to x, and 2 would get assigned to y.
In C, there are three types of loops: for loops, while loops and do while loops. It is possible to simulate each type A union is similar to a structure, except that it shares storage space between different members.
But if 1, 2 is entered and Enter key is pressed, x would equal 1, but y won't get assigned 2 because scanf was
by writing code using other loops
not expecting a comma in the input string.
Header File - Header files contain declarations to certain functions that may or may not have been used in a Displaying output is a common process, and there are a variety of functions that do exactly that. So far, the printf
Now consider
program function was used in every example as a way of displaying program output.
scanf("%d, %d, %d", &x,&y,&z);
EXPRESSIONS - EXPRESSIONS consist of a mixture of constants, variables and operators printf gives the programmer the power to print output onto the screen, and is relatively simple to use.
If 1 2 3 are entered and Enter key is pressed, 1 would get assigned to x but 2 and 3 won't get assigned to y or z,
Const - The const keyword is used to define a constant The most basic way of reading input is by calling the function getchar. getcha reads one character from the
simply because the numbers are not separated with commas.
DECLARATION - To DECLARE a variable, means to reserve memory space for it. ``standard input.''
Entering 1,2,3 works, but why does 1, 2, 3 also work? scanf ignores spaces, tabs and carriage returns
Scnaf - The easiest way to input a value is to use the library function scanf A companion function is putchar, which writes one character to the ``standard output.''
immediately after the delimiters.
Printf - Printf helps to print output onto the screen A conversion specification indicates how the corresponding argument value is to be converted and formatted
Just don't put a space, tab or carriage return before the delimiter! 1 ,2, 3 won't work. If the user needs to press
Switch - The switch allows testing the value returned by a single expression and then executing the relevant bit before being printed.
return after each number, the following statement should be noted:
of code. gets reads a whole line of input into a string until a newline or EOF is encountered. puts writes a string to the
scanf("%d\n%d\n%d", &x,&y,&z);
output, and follows it with a newline character.
Note that a delimiter shouldn't be put after the last format specifier.
A function can be thought of as a mini-program, where a group of statements are executed when the function is The easiest way is to use a library function called scanf putc, putchar, puts and printf are all used to output values
called. The field width is the amount of space, in character positions, used to print the data item.
Only a limited amount of information is available within each function. Variables declared within the calling
function can't be accessed unless they are passed to the called function as arguments. The only other contact a Files are very useful in storing information for future analysis and retrieval. C language defines a number of
function might have with the outside world is through global variables. functions for handling files.
Local variables are declared within a function. They are created a new each time the function is called, and When a file has to be used either for writing to it or for reading from it, it has to be opened in corresponding mode.
destroyed on return from the function. Values passed to the function as arguments can also be treated like local The function fopen()is used for opening a file.
variables. The function gets() and puts() offer a simple alternative to the use of scanf() and printf() for reading and displaying
Static variables are slightly different; they don't die on return from the function. Instead their last value is retained, strings.
and it becomes available when the function is called again. C communicates with files using a new datatype called a file pointer. This type is defined within stdio.h, and
Global variables don't die on return from a function. Their value is retained, and is available to any other function, written as FILE *
which accesses them. NULL is a character or pointer value. If a pointer, then the pointer variable does not reference any object
The RETURN TYPE is the data type of the RETURN VALUE of the function EOF is a character, which indicates the end of a file. It is returned by read commands of the getc and scanf
The NAME is required as an identifier to the function, so that the computer knows which function is called. Naming families when they try to read beyond the end of a file.
of functions follows the same set of rules as the naming of variables. Preprocessor - The C preprocessor is a program that is executed before the source code is compiled.
Functions can take ARGUMENTS - a function might need extra information for it to work. Arguments are optional. Header Files - Header files contain declarations to functions and variables and have a .h extension
The function BODY is surrounded by curly brackets and contains the statements of the function. Stack - Stacks are dynamic and constantly changing objects
The RETURN VALUE is the value that is passed back to the main program. Functions exit whenever a value is Queue - A queue is an ordered collection of items from which items may be deleted at one end
returned. that is called as the front of the queue
A function that calls itself is said to be a RECURSIVE function. Recursive function calls can be less efficient than Structure - A struct is a collection of variables of different types
a loop.
FILE HANDLING TEXT MODE FORMATTED and UNFORMATTED I/O FUNCTIONS / TEXT MODE UNFORMATTED I/O FUNCTIONS / LINE INPUT OUTPUT
Files are very useful in storing information for future analysis and retrieval. C language defines a number of BINARY MODE It is simple to input or output a single character at a time and C has certain functions, which accept, or prints a
functions for handling files. single character at a time. The getchar() function reads the next character from the standard input; getc(fp) reads
Similar to the functions printf() and scanf(), there are equivalent function which read or write data to files. These
Basic file handling Program the next character from the stream fp. Both return the next character and if the next character cannot be read,
are called fprintf and fscanf and take the file pointer as an additional first argument. Example,
When a file has to be used either for writing to it or for reading from it, it has to be opened in corresponding mode. the non-character constant EOF is returned.Similarly, the function putchar(c) writes the character c to standard
#include<stdio.h>
The function fopen()is used for opening a file; certain systems may also provide specialized ways of opening output; putc(c, fp) writes the character c to the stream fp.
FILE *fptr;
streams connected to I/O devices in a more exotic ways. The following program, illustrates how the functions getchar() and putc() can be used to accept the character
void main()
a. fopen() from the user and copy those contents into a file respectively.
{
#include<stdio.h>
A successful call to fopen returns a pointer of type FILE *, that is, “pointer to FILE”, where FILE is a special type fptr = fopen(“temp.txt”, “w+”);
main()
defined by <stdio.h>. A FILE * (also called “file pointer”) is handled by which an I/O stream in C is referred. I/O fprintf(fptr,”Hello world\n”);
{
functions which do not assume standard input or standard output accept a FILE * argument telling them which fprintf(fptr,”This line contains numbers : %d\n”,20);
char c;
stream to read from or write to. The prototype of fopen is as follows: fprintf(fptr,“%d”,30);
int i;
FILE * fopen(char *filename, char *mode) }
FILE *fp;
The first string is the file name, which can be any string (simple filename or complicated pathname), which is Executing the above program result in a file called temp.txt containing the following text
fp = fopen(“dat.txt”,”w+”);
acceptable to the underlying operating system. Hello world
printf(“Enter the characters”);
The second string is the mode in which the file should be opened. The simple mode arguments are This line contains numbers: 20
while ((c= (char)getchar( ) )!= ‘0’)
1. r - open for reading 30
putc( c,fp );
2. w - open for writing (truncate file before writing, if it exists already) The following program illustrates reading from a file using fscanf ( )
}
3. a - open for writing, appending to file if it exists already #include<stdio.h>
Input: (Enter the characters)
When a file has to be used for reading and writing, the operator ‘+’ can be used. Thus modes “r+” and “w+” lets FILE *fptr;
God helps those who help themselves
reading and writing to the file. However, both cannot be done at the same time; To read or write the read/write void main()
0
indicator must be explicitly repositioned. A normal program in C using files often uses most of the statements {
The following program illustrates how the functions getc() and putchar() can be used to read information from a
used in the sample program given below : int x;
file and display it on the screen :
Example: char ch[80];
#include<stdio.h>
#include <stdio.h> fptr = fopen(“temp.txt”, “r”);
main()
main() fscanf(fptr,”%s”,ch);
{
{ printf(“%s”, ch);
int c;
FILE *fptr; fscanf(fptr,”%s”,ch);
FILE *fp;
fptr = fopen(“sample.dat”,”r+”); printf(“%s”, ch);
fp= fopen(“dat.txt”,”r+”);
if (fptr == NULL) }
while((c=getc(fp))!=EOF)
printf(“Cannot open the file”); Output:
putchar( c );
else Helloworld
}
printf(“File opened successfully”); When the above program is executed, it reads the data from the file called temp.txt and displays the content of
Output:
fclose(fptr); the file.
God helps those who help themselves
}
Output: (Assuming the file sample.dat exists) - File opened successfully
Text mode Unformatted I/O functions The above program would continue to read characters from the file and prints them on the screen till the EOF
Text mode – Formatted I/O functions
It is simple to input or output a single character at a time and C has certain functions, which accept, or prints a (End of File) is reached.
Similar to the functions printf() and scanf(), there are equivalent function which read or write data to files. These
single character at a time. The getchar() function reads the next character from the standard input; getc(fp) reads
are called fprintf and fscanf and take the file pointer as an additional first argument. Consider the following
the next character from the stream fp. Both return the next character and if the next character cannot be read, Line Input and Output (fgets, fputs, etc.)
example,
the non-character constant EOF is returned.Similarly, the function putchar(c) writes the character c to standard The function gets() and puts() offer a simple alternative to the use of scanf() and printf() for reading and displaying
#include<stdio.h>
output; putc(c, fp) writes the character c to the stream fp. strings. In the case of gets(), the string will be entered from the keyboard and will terminate with a newline
FILE *fptr;
The following program, illustrates how the functions getchar() and putc() can be used to accept the character character.
void main()
from the user and copy those contents into a file respectively. char* gets(char *line)
{
#include<stdio.h> reads the next line of text from the standard input and places the characters in the character array pointed to by
fptr = fopen(“temp.txt”, “w+”);
main() line. It returns a pointer to the line, unless it reaches end-offile, in which case it returns a null pointer.
fprintf(fptr,”Hello world\n”);
{ The function
fprintf(fptr,”This line contains numbers : %d\n”,20);
char c; int puts(char *line)
fprintf(fptr,“%d”,30);
int i; writes the string pointed to by line to the standard output and writes a ‘\n’ to terminate it. It returns a nonnegative
}
FILE *fp; value unless there is some kind of a write error, in which case it returns EOF.
Output: (a file called temp.txt containing the text)
fp = fopen(“dat.txt”,”w+”); The following example illustrates the use of gets and puts.
Hello world This line contains numbers: 20 30
Example: while ((c= (char)getchar( ) )!= ‘0’) The following program illustrates reading from a file using fscanf ( )
The function putc( c,fp ); #include<stdio.h>
int fputs(char *line, FILE *fp) } FILE *fptr;
#include<stdio.h> Input: (Enter the characters) void main()
main() God helps those who help themselves {
{ 0 int x;
char line[80]; The following program illustrates how the functions getc() and putchar() can be used to read information from a char ch[80];
gets(line); file and display it on the screen : fptr = fopen(“temp.txt”, “r”);
puts(line); #include<stdio.h> fscanf(fptr,”%s”,ch);
} main() printf(“%s”, ch);
Output: { fscanf(fptr,”%s”,ch);
How are you? int c; printf(“%s”, ch);
How are you FILE *fp; }
fp= fopen(“dat.txt”,”r+”); Output: Helloworld
int fputs(char *line, FILE *fp) while((c=getc(fp))!=EOF)

writes the string pointed to by line to the stream fp. Like puts, fputs returns a nonnegative value or EOF on error. putchar( c ); When the above program is executed, it reads the data from the file called temp.txt and displays the content of

Unlike puts, fputs does not automatically append a \n. Here is a program which demonstrates the use of fputs(). } the file.

#include<stdio.h> Output: Text mode Unformatted I/O functions


#include<conio.h> God helps those who help themselves It is simple to input or output a single character at a time and C has certain functions, which accept, or prints a
main() single character at a time. The getchar() function reads the next character from the standard input; getc(fp) reads
{ The above program would continue to read characters from the file and prints them on the screen till the EOF the next character from the stream fp. Both return the next character and if the next character cannot be read,
FILE *a; (End of File) is reached. the non-character constant EOF is returned.Similarly, the function putchar(c) writes the character c to standard
char b[21]="This is a test"; output; putc(c, fp) writes the character c to the stream fp.
a=fopen("t.t","w+"); The following program, illustrates how the functions getchar() and putc() can be used to accept the character
fputs(b,a); from the user and copy those contents into a file respectively.
Binary mode I/O Functions
getch(); #include<stdio.h>
“Binary” or “b” mode means that no translations are done by the stdio library when reading or writing the file.
} main()
Normally, the newline character “\n” is translated to and from some operating system dependent end-of-line
Output: {
representation (LF on Unix, CR on the Macintosh, CRLF on MS-DOS, or an end-of-record mark on record-
The program has no output. It writes the text to the file char c;
oriented operating systems).
int i;
On MS-DOS systems, without binary mode, a control-Z character in a file is treated as an end-of-file indication;
char *fgets(char *line, int max, FILE *fp) FILE *fp;
neither the control-Z nor any characters following it will be seen by a program reading the file.
reads the next line of input from the stream ‘fp’ and places the characters, including the ‘\n’, in the character array fp = fopen(“dat.txt”,”w+”);
In binary mode, on the other hand, characters are read and written verbatim; newline characters (In MS-DOS,
pointed to by line. The second argument, max, gives the maximum number of characters to be written to the printf(“Enter the characters”);
control-Z characters) are not treated specially.
array and is usually the size of the array. Like gets, fgets returns a pointer to the line it reads or a null pointer if it while ((c= (char)getchar( ) )!= ‘0’)
Binary files are mainly used in image handling programs. For general purposes, it is strongly recommended to
reaches end-of-file. Unlike gets, fgets does include the ‘\n’ in the string and it copies to the array. putc( c,fp );
use text files/text mode I/O operations.
Therefore, the number of characters in the line, plus the \n, plus the \0, will always be less than or equal to max. }

In the following program, the file “t.t” contains 17 characters. Input: (Enter the characters)

The function fgets accepts a number which is 1 more than the number of characters to be read.That is why it God helps those who help themselves

accepts18 since it has to be more. 0

#include<stdio.h> The following program illustrates how the functions getc() and putchar() can be used to read information from a

#include<conio.h> file and display it on the screen :

main() #include<stdio.h>

{ main()

FILE *a; {

char c[21]; int c;

a=fopen("t.t","r+"); FILE *fp;

fgets(c,18,a); fp= fopen(“dat.txt”,”r+”);

printf("The text from file is :\n %s",c); while((c=getc(fp))!=EOF)

} putchar( c );

Output: }

The text from file is: This is a test Output: God helps those who help themselves

printf(“Enter the characters”);


BINARY MODE I/O FUNCTIONS FILE POINTERS / INPUT and OUTPUT USING FILE POINTERS STRINGS – FORMATTED INPUT OUTPUT WITH STRINGS / SPECIAL
“Binary” or “b” mode means that no translations are done by the stdio library when reading or writing the file. C communicates with files using a new datatype called a file pointer. This type is defined within stdio.h, and CHARECTERS
Normally, the newline character “\n” is translated to and from some operating system dependent end-of-line written as FILE *. A file pointer called output_file is declared in a statement like
Formatted Input Output with File Pointers
representation (LF on Unix, CR on the Macintosh, CRLF on MS-DOS, or an end-of-record mark on record- FILE *output_file;
Similarly there are equivalents to the functions printf and scanf, which read or write data to files. These are called
oriented operating systems). Opening a file pointer using fopen
fprintf and fscanf. You have already seen fprintf being used to write data to stderr. The functions are used in the
On MS-DOS systems, without binary mode, a control-Z character in a file is treated as an end-of-file indication; Your program must open a file before it can access it. This is done using the fopen function, which returns the
same way, except that the fprintf and fscanf take the file pointer as an additional first argument.
neither the control-Z nor any characters following it will be seen by a program reading the file. required file pointer. If the file cannot be opened for any reason then the value NULL will be returned. You will
In binary mode, on the other hand, characters are read and written verbatim; newline characters (In MS-DOS, String Handling Functions
usually use fopen as follows
control-Z characters) are not treated specially. These are the third set of the printf and scanf families. They are called sprintf and sscanf.
if ((output_file = fopen("output_file", "w")) == NULL)
Binary files are mainly used in image handling programs. For general purposes, it is strongly recommended to fprintf(stderr, "Cannot open %s\n", "output_file"); sprintf
use text files/text mode I/O operations. fopen takes two arguments, both are strings, the first is the name of the file to be opened, the second is an access puts formatted data into a string which must have sufficient space allocated to hold it. This can be done by

character, which is usually one of: declaring it as an array of char. The data is formatted according to a control string of the same form as that for

“r” Open file for Reading printf.

“w” Create file for Writing sscanf


Keywords
“a” Open file for appending takes data from a string and stores it in other variables as specified by the control string. This is done in the same
Keywords – Predefined meaning
As usual, use the man command for further details by typing man fopen. way that scanf reads input data into variables.
Comments -- Ignored by the compiler
Standard file pointers in UNIX sscanf is very useful for converting strings into numeric v values.
Variable – Name given to the memory location
UNIX systems provide three file descriptors, which are automatically open to all C programs. These are Other String Handling Functions
Expression – Sequence of operands and operators
Stdin The Standard input. The keyboard or a redirected input file. As well as sprintf and sscanf, the UNIX system has a number of other string handling functions within its libraries.
Statement – Combination of c tokens
Stdout The Standard output. The screen or a redirected output file. A number of the most useful ones are contained in the <strings.h> file, and are made available by putting the line
Constant – Value which doesn’t change
Stderr The Standard error. This is the screen, even when output is redirected. This is #include <strings.h>
Data types – Inbuilt definition inside the c compiler.
the conventional place to put any error messages. near to the head of your program file.
Operators – Symbol which tell compiler to act
Since these files are already open, there is no need to use fopen on them. A couple of the functions are described below.
Control structures – Structures which alter the flow of control based on condition or without condition.
Closing a file using fclose strcpy(str1,str2) copies str2 into str1
Function - set of instructions that performs a particular task. It provides the modularity i.e. splitting the entire
The fclose command can be used to disconnect a file pointer from a file. This is usually done so that the pointer strcpy(str1,str2) compares the contents of str1 and str2. Returns 0(false)if both are equal.
task into some smaller sub tasks.
Array - collection of homogeneous elements stored in contiguous memory locations
can be used to access a different file. Systems have a limit on the number of files, which can be open Whole Line Input and Output using File Pointers
simultaneously, so it is a good idea to close a file when you have finished using it.
preprocessor - activities to be performed before processing Predictably, equivalents to gets and puts exist called fgets and fputs. The programmer should be careful in using
This would be done using a statement like
Structures - collection of heterogeneous Elements stored in independent Memory locations. them, since they are incompatible with gets and puts. gets requires the programmer to specify the maximum
fclose(output_file);
Unions - similar to the structures but the Members of union should share the same memory location. It makes number of characters to be read. fgets and fputs retain the trailing newline character on the line they read or
If files are still open when a program exits, the system will close them for you. However it is usually better to close
us to save memory. write, wheras gets and puts discard the newline.
the files properly.
Pointers - a variable which holds the address of the another identifier rather than storing the value. When transferring data from files to standard input / output channels, the simplest way to avoid incompatibility
scanf() -scan formatted. It means that the way which we follow to get input from the user is in some format. Input and Output using file pointers with the newline is to use fgets and fputs for files and standard channels too.
printf() -print formatted. It means that the output that has to be provide to user are in some format. Having opened a file pointer, you will wish to use it for either input or output. C supplies a set of functions to allow For Example, read a line from the keyboard using
getchar() -used to get the single character as input from the user. you to do this. All are very similar to input and output functions that you have already met. fgets(data_string, 80, stdin);
putchar() -used to give the single character as output to the user. Character Input and Output with Files and write a line to the screen using
gets() -used to get the string as input from the user. This is done using equivalents of getchar and putchar, which are called getc and putc. Each takes an extra fputs(data_string, stdout);
puts() -used to give the string as output to the user. argument, which identifies the file pointer to be used for input or output. Special Characters
file -is a group of related data stored in a place on the hard disk. putchar( c ) is equivalent to putc(c,stdout)
NULL EOF
Basic file operations - naming, opening, reading, writing and closing the file. getchar( c) is equivalent to getc(stdin)
NULL, The Null Pointer or Character
Fopen() -is a function helps to open a file in any of the following modes. r - read only, w – write only, a – append
NULL is a character or pointer value. If a pointer, then the pointer variable does not reference any object (i.e. a
fclose() -is a function used to close the file Formatted Input Output with File Pointers
pointer to nothing). It is usual for functions which return pointers to return NULL if they failed in some way. The
getc(),getw(),fscanf() -function used for read operations. Similarly there are equivalents to the functions printf and scanf, which read or write data to files. These are called
return value can be tested. See the section on fopen for an example of this. NULL is returned by read commands
Putc(),putw(),fprintf() -function used for write operations. fprintf and fscanf. You have already seen fprintf being used to write data to stderr.
of the gets family when they try to read beyond the end of an input file. Where it is used as a character, NULL is
Fseek(),ftell(),rewind() – functions used in random file activities. The functions are used in the same way, except that the fprintf and fscanf take the file pointer as an additional
commonly written as '\0'. It is the string termination character, which is automatically appended to any strings in
Ftell() – returns the current position of the file pointer. first argument.
your C program. You usually need not bother about this final \0', since it is handled automatically.
Fseek() – used to move the file pointer to the desired locations.
However it sometimes makes a useful target to terminate a string search. There is an example of this in the
Rewind() – takes the file pointer to the beginning of the file.
string_length function example in the section on Functions in C.
EOF, The End of File Marker
EOF is a character, which indicates the end of a file. It is returned by read commands of the getc and scanf
families when they try to read beyond the end of a file.
SUMMARY Input and Output using file pointers SUMMARY
C is a high-level programming language that forms the basis to other programming languages such as C++, Perl Having opened a file pointer, you will wish to use it for either input or output. C supplies a set of functions to allow An array in the computer's memory can be assumed as a row of consecutive spaces, each of which can store a
and Java. Variables are like containers in the computer's memory – one can store values in them and retrieve or you to do this. All are very similar to input and output functions that you have already met. data item, known as an ELEMENT.
modify them when necessary Character Input and Output with Files The C preprocessor is a program that is executed before the source code is compiled. C preprocessor commands
Local variable is the default storage for a variable in ‘C’ language. Its also called as automatic variables and This is done using equivalents of getchar and putchar, which are called getc and putc. Each takes an extra are called DIRECTIVES, and begin with a pound / hash symbol (#). No white space should appear before the #,
represented by the keyword ‘auto’ argument, which identifies the file pointer to be used for input or output. and a semi colon is NOT required at the end. A directive: #include is already introduced.
When a variable is declared outside the scope of any function that is outside the main function, then the variable putchar( c ) is equivalent to putc(c,stdout) #undef can be used to remove any already created a macro definition. This means the preprocessor will no longer
is called as a global variable. Such variables are called global variables. getchar( c) is equivalent to getc(stdin) make any more text substitutions associated with that word. Also, to change a definition, #undef should be used
Static variable can’t be reinitialized and it is the default storage class for global variables to undefine it, then #define should be used to redefine it.
Arithmetic operators are commonly used for performing arithmetic calculations. Relation operators are used to Formatted Input Output with File Pointers
compare numerical values. A pointer in C refers to a variable that holds the address of another variable
Similarly there are equivalents to the functions printf and scanf, which read or write data to files. These are called
CONDITIONAL BRANCHING is a term that is used to describe the way program flow diverges,depending on The main use of structure is to lump together collections of disparate variable types, so that they can conveniently
fprintf and fscanf. You have already seen fprintf being used to write data to stderr.
certain condition(s) be treated as a unit.
The functions are used in the same way, except that the fprintf and fscanf take the file pointer as an additional
The switch statement can help simplify things a little. It allows testing the value returned by a single expression A union is similar to a structure, except that it shares storage space between different members.
first argument.
and then executing the relevant bit of code. A pointer in C refers to a variable that holds the address of another variable.
Formatted Input Output with Strings
The term "looping" describes the way in which the program executes statements over and over again, before Structure is a collections of disparate variable types
These are the third set of the printf and scanf families. They are called sprintf and sscanf.
exiting the loop and continuing with program flow Structure is a good way of storing related data together
sprintf
In C, there are three types of loops: for loops, while loops and do while loops. It is possible to simulate each type A union is similar to a structure, except that it shares storage space between different members.
puts formatted data into a string which must have sufficient space allocated to hold it. This can be done by
by writing code using other loops
declaring it as an array of char. The data is formatted according to a control string of the same form as that for
Header File - Header files contain declarations to certain functions that may or may not have been used in a Displaying output is a common process, and there are a variety of functions that do exactly that. So far, the printf
printf.
program function was used in every example as a way of displaying program output.
EXPRESSIONS - EXPRESSIONS consist of a mixture of constants, variables and operators sscanf printf gives the programmer the power to print output onto the screen, and is relatively simple to use.
Const - The const keyword is used to define a constant takes data from a string and stores it in other variables as specified by the control string. This is done in the same The most basic way of reading input is by calling the function getchar. getcha reads one character from the
DECLARATION - To DECLARE a variable, means to reserve memory space for it. way that scanf reads input data into variables. ``standard input.''
Scnaf - The easiest way to input a value is to use the library function scanf sscanf is very useful for converting strings into numeric v values. A companion function is putchar, which writes one character to the ``standard output.''
Printf - Printf helps to print output onto the screen Whole Line Input and Output using File Pointers A conversion specification indicates how the corresponding argument value is to be converted and formatted
Switch - The switch allows testing the value returned by a single expression and then executing the relevant bit Predictably, equivalents to gets and puts exist called fgets and fputs. The programmer should be careful in using before being printed.
of code. them, since they are incompatible with gets and puts. gets requires the programmer to specify the maximum gets reads a whole line of input into a string until a newline or EOF is encountered. puts writes a string to the
number of characters to be read. fgets and fputs retain the trailing newline character on the line they read or output, and follows it with a newline character.
A function can be thought of as a mini-program, where a group of statements are executed when the function is write, wheras gets and puts discard the newline. The easiest way is to use a library function called scanf putc, putchar, puts and printf are all used to output values
called. When transferring data from files to standard input / output channels, the simplest way to avoid incompatibility The field width is the amount of space, in character positions, used to print the data item.
Only a limited amount of information is available within each function. Variables declared within the calling with the newline is to use fgets and fputs for files and standard channels too.
function can't be accessed unless they are passed to the called function as arguments. The only other contact a For Example, read a line from the keyboard using Files are very useful in storing information for future analysis and retrieval. C language defines a number of
function might have with the outside world is through global variables. fgets(data_string, 80, stdin); functions for handling files.
Local variables are declared within a function. They are created a new each time the function is called, and and write a line to the screen using When a file has to be used either for writing to it or for reading from it, it has to be opened in corresponding mode.
destroyed on return from the function. Values passed to the function as arguments can also be treated like local fputs(data_string, stdout); The function fopen()is used for opening a file.
variables. Special Characters The function gets() and puts() offer a simple alternative to the use of scanf() and printf() for reading and displaying
Static variables are slightly different; they don't die on return from the function. Instead their last value is retained, strings.
NULL
and it becomes available when the function is called again. C communicates with files using a new datatype called a file pointer. This type is defined within stdio.h, and
EOF
Global variables don't die on return from a function. Their value is retained, and is available to any other function, written as FILE *
NULL, The Null Pointer or Character
which accesses them. NULL is a character or pointer value. If a pointer, then the pointer variable does not reference any object
NULL is a character or pointer value. If a pointer, then the pointer variable does not reference any object (i.e. a
The RETURN TYPE is the data type of the RETURN VALUE of the function EOF is a character, which indicates the end of a file. It is returned by read commands of the getc and scanf
pointer to nothing). It is usual for functions which return pointers to return NULL if they failed in some way. The
The NAME is required as an identifier to the function, so that the computer knows which function is called. Naming families when they try to read beyond the end of a file.
return value can be tested. See the section on fopen for an example of this.
of functions follows the same set of rules as the naming of variables. Preprocessor - The C preprocessor is a program that is executed before the source code is compiled.
NULL is returned by read commands of the gets family when they try to read beyond the end of an input file.
Functions can take ARGUMENTS - a function might need extra information for it to work. Arguments are optional. Header Files - Header files contain declarations to functions and variables and have a .h extension
Where it is used as a character, NULL is commonly written as '\0'. It is the string termination character, which is
The function BODY is surrounded by curly brackets and contains the statements of the function. Stack - Stacks are dynamic and constantly changing objects
automatically appended to any strings in your C program. You usually need not bother about this final \0', since
The RETURN VALUE is the value that is passed back to the main program. Functions exit whenever a value is Queue - A queue is an ordered collection of items from which items may be deleted at one end
it is handled automatically. However it sometimes makes a useful target to terminate a string search. There is an
returned. that is called as the front of the queue
example of this in the string_length function example in the section on Functions in C.
A function that calls itself is said to be a RECURSIVE function. Recursive function calls can be less efficient than Structure - A struct is a collection of variables of different types
a loop. EOF, The End of File Marker
EOF is a character, which indicates the end of a file. It is returned by read commands of the getc and scanf
families when they try to read beyond the end of a file.
SUMMARY SUMMARY SUMMARY
C is a high-level programming language that forms the basis to other programming languages such as C++, Perl An array in the computer's memory can be assumed as a row of consecutive spaces, each of which can store a C is a high-level programming language that forms the basis to other programming languages such as C++, Perl
and Java. Variables are like containers in the computer's memory – one can store values in them and retrieve or data item, known as an ELEMENT. and Java. Dennis Ritchie developed it in 1972. He named it C because there was an existing programming
modify them when necessary The C preprocessor is a program that is executed before the source code is compiled. C preprocessor commands language called B.
Local variable is the default storage for a variable in ‘C’ language. Its also called as automatic variables and are called DIRECTIVES, and begin with a pound / hash symbol (#). No white space should appear before the #, Programs are written in C. Computers don't understand C - the code needs to be compiled and turned into
represented by the keyword ‘auto’ and a semi colon is NOT required at the end. A directive: #include is already introduced. machine (binary) code before the program can be executed.
When a variable is declared outside the scope of any function that is outside the main function, then the variable #undef can be used to remove any already created a macro definition. This means the preprocessor will no longer Binary code is the lowest level language and can be thought of as consisting of 1's and 0's. One should also get
is called as a global variable. Such variables are called global variables. make any more text substitutions associated with that word. Also, to change a definition, #undef should be used a C compiler to work with C. There are many free compilers available for C language.
Static variable can’t be reinitialized and it is the default storage class for global variables to undefine it, then #define should be used to redefine it. C language opens the doors to other languages like C++, Java and even JavaScript.
Arithmetic operators are commonly used for performing arithmetic calculations. Relation operators are used to
compare numerical values. A pointer in C refers to a variable that holds the address of another variable A function can be thought of as a mini-program, where a group of statements are executed when the function is
CONDITIONAL BRANCHING is a term that is used to describe the way program flow diverges,depending on The main use of structure is to lump together collections of disparate variable types, so that they can conveniently called.
certain condition(s) be treated as a unit. A function is CALLED (or INVOKED) when it is needed to branch off from the program flow and execute a group
The switch statement can help simplify things a little. It allows testing the value returned by a single expression A union is similar to a structure, except that it shares storage space between different members. of statements within that function. Once the statements in the function are executed, program flow resumes from
and then executing the relevant bit of code. A pointer in C refers to a variable that holds the address of another variable. the place where the function is called. A few functions: main, printf and scanf were already used.
The term "looping" describes the way in which the program executes statements over and over again, before Structure is a collections of disparate variable types The main function is special, in the way that it's called automatically when the program starts. In C, and other
exiting the loop and continuing with program flow Structure is a good way of storing related data together programming languages, it is possible to create customized functions.
In C, there are three types of loops: for loops, while loops and do while loops. It is possible to simulate each type A union is similar to a structure, except that it shares storage space between different members.
by writing code using other loops POINTERS are VARIABLES that store the memory address of other variables.Suppose if there is an integer
Header File - Header files contain declarations to certain functions that may or may not have been used in a Displaying output is a common process, and there are a variety of functions that do exactly that. So far, the printf variable, x. how to get the address of x using & is known, but how to store the hex value returned by &x?.This is
program function was used in every example as a way of displaying program output. where pointers come into play. A pointer is a variable, so it is declared just like a variable. The only difference is
EXPRESSIONS - EXPRESSIONS consist of a mixture of constants, variables and operators printf gives the programmer the power to print output onto the screen, and is relatively simple to use. that pointer variables must have the DEREFERENCE OPERATOR, *, before its name
Const - The const keyword is used to define a constant The most basic way of reading input is by calling the function getchar. getcha reads one character from the
DECLARATION - To DECLARE a variable, means to reserve memory space for it. ``standard input.'' Displaying output is a common process, and there are a variety of functions that do exactly that. So far, the printf
Scnaf - The easiest way to input a value is to use the library function scanf A companion function is putchar, which writes one character to the ``standard output.'' function was used in every example as a way of displaying program output. Similarly, getting input from the user
Printf - Printf helps to print output onto the screen A conversion specification indicates how the corresponding argument value is to be converted and formatted (or from external files) is another common process. To use the input and output functions, the Standard Input
Switch - The switch allows testing the value returned by a single expression and then executing the relevant bit before being printed. and Output header file (stdio.h) needs to be included in the program.
of code. gets reads a whole line of input into a string until a newline or EOF is encountered. puts writes a string to the
output, and follows it with a newline character. In C, stdin is the standard input file stream and refers to the keyboard. scanf is the equivalent off scanf, with the
A function can be thought of as a mini-program, where a group of statements are executed when the function is The easiest way is to use a library function called scanf putc, putchar, puts and printf are all used to output values stream set to stdin by default. The same applies to the other input functions already encountered: getc and gets
called. The field width is the amount of space, in character positions, used to print the data item. are fgetc and fgets with the input stream set to stdin. This section covers reading data from a file, not from stdin.
Only a limited amount of information is available within each function. Variables declared within the calling
function can't be accessed unless they are passed to the called function as arguments. The only other contact a Files are very useful in storing information for future analysis and retrieval. C language defines a number of
function might have with the outside world is through global variables. functions for handling files.
Local variables are declared within a function. They are created a new each time the function is called, and When a file has to be used either for writing to it or for reading from it, it has to be opened in corresponding mode.
destroyed on return from the function. Values passed to the function as arguments can also be treated like local The function fopen()is used for opening a file.
variables. The function gets() and puts() offer a simple alternative to the use of scanf() and printf() for reading and displaying
Static variables are slightly different; they don't die on return from the function. Instead their last value is retained, strings.
and it becomes available when the function is called again. C communicates with files using a new datatype called a file pointer. This type is defined within stdio.h, and
Global variables don't die on return from a function. Their value is retained, and is available to any other function, written as FILE *
which accesses them. NULL is a character or pointer value. If a pointer, then the pointer variable does not reference any object
The RETURN TYPE is the data type of the RETURN VALUE of the function EOF is a character, which indicates the end of a file. It is returned by read commands of the getc and scanf
The NAME is required as an identifier to the function, so that the computer knows which function is called. Naming families when they try to read beyond the end of a file.
of functions follows the same set of rules as the naming of variables. Preprocessor - The C preprocessor is a program that is executed before the source code is compiled.
Functions can take ARGUMENTS - a function might need extra information for it to work. Arguments are optional. Header Files - Header files contain declarations to functions and variables and have a .h extension
The function BODY is surrounded by curly brackets and contains the statements of the function. Stack - Stacks are dynamic and constantly changing objects
The RETURN VALUE is the value that is passed back to the main program. Functions exit whenever a value is Queue - A queue is an ordered collection of items from which items may be deleted at one end
returned. that is called as the front of the queue
A function that calls itself is said to be a RECURSIVE function. Recursive function calls can be less efficient than Structure - A struct is a collection of variables of different types
a loop.
Keywords SUMMARY SUMMARY
Keywords – Predefined meaning C is a high-level programming language that forms the basis to other programming languages such as C++, Perl An array in the computer's memory can be assumed as a row of consecutive spaces, each of which can store a
Comments -- Ignored by the compiler and Java. Variables are like containers in the computer's memory – one can store values in them and retrieve or data item, known as an ELEMENT.
Variable – Name given to the memory location modify them when necessary The C preprocessor is a program that is executed before the source code is compiled. C preprocessor commands
Expression – Sequence of operands and operators Local variable is the default storage for a variable in ‘C’ language. Its also called as automatic variables and are called DIRECTIVES, and begin with a pound / hash symbol (#). No white space should appear before the #,
Statement – Combination of c tokens represented by the keyword ‘auto’ and a semi colon is NOT required at the end. A directive: #include is already introduced.
Constant – Value which doesn’t change When a variable is declared outside the scope of any function that is outside the main function, then the variable #undef can be used to remove any already created a macro definition. This means the preprocessor will no longer
Data types – Inbuilt definition inside the c compiler. is called as a global variable. Such variables are called global variables. make any more text substitutions associated with that word. Also, to change a definition, #undef should be used
Operators – Symbol which tell compiler to act Static variable can’t be reinitialized and it is the default storage class for global variables to undefine it, then #define should be used to redefine it.
Control structures – Structures which alter the flow of control based on condition or without condition. Arithmetic operators are commonly used for performing arithmetic calculations. Relation operators are used to
Function - set of instructions that performs a particular task. It provides the modularity i.e. splitting the entire compare numerical values. A pointer in C refers to a variable that holds the address of another variable
task into some smaller sub tasks. CONDITIONAL BRANCHING is a term that is used to describe the way program flow diverges,depending on The main use of structure is to lump together collections of disparate variable types, so that they can conveniently
Array - collection of homogeneous elements stored in contiguous memory locations certain condition(s) be treated as a unit.
preprocessor - activities to be performed before processing The switch statement can help simplify things a little. It allows testing the value returned by a single expression A union is similar to a structure, except that it shares storage space between different members.
Structures - collection of heterogeneous Elements stored in independent Memory locations. and then executing the relevant bit of code. A pointer in C refers to a variable that holds the address of another variable.
Unions - similar to the structures but the Members of union should share the same memory location. It makes The term "looping" describes the way in which the program executes statements over and over again, before Structure is a collections of disparate variable types
us to save memory. exiting the loop and continuing with program flow Structure is a good way of storing related data together
Pointers - a variable which holds the address of the another identifier rather than storing the value. In C, there are three types of loops: for loops, while loops and do while loops. It is possible to simulate each type A union is similar to a structure, except that it shares storage space between different members.
scanf() -scan formatted. It means that the way which we follow to get input from the user is in some format. by writing code using other loops
printf() -print formatted. It means that the output that has to be provide to user are in some format. Header File - Header files contain declarations to certain functions that may or may not have been used in a Displaying output is a common process, and there are a variety of functions that do exactly that. So far, the printf
getchar() -used to get the single character as input from the user. program function was used in every example as a way of displaying program output.
putchar() -used to give the single character as output to the user. EXPRESSIONS - EXPRESSIONS consist of a mixture of constants, variables and operators printf gives the programmer the power to print output onto the screen, and is relatively simple to use.
gets() -used to get the string as input from the user. Const - The const keyword is used to define a constant The most basic way of reading input is by calling the function getchar. getcha reads one character from the
puts() -used to give the string as output to the user. DECLARATION - To DECLARE a variable, means to reserve memory space for it. ``standard input.''
file -is a group of related data stored in a place on the hard disk. Scnaf - The easiest way to input a value is to use the library function scanf A companion function is putchar, which writes one character to the ``standard output.''
Basic file operations - naming, opening, reading, writing and closing the file. Printf - Printf helps to print output onto the screen A conversion specification indicates how the corresponding argument value is to be converted and formatted
Fopen() -is a function helps to open a file in any of the following modes. r - read only, w – write only, a – append Switch - The switch allows testing the value returned by a single expression and then executing the relevant bit before being printed.
fclose() -is a function used to close the file of code. gets reads a whole line of input into a string until a newline or EOF is encountered. puts writes a string to the
getc(),getw(),fscanf() -function used for read operations. output, and follows it with a newline character.
Putc(),putw(),fprintf() -function used for write operations. A function can be thought of as a mini-program, where a group of statements are executed when the function is The easiest way is to use a library function called scanf putc, putchar, puts and printf are all used to output values
Fseek(),ftell(),rewind() – functions used in random file activities. called. The field width is the amount of space, in character positions, used to print the data item.
Ftell() – returns the current position of the file pointer. Only a limited amount of information is available within each function. Variables declared within the calling
Fseek() – used to move the file pointer to the desired locations. function can't be accessed unless they are passed to the called function as arguments. The only other contact a Files are very useful in storing information for future analysis and retrieval. C language defines a number of
Rewind() – takes the file pointer to the beginning of the file. function might have with the outside world is through global variables. functions for handling files.
Local variables are declared within a function. They are created a new each time the function is called, and When a file has to be used either for writing to it or for reading from it, it has to be opened in corresponding mode.
destroyed on return from the function. Values passed to the function as arguments can also be treated like local The function fopen()is used for opening a file.
variables. The function gets() and puts() offer a simple alternative to the use of scanf() and printf() for reading and displaying
Static variables are slightly different; they don't die on return from the function. Instead their last value is retained, strings.
and it becomes available when the function is called again. C communicates with files using a new datatype called a file pointer. This type is defined within stdio.h, and
Global variables don't die on return from a function. Their value is retained, and is available to any other function, written as FILE *
which accesses them. NULL is a character or pointer value. If a pointer, then the pointer variable does not reference any object
The RETURN TYPE is the data type of the RETURN VALUE of the function EOF is a character, which indicates the end of a file. It is returned by read commands of the getc and scanf
The NAME is required as an identifier to the function, so that the computer knows which function is called. Naming families when they try to read beyond the end of a file.
of functions follows the same set of rules as the naming of variables. Preprocessor - The C preprocessor is a program that is executed before the source code is compiled.
Functions can take ARGUMENTS - a function might need extra information for it to work. Arguments are optional. Header Files - Header files contain declarations to functions and variables and have a .h extension
The function BODY is surrounded by curly brackets and contains the statements of the function. Stack - Stacks are dynamic and constantly changing objects
The RETURN VALUE is the value that is passed back to the main program. Functions exit whenever a value is Queue - A queue is an ordered collection of items from which items may be deleted at one end
returned. that is called as the front of the queue
A function that calls itself is said to be a RECURSIVE function. Recursive function calls can be less efficient than Structure - A struct is a collection of variables of different types
a loop.
MATRIX ADDITION, SUBTRACTION AND MULTIPLICATION TO FIND SMALLEST and LARGEST ELEMENT IN AN ARRAY SWAPING TWO ARRAYS USING POINTERS
#include<stdio.h> #include <stdio.h>
#include<stdio.h> #define MAX_SIZE 100
#include<conio.h> void check(int arr[], int n) int main() {
void main() { int sourceArray[MAX_SIZE];
{ int small, large; int destinationArray[MAX_SIZE];
int i,j,c,r,k; small = large = arr[0]; int *srcPtr = sourceArray;
int a[20][20],b[20][20],ma[20][20],ms[20][20]; int *destPtr = destinationArray;
int mm[20][20]; for(int i = 1; i < n ;i++){ int SIZE;
clrscr(); if(arr[i] < small) small = arr[i];
printf("\n\t\tINPUT:"); printf("Enter size of array: ");
printf("\n\t\t------"); if(arr[i] > large) scanf("%d", &SIZE);
printf("\n\t\tEnter the value for row and column: "); large = arr[i];
scanf("%d%d",&c,&r); printf("Enter source array elements -\n");
printf("\n\t\tEnter the value for matrix A\n"); } for (int i = 0; i < SIZE; i++) {
for(i=0;i<c;i++) scanf("%d", (sourceArray+i));
{ printf("Smallest Number: %d\n",small); }
for(j=0;j<r;j++) printf("Largest Number: %d", large);
{ } printf("Enter destination array elements -\n");
scanf("%d",&a[i][j]); for (int i = 0; i < SIZE; i++) {
} int main() scanf("%d", (destinationArray+i));
printf("\n"); }
} {
printf("\n\t\tEnter the value for matrix B\n"); int n; for (int i = 0; i < SIZE; i++) {
for(i=0;i<c;i++) printf("Enter the size of array: "); int temp = *(srcPtr + i);
{ scanf("%d",&n); *(srcPtr + i) = *(destPtr + i);
for(j=0;j<r;j++) *(destPtr + i) = temp;
{ int arr[n]; }
scanf("%d",&b[i][j]); printf("Enter the elements of array: ");
} printf("Swapped Source Array: ");
printf("\n"); for(int i=0;i<n;i++) for (int i = 0; i < SIZE; i++) {
} scanf("%d",&arr[i]); printf("%d ", sourceArray[i]);
for(i=0;i<c;i++) check(arr, n); }
{ printf("\n");
for(j=0;j<r;j++) return 0; printf("Swapped Destination Array: ");
{ } for (int i = 0; i < SIZE; i++) {
ma[i][j]=a[i][j]+b[i][j]; printf("%d ", destinationArray[i]);
ms[i][j]=a[i][j]-b[i][j]; printf("\n\t\tOUTPUT:"); }
} printf("\n\t\t-------"); printf("\n");
} printf("\n\t\tThe addition matrix is:\n");
for(i=0;i<c;i++) for(i=0;i<c;i++) return 0;
{ { }
for(j=0;j<r;j++) for(j=0;j<r;j++)
{ {
mm[i][j]=0; printf("\t\t%d",ma[i][j]);
for(k=0;k<c;k++) }
{ printf("\n");
mm[i][j] +=a[i][k]*b[k][j]; }
} printf("\n\t\tThe subtraction matrix is:\n");
} for(i=0;i<c;i++)
} {
SWAPING 2 VALUES FILE COPYING for(j=0;j<r;j++)
{
#include<stdio.h> #include<stdio.h>
printf("\t\t%d",ms[i][j]);
#include<conio.h> #include<stdlib.h>
}
void main() void main()
printf("\n");
{ {
}
int a,b,c; FILE *fp1,*fp2;
printf("\n\t\tThe multiplication matrix is:\n");
clrscr(); int i;
for(i=0;i<c;i++)
printf("enter any two values\n"); clrscr();
{
scanf("%d,%d"a,b); printf("\n\t\tINPUT:");
for(j=0;j<r;j++)
printf("values before swap:%d,%d"a,b); printf("\n\t\t------");
{
c=a; printf("\n\t\tCreated a text file name as sample.txt
printf("\t\t%d",mm[i][j]);
a=b; with the content like 'This is my first program.'");
}
b=c; if((fp1=fopen("D:\sample.txt","r"))==NULL)
printf("\n");
printf("\n values after swap: %d,%d"a,b); {
}
getch(); printf("Cannot open file\n");
getch();
} exit(1);
}
}
if((fp2=fopen("D:\sampledup.txt","w"))==NULL)
REVERSE STRING {
INPUT:
Enter the value for row and column: 2 2
#include <stdio.h> printf("cannot open file\n");
Enter the value for matrix A
#include <string.h> exit(1);
4 3
void rev(char* s) { }
6 2
int l = 0; // Initialize l and r pointers while((i=getc(fp1))!=EOF)
Enter the value for matrix B
int r = strlen(s) - 1; putc(i,fp2);
8 1
char t; fclose(fp1);
0 5
while (l < r) { // Swap characters till l and r meet fclose(fp2);
t = s[l]; // Swap characters printf("\n\t\tOUTPUT:");
OUTPUT:
s[l] = s[r]; printf("\n\t\t-------");
The addition matrix is:
s[r] = t; printf("\n\t\tThe text copied into file sampledup.txt
12 4
l++; // Move pointers towards each other as follows\n");
6 7
r--; printf("\n\t\t");
The subtraction matrix is:
} fp2=fopen("D:\sampledup.txt","r");
-4 2
} while((i=getc(fp2))!=EOF)
6 -3
int main() { putchar(i);
The multiplication matrix is:
char s[100] = "abcde"; fclose(fp2);
32 19
rev(s); // Reversing s getch();
48 16
printf("%s", s); }
return 0;
} INPUT:
Created a text file name as sample.txt with the content like This is my first program.

OUTPUT:
The text copied into file sampledup.txt as follows
This is my first program.
REVERSE AN ARRAY USING POINTERS REVERSE OF A STRING USING POINTERS CONCATENATE TWO STRINGS USING POINTERS
#include <stdio.h> #include <stdio.h> #include <stdio.h>
#include <string.h> void stringConcat(char *dest, const char *src)
#define SIZE 10 {
void reverseString(char *str) { while (*dest != '\0') {
int main() { char *start = str; dest++;
int arr[SIZE] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}; char *end = str + strlen(str) - 1; }
int *startPtr = arr; char temp; while (*src != '\0')
int *endPtr = arr + SIZE - 1; {
while (start < end) { *dest = *src;
// Reverse the array using pointers temp = *start; src++;
while (startPtr < endPtr) { *start = *end; dest++;
int temp = *startPtr; *end = temp; }
*startPtr = *endPtr; *dest = '\0';
*endPtr = temp; start++; }
startPtr++; end--; int main() {
endPtr--; } char str1[100] = "Hello, ";
} } char str2[] = "world!";
stringConcat(str1, str2);
// Display the reversed array int main() { printf("Concatenated string: %s", str1);
printf("Reversed Array: "); char str[100]; return 0;
for (int i = 0; i < SIZE; i++) { printf("Enter a string: "); }
printf("%d ", arr[i]); gets(str);
}
reverseString(str);
return 0; printf("Reversed string: %s", str);
TO FIND ODD or EVEN NUMBER
} return 0; #include<stdio.h>
} #include<conio.h>

TO ADD TWO NUMBERS USING POINTERS void main( )


{
int num;
#include <stdio.h>
DISPLAY THE REVERSE NUMBER. clrscr();
#include <stdio.h> printf(“Enter the number”);
void main() {
main() scanf(“%d”,&num);
int num1 = 5, num2 = 10;
{ if (num%2==0) /* remainder after division by 2*/
int *ptr1, *ptr2, sum;
int n,reverse; printf(“\nThe number is even”);
clrscr(); else
ptr1 = &num1;
printf("Enter any positive number"); printf(“\nThe number is odd”);
ptr2 = &num2;
scanf("%d",&n); getch();
sum = *ptr1 + *ptr2;
while (n!=0) }
{
printf("First number: %d\n", *ptr1);
reverse=n%10;
printf("Second number: %d\n", *ptr2);
printf("%d",reverse);
printf("Sum of the numbers: %d\n", sum);
n=n/10;
}
}
getche();
}
TO PRINT PRIME NUMBERS - 1 to 100. N FIBONACCI NUMBEERS. SUM OF N NUMBERS
#include<stdio.h> #include <stdio.h> #include<stdio.h>
#include<conio.h> int main(void) { #include<conio.h>
void main() int n; int main()
{ int i; {
int i,n=1; int current; int i, n, num, sum=0;
clrscr(); int next; printf("Enter the value of n: ");
printf(“Prime numbers between 1 to 100 are:\n”); int twoaway; scanf("%d", &n);
while(n<=100) printf("How many Fibonacci numbers do you want to printf("Enter %d numbers: ", n);
{ compute? "); for(i=0; i<n; i++)
i=2; scanf("%d", &n); {
while(i<n) if (n<=0) scanf("%d", &num);
printf("The number should be positive.\n"); sum = sum+num;
if(n%i==0) else { }
break; printf("\n\n\tI \t Fibonacci(I) printf("\nSum of all %d numbers = %d", n, sum);
else \n\t=====================\n"); getch();
i++; next = current = 1; return 0;
} for (i=1; i<=n; i++) { }
if(i==n) printf("\t%d \t %d\n", i, current);
printf(“%d\t”,n); twoaway = current+next;
n++; current = next;
} next = twoaway;
SUM OF N NATURAL NUMBERS
getch(); } #include<stdio.h>
} } #include<conio.h>
} int main()
{
TO FIND PRIME NUMBET OR NOT. int i, n, sum=0;
ADD TWO NUMBERS – PRINT THE SUM printf("Enter the value of n: ");
#include <stdio.h>
int main(void) { #include <stdio.h> scanf("%d", &n);

int n; int main(void) { for(i=1; i<=n; i++)

int i; int first, second; sum = sum+i;

int flag; printf("Enter two integers > "); printf("\nSum of %d natural numbers = %d", n, sum);

printf("Enter value of N > "); scanf("%d %d", &first, &second); getch();

scanf("%d", &n); printf("The two numbers are: %d %d\n", first, return 0;

for (i=2, flag=1; (i<(n/2)) && flag; ) { second); }

if ((n % i) == 0) printf("Their sum is %d\n", first+second);

flag = 0; }
PRINT MULTIPLICATION TABLE - number entered by the user
else MULTIPLY TWO NUMBERS.
#include<stdio.h>
i++; #include<stdio.h>
#include<conio.h>
} #include<conio.h>
void main ( )
if (flag) void main ()
{
printf("%d is prime\n", n); {
int num,i;
else int number1, number2; /* Numbers to be multiplied*/
clrscr();
printf("%d has %d as a factor\n", n, i); int result; /* Result stores the result of multiplication*/
printf(“Enter the number”);
} clrscr();
scanf(“%d”,&num);
printf (“Enter two numbers”);
for (i=1;i<=10;i++)
scanf(“%d%d”, &number1,&number2);
{
result=(number1*number2);
printf(“\n%d*%d = %d”,num,i,num*i);
printf(“The result is:%d”, result);
}
getch();
getch();
}
}

You might also like