[go: up one dir, main page]

0% found this document useful (0 votes)
70 views42 pages

Programming: Structures

1. The document discusses C programming concepts like data structures, scopes, and modifiers. 2. It explains the auto and extern modifiers in C - auto means a variable is local to its scope, while extern allows a variable declared in one file to be accessed in another file. 3. Examples are provided to demonstrate how variables with different scopes and modifiers like local, global, auto, and extern can be accessed within and outside their declaration blocks.

Uploaded by

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

Programming: Structures

1. The document discusses C programming concepts like data structures, scopes, and modifiers. 2. It explains the auto and extern modifiers in C - auto means a variable is local to its scope, while extern allows a variable declared in one file to be accessed in another file. 3. Examples are provided to demonstrate how variables with different scopes and modifiers like local, global, auto, and extern can be accessed within and outside their declaration blocks.

Uploaded by

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

C

PROGRAMMING AND
DATA STRUCTURES
Question/Answer (Set 1)

NESO ACADEMY
#include <stdio.h>

int main() {
printf("%d", print f("%s", "Hello World!") );
return 0;
"CAUsers\jaspr\ Downloads\C programs of dennis ritchie\questions-an

Hello World !12


Process returned e (exe) execution time :0.266 s
Press any key to continue.
int main() {
;
printf("%10s", "Hello")
return 0;

NESOACADEMY
#include <stdio.h>

3 int main () { CAUsersjaspr\Downloads\Cprograms of dennis ritchielquestions-answers-set1.exe"


printf ("%s\n", "Hello") : Hello
5 printf ("%10s", "Hello")
return 0:
: Hello
Acess returned e (exe) execution time :e.385 s
Press any key to continue.
int main() {
char c= 255;
c =c + 10;
printf("%d", c);
return 0;

a) 265
b) Some characteraccording to ASCIl table
c)7
d 9

NESO ACADEMY
Q4: Which of the following statement/statements is/are
correct corresponding to the definition of integer:

I. signed int i;
a) Only land V are correct
Il. signed i;
b) Only is correct
l

Ill. unsigned i Le All are correct


IV. long i d) Only IV,V, VIare correct
V. long int i;

VI. long long i

Because integer is implicitly assumed.

NESO ACADEMY
Q5: What does the following progranm fragment prints?
int main() { a) garbage
unsigned i = 1; b) -3
int j= -4;
printf("%u", i+j);
return 0; ) Integer value depends
machine tomachine
None of theabove
from

-3 in 2s complement representation:
Step 1: Take 1s complement of3
3=00000000 00000000 00000000 00000011
1s complement of 3=111 11111 n00
Step 2: Add 1 to the result. It will give
111111 111 111111 1111101 = 4294967293 (on my computer)
NESO ACADEMY
DEFINING ScOPE

Scope = Lifetime

Thearea under which a variable isapplicable or alive.

Strict Definition: a block or a region where avariable is

declared, defined and used and when a block or a


region ends, variable is automatically destroyed.

NESO ACADEMY
#include <stdio. h>

int main() {
Gnt var = 34; Scope of this variable is within
printf("%d", var); main0 function only. Therefore
return ; called LOCAL to main) function.

error: 'var' undeclared (first use in this function)

int fun()

printf("%d", var) Trying to access variable var'


outside main) function
BASIC PRINCIPLE OF SCOPING

{ Contents of
this block is

Contents of not visible to


outer block any block
upto this outside to
this block.
point are
visible to the
internal
block -}
Contents of
this block is
Contents of not visible to
internal
any block
block are not
visible to
outer block.
} outside to
this block

NESO ACADEMY
#include <stdio.h> #include <stdio .h>

