C Introduction
C Introduction
C Introduction
1
C: History
Developed in the 1970s – in conjunction with development
of UNIX(Uniplexed Information Computing System)
operating system
When writing an OS kernel, efficiency is crucial
This requires low-level access to the underlying hardware:
e.g. programmer can leverage knowledge of how data is laid out in
memory, to enable faster data access
UNIX originally written in low-level assembly language – but
there were problems:
No structured programming (e.g. encapsulating routines as “functions”,
“methods”, etc.) – code hard to maintain
Code worked only for particular hardware – not portable
2
C: Characteristics
C takes a middle path between low-level assembly language
Direct access to memory layout through pointer manipulation
Concise syntax, small set of keywords
A high-level programming language like Java:
Block structure
Some encapsulation of code, via functions
Type checking (pretty weak)
3
C: Dangers
C is not object oriented!
Can’t “hide” data as “private” or “protected” fields
You can follow standards to write C code that looks
object-oriented, but you have to be disciplined – will the other
people working on your code also be disciplined?
C has portability issues
Low-level “tricks” may make your C code run well on one
platform – but the tricks might not work elsewhere
The compiler and runtime system will rarely stop your C
program from doing stupid/bad things
Compile-time type checking is weak
No run-time checks for array bounds errors, etc. like in Java
4
Separate compilation
Advantage: Quicker compilation
When modifying a program, a programmer typically edits only
a few source code files at a time.
With separate compilation, only the files that have been edited
since the last compilation need to be recompiled when
re-building the program.
For very large programs, this can save a lot of time.
5
Compiler Vs Interpreter
Builds the code at a time
Builds code line by line
6
7
Header Files Inclusion: The first and foremost component is the inclusion of the
Header files in a C program. A header file is a file with extension .h which contains
C function declarations and macro definitions to be shared between several source
files.
Main Method Declaration: The next part of a C program is to declare the main()
function.
Variable Declaration: The next part of any C program is the variable declaration. It
refers to the variables that are to be used in the function. Please note that in the C
program, no variable can be used without being declared. Also in a C program, the
variables are to be declared before any operation in the function.
Body: The body of a function in the C program, refers to the operations that are
performed in the functions. It can be anything like manipulations, searching,
sorting, printing, etc.
Return Statement: The last part of any C program is the return statement. The
return statement refers to the returning of the values from a function. This return
statement and return value depend upon the return type of the function. For
example, if the return type is void, then there will be no return statement. In any
other case, there will be a return statement and the return value will be of the type
of the specified return type.
8
Escape Sequences:
9
\f Formfeed: Sometimes abbreviated as FF, form feed is a button or command on the
printer that allows the advancement of a printer page. This feature was frequently used on
dot matrix printers since nearly all of them used continuous feed paper rather than single
sheets. The form feed button advanced paper to the start of the next printing page, in the
case of a paper jam, or when loading continuous feed paper.
\v Vertical Tab: Moves the cursor to the next line and same position
Ab\vcd\vef
Ab
cd
ef
10
Tokens in C
We can define the token as the smallest individual element
in C. For `example, we cannot create a sentence without
using words; similarly, we cannot create a program in C
without using tokens in C.
11
Keywords
The following list shows the reserved words in C.
These reserved words may not be used as constants or variables or any other identifier names.
12
Identifiers / Rules of variable naming
A C identifier is a name used to identify a variable, function,
or any other user-defined item.
An identifier starts with a letter A to Z, a to z, or an underscore '_' \
followed by zero or more letters, underscores, and digits (0 to 9).
C does not allow punctuation characters such as @, $, and %
within identifiers.
Can’t Start with digit
C is a case-sensitive programming language. Thus, Manpower
and manpower are two different identifiers in C.
Here are some examples of acceptable identifiers −
Mohd, zara, abc, move_name
a_123, myname50 _temp, j, a23b9, retVal
13
Comments
Comments are like helping text in your C
program and they are ignored by the compiler.
They start with
/* and terminate with the characters */
as shown below −
/* my first program in C */
You cannot have comments within comments
and they do not occur within a string or
character literals.
14
Type Storage size Value range
15
Type Storage size Value range Precision
float 4 byte 1.2E-38 to 6 decimal places
3.4E+38
double 8 byte 2.3E-308 to 15 decimal places
1.7E+308
long double 10 byte 3.4E-4932 to 19 decimal places
1.1E+4932
16
char c;
scanf("%c",&c);
printf("%c",c);
printf("\n%d\n",sizeof(char));
int x;
scanf("%d",&x);
printf("%d",x);
printf("\n%d\n",sizeof(int));
17
If you accidentally input char instead of int value:
scanf() has an internal buffer, and will only read from the stream if that buffer is
empty. scanf tries to find an integer there. If an integer is found, it will report it
successfully read one item (by returning 1) and puts the read value into the
supplied pointer location. However, if an integer is not found, it will return 0 for
"zero items successfully read", and it will not consume anything from the buffer.
18
if (scanf("%d", &x) == 1)
{
printf(“%d”, x);
}
19
float f;
scanf("%f", &f);
printf("%.2f\n", f);
printf("%d\n", sizeof(float));
20
double d;
scanf("%lf",&d);
printf("%lf\n",d);
printf("%d\n",sizeof(double));
21
int x;
char c;
//scanf("%d%c",&x,&c);
scanf("%d",&x);
scanf("%c",&c);
printf(" %d\n",x);
printf(" %c\n",c);
22
Reminder Operator
int a = -3, b = 8;
printf("%d", a % b);
Output is: -3
Reminder is a least positive integer that should be
subtracted from a to make it divisible by b
(mathematically, a = qb + r then 0 ≤ r < |b|).
23
Reminder Operator
C/C++ does like this:
i) (a%b + b)%b
ii) a % n = a – ( n * trunc( a/n ) ).
For example,
8 % -3 = 8 – ( -3 * trunc(8/-3) )
= 8 – ( -3 * trunc(-2.666..) )
= 8 – ( -3 * -2 ) { rounded towards zero }
=8–6
=2