{"id":16298877,"url":"https://github.com/smakss/c-questions","last_synced_at":"2025-04-09T19:27:29.668Z","repository":{"id":55655203,"uuid":"320370637","full_name":"SMAKSS/c-questions","owner":"SMAKSS","description":"A list of C programming language questions and their answers.","archived":false,"fork":false,"pushed_at":"2021-01-08T03:53:19.000Z","size":21,"stargazers_count":1,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-02-15T11:45:44.319Z","etag":null,"topics":["c","question-answering"],"latest_commit_sha":null,"homepage":"","language":null,"has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/SMAKSS.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2020-12-10T19:31:24.000Z","updated_at":"2023-03-09T00:45:07.000Z","dependencies_parsed_at":"2022-08-15T05:40:38.729Z","dependency_job_id":null,"html_url":"https://github.com/SMAKSS/c-questions","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/SMAKSS%2Fc-questions","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/SMAKSS%2Fc-questions/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/SMAKSS%2Fc-questions/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/SMAKSS%2Fc-questions/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/SMAKSS","download_url":"https://codeload.github.com/SMAKSS/c-questions/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248097233,"owners_count":21047193,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["c","question-answering"],"created_at":"2024-10-10T20:45:49.312Z","updated_at":"2025-04-09T19:27:29.645Z","avatar_url":"https://github.com/SMAKSS.png","language":null,"funding_links":[],"categories":[],"sub_categories":[],"readme":"\u003cdiv align=\"center\"\u003e\n  \u003cimg height=\"60\" src=\"https://img.icons8.com/color/344/c-programming.png\"\u003e \n  \u003ch1\u003eC Questions\u003c/h1\u003e\n\n\u003cspan\u003eHere I've just posted multiple **C programming language** question to share the capability of language and also allow people to test their knowledge in this language.\n\nIt can be used as a tool to prepare yourself for interviews or opening new thoughts on the language itself.\n\nThe answer and its short description, is available in the **collapsed sections** below the questions.\n\u003c/span\u003e\n\n\u003c/div\u003e\n\n---\n\n###### 1. What's the output?\n\n```c\nint x = 10;\n\nwhile (x --\u003e 0)\n  printf(\"%d \", x);\n```\n\n- A: It will produce a syntax error\n- B: `0`\n- C: `9 8 7 6 5 4 3 2 1 0`\n- D: It will print `10` indefinitely\n\n\u003cdetails\u003e\u003csummary\u003e\u003cb\u003eAnswer\u003c/b\u003e\u003c/summary\u003e\n\u003cp\u003e\n\n#### Answer: C\n\nThis is a simple loop over variable `x`. In the loop condition, we just decrementing `x` in each iteration (`x--` or `x --` equals to `x = x - 1`) until it gets equal to `0`.\n\n\u003c/p\u003e\n\u003c/details\u003e\n\n---\n\n###### 2. What's the output?\n\n```c\nint x = 10;\n\nwhile (x --\\\n            \\\n             \\\n              \\\n               \u003e 0)\n     printf(\"%d \", x);\n```\n\n- A: It will produce a syntax error\n- B: `∞`\n- C: `0`\n- D: `9 8 7 6 5 4 3 2 1 0`\n\n\u003cdetails\u003e\u003csummary\u003e\u003cb\u003eAnswer\u003c/b\u003e\u003c/summary\u003e\n\u003cp\u003e\n\n#### Answer: D\n\nThis is the same question as the [question 1](#1.-What's-the-output?).\n\nThis is a simple loop over variable `x`. In the loop condition, we just decrementing `x` in each iteration (`x--` or `x --` equals to `x = x - 1`) until it gets equal to `0`.\n\nThe only difference between these two questions is about ignoring the `\\` by the compiler whilst there is a next line (`\\n`) appears after it.\n\n\u003c/p\u003e\n\u003c/details\u003e\n\n---\n\n###### 3. Assume we have a function called foo as follows:\n\n```c\nint foo(int f(int,int), int x) {\n    ...\n}\n```\n\nIs the first parameter valid? If it is valid, what is it? And if it’s not valid, what kind of error it will produce?\n\n- A: Yes, it is valid and it is a function pointer\n- B: No, it is not a valid parameter and it will lead to a syntax error\n- C: No, it is not a valid parameter and it will lead to a semantic error\n- D: Yes, it is valid and it is a C language built-in function\n\n\u003cdetails\u003e\u003csummary\u003e\u003cb\u003eAnswer\u003c/b\u003e\u003c/summary\u003e\n\u003cp\u003e\n\n#### Answer: A\n\nIt is an ordinary function pointer and it will interpret as `int (*f)(int,int)`.\n\n\u003c/p\u003e\n\u003c/details\u003e\n\n---\n\n###### 4. What's the output?\n\n```c\nvoid foo() {\n   static int i = 5;\n\n   printf(\"%d \", ++i);\n}\n\nvoid main() {\n    int i;\n\n    for (i = 0; i \u003c 3; ++i)\n        foo();\n}\n```\n\n- A: `1 2 3`\n- B: `6 6 6`\n- C: `6 7 8`\n- D: `5 6 7`\n\n\u003cdetails\u003e\u003csummary\u003e\u003cb\u003eAnswer\u003c/b\u003e\u003c/summary\u003e\n\u003cp\u003e\n\n#### Answer: C\n\nWhen we define a variable with `static` it will act like a closure so it will remember the last value of itself each time we running it, it will increment the previous value instead of resetting the value to `5`.\n\nAlso, we have to keep in mind the `++i` _(prefix)_ will first increment the value and then assign/return it. So the first call of `foo()` will print `6` for us.\n\n\u003c/p\u003e\n\u003c/details\u003e\n\n---\n\n###### 5. What's the output?\n\n```c\nint i = 0;\n\nfor(i = 0; i \u003c 3; i++) {\n   if (i = 2)\n      continue;\n   printf(\"%d \", i);\n}\n```\n\n- A: `0 1 2`\n- B: `0 1`\n- C: `0 1 2 3`\n- D: It won't print anything\n\n\u003cdetails\u003e\u003csummary\u003e\u003cb\u003eAnswer\u003c/b\u003e\u003c/summary\u003e\n\u003cp\u003e\n\n#### Answer: D\n\nThere is a difference between comparison and assigning. In this case we are assigning the value of `2` to `i` _(i=2)_, whilst we have to compare them with double equal signs. So, since we've used `continue` the print command will be escaped here.\n\n\u003c/p\u003e\n\u003c/details\u003e\n\n---\n\n###### 6. What's the output?\n\n```c\nint i = 1;\n\nwhile (i \u003c 10)\n   printf (\"%d\\n\", i);\n```\n\n- A: It will print 1 indefinitely\n- B: `1`\n- C: `10`\n- D: `1 2 3 4 5 6 7 8 9`\n\n\u003cdetails\u003e\u003csummary\u003e\u003cb\u003eAnswer\u003c/b\u003e\u003c/summary\u003e\n\u003cp\u003e\n\n#### Answer: A\n\nWe don not increment the `i` at any moment, so the condition will always be true and the value of `i` will be always equal to `1`.\n\n\u003c/p\u003e\n\u003c/details\u003e\n\n---\n\n###### 7. what is/are the difference(s) between `malloc()` and `calloc()` memory allocation?\n\n- A: There is no major difference and they both allocate memory from heap area/dynamic memory\n- B: `malloc()` initializes the allocated memory block to zero whilst `calloc()` does not initialize it\n- C: `calloc()` initializes the allocated memory block to zero whilst `malloc()` does not initialize it\n- D: `calloc()` will take two arguments whilst `malloc()` will only take one\n\n\u003cdetails\u003e\u003csummary\u003e\u003cb\u003eAnswer\u003c/b\u003e\u003c/summary\u003e\n\u003cp\u003e\n\n#### Answer: C and D\n\n`calloc()` and `malloc()` both are responsible for allocating memory. `calloc()` will initialize the allocated memory block to zero, whilst `malloc()` doesn’t initialize it. If we try to access the content of the memory block(before initializing) then we’ll get a segmentation fault error(or maybe garbage values), but if we try to do the same thing with `calloc()` we will get `0`. Also, `calloc ()` will take two arguments, the first one is a number of blocks to be allocated and the second one is the size of each block, whilst `malloc ()` will only get one argument and it is for the size of each block.\n\n\u003c/p\u003e\n\u003c/details\u003e\n\n---\n\n###### 8. What is a dangling pointer?\n\n- A: A pointer which points to a deleted memory address\n- B: A pointer which points to a static variable\n- C: A pointer which points to a freed memory address\n- D: A pointer which points to some data without any specific type\n\n\u003cdetails\u003e\u003csummary\u003e\u003cb\u003eAnswer\u003c/b\u003e\u003c/summary\u003e\n\u003cp\u003e\n\n#### Answer: A and C\n\nA dangling pointer is a pointer that points to invalid data or to data which is not valid anymore. So, it can pointers to deleted and freed memory addresses will be counted as dangling pointers.\n\n\u003c/p\u003e\n\u003c/details\u003e\n\n---\n\n###### 9. What is a wild pointer?\n\n- A: A pointer which has not been initialized to anything\n- B: A pointer which points to a garbage memory location\n- C: A pointer which is initialized to NULL\n- D: A pointer which points to a freed memory address\n\n\u003cdetails\u003e\u003csummary\u003e\u003cb\u003eAnswer\u003c/b\u003e\u003c/summary\u003e\n\u003cp\u003e\n\n#### Answer: A and B\n\nA pointer which has not been initialized to anything (not even NULL) is known as a wild pointer. Also, the pointer may be initialized to a non-NULL garbage value that may not be a valid address. So, if any pointer points to uninitialized or garbage location we call it wild pointer.\n\n\u003c/p\u003e\n\u003c/details\u003e\n\n---\n\n###### 10. What's the output?\n\n```c\nprintf(\"\\\");\n```\n\n- A: `\\`\n- B: `''`\n- C: It will produce a syntax error\n- D: It won't print anything\n\n\u003cdetails\u003e\u003csummary\u003e\u003cb\u003eAnswer\u003c/b\u003e\u003c/summary\u003e\n\u003cp\u003e\n\n#### Answer: C\n\n`\\` is an escape sign, but in this particular situation there is nothing to be escaped in the `printf` so, it will escape the `\"` _(double quote sign)_ and it will lead to syntax errors, because, there will be no closing quotes in the `printf` anymore.\n\n\u003c/p\u003e\n\u003c/details\u003e\n\n---\n\n###### 11. What's the output?\n\n```c\nprintf(\"'\\'\");\n```\n\n- A: `\\`\n- B: `''`\n- C: It will produce a syntax error\n- D: It won't print anything\n\n\u003cdetails\u003e\u003csummary\u003e\u003cb\u003eAnswer\u003c/b\u003e\u003c/summary\u003e\n\u003cp\u003e\n\n#### Answer: B\n\n`\\` is an escape sign, in this particular case it will escape the second `'` _(quote sign)_, so, it won't change anything here and the printed element will be `''`.\n\n\u003c/p\u003e\n\u003c/details\u003e\n\n---\n\n###### 12. What's the output?\n\n```c\nprintf(\"\\\\\");\n```\n\n- A: `\\`\n- B: `''`\n- C: It will produce a syntax error\n- D: It won't print anything\n\n\u003cdetails\u003e\u003csummary\u003e\u003cb\u003eAnswer\u003c/b\u003e\u003c/summary\u003e\n\u003cp\u003e\n\n#### Answer: A\n\n`\\` is an escape sign, in this particular case it will escape the second `\\`, so, the final output will be `\\`.\n\n\u003c/p\u003e\n\u003c/details\u003e\n\n---\n\n###### 13. What's the output?\n\n```c\nshort int i;\n\nfor (i = 32765; i \u003c 32768; i++)\n    printf (\"%d\\n\", i);\n```\n\n- A: `32765 32766 32767`\n- B: `32766 32767 32768`\n- C: It will run indefinitely\n- D: It won't print anything\n\n\u003cdetails\u003e\u003csummary\u003e\u003cb\u003eAnswer\u003c/b\u003e\u003c/summary\u003e\n\u003cp\u003e\n\n#### Answer: C\n\nThe max value for `short` variables are `32767`, so, the condition _(`i \u003c 32768`)_ will be never met.\n\n\u003c/p\u003e\n\u003c/details\u003e\n\n---\n\n###### 14. What's the output?\n\n```c\nfloat i = 30;\n\nwhile (f != 31.0) {\n    printf (\"%f\\n\", f);\n    f += 0.1;\n}\n```\n\n- A: `30.1 30.2 30.3 30.4 30.5 30.6 30.7 30.8 30.9`\n- B: `30.000000 30.100000 30.200001 30.300001 30.400002 30.500002 30.600002 30.700003 30.800003 30.900003`\n- C: It will run indefinitely\n- D: It won't print anything\n\n\u003cdetails\u003e\u003csummary\u003e\u003cb\u003eAnswer\u003c/b\u003e\u003c/summary\u003e\n\u003cp\u003e\n\n#### Answer: C\n\nThe floating point variables will automatically cast to float variables, whether the initialization is int or whatever else. So, `30` will cast to `30.000000`. Because of this, in this particular case, the condition will be never met _(`f != 31.0`)_ so, the code will add up `0.1` to the initial value until memory gets overflow.\n\n\u003c/p\u003e\n\u003c/details\u003e\n\n---\n\n###### 15. What's the output?\n\n```c\nint i = 0;\n\nwhile(i\u003c=5);\nprintf(\"%d\\n\", i);\n```\n\n- A: `0 1 2 3 4 5`\n- B: `0 1 2 3 4`\n- C: `0.0 1.0 2.0 3.0 4.0 5.0`\n- D: It won't print anything\n\n\u003cdetails\u003e\u003csummary\u003e\u003cb\u003eAnswer\u003c/b\u003e\u003c/summary\u003e\n\u003cp\u003e\n\n#### Answer: D\n\nIf you take a closer look at the snippet you will notice _(`while(i\u003c=5);`)_ a semicolon _(`;`)_ after the `while` statement, so, it will produce a null statement inside the `while` loop _(`while(i\u003c=5){ ; }`)_ and the snippet will be executing indefinitely because of that and never reaches the `printf` expression.\n\n\u003c/p\u003e\n\u003c/details\u003e\n\n---\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fsmakss%2Fc-questions","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fsmakss%2Fc-questions","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fsmakss%2Fc-questions/lists"}