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

What Is C Programming (Autosaved)

C is a structured programming language developed in 1973 at Bell Labs. It is widely used today due to its structure, machine independence, and role in developing UNIX. The C compilation process involves preprocessing, compiling, assembling, and linking to convert source code into executable object code. Common I/O functions in C are printf() for output and scanf() for input.

Uploaded by

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

What Is C Programming (Autosaved)

C is a structured programming language developed in 1973 at Bell Labs. It is widely used today due to its structure, machine independence, and role in developing UNIX. The C compilation process involves preprocessing, compiling, assembling, and linking to convert source code into executable object code. Common I/O functions in C are printf() for output and scanf() for input.

Uploaded by

mariamgodwin987
Copyright
© © All Rights Reserved
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 29

C Programming

What is C Programming?
• C is a structured programming language developed by Dennis Ritchie in
1973 at Bell Laboratories.
• It is one of the most popular computer languages today because of its
structure, high-level abstraction, machine independent feature etc.
• C language was developed to write the UNIX operating system, hence it is
strongly associated with UNIX, which is one of the most popular network
operating system in use today and heart of internet data superhighway.
• The idea behind creating C language was to create an easy language which
requires a simple compiler and enables programmers to efficiently interact
with the machine/system, just like machine instructions.
• C language compiler converts the readable C language program into
machine instruction.
Why C Language is so popular?
• C language is a very good language to introduce yourself to the
programming world, as it is a simple procedural language which is
capable of doing wonders.
• Programs written in C language takes very less time to execute and
almost executes at the speed of assembly language instructions.
• Initially C language was mainly used for writing system level
programs, like designing operating systems, but there are other
applications as well which can be very well designed and developed
using C language, like Text Editors, Compilers, Network Drivers etc.
Latest Version of C
• The current latest version of C language is C11, which was introduced
in 2011. It is supported by all the standard C language compilers.
• Many new features have been introduced in this version and an
overall attempt to improve compatibility of the C language with C++
language has been made.
Features of C language
• It is a robust/strong language with rich set of built-in functions and
operators that can be used to write any complex program.
• The C compiler combines the capabilities of an assembly language
with features of a high-level language.
• Programs Written in C are efficient and fast.
• This is due to its variety of data type and powerful operators.
• C is highly portable this means that programs once written can be run
on another machines with little or no modification.
Features of C language
• Another important feature of C program, is its ability to extend itself.
• A C program is basically a collection of functions that are supported
by C library.
• We can also create our own functions and add it to C library.
• C language is the most widely used language in operating systems
and embedded system development today.
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++ which work for
both C and C++.
• To install the Turbo C software, you need to follow
following steps.
i. Download Turbo C++
ii. Create turboc directory inside c drive and extract the tc3.zip
inside c:\turboc
iii. Double click on install.exe file
iv. Click on the tc application file located inside c:\TC\BIN to
write the c program
How to install C …
• Now, you need to create a new directory let call it
turboc inside the c: drive.
• Copy the downloaded file into the directory (turboc) in
C drive.
• Now extract the tc3.zip file in c:\truboc directory.
• Double click on the install.exe file and follow steps
• Press enter to continue with installation.
• Change your drive to c, just press c
• Press enter, it will look inside the c:\turboc directory for
the required files.
How to install C ….
• Select Start installation by the down arrow key then
press enter.
• Now C is installed, press enter to read documentation
or close the software.
• Click on the TC application located inside c:\TC\BIN
• Now double click on the tc icon located in c:\TC\BIN
directory to write the c program.
Your First C program.
• Before you far about of C language, you need to learn
how to write, compile and run the first c program.
• To write the first c program, open the C console and
write the following code:
i. #include <stdio.h>
ii. int main(){
iii. printf("Hello C Language");
iv. return 0;
v. }
What does these five lines means?
• #include <stdio.h> includes the standard input
output library functions.
• The printf() function is defined in in this library file
(stdio.h). h, stands for header.
• int main() function is the entry point of every
program in c language. It must appear.
• printf() function is used to print data on the console.
• return 0 statement, returns execution status to the
OS.
• The 0 value is used for successful execution and 1 for
unsuccessful execution.
• The curly braces { } just after the main() function
encloses the body of main() function.
How to compile and run the c program
• There are 2 ways to compile and 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 then run sub menu to run
the c program.
• By shortcut
• Or, press ctrl+f9 keys compile and run the program
directly.
• 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.
#include <stdio.h>
int main(){
printf("Hello C Language");
return 0;
}
Compilation process in c, …
• 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,
Pre-processing, Compiling, Assembling, and
Linking.
Preprocessor
• 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.
• Or All occurrences of “pi” would thus be replaced by
“3.14”
• This is called expanded source code.
• The source code file is given an extension ".c“.
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 can say 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.
Linker
• 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.
Consider the following code segment
#include <stdio.h>
int main()
{
printf("Hello C Program");
return 0;
}
The flow diagram of the above program
In the above flow diagram, the following steps are
taken to execute a program:
• Firstly, the input file, i.e., hello.c, is passed to the
preprocessor, and the preprocessor converts the source
code into expanded source code.
• The extension of the expanded source code would
be hello.i.
• 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.
• This assembly code is then sent to the assembler, which
converts the assembly code into object code.
• After the creation of an object code (hello.obj), the linker
creates the executable file.
• The loader will then load the executable file(hello.exe) for
the execution.
printf() and scanf() functions 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

printf("format string",argument_list);
The format string can be
•%d (integer),
•%c (character),
•%s (string),
•%f (float) etc.
• We can also limit the number of digits or characters
that can be input or output, by adding a number with
the format string specifier, like "%1d" or "%3s“.
• The first one means a single numeric digit and the
second one means 3 characters, hence if you try to
input 42, while scanf() has "%1d", it will take only 4 as
input. Same is the case for output.
The syntax of scanf() function
• The scanf() function is used for input.
• It reads the input data from the console.
• scanf("format string",argument_list);
• Consider a simple example of c language that gets input
from the user and prints the cube of the given number.
Example
#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;
}
• 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.
Enter a number:5 // if you input 5
Cube of number is:125
Program to print sum 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