JJJJJJ
    • Create new note
    • Create a note from template
      • Sharing URL Link copied
      • /edit
      • View mode
        • Edit mode
        • View mode
        • Book mode
        • Slide mode
        Edit mode View mode Book mode Slide mode
      • Customize slides
      • Note Permission
      • Read
        • Only me
        • Signed-in users
        • Everyone
        Only me Signed-in users Everyone
      • Write
        • Only me
        • Signed-in users
        • Everyone
        Only me Signed-in users Everyone
      • Engagement control Commenting, Suggest edit, Emoji Reply
    • Invite by email
      Invitee

      This note has no invitees

    • Publish Note

      Share your work with the world Congratulations! 🎉 Your note is out in the world Publish Note

      Your note will be visible on your profile and discoverable by anyone.
      Your note is now live.
      This note is visible on your profile and discoverable online.
      Everyone on the web can find and read all notes of this public team.
      See published notes
      Unpublish note
      Please check the box to agree to the Community Guidelines.
      View profile
    • Commenting
      Permission
      Disabled Forbidden Owners Signed-in users Everyone
    • Enable
    • Permission
      • Forbidden
      • Owners
      • Signed-in users
      • Everyone
    • Suggest edit
      Permission
      Disabled Forbidden Owners Signed-in users Everyone
    • Enable
    • Permission
      • Forbidden
      • Owners
      • Signed-in users
    • Emoji Reply
    • Enable
    • Versions and GitHub Sync
    • Note settings
    • Note Insights New
    • Engagement control
    • Transfer ownership
    • Delete this note
    • Save as template
    • Insert from template
    • Import from
      • Dropbox
      • Google Drive
      • Gist
      • Clipboard
    • Export to
      • Dropbox
      • Google Drive
      • Gist
    • Download
      • Markdown
      • HTML
      • Raw HTML
Menu Note settings Note Insights Versions and GitHub Sync Sharing URL Create Help
Create Create new note Create a note from template
Menu
Options
Engagement control Transfer ownership Delete this note
Import from
Dropbox Google Drive Gist Clipboard
Export to
Dropbox Google Drive Gist
Download
Markdown HTML Raw HTML
Back
Sharing URL Link copied
/edit
View mode
  • Edit mode
  • View mode
  • Book mode
  • Slide mode
Edit mode View mode Book mode Slide mode
Customize slides
Note Permission
Read
Only me
  • Only me
  • Signed-in users
  • Everyone
Only me Signed-in users Everyone
Write
Only me
  • Only me
  • Signed-in users
  • Everyone
