C Prog Elements
C Prog Elements
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?
A – Z all alphabets
a – z all alphabets
0–9
# % & ! _ { } [ ] ( ) $$$$ &&&& |
space . , : ; ‘ $ “
+-/*=
The Keywords
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’
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
/* ..............
...............*/
%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.