C Programming: by Deepak Majeti M-Tech CSE Mdeepak@iitk - Ac.in
C Programming: by Deepak Majeti M-Tech CSE Mdeepak@iitk - Ac.in
C PROGRAMMING LECTURE
by Deepak Majeti M-Tech CSE [email protected] 17th August IIT Kanpur
Recap
2
C is a high-level language.
Writing a C code. {editors like gedit, vi} Compiling a C code. {gcc c test.c o test}
Keywords
Data Types
int , char, float
Arithmetic Operators
+ (Plus), - (Minus), * (Multiplication), /(Division)
My first C program!
4
#include <stdio.h> // program prints hello world int main() { printf ("Hello world!"); return 0; } Output: Hello world!
C Course, Programming club, Fall 2008
Example 1
5
#include <stdio.h> // program prints a number of type int int main() { int number = 4; printf (Number is %d, number); return 0; } Output: Number is 4
C Course, Programming club, Fall 2008
Example 2
6
#include <stdio.h> // program reads and prints the same thing int main() { int number ; printf ( Enter a Number: ); scanf (%d, &number); printf (Number is %d\n, number); return 0; }
#include <stdio.h>
int main() { /* this program adds two numbers */ int a = 4; //first number int b = 5; //second number int answer = 0; //result answer = a + b; }
C Course, Programming club, Fall 2008
Note
8
Errors
Compilation
Compiler generally gives the line number at which the error is present.
Run time
C programs are sequential making the debugging easier.
User Defined:
Contd
11
Modulus (remainder): %
example:
12%5 = 2;
Assignment by addition: +=
example:
Contd
12
Operator Precedence
13
Meaning of a + b * c ? is it a+(b*c) or (a+b)*c ? All operators have precedence over each other *, / have more precedence over +, - .
If
both *, / are used, associativity comes into picture. (more on this later) example :
Precedence Table
14
| &&
||
C Course, Programming club, Fall 2008
Input / Output
15
printf (); //used to print to console(screen) scanf (); //used to take an input from console(user).
example: printf(%c, a); scanf(%d, &a); More format specifiers
%c %d %i %f %o %s %u %x %%
The character format specifier. The integer format specifier. The integer format specifier (same as %d). The floating-point format specifier. The unsigned octal format specifier. The string format specifier. The unsigned integer format specifier. The unsigned hexadecimal format specifier. Outputs a percent sign.
C Course, Programming club, Fall 2008
& in scanf.
It
Data Hierarchy.
example:
int value can be assigned to float not vice-versa. Type casting.
C Course, Programming club, Fall 2008
Home Work
17
Meaning of
Syntax
Semantics
of a programming language
19
THANK
YOU