Vous êtes sur la page 1sur 30

visit @ http://www.infotechcomputer.

in

“Jh x.ks'kk; ue%“

„C Language Basics‟

 Introduction of Programming.

 Directory Structure of C Language.

 Integrated Development Environment.

 Data Types.

 Printf , Scanf Functions ,Getch function.

 Constants , Variables , Keywords , Operators, Escape Sequence, Format Specifier.

 Decision Making In C Using if statement & switch case default.

 Loop.

 User Defined Functions.

 Preprocessor Directives.

 Pointer.

INFOTECH Computer Education, Kishangarh (Raj)  9829171122 Page 1


visit @ http://www.infotechcomputer.in

Programming languages are classified into two different types.


 Procedural & Non-Procedural

 Procedural languages specify how something is accomplished.


 Non Procedural languages specify what is accomplished without going into detail of how.

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”

INFOTECH Computer Education, Kishangarh (Raj)  9829171122 Page 2


visit @ http://www.infotechcomputer.in

 C is a programming language developed at AT & T’s Bell laboratories of USA in 1972.


 It was designed and written by a man named Dennis Ritchie.
 C is so popular b/c it is reliable, simple and easy to use.

 C is often classified a middle-level language. C is b/w these two categories i.e High level
language and Low level 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 English:

Alphabets Words Sentence Paragraph

Steps in learning C:

Alphabets Constant Instructions Program


Digits Variables
Special Symbols Keywords

 Alphabets A,B,………….Y,Z
A,b,…………..y,z
 Digits 0,1,2,3,4,5,6,7,8,9
 Special Symbols ~ ! @ # % ^ ` & * ( ) _ - + = | \ * + , - : ; “ ‘ < > ? /

INFOTECH Computer Education, Kishangarh (Raj)  9829171122 Page 3


visit @ http://www.infotechcomputer.in

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 are stored in the sub directory BIN. The most important exe. File is TC.exe.

 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.

 Header Files : The sub directory called INCLUDE contains header files. These files also called
 “Include files” are text files like the one you generate with word processor.
 Header files can be combined with your program before it is compiled, in a same way that a
typist can insert a standard heading in a business letter.
 Each Header file has a .h file extension.
 Header files serve several purposes. You can place statements in your program listing that
are not program code but instead message to the compiler. These messages called compiler
directives, can tell the compiler such things as the definition of words and phrases used in
your program.
 Some useful compiler directives have been grouped together in header files, which can be
included in your source code of your program before it goes to the compiler.

INFOTECH Computer Education, Kishangarh (Raj)  9829171122 Page 4


visit @ http://www.infotechcomputer.in

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.

Like: ABC COMPILING 100101000010

SOURCE COMPILER OBJECT


CODE CODE CODE

 The Object code can be immediately executed anytime thereafter.


 Interpreter: A Interpreter translates a program into a machine language one line at a time,
executing each line of the program after it is translated.
 With most Interpreters, the machine readable form is not stored in main memory or on a
secondary storage medium. Therefore, the program must be interpreted each time it is
executed.

 After you’ve written the source file for your program, you need to turn it into an executable
file.
 Compiling: The program you typed is understandable to human being (at least if they know
C). However, it is not understandable to the microprocessor in your computer.

INFOTECH Computer Education, Kishangarh (Raj)  9829171122 Page 5


visit @ http://www.infotechcomputer.in
 There must be two versions of the program. The one you type which is Source file and the
machine language version, which is called the exe file (also called binary file).
 The Compiler which is a part of the IDE, translates this source code into machine language.

 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

Myprog.c MyProg.obj Myprog.exe


Compiler Linker

C Library

INFOTECH Computer Education, Kishangarh (Raj)  9829171122 Page 6


visit @ http://www.infotechcomputer.in

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.

Learning C, as it true with any language, is a largely a matter of practice.


void main(void)
{
printf(“INFOTECH”);
}

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.

INFOTECH Computer Education, Kishangarh (Raj)  9829171122 Page 7


visit @ http://www.infotechcomputer.in

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.