Only me Signed-in users Everyone
Engagement control Commenting, Suggest edit, Emoji Reply
  • Invite by email
    Invitee

    This note has no invitees

  • Publish Note

    Share your work with the world Congratulations! 🎉 Your note is out in the world Publish Note

    Your note will be visible on your profile and discoverable by anyone.
    Your note is now live.
    This note is visible on your profile and discoverable online.
    Everyone on the web can find and read all notes of this public team.
    See published notes
    Unpublish note
    Please check the box to agree to the Community Guidelines.
    View profile
    Engagement control
    Commenting
    Permission
    Disabled Forbidden Owners Signed-in users Everyone
    Enable
    Permission
    • Forbidden
    • Owners
    • Signed-in users
    • Everyone
    Suggest edit
    Permission
    Disabled Forbidden Owners Signed-in users Everyone
    Enable
    Permission
    • Forbidden
    • Owners
    • Signed-in users
    Emoji Reply
    Enable
    Import from Dropbox Google Drive Gist Clipboard
       Owned this note    Owned this note      
    Published Linked with GitHub
    • Any changes
      Be notified of any changes
    • Mention me
      Be notified of mention me
    • Unsubscribe
    # I/O ## 1 What is the output of the following program? ```c #include<stdio.h> int main() { int x; printf("%d",scanf("%d",&x)); /* Suppose that input value given for above scanf is 20 */ return 1; } ``` :::spoiler Answer Output: 1 Explanation: [scanf](https://cplusplus.com/reference/cstdio/scanf/) returns the no. of inputs it has successfully read. ::: ## 2 What is the output of the following program? ```c #include <stdio.h> int main() { printf("\new_c_question\by"); printf("\rgeeksforgeeks"); getchar(); return 0; } ``` :::spoiler Answer Output: geeksforgeeks - [Escape sequences in C](https://en.wikipedia.org/wiki/Escape_sequences_in_C) Explanation: First printf prints “ew_c_questioy”. Second printf has \\r in it so it goes back to start of the line and starts printing characters. ::: ## 3 What is the output of the following program? ```c #include <stdio.h> int main() { printf("\new_"); printf("c_question\by"); printf("\rgeeksforgeeks"); getchar(); return 0; } ``` :::spoiler Answer Output: geeksforgeeks 不同 printf 可以看成印出一長串字串,因此和上例結果相同。 ::: ## 4 以下程式是否有問題,如果沒有,是否有可以改進的地方? ```clike #include <stdio.h> int main(void) { char buff[10]; memset(buff, 0, sizeof(buff)); gets(buff); printf("\n The buffer entered is [%s]\n", buff); return 0; } ``` :::spoiler Answer 這裡的問題出在 gets() 函數的使用,這個函數會從 stdin 中讀取字串,但是卻不會檢查緩衝區的大小,這有可能會造成緩衝區溢位(buffer overflow)的問題,改用標準的 fgets() 函數會是個比較好的方式。 - [fgets() and gets() in C language](https://www.geeksforgeeks.org/fgets-gets-c-language/) ::: ## 5 ```clike #include <stdio.h> char* fun() { return "awake"; } int main() { printf("%s",fun()+ printf("I see you")); getchar(); return 0; } ``` :::spoiler Answer Output: I see you 加上從 "awake" 的地址 + 9 後的 memory location 開始 print 直到 `\0` 為止。 Explaination: 首先 "awake" 因為是 C 語言規格中定義 string literals 會被分配於 “static storage” 當中 (C99 [6.4.5]),因此可以被順利 return 給 `main()` 中的 `printf`。 再來,[prinft()](https://cplusplus.com/reference/cstdio/printf/) On success, the total number of characters written is returned. 所以 `printf("I see you")` 會 return 9 且印出 I see you。 至此,`printf("%s",fun()+ printf("I see you")); ` 會變成 `printf("%s","awake"+ 9);`,這段會變成 I see you 後面被輸出至 stdout 的內容。 ::: ## 6 Predict the output of below programs ```clike #include <stdio.h> int main() { int x, a = 0; x = sizeof(a++) ? printf("Geeks for Geeks\n") : 0; printf("Value of x:%d\n", x); printf("Value of a:%d", a); return 0; } ``` :::spoiler Answer Output: Geeks for Geeks Value of x:16 Value of a:0 Explanation: sizeof is a **compile-time operator**, so at the time of compilation sizeof and its operand get replaced by the result value. The operand is not evaluated (except when it is a **variable length array**) at all; only the type of the result matters. In sizeof operator, a++ will not evaluated. So it will remain same i.e. value of a will be 0. printf returns the number of width. Geeks for Geeks is of 16 width. This will return 16. So x is now 16. ::: ## 7 以下程式會輸出幾個 "-"? ```clike #include <stdio.h> #include <sys/wait.h> #include <unistd.h> int main(void) { int i; for(i=0; i<2; i++){ fork(); printf("-"); } wait(NULL); wait(NULL); return 0; } ``` :::spoiler Answer Output: -------- Explaination: 因為 `printf` 有 buffer。所以 `fork()` 出來的 child process 具有 parent process 的 `printf` buffer,裡面就已經有一個 "-" 了,所以第二輪 `fork()` 出來的兩個 child process 的 `printf` 都已經有一個 "-",再加上自己 call 一次 ` printf("-");`,就會輸出兩個。 可參考 [一個FORK的面試題](https://coolshell.cn/articles/7965.html),裡面有圖解。 若改成 ```clike #include <stdio.h> #include <sys/wait.h> #include <unistd.h> int main(void) { int i; for(i=0; i<2; i++){ fork(); printf("-"); fflush ( stdout ) ; } wait(NULL); wait(NULL); return 0; } ``` Output: ------ ::: ## 8 Predict the output of below program ```clike #include <stdio.h> int main() { while(1) { fprintf(stdout,"hello-std-out"); fprintf(stderr,"hello-std-err"); sleep(1); } return 0; } ``` :::spoiler Answer Output: hello-std-errhello-std-errhello-std-errhello-std-errhello-std-errhello-std-err ...... Explaination: stdout 和 stderr 是不是同裝置描述符。stdout是塊裝置,stderr則不是。對於塊裝置,只有當下面幾種情況下才會被輸入,1)遇到Enter,2)緩衝區滿,3)flush 被呼叫。而stderr則不會。 ::: ## 9 假設 stdin 為 Hello World,請問 stdout 為何? ```clike #include <stdio.h> int main() { char dummy[80]; printf("Enter a string:\n"); scanf("%[^r]",dummy); printf("%s\n",dummy); return 0; } ``` :::spoiler Answer Output: Hello Wo Explaination: 本例的輸出是“Hello, Wo”,scanf中的”%[^r]”是從中作梗的東西。意思是遇到字元r就結束了。 :::

    Import from clipboard

    Paste your markdown or webpage here...

    Advanced permission required

    Your current role can only read. Ask the system administrator to acquire write and comment permission.

    This team is disabled

    Sorry, this team is disabled. You can't edit this note.

    This note is locked

    Sorry, only owner can edit this note.

    Reach the limit

    Sorry, you've reached the max length this note can be.
    Please reduce the content or divide it to more notes, thank you!

    Import from Gist

    Import from Snippet

    or

    Export to Snippet

    Are you sure?

    Do you really want to delete this note?
    All users will lose their connection.

    Create a note from template

    Create a note from template

    Oops...
    This template has been removed or transferred.
    Upgrade
    All
    • All
    • Team
    No template.

    Create a template

    Upgrade

    Delete template

    Do you really want to delete this template?
    Turn this template into a regular note and keep its content, versions, and comments.

    This page need refresh

    You have an incompatible client version.
    Refresh to update.
    New version available!
    See releases notes here
    Refresh to enjoy new features.
    Your user state has changed.
    Refresh to load new user state.

    Sign in

    Forgot password

    or

    By clicking below, you agree to our terms of service.

    Sign in via Facebook Sign in via Twitter Sign in via GitHub Sign in via Dropbox Sign in with Wallet
    Wallet ( )
    Connect another wallet

    New to HackMD? Sign up

    Help

    • English
    • 中文
    • Français
    • Deutsch
    • 日本語
    • Español
    • Català
    • Ελληνικά
    • Português
    • italiano
    • Türkçe
    • Русский
    • Nederlands
    • hrvatski jezik
    • język polski
    • Українська
    • हिन्दी
    • svenska
    • Esperanto
    • dansk

    Documents

    Help & Tutorial

    How to use Book mode

    Slide Example

    API Docs

    Edit in VSCode

    Install browser extension

    Contacts

    Feedback

    Discord

    Send us email

    Resources

    Releases

    Pricing

    Blog

    Policy

    Terms

    Privacy

    Cheatsheet

    Syntax Example Reference
    # Header Header 基本排版
    - Unordered List
    • Unordered List
    1. Ordered List
    1. Ordered List
    - [ ] Todo List
    • Todo List
    > Blockquote
    Blockquote
    **Bold font** Bold font
    *Italics font* Italics font
    ~~Strikethrough~~ Strikethrough
    19^th^ 19th
    H~2~O H2O
    ++Inserted text++ Inserted text
    ==Marked text== Marked text
    [link text](https:// "title") Link
    ![image alt](https:// "title") Image
    `Code` Code 在筆記中貼入程式碼
    ```javascript
    var i = 0;
    ```
    var i = 0;
    :smile: :smile: Emoji list
    {%youtube youtube_id %} Externals
    $L^aT_eX$ LaTeX
    :::info
    This is a alert area.
    :::

    This is a alert area.

    Versions and GitHub Sync
    Get Full History Access

    • Edit version name
    • Delete

    revision author avatar     named on  

    More Less

    Note content is identical to the latest version.
    Compare
      Choose a version
      No search result
      Version not found
    Sign in to link this note to GitHub
    Learn more
    This note is not linked with GitHub
     

    Feedback

    Submission failed, please try again

    Thanks for your support.

    On a scale of 0-10, how likely is it that you would recommend HackMD to your friends, family or business associates?

    Please give us some advice and help us improve HackMD.

     

    Thanks for your feedback

    Remove version name

    Do you want to remove this version name and description?

    Transfer ownership

    Transfer to
      Warning: is a public team. If you transfer note to this team, everyone on the web can find and read this note.

        Link with GitHub

        Please authorize HackMD on GitHub
        • Please sign in to GitHub and install the HackMD app on your GitHub repo.
        • HackMD links with GitHub through a GitHub App. You can choose which repo to install our App.
        Learn more  Sign in to GitHub

        Push the note to GitHub Push to GitHub Pull a file from GitHub

          Authorize again
         

        Choose which file to push to

        Select repo
        Refresh Authorize more repos
        Select branch
        Select file
        Select branch
        Choose version(s) to push
        • Save a new version and push
        • Choose from existing versions
        Include title and tags
        Available push count

        Pull from GitHub

         
        File from GitHub
        File from HackMD

        GitHub Link Settings

        File linked

        Linked by
        File path
        Last synced branch
        Available push count

        Danger Zone

        Unlink
        You will no longer receive notification when GitHub file changes after unlink.

        Syncing

        Push failed

        Push successfully