






Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
C programing language study notes civil engineering The document covers all c programming language topics and trics
Typology: Study notes
1 / 10
This page cannot be seen from the preview
Don't miss anything!







C language Tutorial with programming approach for beginners and professionals, helps you to understand the C language tutorial easily. Our C tutorial explains each topic with programs. The C Language is developed by Dennis Ritchie for creating system applications that directly interact with the hardware devices such as drivers, kernels, etc. C programming is considered as the base for other programming languages, that is why it is known as mother language. It can be defined b y the following ways:
C language is considered as the mother language of all the modern programming languages because most of the compilers, JVMs, Kernels, etc. are written in C language , and most of the programming languages follow C syntax, for example, C++, Java, C#, etc. It provides the core concepts like the array, strings, functions, file handling, etc. that are being used in many languages like C++, Java, C#, etc.
A system programming language is used to create system software. C language is a system programming language because it can be used to do low-level programming (for example driver and kernel). It is generally used to create hardware devices, OS, drivers, kernels, etc. For example, Linux kernel is written in C. It can't be used for internet programming like Java, .Net, PHP, etc.
A procedure is known as a function, method, routine, subroutine, etc. A procedural language specifies a series of steps for the program to solve the problem. A procedural language breaks the program into functions, data structures, etc. C is a procedural language. In C, variables and function prototypes must be declared before being used.
A structured programming language is a subset of the procedural language. Structure means to break a program into parts or blocks so that it may be easy to understand. In the C language, we break the program into parts using functions. It makes the program easier to understand and modify.
C is considered as a middle-level language because it supports the feature of both low-level and high- level languages. C language program is converted into assembly code, it supports pointer arithmetic (low-level), but it is machine independent (a feature of high-level). A Low-level language is specific to one machine, i.e., machine dependent. It is machine dependent, fast to run. But it is not easy to understand. A High-Level language is not specific to one machine, i.e., machine independent. It is easy to understand.
In this tutorial, all C programs are given with C compiler so that you can quickly change the C program File: main.c code.
History of C language is interesting to know. Here we are going to discuss a brief history of the c language. C programming language was developed in 1972 by Dennis Ritchie at bell laboratories of AT&T (American Telephone & Telegraph), located in the U.S.A. Dennis Ritchie is known as the founder of the c language. It was developed to overcome the problems of previous languages such as B, BCPL, etc. Initially, C language was developed to be used in UNIX operating system. It inherits many features of previous languages such as B and BCPL. Let's see the programming languages that were developed before C language.
C is the widely used language. It provides many features that are given below.
How to install C There are many compilers available for c and c++. You need to download any one. Here, we are going to use Turbo C++. It will work for both C and C++. To install the Turbo C software, you need to follow following steps.
You can download turbo c++ from many sites. download Turbo c++
Now, you need to create a new directory turboc inside the c: drive. Now extract the tc3.zip file in c: \truboc directory.
Now, click on the install icon located inside the c:\turboc It will ask you to install c or not, press enter to install. Change your drive to c, press c. Press enter, it will look inside the c:\turboc directory for the required files. Select Start installation by the down arrow key then press enter. Now C is installed, press enter to read documentation or close the software.
Now double click on the tc icon located in c:\TC\BIN directory to write the c program. First C Program
The general architecture of a simple C program typically consists of several vital components. Below is an outline of the essential elements and their purposes: o Header Files: The #include directives at the beginning of the program are used to include header files. Header files provide function prototypes and definitions that allow the C compiler to understand the functions used in the program. o Main Function: Every C program starts with the main function. It is the program's entry point, and execution starts from here. The main function has a return type of int , indicating that it should return an integer value to the operating system upon completion. o Variable Declarations: Before using any variables, you should declare them with their data types. This section is typically placed after the main function's curly opening brace. o Statements and Expressions: This section contains the actual instructions and logic of the program. C programs are composed of statements that perform actions and expressions that compute values.
o Comments: Comments are used to provide human-readable explanations within the code. They are not executed and do not affect the program's functionality. In C, comments are denoted by // for single-line comments and _/ /_ for multi-line comments. o Functions: C programs can include user-defined functions and blocks of code that perform specific tasks. Functions help modularize the code and make it more organized and manageable. o Return Statement: Use the return statement to terminate a function and return a value to the caller function. A return statement with a value of 0 typically indicates a successful execution in the main function , whereas a non-zero value indicates an error or unexpected termination. o Standard Input/Output: C has library functions for reading user input (scanf) and printing output to the console (printf). These functions are found in C programs and are part of the standard I/O library ( stdio.h header file). It is essential to include these fundamental features correctly while writing a simple C program to ensure optimal functionality and readability. Additional Information: There is some additional information about the C programs. Some additional information is as follows: o Preprocessor Directives: C programs often include preprocessor directives that begin with a # symbol. These directives are processed by the preprocessor before actual compilation and are used to include header files, define macros , and perform conditional compilation. o Data Types: C supports data types such as int, float, double, char , etc. It depends on the program's requirements, and appropriate data types should be chosen to store and manipulate data efficiently. o Control Structures: C provides control structures like if-else, while, for , and switch-case that allow you to make decisions and control the flow of the program. o Error Handling: Robust C programs should include error-handling mechanisms to handle unexpected situations gracefully. Techniques like exception handling (using try-catch in C++ ) or returning error codes are commonly employed. o Modularization: As programs grow in complexity , it becomes essential to modularize the code by creating separate functions for different tasks. This practice improves code reusability and maintainability. Remember, the architecture and complexity of a C program can vary significantly depending on the specific application and requirements. The outline is a general overview of a simple C program's structure. Explain the First C program: To write the first C program, open the C console and write the following code:
Step 5: Run the program After successful compilation , you can run the program by executing the generated executable file. Enter the following command into the terminal or command prompt :
1. ./first_program The program will execute, and you will see the output on the console:
Hello, C Language
There are two ways to compile & run the c program by menu and by shortcut.
o Now click on the compile menu, then compile sub-menu to compile the c program. o Then click on the run menu and the sub-menu to run the c program.
o Or, press the ctrl+f9 keys to compile and run the program directly. o You will see the following output on the user screen. o You can view the user screen any time by pressing the alt+f5 keys. o Now press Esc to return to the turbo c++ console. Conclusion: Finally, the first C program introduces the C programming language and its fundamental structure. It illustrates the necessary components for writing, compiling , and running a C program. The program contains the standard input-output library (stdio.h) , which includes routines for output operations such as printf(). The main() function is the program's entry point, from which execution begins. The printf() function is used within the main() method to print the message "Hello, C Language" to the console. A C compiler such as GCC is required to compile the program. The code is stored in a text file with the .c extension , and the compiler is started by typing gcc , followed by the names of the input and output files. The compilation process converts machine-readable instructions from human-readable C code. Once the program has been successfully constructed, it may be started by double-clicking the resultant executable file. In a terminal or command prompt , the executable is called by its file name , followed by ./. After that, the program is performed, and the "Hello C Language" output is shown on the console. By following these instructions, you will get a basic grasp of developing, constructing, and running a C program. It offers the groundwork for further investigating more complex ideas and developing more sophisticated applications using the C programming language. In windows 7 or window 8, it will show a dialog block to ignore and close the application because fullscreen mode is not supported. Click on Ignore button. Now it will showing following console.
Compilation process in c
The compilation is a process of converting the source code into object code. It is done with the help of the compiler. The compiler checks the source code for the syntactical or structural errors, and if the source code is error-free, then it generates the object code. The c compilation process converts the source code taken as input into the object code or machine code. The compilation process can be divided into four steps, i.e., Pre-processing, Compiling, Assembling, and Linking. The preprocessor takes the source code as an input, and it removes all the comments from the source code. The preprocessor takes the preprocessor directive and interprets it. For example, if <stdio.h>, the directive is available in the program, then the preprocessor interprets the directive and replace this directive with the content of the 'stdio.h' file. The following are the phases through which our program passes before being transformed into a n executable form: o Preprocessor o Compiler o Assembler o Linker
The source code is the code which is written in a text editor and the source code file is given an extension ".c". This source code is first passed to the preprocessor, and then the p reprocessor expands this code. After expanding the code, the expanded code is passed to the compiler.
The code which is expanded by the preprocessor is passed to the compiler. The compiler converts this code into assembly code. Or we can say that the C compiler converts the pre-processed code into assembly code.
In the above flow diagram, the following steps are taken to execute a program: o Firstly, the input file, i.e., hello.c, is passed to the preprocessor, and the preprocessor conver ts the source code into expanded source code. The extension of the expanded source code would be hello.i. o The expanded source code is passed to the compiler, and the compiler converts this expanded source code into assembly code. The extension of the assembly code would be hello.s. o This assembly code is then sent to the assembler, which converts the assembly code into object code. o After the creation of an object code, the linker creates the executable file. The loader will then load the executable file for the execution.