Indenting C code, Unix - c

I searched documentation for indent, but I gave up eventually, I want to indent code like this:
int main(int argc, char **argv){
some code;
}
I know indent -kr gives you braces like this, but -kr style also includes
int
main(int argc, char **argv){
some code;
}
and this int in line before main gives me creeps.
Can anyone please tell me option for this?

The particular options that you are interested in are
-npsl
--dont-break-procedure-type
Put the type of a procedure on the same line as its name.
-brf
--braces-on-func-def-line
Put braces on function definition line.
As suggested, the GNU indent manual describes the various options.
Here is a quick script to illustrate the effect of those options on the basic predefined styles:
#!/bin/sh
for opt in gnu linux orig kr
do
echo "** $opt"
indent -st -$opt -npsl -brf hello.c
done
and the input file:
#include <stdio.h>
int main(int argc, char **argv) { int n; for (n = 0; n < argc; ++n) printf("arg%d=%s\n", n, argv[n]); return 0; }
and corresponding output:
** gnu
#include <stdio.h>
int main(int argc, char **argv) {
int n;
for (n = 0; n < argc; ++n)
printf("arg%d=%s\n", n, argv[n]);
return 0;
}
** linux
#include <stdio.h>
int main(int argc, char **argv) {
int n;
for (n = 0; n < argc; ++n)
printf("arg%d=%s\n", n, argv[n]);
return 0;
}
** orig
#include <stdio.h>
int main(int argc, char **argv) {
int n;
for (n = 0; n < argc; ++n)
printf("arg%d=%s\n", n, argv[n]);
return 0;
}
** kr
#include <stdio.h>
int main(int argc, char **argv) {
int n;
for (n = 0; n < argc; ++n)
printf("arg%d=%s\n", n, argv[n]);
return 0;
}
I do not see an option to suppress the space before the { character.

You are likely looking for the -npsl option. The -psl (--procnames-start-lines) option causes the type of a procedure being defined to be placed on the line before the name of the procedure. Thus if you specify that option, all function declarations will be changed from:
int main(int argc, char **argv){
some code;
}
to
int
main(int argc, char **argv){
some code;
}
You can check whether you are including -psl in the common type -kr and remove it, or if not included, you can specify -npsl (--dont-break-procedure-type) and the type will not be placed on a separate line.
There are trade-off with all options. I like braces on the same line as int main() {, but for function definitions I like braces on the next line. e.g.:
type function
{
...
}
So if you have mutually exclusive preferences such as that, you simply choose the one you want a majority of the code to incorporate and tweak the rest. You might give the following invocation a try:
indent -i 4 -lp -ts 8 -lp -lps -br -brs -blf -ce -cdw -pcs -bs -nbc -npsl -saf -sai -saw -nut
It is somewhat of a balanced indent scheme.

Related

where to read cmd input like "myapp.exe input" or "myapp.exe -?" (i using cmd)

When I using Java, i need to type command likejava -?If user type -? at the end, why the application know this to reply output? Please tell me c code to identify the -?
They are passed as the parameters to main():
#include <stdio.h>
int main(int argc, const char* argv[])
{
for (int i = 0; i < argc; i++) {
printf("Arg %i is %s\n", i, argv[i]);
}
}
When compiled and then executed as
myProgram.exe arg1 stuff ?
It would output
Arg 0 is myProgram.exe
Arg 1 is arg1
Arg 2 is stuff
Arg 3 is ?
In C you have three options for your main signature. The first is the one that does not take any parameters int main(void). The second one int main() as mentioned in the comments takes any number of parameters but they are unnamed. The third one however has two parameters int main(int argc, char **argv) the names of the parameters do not matter they are just commonly used. These two parameters serve the purpose to provide the command line parameters to your program.
argc: Is the counter variable which holds the number of the provided arguments separated by spaces
argv: contains the command line arguments as an array of c-strings
Your program implicitely receives always one argument which is the name of the application (or \0 if the host environment can not provide that). Here a little example on how to iterate over the arguments:
#include <stdio.h>
int main(int argc, char **argv) {
for(int i = 0; i < argc; i++) {
printf("%s\n", argv[i]);
}
}
In C, the main is declared as:
int main(int argc, char** argv);
the first argument is the number of parameters while the second one is an array of parameters so, for example in your case you would do:
#include<string.h>
#include<stdio.h>
#define QUESTION_MARK "-?"
int main(int argc, char **argv){
if(argc > 1){
char *qsmark = argv[1];
if(strcmp(qsmark, QUESTION_MARK) == 0){
printf("argv[1] is -?\n");
}
}
return 0;
}
Remember that the first argv is the name of the executable.
Please avoid comparing string by hands though, use the standard library to get if what is pointed by qsmark is actually equal to "-?"
If you can use getopt() or other similar POSIX functions. Then this is one way to go:
#include <stdlib.h>
#include <stdio.h>
#include <getopt.h>
int main(int argc, char** argv)
{
int opt;
while ((opt = getopt(argc, argv, "h?")) != -1)
{
switch (opt)
{
case '?':
case 'h':
printf("Usage: bla bla\n");
break;
}
}
return EXIT_SUCCESS;
}
Example:
~ # /tmp/temp_test -?
Usage: bla bla
~ # /tmp/temp_test -y
/tmp/temp_test: invalid option -- 'y'
Usage: bla bla
~ #
More information in man page.

How to print a variable length string of a single character

I have a need to print a variable number of a given character in conjunction with my formatted output. I was looking for something similar or equivalent to the VBA function String(num, char), but haven't been able to find any. I've written a function to do the job but if there is something built-in that does it I'd love to know. Here's what I have. For the purpose of testing I'm using a sloppy implementation of argv[].
What I want to is print out something like this;
Here's the rough implementation I've come up with;
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
const char * make_string(int num, char character)
{
char *strchars = malloc(num);
for (int i = 0; i < num; i++)
strchars[i] = character;
return strchars;
}
int main(int argc, char *argv[])
{
for (int i = 1; i < argc; i++) {
printf("%s\n", make_string(strlen(argv[i]),'_'));
printf("%s%c %s\n", make_string(strlen(argv[i]),'_'),'|', argv[i]);
}
}
Is there a library function for printing strings of repeating characters like this?
Credit for this answer goes to UmamaheshP for pointing me in the right direction with a comment. This is what I was looking for and was adapted from an example he linked to.
#include <stdio.h>
#include <string.h>
int main (int argc, char *argv[]) {
int i;
char *pad = "________________";
for (i = 1; i < argc; i++)
printf ("%.*s\n%.*s%c %s\n", strlen(argv[i]),
pad,strlen(argv[i]), pad, '|', argv[i]);
}

How to provide parameters with command Line

i'm trying to deliver parameters for a program with the command line.
I want, that the program is working as shown now:
- start the program with parameter "program.exe "
- then the should be useable in the programm
How can i approach this thing?
Here is the essential part of my programm:
int main(){
int length;
unsigned int i=0;
length=strlen(word);
for(i=0;i<length;i++) {
printf("%d",word[i]);
}
}
And i wanted to add this word[] parameter via command line. Thanks!
int main( int argc, char* argv[] ) {
return 0;
}
argc => argument count / command line parameter count
argv[x] => argument value / parameter text at position
For command line arguments Use argv and argc
#include <stdio.h>
#include <stdlib.h>
int main( int argc, char* argv[] )
{
int i;
printf("argc is %d\n",argc);
for(i = 1; i < argc ; i++){
printf("%d \n", atoi(argv[i]));
}
}
Run your program as
./a.out 10 20 30
argc is 4
10
20
30

Some strange problems in the code

Hello I tried to get a external parameters to my main. But somehow the compiler gcc shows me weird problems I have not encountered them and did not find a solution. I would be happy if you help me understand the problems and fix them.
code-
#include <stdio.h>
#include <stdlib.h>
main(int argc, char** argv[])
{
int i;
char temp[99];
for (i=0; i < argc/2 ; i++)
{
temp = argv[i];
argv[i] = argv[argc-i-1];
argv[argc-i-1] = temp;
}
for(i = 0; i < argc ; i++)
{
printf("%s",temp[i]);
}
return(0);
}
errores
error: incompatible types in assignment
warning: assignment from incompatible pointer type
Bad prototype for main: Use int main(), int main(int argc, char** argv) or something compatible.

how to print a string vertically in c?

Ok the output is supposed to look like this:
./a 3 4 8 2
3
4
8
2
This is what I have so far, but I am lost and can only get the first integer to print (we have to use GetInt, which gets the specified integer in the string):
int main (int argc, char*argv []){
int v;
int i;
i = 1;
v = GetInt(argc, argv, i + 1);
if(argc >= 1){
printf("%d\n", GetInt(argc, argv, i));
}
return 0;
}
Looks like you need to use a for-loop to get each value in turn.
Without actually seeing your implementation of GetInt, something like this (Assumes C90):
#include <stdio.h>
int GetInt( int argc, char* argv[], int i ) {
/*
* you may want to use sscanf, strtol or other functions,
* but you haven't specified
*/
return atoi( argv[i] );
}
int main ( int argc, char* argv[] ) {
int i;
for ( i = 1; i < argc; ++i ) {
printf( "%d\n", GetInt( argc, argv, i ) );
}
return 0;
}
Returns:
$ ./a.out 3 4 8 2
3
4
8
2
Error checking omitted and such. It is an exercise to you to figure out how to deal with non-decimal arguments.
As a note, why do you have to use GetInt()? It's completely superfluous and not part of a standard library, and unless you wrote it yourself (or in class), I can't imagine ever using it. This is what I would do:
#include <stdio.h>
int main(int argc, char *argv[])
{
int i;
for(i = 1; i < argc; i++)
{
printf("%s\n", argv[i]);
}
return 0;
}
I know it doesn't use GetInt(), but it's shorter, and you probably shouldn't be using GetInt() because it's kind of useless. Plus, there's already an atoi() function and, if you don't want to use that because it's outdated, a strtol() function you can recast as an int if you have to.
I'm not citicizing you or anything, I'm just wondering why you're being required to use this superfluous, nonstandard function.
Use printf() in a loop. argc is the number of white space separated arguments in argv[], in the example above, argc = 5.

Resources