Vim: Problems with indentation/alignment [duplicate] - c

This question already has answers here:
lining up function parameter lists with vim
(4 answers)
Closed 9 years ago.
So I've got a problem (obviously). I read an interesting article about "Smart Tabs", and I've tested this in Emacs and it works perfectly fine.
So currently I'm using Emacs because I've tried this in vim and I can't quite get it to work. Here are the lines of my .vimrc which I think are pertinent to indentation.
set autoindent " auto/smart indentation
set cindent
set preserveindent
set copyindent
set smarttab " tab and backspace are smart
set tabstop=4 " 4 spaces
set softtabstop=4
If you think something else could be causing the unexpected behavior as outlined below, my vimrc is here for you.
EXPECTED BEHAVIOR
I want a sample program to look like this, with me only typing the code and pressing the enter key every-so-often.
(each "-> " represents a tab, each "." represents a space)
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main (int argc, char *argv[])
{
-> printf ("%s\n",
-> ........teststring);
-> function_with_many_arguments (bacon,
-> ..............................indentation,
-> ..............................problems);
-> return 0;
}
</pre>
That is, I want vim to use tabs ONLY for indentation; NEVER for alignment.
This is because if someone decreases their tab size, I want the function calls and multi-line argument sets to be aligned properly regardless of how many characters a tab is equal to.
CURRENT BEHAVIOR
I've looked at all of the resources I could find, and that's how I put together my vimrc.
Right now, instead of doing the expected behavior, this is what code looks like instead:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main (int argc, char *argv[])
{
-> printf ("%s\n",
-> -> -> teststring);
-> function_with_many_arguments (bacon,
-> -> -> indentation,
-> -> -> problems);
-> return 0;
}
What am I doing wrong? It looks to me like any and all alignment is just two tab characters, whatever they happen to be, and I want it to indent to the right column with spaces instead.

I think this problem went away from me when i started using YouCompleteMe plugin with clang completion. Btw it's one of the best plugins if you seriously want to develop C in Vim.

Related

Codeblocks C Debugging