Function Name one statement

Opening Brace to void main(void)


delimit body of function
{
printf(“ INFOTECH “); semicolon to terminate
} each program statement

closing brace to
delimit body of function

This entire program maintains


in a function called main( )

 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.

INFOTECH Computer Education, Kishangarh (Raj)  9829171122 Page 8


visit @ http://www.infotechcomputer.in
Exploring the printf( ) function
 The general form of printf( ) statement is …..printf(“ <format String> “ , list of variables);
 The printf( ) is a very powerful and versatile function.
 printf( ) is a output function.

 The printf( ) function using a unique format for printing constants and variables.
 Like: format specifier
printf(“ This is a number two : %d”, 2);
 Output: This is a number two : 2
 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.

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.

 Using the format specifier we can print constants as well as numbers….like:


Printf(“ I am %s and I am %d years old”,”Any Name”,25);

left side Right side

 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.

INFOTECH Computer Education, Kishangarh (Raj)  9829171122 Page 9


visit @ http://www.infotechcomputer.in

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.

 A Variable name is any combination of 1 to 8 alphabets, digits or underscore.


 The first character in the variable name must be an alphabet.
 No commas and space are allowed within a variable name & No special symbol other than
underscore can be used in variable name.

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.

INFOTECH Computer Education, Kishangarh (Raj)  9829171122 Page 10


visit @ http://www.infotechcomputer.in
 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 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 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, !)

INFOTECH Computer Education, Kishangarh (Raj)  9829171122 Page 11


visit @ http://www.infotechcomputer.in

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.

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 )

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.

INFOTECH Computer Education, Kishangarh (Raj)  9829171122 Page 12


visit @ http://www.infotechcomputer.in

%c To print single character


%s To print String
%d To print signed decimal integer
%u To print unsigned decimal integer
%f To print floating point
%l (% L) Prefix used with %d %u to specify long integer ….like ( %ld, %lu)

Numeric Data

Whole Number Decimal Number Scientific Number


500 500.00 5002

Data Type Range Byte Format

Signed char -128 to 127 1 %c

unsigned char 0 to 255 1 %c

short signed int -32768 to +32768 2 %d

short unsigned int 0 to 65535 2 %u

long signed int -2147483648 to +2147483647 4 %ld

long unsigned int 0 to 4294967295 4 %lu

-38 38
float 10 to 10 4 %f

-138 138
double 10 to 10 8 %lf

INFOTECH Computer Education, Kishangarh (Raj)  9829171122 Page 13


visit @ http://www.infotechcomputer.in

 Integer, long and short:


Integers always occupies 2 Bytes in memory with a range of (32767 to -32768). Remember that
out of the 2 Bytes use to store an integer, the highest bit ( 16 th bit) is used to store the sign of
the integer.
 This bit is ( 1) if the number is negative (-ve) and 0 when the number is positive (+ve).
 C offers a variation in integer data type that is called long integer value.
The long integer occupies 4 Byte in memory and use long prior to integer in declaration like:
long int num;
long int num=10;
 The range of value that we can hold in long integer will expand tremendously.

In fact short is nothing but our ordinary integer, which we are using all the time without
knowing that it was a short integer.

 Integer, signed and unsigned:


If we know in advance that a value stored in a given integer will always be positive, so we can
use: unsigned int;
in signed integer all of the 15 bits out of 16 bits are used to store data value and the left most
bit is used to store sign of the number whereas in unsigned integer all of the 16 bits are used to
store data values.
 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.

 Chars, signed and unsigned:


The way there is a signed and unsigned integer similarly there are signed and unsigned chars.
 A signed char is same as our ordinary char and has a range from (-128 to 127) where as an
unsigned char has a range from 0 to 255.

INFOTECH Computer Education, Kishangarh (Raj)  9829171122 Page 14


visit @ http://www.infotechcomputer.in
 Float and Double:
