History of C and basics of Programming We

History of C and basics of Programming

We need a compiler first Please install any C compiler in your machine Suggested: Gnu Compiler Collection (gcc) https: //gcc. gnu. org/ Others: https: //cygwin. com/ http: //www. mingw. org/

Why Program? • • • Computer – programmable machine designed to follow instructions Program – instructions in computer memory to make it do something Programmer – person who writes instructions (programs) to make computer perform a task

Main Hardware Component Categories Central Processing Unit Output Device Input Device Main Memory Secondary Storage Devices

Main Memory • • • Holds both program instructions and data Volatile – erased when program terminates or computer is turned off Also called Random Access Memory (RAM)

Main Memory Organization • • • Bit • Smallest piece of memory • Stands for binary digit 8 bits • Has values 0 (off) or 1 (on) Byte • Is 8 consecutive bits 0 1 1 0 0 1 1 1 Word • Usually 4 consecutive bytes • Has an address 1 byte

What is C • C is a very powerful complied system programming language • Introduced by Dennis Ritchie sometime between 1969 and 1973 at AT&T Bell Labs • C is a procedural programming paradigm • C allows for recursion and lexical variable scope all while using static typing to help prevent unintentional errors

Example of a C program

What do you need to write and execute a C program • Text editor to write your code (source file). • Suggested : notepad++ • Compiler to convert human-readable source code into machine-readable machine code (executable/. exe file) • Inbuilt cmd shell or terminal in your machine to see the output Note: C source file usually end with. c

C applications • C is used where speed, space, and portability are important. • Most operating systems are written in C. • Most other computer languages are also written in C. • And most game software is written in C.

Anatomy of a C programs normally begin with a comment. The comment starts with /* The comment ends with */ Next comes the include section Finally you will find in a source file are the functions

Anatomy of a C program Comments. The comment starts with /* The comment ends with */ • Comment describes the purpose of the code in the file, and might include some license or copyright information. • There’s no need to include a comment at the beginning or anywhere else in the file. But it’s good practice and what most C programmers will expect to find.

Anatomy of a C program Include section • Almost every C can do nothing without the help of the external libraries. • Libraries are nothing but an external code that is written by somebody. • You will need to tell the compiler what external code to use by including header files for the relevant libraries. • stdio. h is the most popular library, you will see this very often. • The stdio library contains code that allows you to read and write data from and to the terminal.

Anatomy of a C program Functions • All C code runs inside functions. The most important function you will find in any C program is called the main() function. • The main() function is the starting point for all of the code in your program.

What is main() function body • The computer will start running any program from the main() function, the program doesn't start without this function. • main() function has a return type of int • return values are needed to to decide if the program ran successfully or not. • For example, if you tell your main() function to return 0, this means that the program was successful. If any other value is returned, there was a problem in executing your code. • The function body must be surrounded by braces.

Homework • Install compiler in your machine and run the below code
- Slides: 16