Professional Documents
Culture Documents
Introduction of Programming
Data Types.
Loop.
Preprocessor Directives.
Pointer.
1
Introduction to Programming Languages
The difference b/w Procedural and Non-Procedural language is illustrated in following example:
Procedural language : Like I am sit on a Taxi and give directives to Taxi Driver….The directions might go as :
Drive 600 yards forward, Turn right then Drive 300 yards forward then Stop.
Non-Procedural language: You would simply tell the Driver what you want. “Take me to Tariq Road”
Where C Stands
C is often classified a middle-level language. C is b/w these two categories i.e High level language and Low level
language.
Learning Language
Communication with a computer involves speaking the language the computer understand. Learning any Computer
language is like the same as we learn other spoken languages.
If we want to learn English we first learn alphabets or characters used in the language, then word and then form a
sentence and sentence are combined to form a paragraph. Learning C is similar.
Steps in learning C:
C Character Set
Alphabets A,B,………….Y,Z
A,b,…………..y,z
Digits 0,1,2,3,4,5,6,7,8,9
Special Symbols ~ ! @ # % ^ ` & * ( ) _ - + = | \ [ ] { } : ; “ ‘ < > ? /
2
Directory Structure of C
C :\TC BIN ( Bin Folder contains TC.exe file to execute IDE)
INCLUDE ( Header Files is like text files …….extension .h)
LIB ( Routines for performing specific task…extension .lib)
Don’t change the name of the sub-directories.
Executable Files
Executable files are stored in the sub directory BIN. The most important exe. File is TC.exe.
Library Files
If a programmer uses a function such as printf() to display text on the screen, the code to create the display is
contained in a library file.
A library file has unique characteristics: only those parts that are necessary will be linked to a program, not
the whole file.
Turbo C features an IDE, which provides a platform to C programmers. There is another completely different way to
develop C programs in Turbo C. This is a traditional command – line system, in which editing, compiling, linking,
debugging and executing are performed in Dos environment.
Two different language translators Programs are used to translate High level languages :
1. Compilers 2. Interpreters 3. Assembler
Compiler: A compiler translates a whole program, called the source code, into machine language all at one
time before the program is executed.
Once converted, the program is stored in machine readable form called the object code.
Linking
The text editor produces .c source file, which go to the compiler, which produces .obj files, which go to the
linker, which produces .exe executable files.
Stdio.h
Cs.lib
4
IDE (Integrated Development Environment) & Command line Development
Turbo C features an IDE, which provides a platform to C programmers. There is another completely different way to
develop C programs in Turbo C. This is a traditional command – line system, in which editing, compiling, linking,
debugging and executing are performed in Dos environment.
IDE : It’s also referred to as the Programmer’s Platform. It is a screen display with windows and Pull-down menus.
IDE provides all necessary operations for the development of your C Program, including editing, compiling, linking,
and Program execution. You can even debug your program within the IDE.
Function Definition: All C Programs are divided into units called “Functions”. No matter how many functions there
are in C program, main( ) is the one to which the control is passed from the operating System, when the program is run,
it is the first function to execute.
The word “void” preceding “main” specifies that the function main( ) will not return a value. The second “void”,
in parentheses ------ ( ), specifies that the function takes no argument.
Conclusion: C program consists of functions. The main( ) function is the one to which control is passed when the
program is executed.
Function Define: Our program begins the way all C functions do with a name followed by parentheses. This signals
the compiler that a function is being defined.
Delimiter (Draws a boundary)…{ }……”{“ The Opening Brace indicates that the opening the block of code and
ending Brace “}” terminates the block of code.
Statement Terminator: A statement in C is terminated with a semicolon ( ; ). Semicolon terminates the line. Each
instruction in a C is written as a separate statement.
Structure of a C Program
closing brace to
5
delimit body of function
Function Basics:
Function is always in small case.
No space b/w function name and parenthesis…..like printf ( ) is wrong.
If you are using INPUT and OUTPUT function you have to INCLUDE header file….#include <stdio.h>
and #include <conio.h>.
Function always return a value.
Why was the digit 2 printed, and what effect does the %d have.
String on the left……..and value on the right. The two arguments are separated by a comma.
Format Specifier:
The format specifiers tells printf( ) where to put a value in a string and what format to use in printing the value.
%d tells printf( ) to print the value 2 as decimal integar. Other specifier could be used for the number 2. Like %f
could cause the 2 to be printed as a floating point number.
Printing Strings:
Scanf( ) Function:
scanf ( ) is an input function.
The scanf( ) function can accept input to several variable at once.
The scanf( ) function use ampersand ( & ) preceding the variable names used as arguments.
6
“Fundamentals of C Program”
Constant: A constant is the quantity/value that doesn’t change. This value can be stored at a location in the
memory of the computer. int num=1;
Variable: Variable may be the most fundamental aspect of any computer language. A variable is a space in the
computer’s memory set aside for a certain kind of data and given a name for easy reference.
Variable are used so that the same space in memory can hold different values at different times.
Rules for Variable Names
Variable Definition:
int num; is an example of variable definition.
In a C program all variables must be defined. If you have more than one variable of same type, you can define them
all with one type name, separating the variables name with commas.
Any variable used in a program must be declared before using it in any statement.
When you define variable, the compiler sets aside an appropriate amount of memory to store that variable.
Defining and Declaring Variable: Variable definition specify the name and type of variable, and also set aside
memory space for the variable. A variable declaration in contrast specifies the variable’s name and data type, but
doesn’t set aside any memory for the variable. In most cases the word “Declaration” is used for both meanings.
Initializing Variable: It’s possible to combine a variable definition with an assignment operator so that a variable
is given a value at the same time it’s defined……like
int num = 5;
float num1 = 10.5;
Keywords:
Keywords are the words whose meaning has already been explained to the C compiler (to the Computer).
The Keywords cannot be used as variable names b/c if we do so we are trying to assign a new meaning to the
keyword. It’s safer not to mix up the variable name and the keywords.
There are only 32 keywords available in C some of them are …….
void, if, else, switch, case, default do, for, while, float, int, short, long, signed, unsigned….etc etc.
Operators: Operators are words or symbols that cause a program to do something to variable. There are many
kinds of operators we’ll mention the most common one. Arithmetic and Relational operators and Increment
and Decrement operators.
1. Arithmetic Operators........ ( +, - , * , / , % )
2. Relational Operators…( <, >, <=, >=, ==, !=) which is (less than, greater than, less than equal to, not equal to)
3. Logical Operators………...( &&, ||, !) which is (AND, OR, !)
7
Arithmetic Operator:
Precedence Operator Description
1st * / % Multiplication, division, remainder
2nd +- Addition, subtraction
3rd = Assignment
Note : The fact that (*) and (/) have a higher precedence than (+) and (-).
Remainder operator is used to find the remainder when one number is divided by another.
Escape Sequences: The symbol ( \ ) backslash is considered an escape character. The tab and new line are the
most often used escape sequence.
\n Newline ( for new line)
\t Tab ( moves next 8 space wide field )
\b Backspace ( moves the cursor one space left )
\’ Single quote ( print single quote)
\” Double quote ( print double quote)
\\ Backslash ( print backslash )
Format Specifiers: we use format specifier ( such as %d or %c ) is used to control what format will be used by
printf( ) function to print out a particular variable.
8
Data Type: Note short integer are the same as integer.
In fact short is nothing but our ordinary integer, which we are using all the time without knowing that it was a short
integer.
9
32768
65535
Unsigned almost double the size of the value that it can hold and reserve the same 2 Bytes.
Unsigned integer is nothing but a short unsigned integer.
Up to now we have used sequenced control structure in which the various steps are executed sequencly i.e in the same
order in which they appear in the program.
Computer languages can perform different sets of actions depending on the circumstances. C has 3 major decision-
making structure.
1. if statement
2. if-else statement and
3. switch statements
if (char= = ‘y’)
printf(“You type y”); Terminating semicolon
Structure of if statement
Keyword Body of if statement
10
In the while statement, if the condition is true, the statement in the body of the loop will be executed over and over
until the condition becomes false: In “if “ statement they will be executed only once.
The && and || operators allow two or more conditions to be combined in an if statement.
The switch statement: The switch statement is similar to else-if statement but it shows clearer format. The control
statement which allow us to make decision from a number of choices is called switch…..or switch-case-default.
main( )
{
int i = 2;
clrscr( ); integer expression
switch( i )
keyword {
case 1:
printf(“ I am in case 1 \n”);
break;
case 2:
printf(“ I am in case 2 \n”);
break;
default:
printf(“ I am in default \n”);
}
}
Tips of switch:
1. You can place cases in any order you like.
11
2. You can also allowed to use char values in case and switch.
3. Multiple statements can easily executed in each case.
4. If we have no default case, then the program will continue with the next instruction ( if any ) that follows the
control structure.
5. Is switch a replacement for if.
Yes ……because it offers a better way of writing programs as compared to if.
No …….because in certain situations we are left no choice but to use if.
The disadvantage of switch is that one cannot use case like that……..case i<=20.
Loop
The program that we have developed so far used either sequential or a decision control structure. The ability to perform
a set of instruction repeatedly. It means we can repeat some portion of the program either a specified number of times
or until a particular condition is being satisfied. This repetitive operation is done through a loop control structure.
We can use three methods to repeat a part of a program.
1. Using a for loop.
2. Using a while loop.
3. Using a do-while loop.
The for loop: It’s most popular looping control. The for loop specify three things about a loop in a single line.
keyword
semicolon semicolon
Body of the for loop: The statement in for loop is terminated with a semicolon, where as the for with the loop
expression is not. That is because the entire combination of the for keyword, the loop expression and the statement
constituting the body of the loop are considered to be a single C statement.
12
int i ;
Operation of for loop: First the initializing expression is executed, then the test expression is checked. If the test
expression is false to begin with, the body of the loop will not be executed at all. If the condition is true, the body of
the loop is executed and following that, the increment expression is executed. The loop will continue to run until
the test expression becomes false……..count becomes 10……at which time the loop will end.
C permits flexibility in the writing of the for loop. For instance…..more than one expression can be used for the
initialize expression and for the increment expression by placing comma b/w them. So that several variables can be
initialized and incremented at once.
Braces { } will be used when we use more than one statement in the body of for loop.
The while loop: The second kind of loop structure available in C is the while loop.
while( count<10)
keyword {
printf(“%d”,count);
count++;
}
The initializing is now included in a variable declaration or The loop variable count is initialized outside the loop in
the declaration like…………….int count=0;
When the loop is first entered, the condition (count<10) is tested. If it’s false, the loop terminates. If it’s true, the
body of the loop is executed.
Increment of the expression can be perform in the body of while loop (count is incremented by the ++ operator).
The do while loop: The last of the three loops in C is do while loop.
The loop is very similar to the while loop----the difference is that in the do loop the test condition is evaluated after
the loop is executed, rather than before.
do
{
countl++; Body of loop
printf(“%d”, count);
}
13
while(count<10); semicolon
The important thing in this loop is that, unlike for and while loops, is terminated with a semicolon.
The body of the loop is first executed, then the test condition is checked, if the test condition is true, the loop is
repeated, if it is false loop terminates.
The important point to notice is that the body of the loop will always be executed at least once, since the test
condition is not checked until the end of the loop.
gotoxy(10,5);
printf("Enter Any Key.........");
ch=getch();
printf("You Press.........%c",ch);
gotoxy(10,10);
printf("Enter Another Key.........");
ch=getche();
printf("You Press.........%c",ch);
gotoxy(10,15);
printf("Enter Another Key.........");
ch = getchar();
gotoxy(40,15);
printf("You Press.........%c",ch);
getch();
PREPROCESSOR DIRECTIVE:
In this if we mention anything will be mentioned above void main: It’s defined as
1) #include 2) #define
#include:
#include: necessary to define it above void(main)
syntax :- #include <filename.h>
< >is used to find header file in include Directory. But if any include file is found in your own directory or drive
then:- syntax:- #include “c:\hello.h”
Note: If include file is found in any specific drive then < > is not used.
MACRO:
#define:
If we want to define macro then we use #define. If we define #define above main( ) it will be reserve only for that file
or for that program.
Macro will be define for one line like:
15
wait
}
Macro calls are like function calls but there are some difference. The difference are :
In a Macro call the preprocessor replaces the macro template with its macro expansion.
Whereas in a Function call the control is passed to a function along with certain arguments, some
calculations are performed in the function and a useful value is returned back from the function.
Usually Macros makes the program to run faster but increase the program size, whereas function make
the program smaller and compact.
If we use a macro hundred times in a program, the macro expansion goes to our source code at hundred
different places, thus increases the program size. In contrast, if a function is used, then even if it is called
for hundred different places in the program, it only take the same space in a program.
Function
A Prototype declares a function ( It‘ll be terminated)
A function call executes a function ( It‘ll be terminated)
A function definition is the function itself ( It‘ll not terminated)
All the variables were defined by name and data type before they were used. The function is declared in a similar way at the beginning of
the program before it is called.
The Prototype tells the compiler the name of the function, the data type the function returns ( If any ), and the number and data types of the
function’s arguments. _________ void sqr( void ) means …the function returns nothing and takes no arguments: hence two voids.
Prototype is written before the main( ) function.
The key thing to remember about the prototype is that the data type of the return value must agree with that of the declarator in the
function definition, and the number of arguments and their data types must agree with those in the function definition.
Calling a Function means executing a function. As we use library function like ( printf( ) and scanf( ) ) …..we can also call our own
function by simply using its name, including the parentheses following the name. The parentheses let the compiler know that you are
referring to a function and not to a variable.
sqr( ); _________________( Ends with semicolon )
16
1. It immediately transfers control from the function back to the calling program.
2. And Second, whatever is inside the parenthesis following return is return as a value to the calling program.
Note: return is not necessary written at the end of function, it can occur anywhere in the function.
function name
For example: The prototype for the printf( ) function is stored in the file stdio.h ( along with the prototypes for
many other functions ).
UDF are functions which we define by our self. There are four categories of UDF.
17
Pointer:
A pointer provides a way of accessing a variable, without referring to the variable directly. The mechanism used for
this is the address of the variable.
In order to use a pointer we must define a pointer as like any variable, but pointer variable set aside to store
the address.
getch();
int sum(int *x,int *y) // Function definition
{
*x = *x + 5;
*y = *y + 10;
return(*x+*y);
}
int *p;
In both cases, the difference between declaring an integer and a pointer that points to an integer (we'll
say "pointer-to-int" from now on) is a single character.
If you declare many pointers on one line in C, you must make sure to put a * by each of the variables.
Also, you can put ints next to pointers-to-ints in a declaration by not putting a * next to the int.
Confused? Try reading some of the next examples, and see if they help visualize the concepts.
Notice that you can put both variables and pointers-to-variables on the same line? On the second
line, both a character (ch) and a pointer-to-character (name) were both declared. On the third line, a
pointer-to-float (num1) and a float (num2 ) were both declared.
Watch Out!
19
and think that you are declaring num1 and num2 both to be of type pointer-to-float. This is NOT the
case, however. The * only applies to the variable it's right in front of, in this case num1. By making
your *'s touch the front of your variables, you can avoid this mistake. Here's what the improved,
easier-to-understand version looks like:
float *num1, num2;
However, if you wanted to declare two pointers-to-float, this is what it looks like:
Notice that they both must have *'s (asteric sign) next to them, so C knows they are pointers.
int *num;
Note: ‘*’ In a definition, it means “pointer data type” just as integer means “integer data type”.
*num = *num1 + 5;
Here it means something else: “Value pointed to by”
20