printf

printf is a part of code segment but found in data segment.....why is it so ?

Sorry, I don't understand your question. What do you mean by "data segment"?
there are four part of a program

1-> code segment [1+]

2-> data segment [0+]

3-> stack segment [0+]

4-> extra segment [0+]

printf is a library function so its a part of code segment and found in data segment....why...?
Last edited on
can you provide us related links..
I don't have any link related with this

I read a book in which this is given bt reason is not described ....... so plz elaborate it



the name of the BOOK is "A BOOK ON C" & "HOW TO PROGRAM DIETTEL & DIETTEL"
they didn't gave enough reason for this....

@MikeBoy
@Kulkarnisr
The code segment, data segment, stack segment, and extra segment are registers of the Intel 8086/8088 microprocessor in the original IBM PC (about 1983).
See:
http://en.wikipedia.org/wiki/Intel_8086

Each of these registers was 16-bits, and combined with another register to give 20-bit address space.

These are often referred to as CS, DS, SS, ES.

The CS register would point to the beginning of the code to execute, and when combined with the Instruction Pointer to the exact instruction executing. So CS:IP is what is executing. Likewise the stack would be at SS:SP.

DS and ES would point at data and be used in a similar manner.

The Intel 8086 gave rise to the Intel 80286, Intel 80386, Intel 80486, Pentium etc. So they are still with us today (I think).

So the code for printf and all other code will be in the code segment (CS) of the executable file. The formatting string for printf will be in the data segment (DS) of the executable file.
@ShodanHo
thanx.....sir
I am not getting u completely.....

u mean to say
for example
char* x="hello sir ji";
printf(x);

x is found in data segment and printf is found in code segment.....
is that so....?

x is a data type dats why it is found in data segment
bt I am not getting why printf is found in data segment being a part of code segment ......?


Last edited on
Hi,
Yes, x would be in the data segment, and printf in the code segment.

So the assembler for:

1
2
3
4
5
int main()
{
  printf("Hello World!\n");
  return 0;
}


would (depending on the operating system and memory model chosen) look something like:

.386
.model small,c  
.stack 100h
 
.data
msg     db "Hello World!",10,0
 
.code
includelib MSVCRT
extrn printf:near
extrn exit:near
 
public main
main proc
        push    offset msg
        call    printf
        push    0
        call    exit
main endp
 
end main

Ref:
http://en.wikipedia.org/wiki/X86_assembly_language

The .code and .data segments can be seen in the executable file, and will be loaded into separate areas of memory.

I not sure if this is really answering your question, so can you supply some more information about why you say printf is found in the data segment.
Topic archived. No new replies allowed.