You are on page 1of 36

C Language

Programming
Introduction
• C is a general purpose language which is very closely asoociated with
UNIX for which it was developed in Bell Laboratories
• Most of the programs of UNIX are written and run with the help of ‘C’
• Many of the important ideas of ‘c’ stem are from BCPL by Martin
Richards
• In 1972, dennies Ritchie at Bell Laboratories wrote C Language which
caused a revolution in computing world.
• From beginning C was intended to be useful for busy programmers to
get things done easily because C is powerful dominant and supple
language.
Why name ‘C’ was given to this language?

vMany of the ideas of C language were derived and


taken from ‘B’ language
vBCPL and CPL are previous versions of ‘B’
language
vAs many features came from B it was named as ‘C’
About ‘C’

C programming language – structured and disciplined approach to


program design
v C supports functions that enables easy maintainability of code, by
breaking large file into smaller modules
vComments in C provides easy readability
vC is a powerful language
vC programs built from:
• Variable and type declarations
• Functions
• Statements
• Expressions
Structure of ‘C’ Programs

ØBefore going and reading the structure of C programs we


need to have a basic knowledge of the following:
vC’s character set
vC’s keywords
vThe general structure of a ‘C’ Program
vHow to end a statement
vFree format language
vHeader Files and library functions
C’s Character Set
C does not use every character set and key found on modern computers. The only
characters that C – language uses for its program are as follows:

A – Z all alphabets
a – z all alphabets
0–9
# % & ! _ { } [ ] ( ) $$$$ &&&& |
space . , : ; ‘ $ “
+-/*=
The Keywords

ü“Keywords” are words that have special meaning


to the C compiler
üTheir meaning cannot be changed at any
instance
üServe as basic building blocks for program
statements
üAll keywords are written in only lowercase.
// comments
Header Files

üThe files that are specified in the include section is


called as Header File
üThese are precompiled files that has some
functions defined in them
üWe can call those functions in our program by
supplying parameters
üHeader files is give an extension .h
üC source file is given an extension .c
Main function

üThis is the “Entry Point” of a program


üWhen a file is executed, the start point is the main
function
üFrom main function the flow goes as per the
programmers choice
üThere may or may not be other functions written
by user in a program
üMain function is compulsory for any C program
Running a ‘C’ Program
ØType a program (any editor/IDE)
ØSave it.
ØCompile the program – This will generate an .exe file
(executable)
ØRun the program (Actually the exe created out of
compilation will run and not the .c file
ØIn different compiler we have different option for
compiling and running
Program Elements
v Variables
v Constants
v Identifiers
v Operators
v Control Structures
v Keywords
v Data Types
Keywords in “C”
auto double int struct
break else long switch
case enum register typedef
char etern return union
const float short unsigned
continue for signed void
default goto sizeof volatile
do if static while
The Identifiers
§ They are programmer-chosen names to represent parts of the
program: variables, functions, etc.
not accepted/
§ Cannot use C keywords as identifiers switch
invalid
§ Must begin with alpha character or _ followed by
alphanumeric, or _ payroll _payroll __payroll accepted/valid
§ Upper - and lower-case characters are important
(case sensitive) salary SALARY SalAry treated as 3 variables
§ Must consist of only letters, digits or underscore ( _ ).
§ Only first 31 characters are significant
§ Must NOT contain spaces ( ). account_salary accepted/valid
account salary not accepted/
invalid
Constants
Constants refer to fixed values that may not be altered by the
program.

Constants refer to fixed values that the program may not alter
during its execution.
What Are Variables in C?
vA Variable is used to store any data value
vVariables are used to store values that can be changed during the
program execution.
vVariables in C have the same meaning as variables in algebra. That is,
they represent some unknown data, or value.

x=a+b
z + 2 = 3(y-5)
vRemember that variables in algebra are represented by a single
alphabetic character.
DECLARATIONS
vConstants and variables must be declared before they can be used
vA constant declaration specifies the type, the name and the value
of the constant
vAny attempt to alter the value of a variable defined as constant
results in an error message by the compiler
vA variable declaration specifies the type, the name and possible the
initial value of the variable
vWhen you declare a constant or a variable, the compiler:
ü Reserves a memory location in which to store the value of the
constant or variable
ü Associates the name of the constant or variable with the memory
location
Declaration of Variables
and Constants
int A =5; Aà5
float B = 1.75; Bà1.75
char code = ‘x’ codeà‘x’

# define total 100


# define amount 75.35
# define code ‘m’
# define word “hello”

const float version = 3.20;


Declaring Variables
vBefore using a variable, you must give the compiler some information
about the variable; i.e., you must declare it.
vThe declaration statement includes the data type of the variable
vexamples of variable declarations:
int length;
float area;
vVariables are not automatically initialized. For example, after
declaration
int sum;
the value of the variable sum can be anything (garbage)
vThus, it is good practice to initialize variables when they are declared.
vOnce a value has been placed in a variable, it stays there until the
program alters it.
Data types in C
There are three classes of data types :
vPrimitive data types
int, float, double char
vAggregate OR derived data types
ü Arrays come under this category
ü Arrays can contain collection of int or float or
char or double data
vUser defined data types
Structures fall under this category
Operators
An operator is a symbol that tells the compiler to
perform specific mathematical, relational or logical
manipulations.

Types of Operators
1. Arithmetic Operator
2. Relational Operator
3. Logical Operator
4. Assignment Operator
Header Files
vThe files that are specified in the include section
is called as header file
vThese are precompiled files that has some
functions defined in them
vWe can call those functions in our program by
supplying parameters
vHeader file is given an extension .h
vC Source file is given an extension .c
Comments in C
vSingle line comment
// (double slash)
termination of comment is by pressing
enter key
vMulti line comment
/* ..............
...............*/

This can span over to multiple lines


INPUT and OUTPUT
• Input
• scanf(”%d”, &a);
• Gets an integer value from the user
and stores it under the name “a”
• Output
• printf(“%d”, a);
• Prints the value present in variable
Conversion Character How the corresponding argument is printed

%c as a character
%s as a string
%d as a decimal integer
%f as a floating point
%e as a floating point number in scientific notation
%lf as double
%g in the e-format of f-format which ever is shorter

Examples
1. printf(“%c”,x);
2. printf(“%d”,y);
3. printf(“%f”,z);
4. printf(“%s”,name);
Example
printf(“%c %3c %5c\n”, ‘A’, ‘B’, ‘C’);
Conversion Specification
Conversion Character
Valid characters in the input stream

%c to a character
%d to a decimal integer
%f to a floating point number (float)
%lf to a floating point number (double)
%s to a string

Examples
1. scanf(“%d”,&num);
2. scanf(“%c”,&khar);
3. scanf(“%s”, name);
4. scanf(“%lf”,&duble);
int main(int argc, const char * argv[])

The parameters:
argc - argument count,
argv - argument vector
respectively give the number and value of the program's
command-line arguments.
int main (int argc, const char * argv[])

•int: return type of the program, also known as “exit code”. If it's 0, the
program ran successfully, if not, there was an execution error.
•main: name of the entry function of the program, which is the function
that is called first when you launch the program.
•int argc: arguments count, or number of arguments passed to your
program. It's basically the length of the next parameter
•char** argv: array of character strings. An array containing the string
arguments passed to your program.

You might also like