C-Programming Study Group
Honey Sharma
3rd year
BTech-open source software
What do you expect from this study group?
Things we will
focus upon:
1.Programming
2.Programming
3.Programming
C?
LANGUAGE? PROGRAMMING LANGUAGE?
C,C++,Java??
Procedural??
OBJECT ORIENTED??
COMPILED
Vs.
INTERPRETED??
Language Types
1.Machine languages
Strings of numbers giving
machine specific
instructions
Example:
+1300042774
2.Assembly Language
English-like abbreviations
representing elementary
computer operations
(translated via
assemblers)
Example:
Load BASEPAY
3.High-level languages
Codes similar to everyday
English
Example:
grossPay = basePay +
overTimePay
KNOW YOUR
TOOLS
GIT AND GITHUB?
Why do I need them?
Turbo c vs. GCC
Turbo C is a 16bit compiler. GCC is a 32 bit
compiler
Turbo C is obsolete -- on second thoughts,
fossil might a better adjective.
Does not support recent enough language
standards like C11 or C++11 or C++14
It is buggy, yes.
Turbo C is proprietary software; you cannot
see its source code (legally)
Turbo C produces poor executable code
GCC is a 32 bit compiler
GCC is supporting recent standards like C11 or
C++14.
GCC is free software, you can download legally,
study and improve its source code.
GCC is able to optimize
Use GCC with a good editor (e.g. emacs), and
a version control system (e.g. git) and a build
automation tool (e.g. GNU make)
The guy who designed Turbo C
moved on - so should you.
C89 vs. c99(c9x) vs. c11(c1x)
C89 or ANSI C( American National Standards
Institute)-1989
c99-underwent further revision in the late 1990s,
leading to the publication of ISO/IEC 9899:1999 in
1999
C11-current version of c programming language
released in 2011.
Introduction
Designed by Dennis
Ritchie at Bell
Laboratories in the
early 1970s
Influenced by
ALGOL 60 (1960),
CPL (Cambridge,
1963),
BCPL (Martin Richard,
1967),
B (Ken Thompson,
1970)
Traditionally used for
systems programming
So lets start coding……….
It's not at all important to get it right the first time. It's vitally important
to get it right the last time. ” - Andrew Hunt and David Thomas
Operators:
1.Arithmatic 2.Logical 3.Relational 4.Assignment
5.Increment
and
Decrement
Operators
Arithmetic Operators
+, - , *, / and the modulus operator %.
+ and – have the same precedence and associate left to right.
3 – 5 + 7 = ( 3 – 5 ) + 7  3 – ( 5 + 7 )
3 + 7 – 5 + 2 = ( ( 3 + 7 ) – 5 ) + 2
*, /, % have the same precedence and associate left to right.
The +, - group has lower precendence than the *, / % group.
3 – 5 * 7 / 8 + 6 / 2
3 – 35 / 8 + 6 / 2
3 – 4.375 + 6 / 2
3 – 4.375 + 3
-1.375 + 3
1.625
Operators
Arithmetic Operators
% is a modulus operator. x % y results in the remainder when x is
divided by y and is zero when x is divisible by y.
Cannot be applied to float or double variables.
Example
if ( num % 2 == 0 )
printf(“%d is an even numbern”, num);
else
printf(“%d is an odd numbern”, num);
Operators
Relational Operators
<, <=, > >=, ==, != are the relational operators. The expression
operand1 relational-operator operand2
takes a value of 1(int) if the relationship is true and 0(int) if relationship is
false.
Example
int a = 25, b = 30, c, d;
c = a < b;
d = a > b;
value of c will be 1 and that of d will be 0.
Operators
Logical Operators
&&, || and ! are the three logical operators.
expr1 && expr2 has a value 1 if expr1 and expr2 both are
nonzero.
expr1 || expr2 has a value 1 if expr1 and expr2 both are nonzero.
!expr1 has a value 1 if expr1 is zero else 0.
Example
if ( marks >= 40 && attendance >= 75 ) grade = ‘P’
If ( marks < 40 || attendance < 75 ) grade = ‘N’
21
Operators
Assignment operators
The general form of an assignment operator is
v op= exp
Where v is a variable and op is a binary arithmetic operator. This
statement is equivalent to
v = v op (exp)
a = a + b can be written as a += b
a = a * b can be written as a *= b
a = a / b can be written as a /= b
a = a - b can be written as a -= b
The operators ++ and –- are called increment and
decrement operators.
a++ and ++a are equivalent to a += 1.
a-- and --a are equivalent to a -= 1.
++a op b is equivalent to a ++; a op b;
a++ op b is equivalent to a op b; a++;
Example
Let b = 10 then
(++b)+b+b = 33
b+(++b)+b = 33
b+b+(++b) = 31
b+b*(++b) = 132
22
Operators
INCREMENT AND DECREMENT OPERATOR
The operators ++ and –- are called increment and decrement operators.
a++ and ++a are equivalent to a += 1.
a-- and --a are equivalent to a -= 1.
++a op b is equivalent to a ++; a op b;
a++ op b is equivalent to a op b; a++;
Example
Let b = 10 then
(++b)+b+b = 33
b+(++b)+b = 33
b+b+(++b) = 31
b+b*(++b) = 132
Logical Operators
 C defines these logical operators: <, >, <=,
