Basics of “C” Programming Terminologies of ‘C’ 1.Keywords 2.Identifiers 3.Variables 4.Constants 5.Special Symbols 6.Operators 7.Character & String.
Post on 26-Dec-2015
222 Views
Preview:
Transcript
Terminologies of ‘C’
1. Keywords2. Identifiers3. Variables4. Constants5. Special Symbols6. Operators7. Character & String
1. Keywords
Keywords are the reserved words whose meaning has already been explained to the C compiler. C has 32 keywords.These keywords combined with a formal syntax form a C programming language.Rules to be followed for all programs written in C:
All keywords are lower-cased.
C is case sensitive, do-while is different from DO WHILE.
Keywords cannot be used as a variable or function name.
2. Identifiers
Identifiers refer to the name of variables, functions and arrays.These are user-defined names and consist of sequence of letters and digits, with a letter as a first character.Both uppercase and lowercase letters are permitted, although lowercase letters are commonly used .The underscore character is also permitted in identifiers. It is usually used as a link between two words in long identifiers.
Identifier Names
Some correct identifier names are -
arena, s_countmarks40class_one
Some erroneous identifier names are -1stsstoh!godstart….end
X
The number of characters in the variable that are recognized differs from compiler to compiler
An identifier cannot be the same as a C keyword
3. Variables
Variables are named locations in memory that are used to hold a value that may be modified by the program.Unlike constants that remain unchanged during the execution of a program.A variable may take different values at different times during execution.The syntax for declaring a variable is –
DataType IdentifierName ;Example- int num;
long int sum , a;
4. Constants
Constants are the fixed values that do not change during the execution of a program.C supports several types of constants.
Numeric ConstantsInteger constantsReal constants
Character ConstantsSingle character constantString Constants
5. Special Symbols
! , @, #, $ , & , * , ….. These all symbols that can be find on Keyboard, are called Special Symbols.
Every symbol has its special meaning in different respect at different place that’s why it is called Special Symbols.
6. Operators
Operator is a symbol that operates on one or more operands and produces output.
Example - c = a + b ;
In the above Example the symbols + and = are operators that operate on operands a, b , and c .
7. Character & String
The Characters that can be used to form words, numbers and expressions depend upon the computer on which the program is running.The characters in C are grouped into the following categories :
LettersDigitsSpecial charactersWhite SpacesRemember that a character ‘a’ is not equivalent to the string “a”.
Why use C?
• Mainly because it produces code that runs nearly as fast as code written in assembly language. Some examples of the use of C might be: – Operating Systems – Language Compilers – Assemblers – Text Editors – Print Spoolers – Network Drivers – Modern Programs – Data Bases – Language Interpreters – Utilities
Why C Still Useful?• C provides:
Efficiency, high performance and high quality flexibility and power many high-level and low-level operations middle level Stability and small size code Provide functionality through rich set of function libraries Gateway for other professional languages like C C++ Java
• C is used: System software Compilers, Editors, embedded systems data compression, graphics and computational geometry, utility
programs databases, operating systems, device drivers, system level
routines there are zillions of lines of C legacy code Also used in application programs
Software Development Method• Requirement Specification
– Problem Definition• Analysis
– Refine, Generalize, Decompose the problem definition• Design
– Develop Algorithm • Implementation
– Write Code• Verification and Testing
– Test and Debug the code
Development with C• Four stages
Editing: Writing the source code by using some IDE or editor Preprocessing or libraries: Already available routines compiling: translates or converts source to object code for a
specific platform source code -> object code linking: resolves external references and produces the
executable module
Portable programs will run on any machine but….. Program correctness and robustness are most important than
program efficiency
History• In 1972 Dennis Ritchie at Bell Labs writes C and in 1978 the publication of The C Programming Language by Kernighan & Ritchie caused a revolution in the computing world
• In 1983, the American National Standards Institute (ANSI) established a committee to provide a modern, comprehensive definition of C. The resulting definition, the ANSI standard, or "ANSI C", was completed late 1988.
History of C• Evolved from two previous languages
– BCPL , B• BCPL (Basic Combined Programming Language) used for
writing OS & compilers• B used for creating early versions of UNIX OS• Both were “typeless” languages• C language evolved from B (Dennis Ritchie – Bell labs)• C was developed by Dennis Ritchie at AT & T bell laboratory
of USA in 1972.
History of C
• Hardware independent• Programs portable to most computers• Dialects of C
– Common C – ANSI C
• ANSI/ ISO 9899: 1990• Called American National Standards Institute ANSI C
• Case-sensitive
C is Middle Level Language
• C stands in between these two categories.• Since it was designed to have both:
• A relatively good programming efficiency as compared to Machine Oriented Languages .
• A relatively good Machine efficiency as compared to Problem Oriented Languages .
• That’s WHY it is called a Middle Level Language.
Application Areas Of C
• C was initially used for systems programming.• A system program forms a portion of the operating
system of the computer or its support utilities.• Operating Systems, Interpreters, Editors, Assembly
programs are usually called system programs.• The UNIX operating system was developed using C.• There are C compilers available for almost all types
of PC’s.
A simple Program of C/* Write a program to print a message */
#include<stdio.h>
#include<conio.h>
void main()
{
printf(“ C Programming”);
getch( );
}
Comment about the program should be enclosed within ‘/*’ & ‘*/’.
printf() is a function which is used to print messages on the screen.
main() is a function from which execution of the program starts.
Here stdio.h is a library file which contains standard input/output functions , keywords etc.
#include<> is used to define the library file that is to be used in the program for compiler information.
Compilation & Execution of a Program
C Preprocessor
Expanded C Source Code
C Compiler
Target Assembly Code
Linker
Executable Code
Loader
Output
C Source Code
Basics of C Environment• C systems consist of 3 parts
– Environment– Language– C Standard Library
• Development environment has 6 phases– Edit– Pre-processor– Compile– Link– Load– Execute
Basics of C Environment
Editor DiskPhase 1
Program edited in Editor and storedon disk
Preprocessor DiskPhase 2
Preprocessor program processesthe code
Compiler DiskPhase 3
Creates object code and stores on disk
Linker DiskPhase 4
Links object code with libraries and stores on disk
Basics of C Environment
LoaderPhase 5
Puts program in memory
Primary memory
CPUPhase 6
Takes each instructionand executes it storingnew data values
Primary memory
Execution Process of a Program
PROCESS SHORT CUT FILE NAME
Save F2 abc.cCompile Alt + F9 abc.obj
Execute Ctrl + F9 abc.exe
Back up Again F2 abc.bak
Control FLOW Statements
• C language has decision making capabilities and supports controlling of statements.
• C supports following decision making statements: 1. IF
2.IF-ELSE and its various form3. Switch
4. Conditional Operator
If statement
• The if statement is a powerful decision making statement.
• The if statement is used to control the flow of execution of statements.
• It is a two way decision statement which is used in conjunction with an expression.
Different forms of If statement
• Simple If statement.
• If…..else statement.
• Nested if…..else statement.
• Else if ladder.
Simple if statement
• If the if expression evaluates to true, the block following the if statement or statements are executed.
• The general form of a simple if statement is :
if (test-expression) { statement-block; } statement-x;
If….Else statement
• The general form of if……else statements is :
• IF the if expression evaluates to true, the block following the if statement or statements are executed.
• The else statement is optional. It is used only if a statement or a sequence of statements are to be executed in case the if expression evaluates to false.
Else if ladder • The general form of else if ladder is:
• The if – else – if statement is also known as the if-else-if ladder or the if-else-if staircase.
• The conditions are evaluated from the top downwards.
Nested if…else statement
• The general form of nested if…else statement is:if (test-expression 1)
{
if (test-expression 2)
{
statement 1;
}
else
{
statement 2;
}
}
else
{
statement 3;
}
Statement x;
Switch – case statement
• The switch-case control statement is a multi-way decision maker that tests the value of an expression against a list of integers or character constants.
• When a match is found, the statements associated with that constant are executed.
• The syntax of switch-case is:-
Break statement
• When the keyword break is encountered inside any c loop, control automatically passes to the first statement after the loop.
• A break is usually associated with an if.• The general syntax of break statement is:
For(; ;)
{
--------
--------
if (error)
break;
-------
}
--------
Continue statement
• In some programming situations we want to take the control to the beginning of the loop, by passing the statements inside the loop which have not yet been executed. The Keyword continue allows us to do this.
• When the keyword continue is encountered inside any C loop, control automatically passes to the beginning of the loop.
• A continue is usually associated with an if.
#include<stdio.h>
void main()
{
int i,j;
for(i=1;i<=2;i++)
{
for(j=1;j<=2;j++)
{
if(i==j)
continue;
printf(“%d%d”,i,j);
}
} }
top related