Sunteți pe pagina 1din 10

C Programming

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

www. v3academyCS .com , +91-9871703397 Page 1


C Programming

SETTING UP C
1. U need a C compiler (optionally debugger). Try compiling on command
prompt (rather than using IDEs).

C compilers for windows: TCC, gcc, VC++


Using gcc is recommended (for windows u need to install Cygwin or the
Minimalist GNU for Windows (MingW) from http://www.mingw.org. GCC is the
most popular C compiler.

Popular C/C++ IDEs for windows: TC, CDT, Dev C++, Code::Blocks, QT Creator,
Visual Studio Express

2. Plain Text Editor notepad, VI, Emacs, Gedit etc.

3. Setting up environment etc (if required).

What will not cover be covered

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?

Most books on C follow a fairly predictable course through C history, keywords,


data types, operators, expressions, syntax, control-flow constructs, looping
constructs, subroutines, etc.

Lets Challenge this!

We will directly dive-in C!

www. v3academyCS .com , +91-9871703397 Page 2


C Programming

Diving In... C!

Need to write high-performance code for a new game?


Use advanced third-party library in your iPhone app?
Need to write new device drivers?
Want to write a new OS (OS interfaces, file systems, networking protocols, CPU
scheduling, disk routines etc)?

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.

*** Why ***

<Performance>

www. v3academyCS .com , +91-9871703397 Page 3


C Programming

www. v3academyCS .com , +91-9871703397 Page 4


C Programming

C is a language for small, fast programs


C works at a much lower level than most other languages.
Hence understanding C gives you a much better idea of whats really going on. C
can even help you better understand other languages as well.

The Way C Works

Output, typically is an executable (.exe file on windows) and it contains machine


code.

Three C standards
1. ANSI C (late 1980s)
2. C99
3. C11

www. v3academyCS .com , +91-9871703397 Page 5


C Programming
A Complete C Program

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.

www. v3academyCS .com , +91-9871703397 Page 6


C Programming
Instructions Types:

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;

Combine declaration and assignment (initialization):


int loanAmount = 10000;
float interest2Pay = 12.36;
char card_type = Q;

Combine multiple declarations:


int loanAmount = 10000, loadPeriod = 3;
float interest2Pay = 12.36, height=1.75;
char card_type = Q, currency=$;

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)

www. v3academyCS .com , +91-9871703397 Page 7


C Programming
4. Function Calls
int main()
{
display(); // This is a function call instruction
}

Compilation, Linking and Execution

Pre-processor Directives (start with #):


- Include (#include)
- Macros (#define)
Header files are not compiled on their own. Their content is included in source code.
Header files are required for FWD Declaration.
Compilation goes fine .obj is created. Obj is binary & machine code but not
executable.
Object files are linked (together and with libraries) to create an executable.

www. v3academyCS .com , +91-9871703397 Page 8


C Programming
Linker needs the definitions of all functions (standard C library or user defined) for
linking purpose.

Conversions
Integer and Float Conversions
An arithmetic operation between:
1. integer and integer -> integer
2. float and float -> float
3. integer and float -> float

Operation <Result> Operation <Result>


5/2 2 2/5 0
5.0/2 2.5 2.0/5 0.4
5/2.0 2.5 2/5.0 0.4
5.0/2.0 2.5 2.0/5.0 0.4

Type Conversion in Assignment


What if - Variable is of one type and expression is of another? E.g. int i = 5.0/2;

Arithmetic Result Value Arithmetic Resu Value


Instruction Stored Instruction lt Stored
int k = 2/9 0 0 float f = 2/9 0 0.0
int k = 2.0/9 0.2222 0 float f = 2.0/9 0.222 0.2222
2
int k = 2/9.0 0.2222 0 float f = 2/9.0 0.222 0.2222
2
int k = 2.0/9.0 0.2222 0 float f = 2.0/9.0 0.222 0.2222
2
int k = 9/2 4 4 float f = 9/2 4 4.0
int k = 9.0/2 4.5 4 float f = 9.0/2 4.5 4.5
int k = 9/2.0 4.5 4 float f = 9/2.0 4.5 4.5
int k = 9.0/2.0 4.5 4 float f = 9.0/2.0 4.5 4.5

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)

www. v3academyCS .com , +91-9871703397 Page 9


C Programming
2. Use Parentheses for overriding precedence

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;

float g = big / 2 + big * 4 / big big + abc /3;


abc = 1.5; big = 3
<ans: 2.5>

So all the arithmetic (and algebraic) expressions can be converted to C expression.


E.g.

Algebraic Expression C Expression


ab-cd a*bc*d
(m+n)(a+b) (m+n)*(a+b)
3x2 + 2x + 5 3*x*x+2*x+5

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!

www. v3academyCS .com , +91-9871703397 Page 10

S-ar putea să vă placă și