int main(),{
int main() { int var = 3;
int var = 3;
int var = 4; {
int var = 4;
printf("%d\n", var); printf("%d\n", var);
printf("%d", var); }
return 0; printf("%d", var);
return 0;
#include <stdio. h>
int fun();

This variable is outside of all functions.


int var = 10;
Therefore called a GLOBAL Variable

int main() {
int var = 3;
printf("%d\n", var; Output: 3
fun()
return 0;
Output: 10

int fun(O

It access the GLOBAL


printf("%d", (var will Variable.
C

PROGRAMMING AND
|DATASTRUCTURES
Modifiers -auto and extern

NESO ACADEMY
WHAT IsAUTO MODIFIER?
Auto means Automatic
Variables declared inside a scope bydefault are automatic variables.

Syntax: auto int some_variable_name;

#include <stdio.h> #include <stdio.h>

int main() { int main() {


int var; auto int var;
return O; return0;
}
#include <stdio.h>

int main() {
auto int var;
printf("%d", var);
return 0;
#include <stdio.h>

int var;
int main() {
printf( "%d", var};
return O;
ExTERN MODIFIER

int var; extern int var;

Declaration and Definition Declaration

Extern is short name for external.


Used when a particular file needs toaccess a variable from
anotherfile.

NESO ACADEMY
main.c xother.c x

int a 57
2 int a

& others
4include <stdio.h>

3 extern int a:
int main ()
5
6
7
printf ("$d", a)
return 0:
: BCUsersjaspr\Documentslmy, program_ 1\bin\ Debuglmy.program_1.exe

8 Wocess returned e (xe) execution time : e.448 s


Press any key to continue.

Logs & others

Code::Blocks x9 Searchresults xCccc xo


main.c x other.c x

1
6include <stdio.h>
2
3 extern int a;
4 extern inh
5 extern int a;
6 int main () ClUsersljaspr\Documents\imy, program_ 1\bin\Debugimy program_1.exe
5
8| printf ("%d", a)
return
; Process returned e (exe) execution time : e.430 s

0:
10 Press any key to continue.
11

Logs & others

Code::Blocks xSearch results Cccc x

Build: Debug in progran_1 ny

ming32-gcc.exe -Wall -g-std=c99 -C C:\User


mingw32-g++.exe -o bin\ Debug\my program_1.ex
Output file is bin\ Debug\my program_1.exewit
Process terminated wi th status 0 (0 minute(s)
o error(3), 0 warn ing(3) (0 minute (s) 1 secd ,
Management main.c x other.c x
Projects Symbols
#include <stdio.h>
OWorkspace
my program_1 3 int a = 9;
4 iny main ()
Sources
5
) main.c Documents\my Debug\my program_1.exe
6 extern int a: C\Usersjaspr\ program_1\lbin\

other.c
7
8
printf ("%d", a)
return 0;
: 9
Process returned e (exe)execution time :e.358 s
9 Press any key to continue.
10

Logs & otthers


Code::Blocks x9 Search results xCccc xO
Build lo

Build: Debug in my progranl (oompile


ingw32-gcc. exe -Wall -
-std=c99 -c C:\Users\jaspr
mingw32-gcc.exe -Wall -g -std=c99 -c C:\Users\jasp:
ing32-g+t.exe -o bin\Debug\my_program_1.exe obj\a
Output file is bin\ Debug \my program_1.exe with size
Process termina ted ith status 0
(0 minute (s) 1 sed ,
O orror (s),0 warning (s) (0 minute (s), 1 second (s))
TAKE AWAYS

1. When we write extern some_data type some_variable _name; n


memory is allocated.Only property ofvariable is announced.
2. Multiple declarations of extern variable is allowed within the file. This is

3. Extern variable says tocompiler o


not the case with automatic variables.
outside from my scope and you
will find the definition of the variable that I declared".

4. Compilerbelieves thatwhatever the extern variable said is true and


produce no error. Linker throws an error when he finds no such variable
exist.

5. When an extern variable is initialized,then memory for thisvariable is


allocated and it will be considered defined.

NESO ACADEMY
C

PROGRAMMING AND
DATA STRUCTURES
Modifier -register
NESO ACADEMY
Register
Memory

Increasing order of Cache


access time ratio
Memory

Main Memory Primary Memory

Magnetic Disks
Auxillary

Memory
Magnetic Tapes
#include <stdio.h>

int main() {
register int var;
return 0;
WHAT ISREGISTER MODIFIER?

Register keyword hints the conmpiler to store a variable


inregister memory.

This is done because access time reduces greatly for


most frequently referred variables

This the choice of compiler whether


is it puts the given
variable in register or not.

Usuallycompiler themselves do the necessary


optimizations
NESO ACADEMY
C

PROGRAMMING AND
DATA STRUCTURES
Modifier -static
NESO ACADEMY
"main.c add.c

x
1 int increment ()

int count 0:
count count + li
5 return count

Logs & others

A Code::Blocks x9 Search results x Ccc xO Build log xBuild messages x CpoCheck x opCheck messages x Cscope xDebugger
Management
main.c x add.cx
Projects Symbols
#include <stdio.h>
oWorkspace 2 #include <stdlib.h>
3
Smy-project1
Sources

add.c
main.c
4
5 {int main ()

int value:
value = increment ()
CUsersjaspr\Documentsimy-project1\bin\ Debugimyproject1.exe

Fspcess returned e (exe) execution time : 1.459 s


value = increment () Pess any key to continue.
value increment ()
10
11
12
printf ("%d",value)
return
:

0;
13
14

Logs & others

Code::Blocks x9 Search results x Cccc x

C:\Users\jaspr\Documents \my-projectl\main.c:
C: \Usera \ja spr\Documen ts \my-projectl \ma in.c:
=
7}
value increment ()

mingw32-g+t.exe -o bin\Debug \my-projectl.exe


Output tile is bin\ Debug \my-projectl.exe with
Process termina ted with status 0 (0 minute (s)
O error (s), 1 warning (s) (0 minute (s) 6 sec0 ,
main.c x "adH,c x

1 int count
2
3 int increment ()
4 P{
5 count count + li
6 return count
7
8
main.c x add.c x

1 4include <stdio.h>
2 #include <stdlib.h>
3
4
5
6
{ int main()

int value: CAUsersljaspr\[limcuments\my-project1\bin\ Debugimy- project1.exe


value = increment () # 3
8 value = increment () Process returned (exe) execution time : e.388 s

10
value increment :
() Press any key to continue.

11 printf ("$d", value) :


12 return 0:
13
14

Logs & others


main.c x add.c x

1 #include <stdio.h>
2 #$include <stdlib.h>
3
4

5
6
{ int main ()

int value: BCAUsersjaspr\|Mpcuments\my-project1\bin\ Debug\imy-project1.exe

7 value = increment ()#


8 value = increment () Process returned e (exe)execution time : e.388 s

10
value increment (): Press any key to continue.

11 printf ("%d", value) :


12 return 0;

13
14

Logs & others


Management main.c x add.c x
Projects Symbols
#include <stdio.h>
oWorkspace 2 #include <stdlib.h>
Smy-project1 3
Sources
4 extern int count
5 int main () CAUsersjasp\ Documents\my-projecti\bin\ Debuglmy-project1.exe
add.c
6 pI 6
main.c 7 int value: execution time : e.616 s
8 Process returned e (exe)
value = increment () Press any key to continue.
9 value increment ()#
10 value m increment()
11 count - count + 3:
12 value = count:
13 printf ("%d", value)
14 return 0:
15
16

Logs & others

4 Code::Blocks x9 Search results x Cccc xBuild log


C:\Users\jaspr\Documents\my-projectl\main.c: In funct
C: \Users \ja spr\ Documen ts \my-projectl \main. c:8:5: war
value =
increment ()

mingw32-g++.exe -o bin\Debug \my-projecti.exe obj\De


Output tile is bin\ Debug\my-projectl.exe with size 7
Process termina ted wi th status 0 (0 minute (s), 1 sec
O rror (a), 1 warning (s) (0 minute (s) 1 second (s) , )
int count

int increment ()

count = count + 1:
return count
main.c "add.c
1 static int count
2

4P
5
int increment ()

count count + l:
6 return count

Logs & others

Code::Blocks x9 Search results xCccc xO


Build log Build messages x CopCheck xCppCheck messages x Cscope xDebugger
main.c x add.c x

2
1 #include <stdio h> .
#include <stdlib.h>

extern int count


5 inm main ()
6
7 int value:
8 value =
increment ()1
9
10
value -
increment () :
value increment ()
11| count count + 3:
12 value =
count:
13 printf ("$d", value)
14 return 0:
15
16

Logs & others

Code::Blocks x9 Search results x Cccc xBuildlog Build messages CppCheck xCppCheck messages xACscope x Debugger

x
Management add.cX
main.c x

Projects Symbols
#include <stdio.h>
OWorkspace 2 #include <stdlib.h>
Smy-projectt 3
e Sources

-Oadd.c
main.c
4
5
6
8
{Wextoern
int main
int count:
()

int value:
value =
increment () :: 3
C\UsersljaspnDocuments\my-project1\bin\Debugimy-project1.exe

Process returned e (exe) execution time : e.431 s


9 value - increment ()
10 Press any key to continue.
value increment()
11 count count 3:
12 valueCount:
13 printf ("%d", value)
14 return 0;
15
16

Logs & others

Code: :Blocksx9Searchresults x Cccc x Build le

C:\Users\jaspr\Documents\my-projectl \main.c: In fun


C:\Users\jaspr\Documents \my-projectI \main.c:8:5: wa
value = increment () #

mingw32-g++.exe -o bin\Debug \my-projectl.exe obj\D


Output file is bin\Debug\my-projectl.exe with size
Process termina ted wi th status 0 (0 minute (s) 1 se ,
O rror (s), 1 Marning (s) (0 minute (s), 1 s0cond (3) )
int incremehf ()

static int count


count = count + li
return count
Management
main.c x add.cx
Projects Symbols
#include <stdio.h>
OWorkspace 2 #include <stdlib.h>
Smy-project1 3
Sources

-Dadd.c
main.c
4
5
6
7
8
{wextern int counts
int main ()

int value:
value = increment ()
3
D c\Usersljasp\ Documents\my project1\bin\Debug\my project1.exe

Process returnede (exe)execution time : e.368 s


Press y key to continue.
value increment()
10 value increment()#
11 count count31
12 valueCount:
13 printf("%d", value)
14 return 0;
15
16

Logs & others

Code::Blocks x Search results xCcoc xBuil


---Build:Debug in my-projecti (compi
mingw32-gcc.exe -Wall -g -stdmc99 -c C:\Users\ja
mingw32-g++.exe -o bin\ Debug \my-projectl.exe obj
Output file is bin\ Debug\my-projecti.exe with siz
Process termina ted with status 0
(O minute (s) 1 ,
O error (s), 0 ra rning (s) (0 minute (s) 1 second , (
int increment 0
int var - 3:
static int count = var:
count count + l:
return count
TAKEAWAYS

1. Static variable remains in memoryeven if itis


declared within a block on the other hand
automatic variable is destroyed after the
completion offunction in was declared.
which it

2. Static variable if declared outside the scope of any

function act like globalvariable but onlywithin


will
the file in which it is declared.
3. Youcan only assign aconstant literal (orvalue) to
astatic variable.

NESO ACADEMY

You might also like