>= and == (the equivalence operator)
 You can compare any variable. Characters
are compared based on their ASCII values.
 All answers will be true (not zero) or false
(0)
 You can extend the logic with && (and), !
(not) and || (or).
C Standard Header Files you may want to use
Standard Headers you should know about:
stdio.h – file and console (also a file) IO: perror, printf, open, close, read, write, scanf, etc.
stdlib.h - common utility functions: malloc, calloc, strtol, atoi, etc
string.h - string and byte manipulation: strlen, strcpy, strcat, memcpy, memset, etc.
ctype.h – character types: isalnum, isprint, isupport, tolower, etc.
errno.h – defines errno used for reporting system errors
math.h – math functions: ceil, exp, floor, sqrt, etc.
signal.h – signal handling facility: raise, signal, etc
stdint.h – standard integer: intN_t, uintN_t, etc
time.h – time related facility: asctime, clock, time_t, etc.
The Preprocessor
• The C preprocessor permits you to define simple macros that
are evaluated and expanded prior to compilation.
• Commands begin with a ‘#’. Abbreviated list:
– #define : defines a macro
– #undef : removes a macro definition
– #include : insert text from file
– #if : conditional based on value of expression
– #ifdef : conditional based on whether macro defined
– #ifndef : conditional based on whether macro is not defined
– #else : alternative
– #elif : conditional alternative
– defined() : preprocessor function: 1 if name defined, else 0
#if defined(__NetBSD__)
THANK YOU

