Introduction To C Language and Linux
Introduction To C Language and Linux
Introduction To C Language and Linux
1
Topics to be covered
• Introduction to Programming in C & Restricted Exposure to Linux
- Today
• Data, Operators, I/O - Tomorrow
• Conditional Expressions, Control Flow – 23 rd Aug.
• Loops
• Functions for structure and Recursion
• Pointer and Arrays
• Dynamic allocation
• Structures and Applications, Storage Classes
• Pre-processor, File Handling, Math library
• Algorithms: searching, sorting
3
About C
• GNU : GNU's Not Unix
– GNU C: gcc is a standard compiler
• C is non portable
– Terms: Compiler (human -> machine [once]),
Interpreter (instructions -> machine [each time the
program is run])
• C is a high level language
– One line in c maps to many lines of assembly code
4
My first C program!
/* thou shalt begin from somewhere*/
#include <stdio.h>
int main() {
int number;
scanf(“%d”, &number);
printf (“%d\n”, number);
return 0;
}
6
1. Programming on Linux
• Linux command line: GNU-C
– Use console based editors: vi, emacs, nano
– Or text based editors: kwrite, gedit, kate
• IDE
– Eclipse *
http://www.eclipse.org/cdt/downloads.php
7
Linux Familiarization
• Common shell commands
– Remember, commands are issued to a shell
– pwd, ls, dir, mkdir, cd, date, whoami
– touch, cp, mv, rm, chmod, cat, less, more, tail
– man
– Commands are programs (usually in /usr/bin, /bin/)
– Most commands take options and input
• ls ls -a ls -l ls -lt ls -ltr
• Everything is case-sensitive
• Tab completion, command history
8
Files, directories and permissions
• Directory
drwxr-xr-x 2 nitinm cse 4096 2008-08-13 22:46 Pictures
• File
-rw-r--r-- 1 nitinm cse 3446 2008-08-14 15:16 test.c
• Special files (advanced)
– .a : static library
– .so : shared object (dynamic)
– Pipes : fifo / buffered prwx--x--x
– Device files : /dev/cdrom etc.
9
Programming on Linux contd…
• Writing programs
– Use any editor (graphical, console)
– Save file as <filename>.c
• Compiling programs
– gcc <filename>.c gcc funnysingh.c –o funnysingh
• Running programs
– ./a.out ./funnysingh
(executable files need to have executable
permissions. $chmod +x <executable>)
10
Compilation is not a single stage
• Pre process : cpp (C Preprocessor) gcc –E
– Removes comments, includes #include files
• Compile : gcc –c (GNU compiler)
– main step, compilation, change into machine code
• Link : ld (GNU linker)
– link executables
12
Or 3. Work on windows, yet use gcc
13
• Why doesn’t my windows binary run on linux?
– File format: exe and elf
• man elf
– In linux, program does system calls.
– Libraries are different
14
Good programming practices
Indentation
15
Good programming practices
contd..
• Variables names
– Not too short, not too long
– Always start variable names with small letters
– On work break
• Capitalize: myVariable, OR
• Separate: my_variable
16
Good programming practices
contd...
• Put comments
#include <stdio.h>
int main() {
/* this program adds
two numbers */
int a = 4; //first number
int b = 5; //second number
int res = 0; //result
res = a + b;
}
17
Good programming practices
18