Learning to use GCC - c

I am a student of computer science and for reasons beyond my comprehension i have only been taught to code in the TURBO compiler.
But now i have realized that i should learn to code in GCC. The problem is that i dont know any of the header files and therefore i am not able to use any of the built in functions.
All the GCC tutorials i have seen are for beginners. I am at more of an intermediate state of learning and hence if any of you know of a book or a website where i can learn the details about the header files then it would be helpful.
NOTE: just to be clear-i am a linux user.

Conio.h is not the standard header file it is available only in turbo c compiler
Check out these links
http://www.gnu.org/s/hello/manual/libc/index.html
http://www.di-mgt.com.au/src/CStdLib.html
and the previous stackoverflow question List of standard header files in C and C++
http://gcc.gnu.org/onlinedocs/gcc/
http://linux.die.net/man/1/gcc
http://www.cplusplus.com/reference/clibrary/
http://en.wikipedia.org/wiki/C_standard_library
http://publications.gbdirect.co.uk/c_book/
http://www.utas.edu.au/infosys/info/documentation/C/CStdLib.html

Most of the header files used by gcc are either C C++ or POSIX compliant. Therefore you need to learn the language. printf is in stdio.h in every C compiler that is standards compliant.
There are specific linux ones but you probably dont care about those. If you ware using a linux system the 'man' command is your friend - but it tells you the header file.
man 3 printf
produces
NAME
printf, fprintf, sprintf, snprintf, vprintf, vfprintf, vsprintf, vsnprintf - formatted output conversion
SYNOPSIS
#include <stdio.h>
....

Like if you want to use printf() (for eg..), just type man printf (man stands for manual) in your terminal if Linux or in google and you'l get all the thing you need to use printf:
Synopsis
#include <stdio.h>
int printf (const char *format, ...);
int fprintf (FILE *stream, const char *format, ...);
int sprintf (char *str, const char *format, ...);
int snprintf (char *str, size_t size, const char *format, ...);
#include <stdarg.h>
int vprintf (const char *format, va_list ap);
int vfprintf (FILE *stream, const char *format, va_list ap);
int vsprintf (char *str, const char *format, va_list ap);
int vsnprintf (char *str, size_t size, const char *format, va_list ap);
Then to use printf(), you see that you need to include stdio.h:
#include <stdio.h>
Normally you can compile without including it but it gives you warning..
Moreover use <> to include standart header files and "" to include your headers like: #include "myHeaderinMyLocalFolder.h"

If TURBO-C (which I guess you meant) is anything like TURBO-PASCAL, with it's library of console functions, there is nothing like that in standard C. And standard C is what is usedby gcc, so if you know how to use the functions in e.g. stdio.h then there is not much difference.

Related

Is including stdarg.h necessary for "..." argument in header files?

