• Tidak ada hasil yang ditemukan

What is the difference between declaration and definition of a variable/function

N/A
N/A
Protected

Academic year: 2023

Membagikan "What is the difference between declaration and definition of a variable/function"

Copied!
3
0
0

Teks penuh

(1)

Introduction to programming language C Some sorts of questions

1. What is the difference between declaration and definition of a variable/function?

Solution: Declaration of a variable/function simply declares that the variable/function exists somewhere in the program but the memory is not allocated for them. When a variable is declared, the program knows the data type of that variable. In case of functions, their data types, the order of arguments and the return type. When we define a variable /function, apart from the role of declaration, it also allocates memory for that variable/functions. Declaration can be any number of times but definition only one time.

extern int I; //only declaration int x; //both definition and declaration 2. What are different storage class specifies in C?

auto,register,static,extern.

3. When should we use pointers in a C program?

a. To get address of a variable.

b. For achieving pass by reference in c.

c. To pass large structures.

d. To implement linked data structures.

4. What is a NULL pointer?

It is a pointer that doesn’t point to a valid location. At the time of declaration we don’t know the value. So it can initialize as NULL.

5. What is a dangling pointer?

It is a pointer that doesn’t point to a valid memory. Dangling pointer arise when an object is deleted or de-allocated, without modifying the value of the pointer, so that the pointer still points to the memory location of the de-allocated memory.

int *ptr=(int *)malloc(sizeof(int));

free(ptr);

*ptr=10; //it’s a dangling reference 6. What is memory leak?

Memory leak occurs when programmers create a memory in heap and forget to delete it.

void func(){

int *ptr=(int *)malloc(sizeof(int));

return;

}

7. What are local static variables? What is their use?

A local static variable is a variable whose lifetime doesn’t end with a function call where it is declared. It extends for the lifetime of complete program. All calls to the function share the same copy of local static variables. It is used to count the number of function call. It’s default value is 0.

(2)

#include<stdio.h>

void func(){

static int x;

printf(“%d”,x);

x=x+1;

}

Int main(){

func();

func();

}

Output::01 8. Static function

It has a scope that is limited to its object file. It Is only visible in its object file. It is restricted to access.

First.c

static void st(void){

printf(“this”);

}

Second.c int main(){

st();

}

This will cause an error.

9. Difference between ++*p,*p++,*++p.

Associativity right to left and need precedence.

++*p=++(*p)

*p++=*(p++)

*++p=*(++p)

10. What is l-value and r-value?

l-value or location value refers to an expression that can be used on left side of assignment operator.

A=3;

l-value=A r-value=3

l-value can be modifiable or non-modifiable(constant).

11. What is a pointer on pointer?

It’s a pointer variable which can hold the address of another pointer variable.

int x=5, *p=&x,**q=&p;

12. Distinguish between malloc() and calloc().

Both allocates memory from heap area/dynamic memory. By default, calloc() fills the allocated memory with 0’s.

(3)

13. What is keyword auto for?

By default, every local variable is automatic(auto).

auto int I;

int j;

//both are auto

14. extern is used to resolve the global scope.

int main(){

extern int I;

printf(“this”);

} int i=20;

15. if a variable is used most frequently then use register for using CPU register.

register int i=10;

16. Purposes of typedef.

Alias the existing type. Simplify the complex declaration type.

17. Can a program be compiled without main() function?

Yes, compiled but not execute.

18. %i all supported format can input.

19. “\” means escape.

20. FILE is not built-in-data type, it is structure in stdio 21. Type casting is conversion of one type to another.

int x=10;

float y=(float)x;

22. <> is used to go to built in include path for searching

“ “ search in current folder then built in include path.

23. Printf(),scanf() return integer how many character it successfully print.

Referensi

Dokumen terkait

1) Reading the novel more than once. 2) Exploring the main data, relating to the object that will be analyzed. 3) Marking the important part of the novel. 4) Taking note on the

The objectives of this study are to know the differences of students learning outcome and memory retention between mind maps and notes write and stacking (NWS)

The object of the study is Novel The Scarlet Letter written by Nathaniel Hawthorne and the movie version of The Scarlet Letter. directed by

The object of research used in this study is the integrity of financial statements, good corporate governance, financial distress, and audit quality in consumer goods sector companies

Additionally, we conclude that high luminance in a small area of the display, like the test stimulus with a 10° viewing angle, tends to be Figure 1.Summary of the psychophysical

Objectives Regarding the fact that the Quran is the guidance book for human beings, which leads them to the peak of perfec- tion, writers try to investigate the importance of correct

INSTITUTION OF RESEARCH AND COMMUNITY SERVICE UM JEMBER 16 A DIFFERENCE BETWEEN THE EFFECT OF TRANSFEMORAL PROSTHESIS USE AND AXILLARY CRUTCHES TOWARD THE QUALITY OF LIFE Nur

Media Exposure moderates the positive relationship between Green Accounting and CSR Discloruse It is important for the company to present a report on the company's activities,