0% found this document useful (0 votes)
10 views29 pages

Lab 2

Uploaded by

Belete Siyum
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views29 pages

Lab 2

Uploaded by

Belete Siyum
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 29

C Programming Language: Introduction

C programming languages can be defined by the following


ways:
 Mother language

► Most of the compilers, JVMs, Kernels, etc. are written in C


language
► Most of the programming languages follow C syntax
System programming language
► Used to develop system software(e.g. OS, drivers, kernels,
etc.).
Procedure-oriented programming language
► In C, variables and function prototypes must be declared
before being used.
Introduction (cont’d)
 Structured programming language
► Structure means to break a program into parts or
blocks so that it may be easy to understand.
► A structured programming language is a subset of the
procedural language
► In the C language, we break the program into parts using
functions.
 Mid-level programming language
 It supports the feature of both low and high-level

 As low level PLs: convert a program into assembly code, it

supports pointer arithmetic


 As high level PLs: machine independent
Introduction (cont’d)

Reading Assignments
 History of C Language
 Features of C Language
 How to install C
First C Program

Frist C program
C program of components
► Header Files ► Additional Information
► Main Function ► Preprocessor Directives:
► Variable Declarations ► Data Types:
► Statements and Expressions ► Control Structures:
► Comments ► Error Handling:
► Functions ► Modularization:
► Return Statement ► Etc.
► Standard I/O
C program of components (cont’d)

1. Header Files
► #include directives
► provide function prototypes and definitions that
allow the C compiler to understand the functions
used in the program..
2. Main Function
► Every C program starts with the main function.
► Program's entry point, and execution starts from
here.
► Has a return type of int, indicating that it should
return an integer value to the OS upon completion.
C program of components (cont’d)
3. 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
4. Statements and Expressions
► This section contains the actual instructions and logic of the
program
5. Comments
► Provide human-readable explanations within the code.
► Denoted by // for single-line comments
► /* */ for multi-line comments
C program of components (cont’d)

6. 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.
7. Return Statement
►Use the return statement to terminate a function
►A return statement with a value of 0 typically
indicates a successful execution in the main function
►non-zero value indicates an error or unexpected
termination.
C program of components (cont’d)

8. Functions
►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).
printf() and scanf()
► The printf() and scanf() functions are used for input and
output in C language.
► Both functions are inbuilt library functions, defined in

stdio.h
A. printf(): used for output
► It prints the given statement to the console.
Syntax: printf("format string",argument_list);

► The format string can be %d (integer), %c (character), %s


(string), %f (float) etc.
printf() and scanf() (cont’d)
B. scanf() function
► The scanf() function is used for input.
► It reads the input data from the console.
Syntax: scanf("format string",argument_list);

Example1: Program to print cube of given number


printf() and scanf() (cont’d)
Example1: Program to print the sum of two numbers
Variables in C
► A variable is the name of the memory location.
► It is used to store information
► Its value can be reused several times

Syntax: data_type variable_name;


► data_type: represents the type of data that variable can hold
► Example: int, float (floating-point number), char
(character), double (a double-precision floating-point
number).
► variable_name: It is the identifier for the variable
Variables in C(cont’d)
Reading Assignment
Rules for defining variables
Types of Variables in C
There are many types of variables in c:
1.local variable

2.global variable

3.static variable

4.automatic variable

5.external variable
Types of Variables in C (cont’d)
1. Local Variable
► A variable that is declared inside the function

► It must be declared at the start of the block.

► You must have to initialize the local variable before it is used.

2. Global Variable
► A variable that is declared outside the function or block

► Any function can change the value of the global variable

► It is available to all the functions


Types of Variables in C (cont’d)
Example

3. Static Variable
► A variable that is declared with the static keyword

► It retains its value between multiple function calls.

Example

► If you call this function1 many times,


► The local variable will print the same value for each function call,

► static variable will print the incremented value in each function call.
Types of Variables in C (cont’d)
► Automatic Variable
► All variables in C that are declared inside the block, are

automatic variables by default.


► We can explicitly declare an automatic variable
using auto keyword.
Types of Variables in C (cont’d)
External Variable
►We can share a variable in multiple C source files by

using an external variable.


►To declare an external variable, you need to use extern

keyword.
►Syntax: extern int x=10;//external variable
Data Types in C
► A data type specifies the type of data that a variable can
store such as integer, floating, character, etc.
► There are the following data types in C language

Basic Data Type int, char, float, double

Derived Data Type array, pointer, structure, union

Enumeration Data Type enum

Void Data Type void


Data Types in C: Structures
► User defined data type that allows to combine data items of
different kinds; are used to represent a record.
Syntax: Defining a Structure

Example :
Data Types in C : Structures
► The structure tag is optional
► Each member definition is a normal variable definition,

such as int i; or float f; or any other valid variable


definition.
► At the end of the structure's definition, before the final

semicolon, you can specify one or more structure


variables but it is optional.
Data Types in C:Accessing Structure Members

► Use the member access operator (.)


► You would use the keyword struct to define variables of

structure type.
Example:
Data Types in C: Structures as Function Arguments

►We can pass a structure as a function argument in the


same way as you pass any other variable or pointer.
Example:
Data Types in C: Pointers to structures

► We can define pointers to structures in the same way as


you define pointer to any other variable
Data Types in C: union

► A union allows to store different data types in the same


memory location.
► Only one member can contain a value at any given time.

► Provide an efficient way of using the same memory

location for multiple-purpose.


► Defining a Union:
Data Types in C: union

► The union tag is optional and each member definition is


a normal variable definition,
► E.g. int i; or float f; or any other valid variable definition.
► At the end of the union's definition, before the final
semicolon, you can specify one or more union variables
but it is optional.
Example:
Data Types in C: Accessing union members
► To access any member of a union, we use the member
access operator (.)
► You would use the keyword union to define variables of

union type.
Reading Assignment
Keywords in C

You might also like