C - ISRO

Post on 28-Jan-2015

113 views 0 download

Tags:

description

Its a presentation

Transcript of C - ISRO

Ca series of presentations to help a bunch of

brilliant space scientists understand a brilliant programming language

Why?

You wrote a really complex C program called

super_complex_no_idea_what_i_justwrote.c

Why?

You use gcc to compile it . . .

$ gcc super_complex_no_idea_what_i_justwrote.c

Why?

compiling . . .

Why?

no errors. . .

Why?

$./a.outSegmentation Fault$

Why?• Discovered a piece of C code in a book or the internet that solves your problem

• Copied it into your program and somehow made it compile

• No testing whatsoever

• Program runs and gives you right output for your small set of conveniently selected input

• Job done. Decided to comment your code later. Forgotten all about it.

• Three months later someone comes and tells you to change your code for another input or someone comes and tells you that your code is not working.

• Open your source code and stare at it ….. trying to make sense of what you did

• Delete everything and go back to search a book or the internet to solve your problem

hello world#include <stdio.h>

int main(void){

printf(“Hello World\n”);return 0;

}

$ gcc hello.c

$ a.outHello World$

text file named hello.c

compile

execute

Lets analyze it in detail . . . .

a simple C program

#include <stdio.h>

int main(void){

printf(“Hello World\n”);return 0;

}

Instructs the preprocessor to add the contents of the header file stdio.h into hello.c

next line

a simple C program

#include <stdio.h>

int main(void){

printf(“Hello World\n”);return 0;

}

Instructs the preprocessor to add the contents of the header file stdio.h into hello.c

Why does ‘stdio.h ‘have angle brackets <>?

What is stdio.h? Where is it stored?

What is # include ?

a simple C program

#include <stdio.h>

int main(void){

printf(“Hello World\n”);return 0;

}

Pre processor directive

• Instructions meant for the pre-processor

• Always being with a ‘#’ symbol

• #include puts every line in the file stdio.h into hello.c

• Other pre processor directive examples: #define, #ifdef #pragma, etc.

a simple C program

#include <stdio.h>

int main(void){

printf(“Hello World\n”);return 0;

}

Pre processor directive

• < angle brackets > tells the #include directive to search for the file stdio.h in the standard C header file location.

• The default standard C header files in a Unix machine is /usr/header,

• We can use “ ” instead of < > to tell the include directive to first search for the file in the same directory as your C file, then the standard .

a simple C program

#include <stdio.h>

int main(void){

printf(“Hello World\n”);return 0;

}

Standard C headers

• A file ending with ‘.h’ is known as a C header file.

• A set of header files are available by default with the C programming language. These are known as standard C headers

• The standard C headers contains declarations of system functionsthat allows you to invoke system calls and system libraries

a simple C program

#include <stdio.h>

int main(void){

printf(“Hello World\n”);return 0;

}

Standard C headers

• stdio.h is one of the standard C headers that defines all standard input and output functions

• In this program, we have an output function printf which is declared in stdio.h

• stdio.h location is /usr/header/stdio.h

a simple C program

#include <stdio.h>

int main(void){

printf(“Hello World\n”);return 0;

}

main is the first function called when you execute your program.

next line

a simple C program

#include <stdio.h>

int main(void){

printf(“Hello World\n”);return 0;

}

main is the first function called when you execute your program.

What is int main(void)?

next line

Who calls main() function?

Who decided the name ‘main()’? Why can’t I write my own function ‘start()’ ?

a simple C program

#include <stdio.h>

int main(void){

printf(“Hello World\n”);return 0;

}

Main function

• Functions are a set of C instructions enclosed under a particular name. Eg:

• Functions make our code readable

int add(int a, int b){ int sum; sum = a + b; return sum;}

function name

return type parameters

instructions

a simple C program

#include <stdio.h>

int main(void){

printf(“Hello World\n”);return 0;

}

Main function

•main() is a special function that is first called when a C program is executed. Every C program must have only one main() function to execute.

• int main( void ) tells us that the main() function takes no parameters as input and returns an integer as output

•The main function is called by runtime environment of an operating system (Eg: in Unix, the program is executed by the shell interpreter. )

a simple C program

#include <stdio.h>

int main(void){

printf(“Hello World\n”);return 0;

}

C Standard 99

• C Standard 99 – is one of the many standards that put forward rules on how C programming language should be designed on different platforms (like Unix, Windows. Solaris, etc). This ensures a common functionality on all platforms

