How printf() function in c actually works? [closed] - c

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 4 years ago.
Improve this question
We all know that printf() is a function of <stdio.h>.
I want to know that what's happening under the hood when we type printf("something"); and it gets displayed on the screen... How come it's displayed on the screen and what is the code inside printf() function by which it displays something on the screen?thanks in advance

printf("something"); is the equivalent of fprintf(stdout, "something"); so it prints on the standard output, whatever where finally the output will be done (screen, file, pipe, ...)
because "something" doesn't content special formating (using %) if is printed unchanged, and probably your question moves to be how putchar() / fputc() works
printf doesn't know what a screen / file / pipe / ... is, in the same way scanf doesn't know what a keyboard is, this is not their responsibility

Related

C programming - Expressions? [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 5 years ago.
Improve this question
I am having trouble understanding the following C programming code. Why is this function important in my program? I am talking about the [A-Za-z.......]. Sorry if this is a stupid question. Thanks for your help.
int readResults(loebsdata2017 *results, FILE *fp){
return fscanf(fp, "%[^ ] \"%[A-Za-z \' -]\" %d %[A-Z] %[A-Z] %s %s ", results->loebsnavn, results->rytternavn,
&results->rytteralder, results->rytterhold, results->nationalitet,
results->rank, results->tid);
}
The second argument of an fscanf is the format of what is being scanned. This type of format is present in many similar functions like printf, scanf, and so on.
To know more about this format check the manual pages (man pages) in Linux by typing:
man fscanf
on the terminal. If in Windows, check their manual pages.
About the particular %[A-Za-z \' -] would mean to recognize anything from A to Z, a to z, blank space, ', or -.

Internals of ncurses: console input [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 6 years ago.
Improve this question
How exactly does Ncurses capture input into the console? I would like to implement it myself instead of using ncurses due to the overhead that ncurses is causing.
Thanks!
Very short and basically: It might use the TTY ioctl calls to get and set flags needed for the different modes. Then it could simply use read to read characters in a blocking or non-blocking manner.
Special keys (like the function keys for example) are read using multiple characters which are parsed.

How to read multiple lines in string until specified character in C [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 6 years ago.
Improve this question
Need to read everything until say *** comes up:
Input:
Hey there
how are
you
***
Output:
Hey there
how are
you
Would have used scanf("%[^***]s) but can't read all lines at once.
Only having basic C knowledge
The way I would do this is read one line at a time (with a funcion such as fgets instead of scanf) and then see if the line that you last read is equal to ***. You can use use strcmp to do that but you can also do it by hand if you are not allowed to use strcmp for some reason.

Interpreting a shellcode [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
I am calling a shellcode using buffer overflow to spawn a root shell. Can somebody explain what this shellcode exactly does? I have tried different shellcodes to spawn a root shell, but this was the only one which worked for me.
\x31\xdb\x89\xd8\xb0\x17\xcd\x80\x31\xdb
\x89\xd8\xb0\x2e\xcd\x80\x31\xc0\x50\x68
\x2f\x2f\x73\x68\x68\x2f\x62\x69\x6e\x89
\xe3\x50\x53\x89\xe1\x31\xd2\xb0\x0b\xcd
\x80
On first glance, the code appears to do setuid(0), then setgid(0), then call sys_execve() on some values (which include ASCII codes for "/bin//sh").
Looks like this is pure "payload" code, since I don't see anything to ensure the code is executed on the first place (buffer overflow, stack smashing, etc.).
(Thanks to #Hans Lub for the disassembler link)

Printf its variations and its prototype understanding [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
Can anybody explain the variations in printf syntax..i am confused in some of it
like
printf(5+"hello my friend");//i have problem this addition of 5
printf("hello""my""friend");//i have problem with double apostrophe
what kind of printf prototype do these follows ?
Is this have anything to do with dynamic linking?
Can anybody show some other weird printfs and explain them.
A string in C is accessed via a pointer to a char (see H2CO3's comment for a more precise definition). If you add 5 to a pointer to a char, you start the string 5 characters later. So 5+"hello my friend" points to " my friend", skipping "hello".
When a C compiler sees two strings with nothing (except possibly whitespace) in between, it treats them as a single string. This makes it easier to break long strings in multiple lines.
So "hello""my""friend" compiles into exactly the same thing as "hellomyfriend" or
"hello"
"my"
"friend"
None of these has anything to do with printf, and a lot to do with strings.

Resources