Float occupies 4 Bytes in memory and can range from 10-38 to 1038 whereas double data type
occupies 8 Bytes in memory and has a range from 10308 to 10-308 .A variable of double can be
declared as:
double num;

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

 C uses the keyword “ if ” for basic decision making statement.


 The if statement is similar to the while .
 Without any operator, we cannot apply more than one condition.
 If you use only one statement after condition, then braces{ } sign is optional.
if( This Condition is True)
execute this statement;
 If the condition is true, then the statement is executed otherwise not.
 Relational Operators (= =, ! =, <, >, <=, >=) allow us to compare two values.
 Note that (=) is used for assignment, whereas (= =) is used for comparison of two quantities.

INFOTECH Computer Education, Kishangarh (Raj)  9829171122 Page 15


visit @ http://www.infotechcomputer.in

Relational Operator Conditional expression

if (char= = „y‟)
{
printf(“infotech”); Terminating semicolon

} Structure of if
If statement
Body of if statement

 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.

When the test expression is true, the statement will execute in the body of if statement. It does
nothing when it’s false. We can execute a group of statement if and only if the test expression is
not true.
This is a purpose of the else statement.

Condition expression Body of if statement


keyword
if (char= = „y‟)
keyword printf(“You type y”);
else
printf(“\nYou did not type y.”);
Body of else statement

INFOTECH Computer Education, Kishangarh (Raj)  9829171122 Page 16


visit @ http://www.infotechcomputer.in

 They are composed of double symbols ( || ), ( &&).


 Logical Operators have a lower precedence than the relational operators….such as ( = = ).
The relational operators are evaluated first then the logical operators.
 Logical Operators are always evaluated from left to right……….
if( a<b && b<c )…..a<b will be evaluated first.

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”);
}
}

INFOTECH Computer Education, Kishangarh (Raj)  9829171122 Page 17


visit @ http://www.infotechcomputer.in

1. You can place cases in any order you like.


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.

6. We can only use integer or a char in case. float is not allowed.

 The advantage of switch over if is that ….switch looks more structured and manageable
than if.

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.

INFOTECH Computer Education, Kishangarh (Raj)  9829171122 Page 18


visit @ http://www.infotechcomputer.in

It’s most popular looping control. The for loop specify three things about a loop in a single line.

1. Setting a loop counter to an initial value.


2. Testing the loop counter.
3. Increasing / Decreasing the value of loop counter each time the program within the loop
has been executed.

no semicolon here

for( initialize expression ; test expression ; increment/decrement expression)

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.

int i ;

for( i=0; i<=10; i++)


printf(“%d”,i);

INFOTECH Computer Education, Kishangarh (Raj)  9829171122 Page 19


visit @ http://www.infotechcomputer.in

 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 second kind of loop structure available in C is the while loop.

Structure of while loop:


int count=0;
loop expression

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.

INFOTECH Computer Education, Kishangarh (Raj)  9829171122 Page 20


visit @ http://www.infotechcomputer.in
 Increment of the expression can be perform in the body of while loop (count is incremented
by the ++ operator).

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.

keyword

do
{
countl++; Body of loop
printf(“%d”, count);
}

while(count<10); semicolon

keyword loop expression

 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.

 getch( ), getche( ) getchar( ) gets( ) :


 You need to press Enter key in scanf( ) function, we don’t need to press Enter key in
getch function.
 Both purpose is Input in a character.
 If we take Input in getch( ), it cannot display.

INFOTECH Computer Education, Kishangarh (Raj)  9829171122 Page 21


visit @ http://www.infotechcomputer.in
 If we take Input in getche( ) then it shows the Input character.
 getchar( ) works similarly and echoes the character that you typed, but require Enter
key.

 Syntax : char = getch( ); / getche( );


Like………… ch = getch( ); It takes Input and goes in ch.