#ifndef WHATEVER_H
#define WHATEVER_H
void test(const char *format, ...); // would you have to #include <stdarg.h> for ... on argument, or is it ok if you don't use it
#endif // WHATEVER_H
So if I were to have a header file like this, where I needed to put ... as my argument for my void test function, would I have to include stdarg.h for the ... argument, or is it not mandatory?
The header does not need to include <stdarg.h> if the prototypes only include the ellipsis (, ...) notation. The code implementing the test() function will need to include <stdarg.h>, but the header declaring it need not.
However, you should often consider creating a second function, void vtest(const char *format, va_list args); to match the test() function in the header, and then you do need <stdarg.h> to define the va_list type (and the implementation code no longer needs a separate #include <stdarg.h>). With the vtest() declaration in the header, the implementation of the test() function becomes trivial:
void test(const char *format, ...)
{
va_list args;
va_start(args, format);
vtest(format, args);
va_end(args);
}
This is particularly simple since there is no return value to relay, but returning a value is not very much harder. It's often a good idea to implement a variadic function like test() using this scheme, even if you don't expose the vtest() function — it is rather likely that you'll want the extra flexibility it provides eventually.
No you do not need to, but it will rather hard to access parameters hidden behind the ... without macros from this header.
you will need to know your implementation for example for gcc you can use:
__builtin_va_start(v,l)
__builtin_va_end(v)
__builtin_va_arg(v,l)
__builtin_va_copy(d,s)

Include only a particular definition from header file

I have used strlen() function from the string.h library without including the header which I want to include from the header file which is initially implemented, because I am writing my own implementation of strcpy(),if I include the header it says it's multiple definitions of strcpy ().
So how do I include only a particular definition from the header file.
Do I need to use extern keyword?
#include <stdio.h>
#include <stdlib.h>
#include "exercise 5.5.h"
int main() {
char *s = "hello";
char *t = "helli";
int n = 3;
if (n > strlen(t))
printf("\nsorry the value of n is greater than the size of t");
else {
S = strncpy(s, t, n);
printf("\nther is %d", x);
}
}
The header has definition of strncpy
Terminal trace
exercise. 5.5_main.c:10:7: incompatible implicit declaration of built-in function "strien
exercise 5.5 main.c:10:7: note: include <string.h> or provide a declaration of 'strlen
I don't want to include string.h but how do I explicitly provide definition of strlen
Header
char* strncat(char *s, char *t, int n);
char* strncpy(char *s, char *t, int n);
int strncmp(char *s,char *t, int n);
Reimplementing a standard library function like strcpy can be tricky. Since it's a standard library function, its name is in a sense "reserved" -- you're not supposed to use it yourself. (It's not quite as strongly reserved as is a keyword like switch, but it's still generally a bad idea to try to write a function named strcpy -- not to mention the fact that it's usually perfectly unnecessary!)
In answer to your explicit question, no, there's no way to "selectively include" just your own selection of the declarations in a system header file such as <string.h>.
If for some reason you need to write your own version of strcpy, you have several choices, depending on circumstances.
Rename your own function. For example, call it my_strcpy. (This is the usual approach.)
Make sure the definition of your function is perfectly correct, and matches the declaration in the standard header file exactly. For example, if you have strcpy(char *dst, char *src) {...} or char *strcpy(char *dst, char *src) {...}, those are both wrong -- it needs to be char *strcpy(char *dst, const char *src) {...}.
Don't use the standard strlen function, either, meaning that you don't have to do a #include <string.h> at all. If you need it, write your own version of strlen, too. (This is often the requirement if the reason you're writing your own strcpy is as a teaching exercise: often the assignment says "You may not use any other functions from the standard library.")
Instead of doing a #include <string.h> because you're calling strlen, provide your own prototype for it at the top of your file: extern size_t strlen(const char *);. (This is generally an extremely bad idea for several reasons, and is not a step to be taken except under extreme circumstances and when you know exactly what you're doing.)
It may also be significant to note whether the "redefinition" error you're getting is coming from the compiler or the linker. If it's a compile-time error such as "conflicting types for 'strcpy'", it indicates you probably need to pay attention to point 2 just above. But if it's a link-time error like "ld: duplicate symbol '_strcpy'" there may not be anything you can do about it, and you'll have to fall back on point 1.

How does printf locate and replace %s 's? [duplicate]

This question already has answers here:
Closed 12 years ago.
Possible Duplicate:
source code of c/c++ functions
I was wondering where I can find the C code that's used so that when I write printf("Hello World!"); in my C programm to know that it has to print that string to STDOUT. I looked in <stdio.h>, but there I could only find its prototype int printf(const char *format, ...), but not how it looks like internally.
Here's the GNU version of printf... you can see it passing in stdout to vfprintf:
__printf (const char *format, ...)
{
va_list arg;
int done;
va_start (arg, format);
done = vfprintf (stdout, format, arg);
va_end (arg);
return done;
}
See here.
Here's a link to vfprintf... all the formatting 'magic' happens here.
The only thing that's truly 'different' about these functions is that they use varargs to get at arguments in a variable length argument list. Other than that, they're just traditional C. (This is in contrast to Pascal's printf equivalent, which is implemented with specific support in the compiler... at least it was back in the day.)

including of sprintf_s method in c [duplicate]

I have a C program that uses sprintf_s. It works fine in Windows, but when I compile my code in Linux it gives this error:
sprintf_s was not declared in this scope.
Why does this happen and how can I fix it?
It's not standard, you won't find such function on Linux.
Standard function snprintf should have a similar semantics.
sprintf_s is not part of the standard C library, so it is not portable, thus you are not able to use it in Linux.
BUT you have snprintf, which is very similar and it should help you to accomplish what you want.
sprintf_s is only part of Annex K, an optional Annex to the C11 standard:
Annex K
...
K.2 Scope
This annex specifies a series of optional extensions that can be useful in the mitigation of
security vulnerabilities in programs, and comprise new functions, macros, and types
declared or defined in existing standard headers.
...
K.3.5.3.6 The sprintf_s function
Synopsis
#define __STDC_WANT_LIB_EXT1__1
#include <stdio.h>
int sprintf_s(char * restrict s, rsize_t n,
const char * restrict format, ...);
(emphasis added)
It never made it into POSIX (or Linux) (and is not missed at all, there are even arguments about its usefulness in the committee).
For better portability, use snprintf which is part of the core standard and provides all the functionality you'll need.
sprintf_s is not part of the standard C library, and you won't be able to use it in Linux.
However, snprintf is standard and should do the same task.
During a porting of my program from Windows to Linux, I wrote following implementation in my own windows.h:
inline int sprintf_s(char* buffer, size_t sizeOfBuffer, const char* format, ...)
{
va_list ap;
va_start(ap, format);
int result = vsnprintf(buffer, sizeOfBuffer, format, ap);
va_end(ap);
return result;
}
template<size_t sizeOfBuffer>
inline int sprintf_s(char (&buffer)[sizeOfBuffer], const char* format, ...)
{
va_list ap;
va_start(ap, format);
int result = vsnprintf(buffer, sizeOfBuffer, format, ap);
va_end(ap);
return result;
}
snprintf is insecure, only sprintf_s is secure.
snprintf does not guarantee to add a final \0, leading to possible subsequent overflows.
look at https://github.com/rurban/safeclib for a proper implementation.

Code for printf function in C [duplicate]

This question already has answers here:
Closed 12 years ago.
Possible Duplicate:
source code of c/c++ functions
I was wondering where I can find the C code that's used so that when I write printf("Hello World!"); in my C programm to know that it has to print that string to STDOUT. I looked in <stdio.h>, but there I could only find its prototype int printf(const char *format, ...), but not how it looks like internally.
Here's the GNU version of printf... you can see it passing in stdout to vfprintf:
__printf (const char *format, ...)
{
va_list arg;
int done;
va_start (arg, format);
done = vfprintf (stdout, format, arg);
va_end (arg);
return done;
}
See here.
Here's a link to vfprintf... all the formatting 'magic' happens here.
The only thing that's truly 'different' about these functions is that they use varargs to get at arguments in a variable length argument list. Other than that, they're just traditional C. (This is in contrast to Pascal's printf equivalent, which is implemented with specific support in the compiler... at least it was back in the day.)

Resources