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

7864 File Week 3

This document outlines the course content for Week 3 of an Introduction to C Programming course. The week will cover scope rules in C, including global and local variables as well as formal parameters. It will also cover the sizeof operator in C, explaining how it can be used to determine the size of variables and data types. The objectives are to understand the difference between global and local variables and determine the use of the sizeof operator in C programming.

Uploaded by

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

7864 File Week 3

This document outlines the course content for Week 3 of an Introduction to C Programming course. The week will cover scope rules in C, including global and local variables as well as formal parameters. It will also cover the sizeof operator in C, explaining how it can be used to determine the size of variables and data types. The objectives are to understand the difference between global and local variables and determine the use of the sizeof operator in C programming.

Uploaded by

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

WEEK 3

COURSE OUTLINE

COURSE CODE : IT
TITLE : Introduction to C Programming 2
TARGET POPULATION : All BS Information Technology Students
INSTRUCTOR : MA. ANJELLY E. FUSINGAN

Overview:

C is a general-purpose, high-level language that was originally developed by Dennis M. Ritchie to


develop the UNIX operating system at Bell Labs. C was originally first implemented on the DEC PDP-11
computer in 1972. In 1978, Brian Kernighan and Dennis Ritchie produced the first publicly available
description of C, now known as the K&R standard. The UNIX operating system, the C compiler, and
essentially all UNIX application programs have been written in C.

Content
 Scope Rules
 Sizeof Operator in C

Objectives:

 Know the difference between global and local variables.


 Determine the use of sizeof operator in C programming.
Instruction to the Learner

Each chapter in this module contains a major lesson involving the use of Flowchart and its purpose.
The units are characterized by continuity, and are arranged in such a manner that the present unit is
related to the next unit. For this reason, you are advised to read this module. After each unit, there are
exercises to be given. Submission of task given will be every Monday during your scheduled class hour.

SCOPE RULES

A scope in any programming is a region of the program where a defined variable can have its existence
and beyond that variable it cannot be accessed. There are three places where variables can be declared
in C programming language:

 Inside a function or a block which is called local variables,

 Outside of all functions which is called global variables.

 In the definition of function parameters which are called formal parameters.

Let us understand what are local and global variables, and formal parameters.

Local Variables

Variables that are declared inside a function or block are called local variables. They can be used only by
statements that are inside that function or block of code. Local variables are not known to functions
outside their own. The following example shows how local variables are used. Here all the variables a, b,
and c are local to main() function.

#include<stdio.h>

int main ()

/* local variable declaration */

int a, b;

int c;

/* actual initialization */

a = 10;

b = 20;
c = a + b;

printf ("value of a = %d, b = %d and c = %d\n", a, b, c);

return 0;

Global Variables

Global variables are defined outside a function, usually on top of the program. Global variables hold
their values throughout the lifetime of your program and they can be accessed inside any of the
functions defined for the program.

A global variable can be accessed by any function. That is, a global variable is available for use
throughout your entire program after its declaration. The following program shows how global variables
are used in a program.

#include<stdio.h>

/* global variable declaration */

int g;

int main ()

/* local variable declaration */

int a, b;

/* actual initialization */

a = 10;

b = 20;

g = a + b;

printf ("value of a = %d, b = %d and g = %d\n", a, b, g);

return 0;

A program can have same name for local and global variables but the value of local variable inside a
function will take preference. Here is an example:

#include<stdio.h>

/* global variable declaration */


int g = 20;

int main ()

/* local variable declaration */

int g = 10;

printf ("value of g = %d\n", g);

return 0;

When the above code is compiled and executed, it produces the following result:

value of g = 10

Formal Parameters

Formal parameters are treated as local variables with-in a function and they take precedence over global
variables. Following is an example:

#include<stdio.h>

/* global variable declaration */

int a = 20;

int main ()

/* local variable declaration in main function */

int a = 10;

int b = 20;

int c = 0;

printf ("value of a in main() = %d\n", a);

c = sum( a, b);

printf ("value of c in main() = %d\n", c);

return 0;

/* function to add two integers */


int sum(int a, int b)

printf ("value of a in sum() = %d\n", a);

printf ("value of b in sum() = %d\n", b);

return a + b;

When the above code is compiled and executed, it produces the following result:

value of a in main() = 10

value of a in sum() = 10

value of b in sum() = 20

value of c in main() = 30

Initializing Local and Global Variables

When a local variable is defined, it is not initialized by the system, you must initialize it yourself. Global
variables are initialized automatically by the system when you define them, as follows:

Data Type Initial Default Value


int 0
char '\0'
float 0
double 0
pointer NULL
It is a good programming practice to initialize variables properly, otherwise your program may produce
unexpected results, because uninitialized variables will take some garbage value already available at
their memory location.

Sizeof operator in C
The sizeof operator is the most common operator in C. It is a compile-time unary operator and used to
compute the size of its operand. It returns the size of a variable. It can be applied to any data type, float
type, pointer type variables.
When sizeof() is used with the data types, it simply returns the amount of memory allocated to that data
type. The output can be different on different machines like a 32-bit system can show different output
while a 64-bit system can show different of same data types.
Example:

#include <stdio.h>

int main() {
int a = 16;

printf("Size of variable a : %d\n",sizeof(a));

printf("Size of int data type : %d\n",sizeof(int));

printf("Size of char data type : %d\n",sizeof(char));

printf("Size of float data type : %d\n",sizeof(float));

printf("Size of double data type : %d\n",sizeof(double));

return 0;

Output
Size of variable a : 4

Size of int data type : 4

Size of char data type : 1

Size of float data type : 4

Size of double data type : 8

When the sizeof() is used with an expression, it returns the size of the expression. Here is an example.

Example
#include <stdio.h>

int main() {

char a = 'S';

double b = 4.65;

printf("Size of variable a : %d\n",sizeof(a));

printf("Size of an expression : %d\n",sizeof(a+b));

int s = (int)(a+b);

printf("Size of explicitly converted expression : %d\n",sizeof(s));

return 0;

Output

Size of variable a : 1

Size of an expression : 8
Size of explicitly converted expression : 4

You might also like