#include <stdio.h>
#include <conio.h>
main()
{
char ch;
clrscr();

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();

Start/ Stop Expression

PRINT
INPUT
Decision

INFOTECH Computer Education, Kishangarh (Raj)  9829171122 Page 22


visit @ http://www.infotechcomputer.in

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.

 #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:

Assignment: Instead of using clrscr( ) function, we want to use cls.


We have to create header file name……clear.h and write….
#define cls clrscr()
Note : In one time one identifier will be used, we have to use two buffer for two defined
macros.
#define cls clrscr()
#define go gotoxy(20,5)
#define wait printf(“Sadequain”); getch();
 How to use defined Macros in our C Program.
First we have to include the header file in which the Macro is defined like…….

INFOTECH Computer Education, Kishangarh (Raj)  9829171122 Page 23


visit @ http://www.infotechcomputer.in
#include <clear.h>

main()
{
cls
go
wait
}

 Macros V/s Function

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.

 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)

INFOTECH Computer Education, Kishangarh (Raj)  9829171122 Page 24


visit @ http://www.infotechcomputer.in
 Function Declaration ( Prototype ):
void sqr( void ); ____________________Note semicolon

What its purpose:

 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 the Function:

 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 )
 Function Definition: void sqr(void) _________ (No termination)
 Function declarator is not a program statement that’s why it is not terminated. Rather, it
tells the compiler that a function is being defined.

INFOTECH Computer Education, Kishangarh (Raj)  9829171122 Page 25


visit @ http://www.infotechcomputer.in
 Function is usually enclosed in braces.

 The return statement:


The return( ) statement has two purposes.
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.

int add(int a,int b ) ; ____(Prototype)


main( )
{
int num1,num2,res; “Actual arguments ” (function call)
num1= 5; “Formal arguments ”
num2=10;

res = add(num1,num2); int add(int a, int b)


{
printf(“Addition = %d”,res); int d;
getch(); d = a+b;
} return( d );
}

function name

 Header Files and Prototyping:

 Header files are used to provide prototypes of library functions.


 Prototype can provide a mean of avoiding type mismatches b/w actual arguments and
formal arguments when a user written function is called. Header files can give this same
protection for library functions.

INFOTECH Computer Education, Kishangarh (Raj)  9829171122 Page 26


visit @ http://www.infotechcomputer.in
For example: The prototype for the printf( ) function is stored in the file stdio.h ( along with the
prototypes for many other functions ).

 User Define Function:

UDF are functions which we define by our self. There are four categories of UDF.

1. Take Nothing return nothing _____void function name(void)


2. Take argument return nothing ____void function name(int)
3. Take nothing return argument ____int function name(void)
4. Take argument return argument __ int function name(int)

Argument is those values which we pass.

 Function known as a group of routine which have specific task to perform.


 Built in Function are those which are provided by language.
 User define function are define by user to perform specific task.

INFOTECH Computer Education, Kishangarh (Raj)  9829171122 Page 27


visit @ http://www.infotechcomputer.in

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.

Defining Pointer variable:

Indicates the pointer Name of pointer variable


will point to
integer int *num;

indicates variable is a pointer


(i.e... It will hold a address).

General reasons of using pointers.


 To return more than one value from function.
 To pass Array and Strings more conveniently from one function to another.

Example of using Pointer: (Calling function by reference)

int sum(int *,int *); // Prototype of sum function.


Takes address of variable
main() Address
{
int num1,num2; 1310 5
clrscr();
1312 10

INFOTECH Computer Education, Kishangarh (Raj)  9829171122 Page 28


visit @ http://www.infotechcomputer.in

printf(“Enter 2 Numbers …….”);


scanf(“%d %d”,&num1,&num2);

res = sum(&num1,&num2); Function calling (note arguments 1310 1312 takes address not value)

printf(“Sum of %d and %d = %d”,num1,num2,res);


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.

Here are some more examples of declaring pointers:

int *num1, *num2;

char ch, *name;

float *num1, num2;

INFOTECH Computer Education, Kishangarh (Raj)  9829171122 Page 29


visit @ http://www.infotechcomputer.in
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!

It's easy to see the line

float * num1, num2;


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:

float *num1, *num2;

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”

INFOTECH Computer Education, Kishangarh (Raj)  9829171122 Page 30

Vous aimerez peut-être aussi