I've got a new challenge to return the factorial of a number. Got ideas on how to do this, but the challenger has given some starting code - which is shown below.
Now this isn't how I would have started it (with my extremely limited experience!) - BUT I wasn't sure how system would grab some text & place within an int array - hence I tried running it within codeblocks, debugging and looking at the watch table. However I can't see 'num'.
So I tried copying num to num1:
int num1[30] = {0};
memset(num1[0],num[0], sizeof(num));
that doesn't seem to affect anything...
So question really is - is there something wrong with my codeblocks config (it debugs other programs and I've tried both cygwin & MiniGW) or is there another reason for this behavious?
#include <stdio.h>
#include <string.h>
void FirstFactorial(int num[]) {
// code goes here
printf("%d", num);
}
int main(void) {
// keep this function call here
FirstFactorial(gets(stdin));
return 0;
}

debugging global variable in eclipse (C/C++)

I wrote this sample C program:
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>
#define SIZE 10
typedef struct _sampleStruct{
int f1;
double f2;
int f3[SIZE];
}sampleStruct;
sampleStruct g_s;
int main() {
sampleStruct s;
sampleStruct zeroed = {0};
s.f1 = g_s.f1 = 1;
s.f2 = g_s.f2 = 2.0;
for (int i = 0; i < SIZE; ++i) {
s.f3[i] = g_s.f3[i] = i*10;
}
memset(&s, 0, sizeof(sampleStruct));
printf("s: %d, g_s: %lf, zeroed: %d", s.f1, g_s.f2, zeroed.f3[3]);
return 0;
}
I'm using Eclipse Mars IDE and I want to debug this code.
the s and zeroed variables are shown in the variables tab.
however, the g_s (which is a global variable) is not shown there.
is there a way for me to see its fields and how they're changing etc.?
EDIT: just figured out there's a "add global variables" button but it is grayed, so I can't press it.
ok, so after some more googling about it I found this .
tl;dr - the problem was that I was using the default GDB(DSF) process launcher. apparently this is a known issue (more details in the link above).
what you should do to solve it:
go to Run-->Debug Configurations and check at the bottom of the opened window which debug launcher you are using. if it is the GDB(DSF) then press the "Select other..." link below it. In the opened window mark "Use configuration specific settings" and then choose a different launcher (in my case it is "Legacy Create Process Launcher").
now the "add global variables should not be gray and you can click it and choose the variables you want to watch.
leaving the question just in case someone will encounter the same problem.
I hope that you find this visual description helpful.
Click the Add Global Variables icon (green dot with eye-glasses above it):
Which is located under the Variables tab:
Here is a zoom-out of both:

Visual Studio C Program: How to print symbols for card suits?

I'm trying to make a card game and I want to use the actual card suit symbols to print cards as so:
5♣ J♦ 10♠ Q♥
Problem is I literally have zero idea how to code these symbols to print successfully in a program.
You'll need to use the unicode characters for those symbols along with a font that supports them. This page lists the unicode character code for various suits. They are:
Spade = U+2660, Heart = U+2665, Diamond = U+2666, Heart = U+2663
This will give you black suits. There's also characters for white suits.
You'll also need to make sure you are using wchar_t to represent the characters, not char as it won't be wide enough. Also, make sure you use functions like wprintf to do your output.
With Windows console font set to "Lucida Console" the following works:
#include <stdio.h>
int main (void)
{
int i;
for(i=3; i<=6; i++)
printf("%c", i);
printf("\n");
return 0;
}
Program output:
♥♦♣♠
Similarly with "Consolas" font.

c doesn't print "┌──┐" character correctly

Good afternoon, I'm facing a problem on my c code and I don't know what is causing it.
Every time I try to print characters like these: "┌──┐" my program simply prints some strange characters, like on this screenshot:
I'm using Qt Creator on Windows, with Qt version 5.5.0 MSVC 64 bits. The compiler is the Microsoft Visual C++ Compiler 12.0 (amd64).
I tried changing the locale but with no success. The only way I found to print these characters was to define them as int variables with the ASCII code and printing them, but it led to some really extensive and ugly coding, like this:
int cSupEsq = 218; //'┌'
int cSupDir = 191; //'┐'
int cInfEsq = 192; //'└'
int cInfDir = 217; //'┘'
int mVert = 179; //'│'
int mHor = 196; //'─'
int espaco = 255; //' '
int letraO = 111; //'o'
//Inicia limpando a tela da aplicação
clrscr();
//Linha 1
printf("%c", cSupEsq);
for (i = 1; i < 79; i++) { printf("%c", mHor); }
printf("%c", cSupDir);
Is there any way I can make the program treat these characters correctly? What could be causing this problem?
Your solution to use the OEM code points is the right way to go, codepage 850/437 is the default code page for the console and therefore should work. You could also use SetConsoleOutputCP to ensure the correct code page is used for the console.
Having said that, what is happening when you do not use your workaround is that the source file is being saved using a different codepage ie. not codepage 850/437. The in memory representation of the source code is Unicode (probably UTF-8), when you save the file the in memory representation of the characters are mapped to the target code page for the file.
What you can do is to save the file using the 850/437 codepage as the target, I don't know how you do this in Qt Creator (If you can at all), in Visual Studio for example you can select the down arrow on the Save button and select "Save with encoding", you can then proceed to select the target codepage, in your case code page 850. This will ensure that the in memory code points are mapped correctly to the file to be compiled.
I hope that helps explain the issue.
It shouldn't be necessary to print the characters one at a time. Instead, you can use an escape sequence:
printf("\xDA\xBF\xC0\xD9\xB3\xC4\xFF");

Why's _itoa causing my program to crash?

The following code just keeps on crashing when it reaches the part with _itoa, I've tried to implement that function instead and then it got even weirder, it just kept on crashing when I ran it without the debugger but worked fine while working with the debugger.
# include "HNum.h"
# include <stdio.h>
# include <stdlib.h>
# include <string.h>
# include <assert.h>
# define START_value 30
typedef enum {
HNUM_OUT_OF_MEMORY = -1,
HNUM_SUCCESS = 0,
} HNumRetVal;
typedef struct _HNum{
size_t Size_Memory;
char* String;
}HNum;
HNum *HNum_alloc(){
HNum* first = (HNum*)malloc(sizeof(HNum));
if(first==NULL){
return NULL;
}
first->String =(char*)malloc(sizeof(START_value));
if(first->String==NULL){
return NULL;
}
first->Size_Memory = START_value; // slash zero && and starting from zero index;
return first;
}
HNumRetVal HNum_setFromInt(HNum *hnum, int nn){
itoa(nn,hnum->String,10);
}
void main(){
HNum * nadav ;
int h = 13428637;
nadav = HNum_alloc();
nadav->String="1237823423423434";
HNum_setFromInt(nadav,h);
printf("nadav string : %s \n ",nadav->String);
//printf("w string %s\n",w->String);
//printf("nadav string %s\n",nadav->String);
HNum_free(nadav);
}
I've been trying to figure this out for hours and couldn't come up with anything...
The IDE I'm using is Visual Studio 2012 express, the crash shows the following:
"PROJECT C.exe has stopped working
windows can check online for a solution to the program."
first->String =(char*)malloc(sizeof(START_value));
should be
first->String = malloc(START_value);
The current version allocates space for sizeof(int)-1 characters (-1 to leave space for the nul terminator). This is too small to hold your target value so _itoa writes beyond memory allocated for first->String. This results in undefined behaviour; it is quite possible for different runs to fail in different places or debug/release builds to behave differently.
You also need to remove the line
nadav->String="1237823423423434";
which leaks the memory allocated for String in HNum_alloc, replacing it with a pointer to a string literal. This new pointer should be considered to be read-only; you cannot write it it inside _itoa
Since I'm not allowed to comment:
simonc's answer is correct. If you find the following answer useful, you should mark his answer as the right one:P
I tried that code myself and the only thing missing is lets say:
strcpy(nadav->String, "1237823423423434"); INSTEAD OF nadav->String="1237823423423434";
and
first->String = malloc(START_value); INSTEAD OF first->String =(char*)malloc(sizeof(START_value));
Also, maybe you'd have to use _itoa instead of itoa, that's one of the things I had to change in my case anyhow.
If that doesn't work, you should probably consider using a different version of VS.

Resources