how to use printf on the command line?












0















I wanted to try using a simple printf on terminal to see if i can directly program on it , but didn't work; I wrote on it these 2 lines:



~$ #include<stdio.h>
~$ printf("E");


and it says



bash: syntax error near unexpected token '"E"'



I don't see anything wrong...?










share|improve this question



























    0















    I wanted to try using a simple printf on terminal to see if i can directly program on it , but didn't work; I wrote on it these 2 lines:



    ~$ #include<stdio.h>
    ~$ printf("E");


    and it says



    bash: syntax error near unexpected token '"E"'



    I don't see anything wrong...?










    share|improve this question

























      0












      0








      0








      I wanted to try using a simple printf on terminal to see if i can directly program on it , but didn't work; I wrote on it these 2 lines:



      ~$ #include<stdio.h>
      ~$ printf("E");


      and it says



      bash: syntax error near unexpected token '"E"'



      I don't see anything wrong...?










      share|improve this question














      I wanted to try using a simple printf on terminal to see if i can directly program on it , but didn't work; I wrote on it these 2 lines:



      ~$ #include<stdio.h>
      ~$ printf("E");


      and it says



      bash: syntax error near unexpected token '"E"'



      I don't see anything wrong...?







      command-line






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Mar 9 at 10:53









      wattbattwattbatt

      132




      132






















          1 Answer
          1






          active

          oldest

          votes


















          3














          Your shell isn't a C interpreter - it has its own syntax, and its own printf function, which aside from inheriting many of the format specifiers from the C function is quite separate



          Valid forms are



          printf 'En'


          or



          printf '%cn' E


          There is no equivalent of the #include directive (which would just be an - ignored - comment as far as the shell is concerned)





          If you want to actually write a small C program from the command line, you can do that with cat using a here document:



          $ cat > main.c
          #include <stdio.h>

          int main(void) {
          printf("Hello world!n");
          return 0;
          }


          Terminate your input py pressing Ctrl+D. Then you can compile the program using gcc:



          gcc -o my_first_prog main.c


          and finally run it from the shell



          $ ./my_first_prog 
          Hello world!





          share|improve this answer


























          • this is quite silly then, my professor was teaching about the "main" function arguments and wrote on the blackboard a C program with main taking 2 ints from the terminal...saying it could all be written on the shell but i guess it's a lie then

            – wattbatt
            Mar 9 at 11:11






          • 1





            @wattbatt perhaps a misunderstanding: you can certainly write a C program from the command line - however you can't expect the Bash shell to interpret and run it.

            – steeldriver
            Mar 9 at 11:16











          • @wattbatt I have added what I imagine your professor had in mind

            – steeldriver
            Mar 9 at 11:26






          • 1





            @wattbatt "but i guess it's a lie then" – apparently you're a newbie with shell and C programming, that's fine, everyone starts as a newbie. But please stop for a moment and think about the probability of your professor knowing something fundamental incorrectly or even telling straight lies, versus you misunderstanding something.

            – egmont
            Mar 9 at 11:38











          Your Answer








          StackExchange.ready(function() {
          var channelOptions = {
          tags: "".split(" "),
          id: "89"
          };
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function() {
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled) {
          StackExchange.using("snippets", function() {
          createEditor();
          });
          }
          else {
          createEditor();
          }
          });

          function createEditor() {
          StackExchange.prepareEditor({
          heartbeatType: 'answer',
          autoActivateHeartbeat: false,
          convertImagesToLinks: true,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          bindNavPrevention: true,
          postfix: "",
          imageUploader: {
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          },
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          });


          }
          });














          draft saved

          draft discarded


















          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2faskubuntu.com%2fquestions%2f1124273%2fhow-to-use-printf-on-the-command-line%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown

























          1 Answer
          1






          active

          oldest

          votes








          1 Answer
          1






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes









          3














          Your shell isn't a C interpreter - it has its own syntax, and its own printf function, which aside from inheriting many of the format specifiers from the C function is quite separate



          Valid forms are



          printf 'En'


          or



          printf '%cn' E


          There is no equivalent of the #include directive (which would just be an - ignored - comment as far as the shell is concerned)





          If you want to actually write a small C program from the command line, you can do that with cat using a here document:



          $ cat > main.c
          #include <stdio.h>

          int main(void) {
          printf("Hello world!n");
          return 0;
          }


          Terminate your input py pressing Ctrl+D. Then you can compile the program using gcc:



          gcc -o my_first_prog main.c


          and finally run it from the shell



          $ ./my_first_prog 
          Hello world!





          share|improve this answer


























          • this is quite silly then, my professor was teaching about the "main" function arguments and wrote on the blackboard a C program with main taking 2 ints from the terminal...saying it could all be written on the shell but i guess it's a lie then

            – wattbatt
            Mar 9 at 11:11






          • 1





            @wattbatt perhaps a misunderstanding: you can certainly write a C program from the command line - however you can't expect the Bash shell to interpret and run it.

            – steeldriver
            Mar 9 at 11:16











          • @wattbatt I have added what I imagine your professor had in mind

            – steeldriver
            Mar 9 at 11:26






          • 1





            @wattbatt "but i guess it's a lie then" – apparently you're a newbie with shell and C programming, that's fine, everyone starts as a newbie. But please stop for a moment and think about the probability of your professor knowing something fundamental incorrectly or even telling straight lies, versus you misunderstanding something.

            – egmont
            Mar 9 at 11:38
















          3














          Your shell isn't a C interpreter - it has its own syntax, and its own printf function, which aside from inheriting many of the format specifiers from the C function is quite separate



          Valid forms are



          printf 'En'


          or



          printf '%cn' E


          There is no equivalent of the #include directive (which would just be an - ignored - comment as far as the shell is concerned)





          If you want to actually write a small C program from the command line, you can do that with cat using a here document:



          $ cat > main.c
          #include <stdio.h>

          int main(void) {
          printf("Hello world!n");
          return 0;
          }


          Terminate your input py pressing Ctrl+D. Then you can compile the program using gcc:



          gcc -o my_first_prog main.c


          and finally run it from the shell



          $ ./my_first_prog 
          Hello world!





          share|improve this answer


























          • this is quite silly then, my professor was teaching about the "main" function arguments and wrote on the blackboard a C program with main taking 2 ints from the terminal...saying it could all be written on the shell but i guess it's a lie then

            – wattbatt
            Mar 9 at 11:11






          • 1





            @wattbatt perhaps a misunderstanding: you can certainly write a C program from the command line - however you can't expect the Bash shell to interpret and run it.

            – steeldriver
            Mar 9 at 11:16











          • @wattbatt I have added what I imagine your professor had in mind

            – steeldriver
            Mar 9 at 11:26






          • 1





            @wattbatt "but i guess it's a lie then" – apparently you're a newbie with shell and C programming, that's fine, everyone starts as a newbie. But please stop for a moment and think about the probability of your professor knowing something fundamental incorrectly or even telling straight lies, versus you misunderstanding something.

            – egmont
            Mar 9 at 11:38














          3












          3








          3







          Your shell isn't a C interpreter - it has its own syntax, and its own printf function, which aside from inheriting many of the format specifiers from the C function is quite separate



          Valid forms are



          printf 'En'


          or



          printf '%cn' E


          There is no equivalent of the #include directive (which would just be an - ignored - comment as far as the shell is concerned)





          If you want to actually write a small C program from the command line, you can do that with cat using a here document:



          $ cat > main.c
          #include <stdio.h>

          int main(void) {
          printf("Hello world!n");
          return 0;
          }


          Terminate your input py pressing Ctrl+D. Then you can compile the program using gcc:



          gcc -o my_first_prog main.c


          and finally run it from the shell



          $ ./my_first_prog 
          Hello world!





          share|improve this answer















          Your shell isn't a C interpreter - it has its own syntax, and its own printf function, which aside from inheriting many of the format specifiers from the C function is quite separate



          Valid forms are



          printf 'En'


          or



          printf '%cn' E


          There is no equivalent of the #include directive (which would just be an - ignored - comment as far as the shell is concerned)





          If you want to actually write a small C program from the command line, you can do that with cat using a here document:



          $ cat > main.c
          #include <stdio.h>

          int main(void) {
          printf("Hello world!n");
          return 0;
          }


          Terminate your input py pressing Ctrl+D. Then you can compile the program using gcc:



          gcc -o my_first_prog main.c


          and finally run it from the shell



          $ ./my_first_prog 
          Hello world!






          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Mar 9 at 11:25

























          answered Mar 9 at 11:04









          steeldriversteeldriver

          68.9k11113184




          68.9k11113184













          • this is quite silly then, my professor was teaching about the "main" function arguments and wrote on the blackboard a C program with main taking 2 ints from the terminal...saying it could all be written on the shell but i guess it's a lie then

            – wattbatt
            Mar 9 at 11:11






          • 1





            @wattbatt perhaps a misunderstanding: you can certainly write a C program from the command line - however you can't expect the Bash shell to interpret and run it.

            – steeldriver
            Mar 9 at 11:16











          • @wattbatt I have added what I imagine your professor had in mind

            – steeldriver
            Mar 9 at 11:26






          • 1





            @wattbatt "but i guess it's a lie then" – apparently you're a newbie with shell and C programming, that's fine, everyone starts as a newbie. But please stop for a moment and think about the probability of your professor knowing something fundamental incorrectly or even telling straight lies, versus you misunderstanding something.

            – egmont
            Mar 9 at 11:38



















          • this is quite silly then, my professor was teaching about the "main" function arguments and wrote on the blackboard a C program with main taking 2 ints from the terminal...saying it could all be written on the shell but i guess it's a lie then

            – wattbatt
            Mar 9 at 11:11






          • 1





            @wattbatt perhaps a misunderstanding: you can certainly write a C program from the command line - however you can't expect the Bash shell to interpret and run it.

            – steeldriver
            Mar 9 at 11:16











          • @wattbatt I have added what I imagine your professor had in mind

            – steeldriver
            Mar 9 at 11:26






          • 1





            @wattbatt "but i guess it's a lie then" – apparently you're a newbie with shell and C programming, that's fine, everyone starts as a newbie. But please stop for a moment and think about the probability of your professor knowing something fundamental incorrectly or even telling straight lies, versus you misunderstanding something.

            – egmont
            Mar 9 at 11:38

















          this is quite silly then, my professor was teaching about the "main" function arguments and wrote on the blackboard a C program with main taking 2 ints from the terminal...saying it could all be written on the shell but i guess it's a lie then

          – wattbatt
          Mar 9 at 11:11





          this is quite silly then, my professor was teaching about the "main" function arguments and wrote on the blackboard a C program with main taking 2 ints from the terminal...saying it could all be written on the shell but i guess it's a lie then

          – wattbatt
          Mar 9 at 11:11




          1




          1





          @wattbatt perhaps a misunderstanding: you can certainly write a C program from the command line - however you can't expect the Bash shell to interpret and run it.

          – steeldriver
          Mar 9 at 11:16





          @wattbatt perhaps a misunderstanding: you can certainly write a C program from the command line - however you can't expect the Bash shell to interpret and run it.

          – steeldriver
          Mar 9 at 11:16













          @wattbatt I have added what I imagine your professor had in mind

          – steeldriver
          Mar 9 at 11:26





          @wattbatt I have added what I imagine your professor had in mind

          – steeldriver
          Mar 9 at 11:26




          1




          1





          @wattbatt "but i guess it's a lie then" – apparently you're a newbie with shell and C programming, that's fine, everyone starts as a newbie. But please stop for a moment and think about the probability of your professor knowing something fundamental incorrectly or even telling straight lies, versus you misunderstanding something.

          – egmont
          Mar 9 at 11:38





          @wattbatt "but i guess it's a lie then" – apparently you're a newbie with shell and C programming, that's fine, everyone starts as a newbie. But please stop for a moment and think about the probability of your professor knowing something fundamental incorrectly or even telling straight lies, versus you misunderstanding something.

          – egmont
          Mar 9 at 11:38


















          draft saved

          draft discarded




















































          Thanks for contributing an answer to Ask Ubuntu!


          • Please be sure to answer the question. Provide details and share your research!

          But avoid



          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.


          To learn more, see our tips on writing great answers.




          draft saved


          draft discarded














          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2faskubuntu.com%2fquestions%2f1124273%2fhow-to-use-printf-on-the-command-line%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          Popular posts from this blog

          數位音樂下載

          When can things happen in Etherscan, such as the picture below?

          格利澤436b