C Programming – Features & The First C Program
Introduction to C Programming
In this section, the instructor introduces C programming and its features. The importance of dividing a task into smaller tasks is emphasized, and the concept of procedural language is explained.
Features of C Programming
- Portability and less lines of code are important features of C programming.
- C programming divides a program into smaller programs called procedures or functions.
- C programming offers a middle level of abstraction, making it easier for human beings to program while still allowing access to system-level features.
- C programming is popular for developing both system-level applications like kernels, device drivers, operating systems etc. and various software applications like games, editors and other graphic-rich applications.
Example Code in Code Blocks IDE
- Comments are written using double slashes in the first line. They are meant for developers' better understanding as they do not affect the compiler.
- #include is used to include header files that contain function declarations used in the program.
- int main() is where the execution of the program begins. It returns an integer value indicating whether the program executed successfully or not.
- printf() function prints output on console window.
Pre-processor and Functions
This section explains the role of pre-processor in C programming and the basic syntax of defining a function.
Pre-processor
- The pre-processor replaces textual content followed by hash symbol (#) with actual content before the compilation process begins.
- Header files, such as stdio.h, consist of declarations of functions like printf and scanf. Including header files is important to tell which functions you are going to use in your program.
- The pre-processing increases the size of the code, resulting in an expanded source code.
Functions
- A C function consists of a group of statements intended to solve a particular problem, while variables are entities used to store values for computation.
- The main function is predefined and serves as the entry point from where actual computation begins. It prints "Neso Academy" as output.
- The basic syntax for defining a function includes specifying its return type, name, parameter types enclosed within brackets, and statements enclosed within curly braces.
- After completing all its statements, a function returns an integer value. In our example, return 0 indicates successful execution while any other integer indicates something went wrong.
printf Function
This section explains how to use the printf function in C programming.
printf Function
- The printf function is predefined somewhere in the C standard library containing definitions of many functions like printf.
- To use this function, we simply put it inside our code followed by one parameter enclosed within quotes. We don't need to put curly brackets after this function; instead, we put a semicolon.
Introduction to C Programming
In this section, we learn how to execute a C program using Code Blocks. We also understand the importance of header files and standard libraries in C programming.
Executing a C Program
- To execute a C program in Code Blocks, click on the "Build and Run" button.
- If it's your first time executing the code, you need to use both "Build" and "Run."
- The output will be displayed in the console window.
- Click on the "Run" button if you are executing for the second time or not making any changes to the code.
Header Files and Standard Libraries
- Header files like stdio.h contain only declarations of functions like printf.
- Standard libraries consist of actual definitions of functions.
- The preprocessor combines declarations with source code to produce expanded source code.
- The linker maps prototypes mentioned by the preprocessor to actual codes written in standard library.
Importance of Separating Header Files and Standard Libraries
- Maintaining header files and standard libraries separately is essential for faster computation of programs.
- If preprocessor gets all actual definitions of functions, it increases code size exponentially, leading to slower compilation speed.
Exercise: Making Changes to a C Program
In this section, we learn how to make changes to a C program using Code Blocks. We also see how errors are displayed in case there are any issues with our code.
Making Changes
- Install Code Blocks and try executing the given C program.
- Make changes one by one: comment out #include, remove semicolon after printf, remove int return type from main function, replace 0 with some character (written with single quotes), replace 0 with some other integer value, remove semicolon after return statement.
Error Display
- Errors are displayed in the "Build Messages" tab.
- If there are no errors, the message "Build finished successfully" is displayed.