Documente Academic
Documente Profesional
Documente Cultură
Contents
SETTING UP C............................................................................................................. 2
What will not cover be covered...............................................................................2
Predictable Path?..................................................................................................... 2
Diving In... C!.............................................................................................................. 3
C is a language for small, fast programs....................................................................4
The Way C Works........................................................................................................ 4
Three C standards................................................................................................... 4
A Complete C Program............................................................................................... 5
Instructions Types:................................................................................................... 6
1. Variables Declaration..................................................................................... 6
2. Arithmetic...................................................................................................... 6
3. Input/ Output................................................................................................. 6
4. Function Calls................................................................................................. 7
Compilation, Linking and Execution............................................................................7
Conversions................................................................................................................ 8
Integer and Float Conversions................................................................................. 8
Type Conversion in Assignment...............................................................................8
Operators Precedence............................................................................................. 8
Scanf()........................................................................................................................ 9
SETTING UP C
1. U need a C compiler (optionally debugger). Try compiling on command
prompt (rather than using IDEs).
Popular C/C++ IDEs for windows: TC, CDT, Dev C++, Code::Blocks, QT Creator,
Visual Studio Express
We will essentially cover all the topics of C found in any given ANSI C text book.
What we will not cover is:
- embedded C
- System programming (Sockets and Networking, Processes and System Calls,
IPC, multi-threading etc)
- Data Structures in C
- Code Profiling, Unit Testing, Code Coverage, Performance, Scalability etc (using
tools or otherwise)
- Mixing C and C++ code
- C tests
- Metaprogramming
etc.
Predictable Path?
Diving In... C!
C is here to help...
C is the de facto choice of the language for system programming and embedded
programming.
UNIX was the first operating system written in C. Later Microsoft Windows, Mac
OS X, and GNU/Linux were all written in C. Today it is extremely difficult to find an
operating system not written in C (or C++).
Perl, PHP, and Python are all written in C. And so are Apache HTTP web
server, email systems, software libraries providing graphical interfaces, and
efficient numerical, statistical, encryption, and compression algorithms.
<Performance>
Three C standards
1. ANSI C (late 1980s)
2. C99
3. C11
Anything that starts with # is a pre-processor directive. This code is not compiled
(in fact this code doesnt even reach to compiler). Hence this is never converted
into machine code and executed.
*** All executable code must be inside functions/procedures. E.g. puts (hi);
or i = 2+3; ***
Code that gets converted to machine code and finally executed must be inside a
function.
1. Variables Declaration
Variable declaration
type name;
Variable naming rules (also called identifier)
Must not be a Keyword (reserved C words like int, for, puts etc)
Use only: alphabet, number, _ (underscore)
1st character must be alphabet or underscore
Basic Data Types integer (*** not natural nos ***), float (real numbers),
character
E.g. int loanAmount; float interest2Pay; char card_type;
*** char variable can store only one character (alphanumeric + others) ***
Literals (constants)
10 (int literal) 5.235 (float literal) Q (char literal)
= is assignment operator
int loanAmount;
loanAmount = 10000;
float interest2Pay;
interest2Pay = 12.36;
char card_type;
card_type = Q;
2. Arithmetic
*, /, %, -, + are the arithmetic operators
E.g. int i = 5 + 3 / 2 1 * 2;
float area = length * breadth;
3. Input/ Output
puts(); printf(); scanf() etc
puts() adds newline while printf doesnt (but printf can print all datatypes)
Conversions
Integer and Float Conversions
An arithmetic operation between:
1. integer and integer -> integer
2. float and float -> float
3. integer and float -> float
Operators Precedence
Priority Operators Description
1st * / % multiplication, division, modular
division
2nd + - addition, subtraction
3rd = assignment
Tips:
1. Expressions are evaluated from Left to Right (this is extremely useful in
case of tie)
E.g. int i = 2 * 3 / 4 + 4 / 4 + 8 2 + 5 / 8;
i = 6 / 4 + 4 / 4 + 8 2 + 5 / 8;
i = 1 + 4 / 4 + 8 2 + 5 / 8;
i = 1 + 1 + 8 2 + 5 / 8;
i = 1 + 1 + 8 2 + 0;
i = 2 + 8 2 + 0;
i = 10 2 + 0;
i = 8 + 0;
i = 8;
Scanf()
%c for char, %d for int and %f for float. Put & before the variable name.
e.g. int i;
scanf ( %d , &i)
char ch;
scanf ( %c , &ch)
float f;
scanf ( %f , &f)
Whitespaces space, tab and newline char.
*** scanf() ignores whitespaces before int and float but not before
characters ***
*** scanf() can read multiple values at a time.
*** space in format string before %c will ignore all white spaces!