Académique Documents
Professionnel Documents
Culture Documents
in
„C Language Basics‟
Introduction of Programming.
Data Types.
Loop.
Preprocessor Directives.
Pointer.
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”
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 C:
Alphabets A,B,………….Y,Z
A,b,…………..y,z
Digits 0,1,2,3,4,5,6,7,8,9
Special Symbols ~ ! @ # % ^ ` & * ( ) _ - + = | \ * + , - : ; “ ‘ < > ? /
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.
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.
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.
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
C Library
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.
closing brace to
delimit body of 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.
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.
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, !)
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)
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.
Numeric Data
-38 38
float 10 to 10 4 %f
-138 138
double 10 to 10 8 %lf
In fact short is nothing but our ordinary integer, which we are using all the time without
knowing that it was a short 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(“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.
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”);
}
}
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.
It’s most popular looping control. The for loop specify three things about a loop in a single line.
no semicolon here
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 ;
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.
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.
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
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.
#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();
PRINT
INPUT
Decision
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:
main()
{
cls
go
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.
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.
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 )
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.
Note: return is not necessary written at the end of function, it can occur anywhere in the
function.
function name
UDF are functions which we define by our self. There are four categories of UDF.
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.
res = sum(&num1,&num2); Function calling (note arguments 1310 1312 takes address not value)
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.
Watch Out!
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”