0% found this document useful (0 votes)
209 views8 pages

Lab 1 C Programming

The document discusses the basics of the C programming language. It describes C as a simple, machine-independent, mid-level language with structured programming, a rich library, memory management capabilities, and fast speed. It also provides examples of writing, compiling, and running simple C programs that demonstrate input/output functions like printf and scanf.
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
Download as docx, pdf, or txt
0% found this document useful (0 votes)
209 views8 pages

Lab 1 C Programming

The document discusses the basics of the C programming language. It describes C as a simple, machine-independent, mid-level language with structured programming, a rich library, memory management capabilities, and fast speed. It also provides examples of writing, compiling, and running simple C programs that demonstrate input/output functions like printf and scanf.
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1/ 8

Lab no: 2

Objective: Get familiar with basic programs of c


language
C is the widely used language. It provides many features that are given below.

1. Simple

2. Machine Independent or Portable

3. Mid-level programming language

4. structured programming language

5. Rich Library

6. Memory Management

7. Fast Speed

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.

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.
The following are the phases through which our program passes before being
transformed into an executable form:

o Preprocessor

o Compiler

o Assembler

o Linker

First C Program
Before starting the abcd 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:

1. #include <stdio.h>    
2. int main(){    
3. printf("Hello C Language");    
4. return 0;   
5. }  

#include <stdio.h> includes the standard input output library functions. The


printf() function is defined in stdio.h .

First C Program
Before starting the abcd 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:

1. #include <stdio.h>    
2. int main(){    
3. printf("Hello C Language");    
4. return 0;   
5. }  

#include <stdio.h> includes the standard input output library functions. The


printf() function is defined in stdio.h .

int main() The main() function is the entry point of every program in c


language.

printf() The printf() function is used to print data on the console.

return 0 The return 0 statement, returns execution status to the OS. The 0 value is
used for successful execution and 1 for unsuccessful execution.

Comments in C
Comments in C language are used to provide information about lines of code. It is
widely used for documenting code. There are 2 types of comments in the C language.
1. Single Line Comments

2. Multi-Line Comments

Single Line Comments


Single line comments are represented by double slash \\. Let's see an example of a
single line comment in C.

#include<stdio.h>    
int main(){    
    //printing information    
    printf("Hello C");    
return 0;  
}      

Multi Line Comments


Multi-Line comments are represented by slash asterisk \* ... *\. It can occupy many
lines of code, but it can't be nested. Syntax:

Let's see an example of a multi-Line comment in C.

#include<stdio.h>    
int main(){    
    /*printing information   
      Multi-Line Comment*/  
    printf("Hello C");    
return 0;  
}       

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:

1. 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.

1. scanf("format string",argument_list);  

In C programming,  printf()  is one of the main output function. The function sends
formatted output to the screen.
Example 1: C Output
#include <stdio.h>
int main()
{
// Displays the string inside quotations
printf("C Programming");
return 0;
}
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;  
}    

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.

1. #include<stdio.h>    
2. int main(){    
3. int x=0,y=0,result=0;  
4.   
5. printf("enter first number:");  
6. scanf("%d",&x);  
7. printf("enter second number:");  
8. scanf("%d",&y);  
9.   
10. result=x+y;  
11. printf("sum of 2 numbers:%d ",result);  
12.   
13. return 0;  
14. }    

You might also like