0% found this document useful (0 votes)
15 views

c

sdfgs
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
15 views

c

sdfgs
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 37

• 1) Simple

• C is a simple language in the sense that it provides a structured


approach (to break the problem into parts), the rich set of library
functions, data types, etc.
• 2) Machine Independent or Portable
• Unlike assembly language, c programs can be executed on different
machines with some machine specific changes. Therefore, C is a
machine independent language.
• 3) Mid-level programming language
• Although, C is intended to do low-level programming. It is used to
develop system applications such as kernel, driver, etc. It also
supports the features of a high-level language. That is why it is
known as mid-level language.
• 4) Structured programming language
• C is a structured programming language in the sense that we
can break the program into parts using functions. So, it is easy
to understand and modify. Functions also provide code
reusability.
• 5) Rich Library
• C provides a lot of inbuilt functions that make the development
fast.
• 6) Memory Management
• It supports the feature of dynamic memory allocation. In C
language, we can free the allocated memory at any time by
calling the free() function.
• 7) Speed
• The compilation and execution time of C language is fast since there are
lesser inbuilt functions and hence the lesser overhead.
• 8) Pointer
• C provides the feature of pointers. We can directly interact with the
memory by using the pointers. We can use pointers for memory,
structures, functions, array, etc.
• 9) Recursion
• In C, we can call the function within the function. It provides code
reusability for every function. Recursion enables us to use the approach
of backtracking.
• 10) Extensible
• C language is extensible because it can easily adopt new features.
• 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.
• Download Turbo C++
• Create turboc directory inside c drive and extract the tc3.zip inside c:\turboc
• Double click on install.exe file
• Click on the tc application file located inside c:\TC\BIN to write the c program
• 1) Download Turbo C++ software
• You can download turbo c++ from many sites. download Turbo c++
• 2) Create turboc directory in c drive and extract the tc3.zip
• Now, you need to create a new directory turboc inside the c: drive. Now extract the
tc3.zip file in c:\truboc directory.
• 3) Double click on the install.exe file and follow steps
• Now, click on the install icon located inside the c:\turboc
• General Overview of a Simple C Program's Structure:
• 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:
• 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.
• 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.
• 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.
• 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.
• 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.
• 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.
• 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.
• 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.
• 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.
• 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.
• 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.
• 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.
• 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:
• Code:
• #include <stdio.h>
• int main(){
• printf("Hello C Language");
• return 0;
• }
• Let us first study the various parts of this C
program:
• #include <stdio.h>:
• In this line, the program includes the standard
input/output library (stdio.h) due to the
preprocessor directive. For input and output
tasks, the stdio.h library contains methods like
printf and scanf.
• int main() { ... }:
• It is the main function which is the entry point of the C program. The
program starts executing from the beginning of the main function.
• printf("Hello World!\n");:printf("Hello World!");:
• Use the printf() function to print formatted output to the console. In this
example, the string "Hello, C Language" is printed, followed by a newline
character (n) which moves the pointer to the following line after the
message is displayed.
• return 0;
• When the return statement is 0, the program has been completed. When
determining the state of a program, the operating system frequently uses
the value returned by the main function. A return value of 0 often
indicates that the execution was successful.
• After compilation and execution, this C program will quit with a status
code 0 and output "Hello, C Language" to the terminal.
To write, compile, and run your first C program, follow these
steps:

• Step 1: Open a text editor


• Open a text editor of your choice, such as
Notepad, Sublime Text, or Visual Studio Code.
It will be where you write your C code.
• Step 2: Write the C program
• Now, copy and paste the following code into
the text editor:
• #include <stdio.h>
• int main() {
• printf("Hello, C Language");
• return 0;
• }
• Step 3: Save the file
• After that, save the file with a .c extension
such as first_program.c. This extension
indicates that it is a C source code file.
• Step 4: Compile the program
• Now, compile the program in the command
prompt.
• 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:
• ./first_program
• The program will execute, and you will see the
output on the console:
Output:
• Hello, C Language
• How to compile and run the C program
• There are two ways to compile & run the c program by menu and by
shortcut.
• By Menu
• Now click on the compile menu, then compile sub-menu to compile
the c program.
• Then click on the run menu and the sub-menu to run the c program.
• By shortcut
• Or, press the ctrl+f9 keys to compile and run the program directly.
• You will see the following output on the user screen.
• You can view the user screen any time by pressing the alt+f5 keys.
• Now press Esc to return to the turbo c++ console.
• Compilation process in c
• What is a compilation?
• 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
• Preprocessor
• Compiler
• Assembler
• Linker
• Preprocessor
• 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 preprocessor expands this code. After expanding
the code, the expanded code is passed to the compiler.
• 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.
• Assembler
• The assembly code is converted into object code by using an assembler. The name of the object
file generated by the assembler is the same as the source file. The extension of the object file in
DOS is '.obj,' and in UNIX, the extension is 'o'. If the name of the source file is 'hello.c', then the
name of the object file would be 'hello.obj'.
• Linker
• Mainly, all the programs written in C use library functions. These library functions are pre-
compiled, and the object code of these library files is stored with '.lib' (or '.a') extension. The main
working of the linker is to combine the object code of library files with the object code of our
program. Sometimes the situation arises when our program refers to the functions defined in
other files; then linker plays a very important role in this. It links the object code of these files to
our program. Therefore, we conclude that the job of the linker is to link the object code of our
program with the object code of the library files and other files. The output of the linker is the
executable file. The name of the executable file is the same as the source file but differs only in
their extensions. In DOS, the extension of the executable file is '.exe', and in UNIX, the executable
file can be named as 'a.out'. For example, if we are using printf() function in a program, then the
linker adds its associated code in an output file.
• Let's understand through an example.
• hello.c
• printf() and scanf() in C
• The printf() and scanf() functions are used for input and
output in C language. Both functions are inbuilt library
functions, defined in stdio.h (header file).
• printf() function
• The printf() function is used for output. It prints the given
statement to the console.
• The syntax of printf() function is given below:
• printf("format string",argument_list);
• The format string can be %d (integer), %c (character), %s
(string), %f (float) etc.
• scanf() function
• The scanf() function is used for input. It reads
the input data from the console.
• scanf("format string",argument_list);
• Program to print cube of given number
• Let's see a simple example of c language that gets input from the user and
prints the cube of the given number.
• #include<stdio.h>
• int main(){
• int number;
• printf("enter a number:");
• scanf("%d",&number);
• printf("cube of number is:%d ",number*number*number);
• return 0;
• }
• Output
• enter a number:5 cube of number is:125
• The scanf("%d",&number) statement reads
integer number from the console and stores
the given value in number variable.
• The printf("cube of number is:%d
",number*number*number) statement prints
the cube of number on the console.
• Program to print sum of 2 numbers
• Let's see a simple example of input and output
in C language that prints addition of 2
numbers.
• #include<stdio.h>
• int main(){
• int x=0,y=0,result=0;

• printf("enter first number:");
• scanf("%d",&x);
• printf("enter second number:");
• scanf("%d",&y);

• result=x+y;
• printf("sum of 2 numbers:%d ",result);

• return 0;
• }
• Output
• enter first number:9 enter second number:9 sum of 2 numbers:18

You might also like