• C Standard 99 – an ISO defined standard states that a C program should have the any one of the two main() function definitions int main(void)

int main(int argc, char* argv[])

a simple C program

#include <stdio.h>

int main(void){

printf(“Hello World\n”);return 0;

}

printf function prints the characters “Hello World ” to the standard output stream. next line

a simple C program

#include <stdio.h>

int main(void){

printf(“Hello World\n”);return 0;

}

printf function prints the characters “Hello World ” to the standard output.

What do you mean by ‘standard output

stream’?

a simple C program

#include <stdio.h>

int main(void){

printf(“Hello World\n”);return 0;

}

Standard Streams

• Standard output in C language is defined as the output to the terminal screen. ( in Unix, it is the shell window where the program was executed)

• Along with standard output stream, C defines the standard input stream (keyboard) and standard error stream(screen again)

• These are also defined in stdio.h as per C Standard 99 definitions

a simple C program

#include <stdio.h>

int main(void){

printf(“Hello World\n”);return 0;

}return the value 0.

a simple C program

#include <stdio.h>

int main(void){

printf(“Hello World\n”);return 0;

}return the value 0.

Why are we returning ‘0’ ?

Who did we just send ‘0’ to?

a simple C program

#include <stdio.h>

int main(void){

printf(“Hello World\n”);return 0;

}End of program

• The last line of main() function has a return 0; line where 0 is defined as a successful execution in C Standard 99.

• It tells the runtime environment that the C program successfully executed all its lines of code.

• The significance of this return value arises when the runtime environment executes multiple C programs. It helps it keep track of how many programs successfully executed.

compile

$ gcc hello.c

Run a program namedgcc to compile the filehello.c

Notice that anotherfile called a.outis created in thesame folder

compile

$ gcc hello.c

Run a program namedgcc to compile the filehello.c

Notice that anotherfile called a.outis created in thesame folder

What is gcc ?

What exactly doyou mean by

‘compile’?

Whats inside my executable ‘a.out’ ?

compile

$ gcc hello.c Compile

• The process of converting a high level language (such as C language) instructions to a low level language (such as machine language 1’s and 0’s) instructions

• Strictly speaking, the process of compiling does not yield an executable file… It is only one of the many steps involved in creating it.

compile

$ gcc hello.c What reallyhappens is…Preprocessor (gcc)

Compiler (gcc)

Assembler (as)

Linker (ld)

hello.i

hello.s

hello.o

a.out

preprocessor to expand macros and includes header files

actual compilation of preprocessed source code to assembly language

convert assembly language into machine code and generate an object file

linking of object file with C run time libraries to create an executable

compile

$ gcc hello.cGNU Compiler Collection(GCC)

• This is a compiler system that is produced by the GNU (GNU Not Unix) Project.

• Originally named GNU C Compiler, later changed after the inclusion of languages like C++, Ada, Fortran, Lisp, Java, Objective-C, Go, and many more.

• Its open source!!

• Website: http://gcc.gnu.org

compile

$ gcc hello.c a.out

• The executable that is the available after the entire compilation and linking process

• Its all 1’s and 0’s (binary) which only the machine can understand… you definitely cannot

• All executables that are generated are always named a.out by default (can be overridden with the –o flag in gcc )

• ‘a.out’ name comes from a really old file format for executable files. Nowadays all executable files follow the ELF (Executable and Linkable Format)

execute$ a.outHello World$

Run a.out and seethe line “Hello World”on the screen.

execute$ a.outHello World$

Run a.out and seethe line “Hello World”on the screen.

Wow!! That looked easyHow did it all happen?

execute$ a.outHello World$

From Executable to a Process

HardDisk

a.outMemory Operating

System

pid 22134

• a.out file is loaded by the operating system to memory (RAM) as an executing process.

• Each process gets a unique id called process id (pid)

CPUstarting address

execute$ a.outHello World$

Memory Layout of your C program

Command line arguments, Environment variablesStack Segment

Heap Segment

BSS Segment

Initialized Data Segment

Text Segment

Now you know where the ‘segment’ in segmentationfault comes from

Memory Region

… and that’s how it works

what next?File I/O

Pointers & Memory Allocation

Data types& structures

Debugging

Macro and Pre Processor

Version Management

Multi file projects

Standard C Library

the creator of C…

Dennis Ritchie 9th Sept 1941 – 12th Oct 2011

thank you

Choose a job you love, and you will never have to work a day in your life

- Confucious