c - isro
DESCRIPTION
Its a presentationTRANSCRIPT
![Page 1: C - ISRO](https://reader033.vdocuments.mx/reader033/viewer/2022061300/54c827db4a7959c0108b45de/html5/thumbnails/1.jpg)
Ca series of presentations to help a bunch of
brilliant space scientists understand a brilliant programming language
![Page 2: C - ISRO](https://reader033.vdocuments.mx/reader033/viewer/2022061300/54c827db4a7959c0108b45de/html5/thumbnails/2.jpg)
Why?
You wrote a really complex C program called
super_complex_no_idea_what_i_justwrote.c
![Page 3: C - ISRO](https://reader033.vdocuments.mx/reader033/viewer/2022061300/54c827db4a7959c0108b45de/html5/thumbnails/3.jpg)
Why?
You use gcc to compile it . . .
$ gcc super_complex_no_idea_what_i_justwrote.c
![Page 4: C - ISRO](https://reader033.vdocuments.mx/reader033/viewer/2022061300/54c827db4a7959c0108b45de/html5/thumbnails/4.jpg)
Why?
compiling . . .
![Page 5: C - ISRO](https://reader033.vdocuments.mx/reader033/viewer/2022061300/54c827db4a7959c0108b45de/html5/thumbnails/5.jpg)
Why?
no errors. . .
![Page 6: C - ISRO](https://reader033.vdocuments.mx/reader033/viewer/2022061300/54c827db4a7959c0108b45de/html5/thumbnails/6.jpg)
Why?
$./a.outSegmentation Fault$
![Page 7: C - ISRO](https://reader033.vdocuments.mx/reader033/viewer/2022061300/54c827db4a7959c0108b45de/html5/thumbnails/7.jpg)
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
![Page 8: C - ISRO](https://reader033.vdocuments.mx/reader033/viewer/2022061300/54c827db4a7959c0108b45de/html5/thumbnails/8.jpg)
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
![Page 9: C - ISRO](https://reader033.vdocuments.mx/reader033/viewer/2022061300/54c827db4a7959c0108b45de/html5/thumbnails/9.jpg)
Lets analyze it in detail . . . .
![Page 10: C - ISRO](https://reader033.vdocuments.mx/reader033/viewer/2022061300/54c827db4a7959c0108b45de/html5/thumbnails/10.jpg)
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
![Page 11: C - ISRO](https://reader033.vdocuments.mx/reader033/viewer/2022061300/54c827db4a7959c0108b45de/html5/thumbnails/11.jpg)
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 ?
![Page 12: C - ISRO](https://reader033.vdocuments.mx/reader033/viewer/2022061300/54c827db4a7959c0108b45de/html5/thumbnails/12.jpg)
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.
![Page 13: C - ISRO](https://reader033.vdocuments.mx/reader033/viewer/2022061300/54c827db4a7959c0108b45de/html5/thumbnails/13.jpg)
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 .
![Page 14: C - ISRO](https://reader033.vdocuments.mx/reader033/viewer/2022061300/54c827db4a7959c0108b45de/html5/thumbnails/14.jpg)
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
![Page 15: C - ISRO](https://reader033.vdocuments.mx/reader033/viewer/2022061300/54c827db4a7959c0108b45de/html5/thumbnails/15.jpg)
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
![Page 16: C - ISRO](https://reader033.vdocuments.mx/reader033/viewer/2022061300/54c827db4a7959c0108b45de/html5/thumbnails/16.jpg)
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
![Page 17: C - ISRO](https://reader033.vdocuments.mx/reader033/viewer/2022061300/54c827db4a7959c0108b45de/html5/thumbnails/17.jpg)
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()’ ?
![Page 18: C - ISRO](https://reader033.vdocuments.mx/reader033/viewer/2022061300/54c827db4a7959c0108b45de/html5/thumbnails/18.jpg)
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
![Page 19: C - ISRO](https://reader033.vdocuments.mx/reader033/viewer/2022061300/54c827db4a7959c0108b45de/html5/thumbnails/19.jpg)
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. )
![Page 20: C - ISRO](https://reader033.vdocuments.mx/reader033/viewer/2022061300/54c827db4a7959c0108b45de/html5/thumbnails/20.jpg)
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[])
![Page 21: C - ISRO](https://reader033.vdocuments.mx/reader033/viewer/2022061300/54c827db4a7959c0108b45de/html5/thumbnails/21.jpg)
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
![Page 22: C - ISRO](https://reader033.vdocuments.mx/reader033/viewer/2022061300/54c827db4a7959c0108b45de/html5/thumbnails/22.jpg)
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’?
![Page 23: C - ISRO](https://reader033.vdocuments.mx/reader033/viewer/2022061300/54c827db4a7959c0108b45de/html5/thumbnails/23.jpg)
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
![Page 24: C - ISRO](https://reader033.vdocuments.mx/reader033/viewer/2022061300/54c827db4a7959c0108b45de/html5/thumbnails/24.jpg)
a simple C program
#include <stdio.h>
int main(void){
printf(“Hello World\n”);return 0;
}return the value 0.
![Page 25: C - ISRO](https://reader033.vdocuments.mx/reader033/viewer/2022061300/54c827db4a7959c0108b45de/html5/thumbnails/25.jpg)
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?
![Page 26: C - ISRO](https://reader033.vdocuments.mx/reader033/viewer/2022061300/54c827db4a7959c0108b45de/html5/thumbnails/26.jpg)
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.
![Page 27: C - ISRO](https://reader033.vdocuments.mx/reader033/viewer/2022061300/54c827db4a7959c0108b45de/html5/thumbnails/27.jpg)
compile
$ gcc hello.c
Run a program namedgcc to compile the filehello.c
Notice that anotherfile called a.outis created in thesame folder
![Page 28: C - ISRO](https://reader033.vdocuments.mx/reader033/viewer/2022061300/54c827db4a7959c0108b45de/html5/thumbnails/28.jpg)
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’ ?
![Page 29: C - ISRO](https://reader033.vdocuments.mx/reader033/viewer/2022061300/54c827db4a7959c0108b45de/html5/thumbnails/29.jpg)
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.
![Page 30: C - ISRO](https://reader033.vdocuments.mx/reader033/viewer/2022061300/54c827db4a7959c0108b45de/html5/thumbnails/30.jpg)
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
![Page 31: C - ISRO](https://reader033.vdocuments.mx/reader033/viewer/2022061300/54c827db4a7959c0108b45de/html5/thumbnails/31.jpg)
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
![Page 32: C - ISRO](https://reader033.vdocuments.mx/reader033/viewer/2022061300/54c827db4a7959c0108b45de/html5/thumbnails/32.jpg)
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)
![Page 33: C - ISRO](https://reader033.vdocuments.mx/reader033/viewer/2022061300/54c827db4a7959c0108b45de/html5/thumbnails/33.jpg)
execute$ a.outHello World$
Run a.out and seethe line “Hello World”on the screen.
![Page 34: C - ISRO](https://reader033.vdocuments.mx/reader033/viewer/2022061300/54c827db4a7959c0108b45de/html5/thumbnails/34.jpg)
execute$ a.outHello World$
Run a.out and seethe line “Hello World”on the screen.
Wow!! That looked easyHow did it all happen?
![Page 35: C - ISRO](https://reader033.vdocuments.mx/reader033/viewer/2022061300/54c827db4a7959c0108b45de/html5/thumbnails/35.jpg)
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
![Page 36: C - ISRO](https://reader033.vdocuments.mx/reader033/viewer/2022061300/54c827db4a7959c0108b45de/html5/thumbnails/36.jpg)
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
![Page 37: C - ISRO](https://reader033.vdocuments.mx/reader033/viewer/2022061300/54c827db4a7959c0108b45de/html5/thumbnails/37.jpg)
… and that’s how it works
![Page 38: C - ISRO](https://reader033.vdocuments.mx/reader033/viewer/2022061300/54c827db4a7959c0108b45de/html5/thumbnails/38.jpg)
what next?File I/O
Pointers & Memory Allocation
Data types& structures
Debugging
Macro and Pre Processor
Version Management
Multi file projects
Standard C Library
![Page 39: C - ISRO](https://reader033.vdocuments.mx/reader033/viewer/2022061300/54c827db4a7959c0108b45de/html5/thumbnails/39.jpg)
the creator of C…
Dennis Ritchie 9th Sept 1941 – 12th Oct 2011
![Page 40: C - ISRO](https://reader033.vdocuments.mx/reader033/viewer/2022061300/54c827db4a7959c0108b45de/html5/thumbnails/40.jpg)
thank you
Choose a job you love, and you will never have to work a day in your life
- Confucious