c

C - Undefined reference to function error with linked files

笑着哭i 提交于 2021-02-18 18:09:30
问题 I started implementing a large program. But I ran into a massive issue. So here is very simplified code of my program. I have a separate .c file for my functions which is normal.c the main program is main.c and I have linked those two with cal.h header file. main.c #include <stdio.h> #include "cal.h" void main() { int num1, num2, ans; num1=5; num2=5; ans=add(num1, num2); printf("%d" ,ans); } normal.c #include "cal.h" int add(int num1, int num2) { return num1+num2; } cal.h #ifndef CAL_H

Initialize a dynamic array in O(1) time in C/C++

夙愿已清 提交于 2021-02-18 17:43:32
问题 Is there any way to initialize a whole dynamic array in O(1) time? Is there something similar to bool a[10] = {false} in the case of a static array? 回答1: No, there is not, not in c++, nor in c, not in any language or CPU I know of. But it can be done with 1 line of code. in C: for a array of char: char a[N]; memset(a, '1', N); // only works for data 1 byte long in C++ std::vector<T> v; std::fill(v.begin(), v.end(), value); 回答2: For a dynamic array, each element you want to set must be

Write a program to swap odd and even bits in integer what is the minimum number of steps required?

梦想的初衷 提交于 2021-02-18 17:42:47
问题 I am trying to solve this one..and my code is as follows #include<stdio.h> int main() { int a, b = 0xaaaaaaaa, c = 0x55555555; printf("\n enter the number: \n"); scanf("%d", & a); a = ((a & b) >> 1) | ((a & c) << 1); printf("\n %d", a); } ..but i am getting some weird outputs..can anyone tell me what errors i m making? 回答1: Your idea is ok. Perhaps you're getting the weird outputs because the bitshift don't work exactly as you may expect. Your variables are of type int . Means - they are

How to import constants from .h file into python module

扶醉桌前 提交于 2021-02-18 16:59:51
问题 What is a recommended way to import a bunch of constants defined in a c-style (not c++, just plain old c) .h file into python module so that it can be used in python's part of a project. In the project we use a mix of languages and in perl I can do this importing by using h2xs utility to generate .pm module. Constants definition looks like #define FOO 1 enum { BAR, BAZ }; etc. C-style comments are also presented an have to be properly handled. 回答1: I recently used the pyparsing library to

What is __libc_start_main and _start?

让人想犯罪 __ 提交于 2021-02-18 16:58:08
问题 From the past few days I have been trying to understand what happens behind the curtain when we execute a C program. However even after reading numerous posts I cannot find a detailed and accurate explanation for the same. Can someone please help me out ? 回答1: You would usually find special names like this for specific uses when compiling and linking programs. Typically, something like _start would be the actual entry point for an executable, it will be located in some object file or library

What if a null character is present in the middle of a string?

烈酒焚心 提交于 2021-02-18 16:57:46
问题 I understand that the end of a string is indicated by a null character, but i cannot understand the output of the following code. #include <stdio.h> #include <string.h> int main(void) { char s[] = "Hello\0Hi"; printf("%d %d", strlen(s), sizeof(s)); } OUTPUT: 5 9 If strlen() detects the end of the string at the end of o, then why doesn't sizeof() do the same thing? Even if it doesn't do the same thing, isn't '\0' A null character (i.e, only one character), so shouldn't the answer be 8? 回答1:

Assign volatile to non-volatile sematics and the C standard

浪子不回头ぞ 提交于 2021-02-18 16:14:29
问题 volatile int vfoo = 0; void func() { int bar; do { bar = vfoo; // L.7 }while(bar!=1); return; } This code busy-waits for the variable to turn to 1 . If on first pass vfoo is not set to 1 , will I get stuck inside. This code compiles without warning. What does the standard say about this? vfoo is declared as volatile . Therefore, read to this variable should not be optimized. However, bar is not volatile qualified. Is the compiler allowed to optimize the write to this bar ? .i.e. the compiler

Assign volatile to non-volatile sematics and the C standard

百般思念 提交于 2021-02-18 16:06:43
问题 volatile int vfoo = 0; void func() { int bar; do { bar = vfoo; // L.7 }while(bar!=1); return; } This code busy-waits for the variable to turn to 1 . If on first pass vfoo is not set to 1 , will I get stuck inside. This code compiles without warning. What does the standard say about this? vfoo is declared as volatile . Therefore, read to this variable should not be optimized. However, bar is not volatile qualified. Is the compiler allowed to optimize the write to this bar ? .i.e. the compiler

Assign volatile to non-volatile sematics and the C standard

拥有回忆 提交于 2021-02-18 16:06:32
问题 volatile int vfoo = 0; void func() { int bar; do { bar = vfoo; // L.7 }while(bar!=1); return; } This code busy-waits for the variable to turn to 1 . If on first pass vfoo is not set to 1 , will I get stuck inside. This code compiles without warning. What does the standard say about this? vfoo is declared as volatile . Therefore, read to this variable should not be optimized. However, bar is not volatile qualified. Is the compiler allowed to optimize the write to this bar ? .i.e. the compiler

I don't understand pointers

社会主义新天地 提交于 2021-02-18 15:18:10
问题 What is a pointer? What is dereferencing? If p is a pointer, what is the difference between *p = some_value and p = other_value ? What does p = &some_variable mean? What is a NULL pointer? What happens when you dereference a NULL pointer? 回答1: Get a stack of yellow sticky notes, a pencil, an eraser, and a pen. Take a sticky note. Divide it with a horizontal line. Write "note #1" on it with a pen in the top part. (This mark is permanent). Write "7" with a pencil in the bottom part. Stick it to