user983223
user983223

Reputation: 1154

run c program - stdio.h where do i get it?

Looking into learning C. As I understand it when I say #include <stdio.h> it grabs stdio.h from the default location...usually a directory inside your working directory called include. How do I actually get the file stdio.h? Do I need to download a bunch of .h files and move them from project to project inside the include directory? I did the following in a test.c file. I then ran make test and it outputted a binary. When I ran ./test I did not see hello print onto my screen. I thought I wasn't seeing output maybe because it doesn't find the stdio.h library. But then again if I remove the greater than or less than signs in stdio the compiler gives me an error. Any ideas?

I'm on a Mac running this from the command line. I am using: GNU Make 3.81. This program built for i386-apple-darwin10.0

#include <stdio.h>

main()
{
  printf("hello");
}

Edit: I have updated my code to include a datatype for the main function and to return 0. I still get the same result...compiles without error and when I run the file ./test it doesn't print anything on screen.

#include <stdio.h>

int main()
{
  printf("hello");
  return 0;
}

Update: If I add a \n inside of the printf it works! so this will work:

#include <stdio.h>

    int main()
    {
      printf("hello\n");
      return 0;
    }

Upvotes: 4

Views: 10731

Answers (4)

MountainMan
MountainMan

Reputation: 785

Just going to leave this here : STILL! in 2018, December... Linux Mint 18.3 has no support for C development.

innocent / # cc ThoseSorts.c

ThoseSorts.c:1:19: fatal error: stdio.h: No such file or directory compilation terminated.

innocent / # gcc ThoseSorts.c

ThoseSorts.c:1:19: fatal error: stdio.h: No such file or directory compilation terminated.

innocent / # apt show libc6

(Abbreviated)::
Package: libc6
Version: 2.23-0ubuntu10
Priority: required
Section: libs
Source: glibc
Origin: Ubuntu
Installed-Size: 11.2 MB
Depends: libgcc1
Homepage: http://www.gnu.org/software/libc/libc.html
Description: GNU C Library: Shared libraries
Contains the standard libraries that are used by nearly all programs on
the system. This package includes shared versions of the standard C library
and the standard math library, as well as many others.

innocent / # apt-get install libc6-dev libc-dev
So, magic... and a minute later they are all installed on the computer and then things work as they should.

Not all distros bundle up all the C support libs in each ISO. Hunh.

hardlyinnocent / # gcc ThoseSorts.c

hardlyinnocent / # ./a.out

20 18 17 16 ... ... ...

Upvotes: 0

alexis
alexis

Reputation: 50200

#include <header.h>, with angle brackets, searches in standard system locations, known to the compiler-- not in your project's subdirectories. In Unix systems (including your Mac, I believe), stdio.h is typically in /usr/include. If you use #include "header.h", you're searching subdirectories first and then the same places as with <header.h>.

But you don't need to find or copy the header to run your program. It is read at compilation time, so your ./test doesn't need it at all. Your program looks like it should have worked. Is it possible that you just typed "test", not "./test", and got the system command "test"? (Suggestion: Don't name your programs "test".)

Upvotes: 0

Your code should have preferably

 printf("hello\n");

or

 puts("hello");

If you want to know where does the standard header file <stdio.h> comes from, you could run your compiler with appropriate flags. If it is gcc, try compiling with

gcc -H -v -Wall hello.c -o hello

Pedantically, a standard header file is even not required to exist as a file; the standard permits an implementation which would process the #include <stdio.h> without accessing the file system (but e.g. by retrieving internal resources inside the compiler, or from a database...). Few compilers behave that way, most really access something in the file system.

Upvotes: 1

Luchian Grigore
Luchian Grigore

Reputation: 258618

If you didn't have the file, you'd get a compilation error.

My guess is the text was printed, but the console closed before you got the chance to see it.

Also, main returns an int, and you should return 0; to signal successful completion.

Upvotes: 1

Related Questions