C Programming Introduction

  • 1.
    C-Programming Study Group HoneySharma 3rd year BTech-open source software
  • 2.
    What do youexpect from this study group?
  • 3.
    Things we will focusupon: 1.Programming 2.Programming 3.Programming
  • 4.
  • 5.
    Language Types 1.Machine languages Stringsof numbers giving machine specific instructions Example: +1300042774 2.Assembly Language English-like abbreviations representing elementary computer operations (translated via assemblers) Example: Load BASEPAY 3.High-level languages Codes similar to everyday English Example: grossPay = basePay + overTimePay
  • 6.
  • 7.
    GIT AND GITHUB? Whydo I need them?
  • 8.
    Turbo c vs.GCC Turbo C is a 16bit compiler. GCC is a 32 bit compiler Turbo C is obsolete -- on second thoughts, fossil might a better adjective. Does not support recent enough language standards like C11 or C++11 or C++14 It is buggy, yes. Turbo C is proprietary software; you cannot see its source code (legally) Turbo C produces poor executable code GCC is a 32 bit compiler GCC is supporting recent standards like C11 or C++14. GCC is free software, you can download legally, study and improve its source code. GCC is able to optimize Use GCC with a good editor (e.g. emacs), and a version control system (e.g. git) and a build automation tool (e.g. GNU make)
  • 9.
    The guy whodesigned Turbo C moved on - so should you.
  • 10.
    C89 vs. c99(c9x)vs. c11(c1x) C89 or ANSI C( American National Standards Institute)-1989 c99-underwent further revision in the late 1990s, leading to the publication of ISO/IEC 9899:1999 in 1999 C11-current version of c programming language released in 2011.
  • 11.
    Introduction Designed by Dennis Ritchieat Bell Laboratories in the early 1970s Influenced by ALGOL 60 (1960), CPL (Cambridge, 1963), BCPL (Martin Richard, 1967), B (Ken Thompson, 1970) Traditionally used for systems programming
  • 13.
    So lets startcoding……….
  • 15.
    It's not atall important to get it right the first time. It's vitally important to get it right the last time. ” - Andrew Hunt and David Thomas
  • 16.
    Operators: 1.Arithmatic 2.Logical 3.Relational4.Assignment 5.Increment and Decrement
  • 17.
    Operators Arithmetic Operators +, -, *, / and the modulus operator %. + and – have the same precedence and associate left to right. 3 – 5 + 7 = ( 3 – 5 ) + 7  3 – ( 5 + 7 ) 3 + 7 – 5 + 2 = ( ( 3 + 7 ) – 5 ) + 2 *, /, % have the same precedence and associate left to right. The +, - group has lower precendence than the *, / % group. 3 – 5 * 7 / 8 + 6 / 2 3 – 35 / 8 + 6 / 2 3 – 4.375 + 6 / 2 3 – 4.375 + 3 -1.375 + 3 1.625
  • 18.
    Operators Arithmetic Operators % isa modulus operator. x % y results in the remainder when x is divided by y and is zero when x is divisible by y. Cannot be applied to float or double variables. Example if ( num % 2 == 0 ) printf(“%d is an even numbern”, num); else printf(“%d is an odd numbern”, num);
  • 19.
    Operators Relational Operators <, <=,> >=, ==, != are the relational operators. The expression operand1 relational-operator operand2 takes a value of 1(int) if the relationship is true and 0(int) if relationship is false. Example int a = 25, b = 30, c, d; c = a < b; d = a > b; value of c will be 1 and that of d will be 0.
  • 20.
    Operators Logical Operators &&, ||and ! are the three logical operators. expr1 && expr2 has a value 1 if expr1 and expr2 both are nonzero. expr1 || expr2 has a value 1 if expr1 and expr2 both are nonzero. !expr1 has a value 1 if expr1 is zero else 0. Example if ( marks >= 40 && attendance >= 75 ) grade = ‘P’ If ( marks < 40 || attendance < 75 ) grade = ‘N’
  • 21.
    21 Operators Assignment operators The generalform of an assignment operator is v op= exp Where v is a variable and op is a binary arithmetic operator. This statement is equivalent to v = v op (exp) a = a + b can be written as a += b a = a * b can be written as a *= b a = a / b can be written as a /= b a = a - b can be written as a -= b
  • 22.
    The operators ++and –- are called increment and decrement operators. a++ and ++a are equivalent to a += 1. a-- and --a are equivalent to a -= 1. ++a op b is equivalent to a ++; a op b; a++ op b is equivalent to a op b; a++; Example Let b = 10 then (++b)+b+b = 33 b+(++b)+b = 33 b+b+(++b) = 31 b+b*(++b) = 132 22 Operators INCREMENT AND DECREMENT OPERATOR The operators ++ and –- are called increment and decrement operators. a++ and ++a are equivalent to a += 1. a-- and --a are equivalent to a -= 1. ++a op b is equivalent to a ++; a op b; a++ op b is equivalent to a op b; a++; Example Let b = 10 then (++b)+b+b = 33 b+(++b)+b = 33 b+b+(++b) = 31 b+b*(++b) = 132
  • 23.
    Logical Operators  Cdefines these logical operators: <, >, <=, >= and == (the equivalence operator)  You can compare any variable. Characters are compared based on their ASCII values.  All answers will be true (not zero) or false (0)  You can extend the logic with && (and), ! (not) and || (or).
  • 24.
    C Standard HeaderFiles you may want to use Standard Headers you should know about: stdio.h – file and console (also a file) IO: perror, printf, open, close, read, write, scanf, etc. stdlib.h - common utility functions: malloc, calloc, strtol, atoi, etc string.h - string and byte manipulation: strlen, strcpy, strcat, memcpy, memset, etc. ctype.h – character types: isalnum, isprint, isupport, tolower, etc. errno.h – defines errno used for reporting system errors math.h – math functions: ceil, exp, floor, sqrt, etc. signal.h – signal handling facility: raise, signal, etc stdint.h – standard integer: intN_t, uintN_t, etc time.h – time related facility: asctime, clock, time_t, etc.
  • 25.
    The Preprocessor • TheC preprocessor permits you to define simple macros that are evaluated and expanded prior to compilation. • Commands begin with a ‘#’. Abbreviated list: – #define : defines a macro – #undef : removes a macro definition – #include : insert text from file – #if : conditional based on value of expression – #ifdef : conditional based on whether macro defined – #ifndef : conditional based on whether macro is not defined – #else : alternative – #elif : conditional alternative – defined() : preprocessor function: 1 if name defined, else 0 #if defined(__NetBSD__)
  • 26.