Search Results

Search found 1848 results on 74 pages for 'printf'.

Page 1/74 | 1 2 3 4 5 6 7 8 9 10 11 12  | Next Page >

  • Printf ubuntu Segmentation fault (core dumped)

    - by Someone
    I have this code: int a; printf("&a = %u\n",(unsigned)&a); printf("a\n"); printf("b\n"); printf("c\n"); printf("d\n"); I tried to print the pointer of a variable. But it fail on the row printf("a\n"); and says Segmentation fault (core dumped) Output: &a = 134525024 Segmentation fault (core dumped) When I remove the row printf("&a = %u\n",(unsigned)&a); from the code, its success. Output: a b c d What worng in my code?

    Read the article

  • will 'printf' always do its job? [closed]

    - by gcc
    printf("/something else/"); //without using \n in printf I know printf() uses a buffer which prints whatever it contains when, in the buffer, "\n" is seen by the buffer. So when we forget to use "\n" in printf ,rarely,buffer will not be emptied.Therefore,printf wont do its job. Am I wrong?

    Read the article

  • C Number to Text problem with ones and tens..

    - by Joegabb
    #include<stdio.h> #include<conio.h> main() { int ones,tens,ventoteen, myloop = 0; long num2,cents2,centeens,cents1,thousands,hundreds; double num; do{ printf("Enter a number: "); scanf("%lf",&num); if(num<=10000 || num>=0) { if (num==0) { printf("\t\tZero"); } num=(num*100); num2= (long)num; thousands=num2/100000; num2=num2%100000; hundreds=num2/10000; num2=num2%10000; if ((num2>=1100) || (num2<=1900)) { tens=0; ones=0; ventoteen=num2%1000; } else { tens=num2/1000; num2=num2%1000; ones=num2/100; num2=num2%100; } if((num2>=11) && (num2<=19)) { cents1=0; cents2=0; centeens=num2%10; } else { cents1=num2/10; num2=num2%10; cents2=num2/1; } if (thousands == 1) printf("One thousand "); else if (thousands == 2) printf("Two thousand "); else if (thousands == 3) printf("Three Thousand "); else if (thousands == 4) printf("Four thousand "); else if (thousands == 5) printf("Five Thousand "); else if (thousands == 6) printf("Six thousand "); else if (thousands == 7) printf("Seven Thousand "); else if (thousands == 8) printf("Eight thousand "); else if (thousands == 9) printf("Nine Thousand "); else {} if (hundreds == 1) printf("one hundred "); else if (hundreds == 2) printf("two hundred "); else if (hundreds == 3) printf("three hundred "); else if (hundreds == 4) printf("four hundred "); else if (hundreds == 5) printf("five hundred "); else if (hundreds == 6) printf("six hundred "); else if (hundreds == 7) printf("seven hundred "); else if (hundreds == 8) printf("eight hundred "); else if (hundreds == 9) printf("nine hundred "); else {} switch(ventoteen) { case 1: printf("eleven ");break; case 2: printf("twelve ");break; case 3: printf("thirteen ");break; case 4: printf("fourteen ");break; case 5: printf("fifteen ");break; case 6: printf("sixteen ");break; case 7: printf("seventeen ");break; case 8: printf("eighteen ");break; case 9: printf("nineteen ");break; } switch(tens) { case 1: printf("ten ");break; case 2: printf("twenty ");break; case 3: printf("thirty ");break; case 4: printf("forty ");break; case 5: printf("fifty ");break; case 6: printf("sixty ");break; case 7: printf("seventy ");break; case 8: printf("eighty ");break; case 9: printf("ninety ");break; } switch(ones) { case 1: printf("one ");break; case 2: printf("two ");break; case 3: printf("three ");break; case 4: printf("four ");break; case 5: printf("five ");break; case 6: printf("six ");break; case 7: printf("seven ");break; case 8: printf("eight ");break; case 9: printf("nine ");break; } switch(cents1) { case 1: printf("and ten centavos ");break; case 2: printf("and twenty centavos ");break; case 3: printf("and thirty centavos ");break; case 4: printf("and fourty centavos ");break; case 5: printf("and fifty centavos ");break; case 6: printf("and sixty centavos ");break; case 7: printf("and seventy centavos ");break; case 8: printf("and eighty centavos ");break; case 9: printf("and ninety centavos ");break; } switch(centeens) { case 1: printf("and eleven centavos ");break; case 2: printf("and twelve centavos ");break; case 3: printf("and thirteen centavos ");break; case 4: printf("and fourteen centavos ");break; case 5: printf("and fifteen centavos ");break; case 6: printf("and sixteen centavos ");break; case 7: printf("and seventeen centavos ");break; case 8: printf("and eighteen centavos ");break; case 9: printf("and nineteen centavos ");break; } switch(cents2) { case 1: printf("and one centavos ");break; case 2: printf("and two centavos ");break; case 3: printf("and three centavos ");break; case 4: printf("and four centavos ");break; case 5: printf("and five centavos ");break; case 6: printf("and six centavos ");break; case 7: printf("and seven centavos ");break; case 8: printf("and eight centavos ");break; case 9: printf("and nine centavos ");break; } } getch(); }while(myloop == 0); return 0; } my code is working fine but the problem is when i input 1 - 90 nothing appears but when i input 100 the output would be fine and that is "One Hundred" and so as 1000 the output would be "One Thousand". thanks for the help..

    Read the article

  • Does printf have side effects?

    - by martani_net
    I have an array of the following strcuture struct T_info { char capitale[255]; char pays[255]; char commentaire[255]; }; struct T_info *tableau; Then when I populate the array and call printf allone on each element it works fine strcpy(tab[line].capitale, buffer); strcpy(tab[line].pays, buffer); strcpy(tab[line].commentaire, buffer); printf("%s\n", tab[line].capitale); printf("%s\n", tab[line].pays); printf("%s\n", tab[line].commentaire); but if I call, printf("%s, %s, %s", tab[line].capitale, tab[line].pays, tab[line].commentaires) I get wrong resutts, is printf changing its parameters or what is going on?

    Read the article

  • Can printf change its parameters??

    - by martani_net
    EDIT: complete code with main is here http://codepad.org/79aLzj2H and once again this is were the weird behavious is happening for (i = 0; i<tab_size; i++) { //CORRECT OUTPUT printf("%s\n", tableau[i].capitale); printf("%s", tableau[i].pays); printf("%s\n", tableau[i].commentaire); //WRONG OUTPUT //printf("%s --- %s --- %s |\n", tableau[i].capitale, tableau[i].pays, tableau[i].commentaire); } I have an array of the following strcuture struct T_info { char capitale[255]; char pays[255]; char commentaire[255]; }; struct T_info *tableau; This is how the array is populated int advance(FILE *f) { char c; c = getc(f); if(c == '\n') return 0; while(c != EOF && (c == ' ' || c == '\t')) { c = getc(f); } return fseek(f, -1, SEEK_CUR); } int get_word(FILE *f, char * buffer) { char c; int count = 0; int space = 0; while((c = getc(f)) != EOF) { if (c == '\n') { buffer[count] = '\0'; return -2; } if ((c == ' ' || c == '\t') && space < 1) { buffer[count] = c; count ++; space++; } else { if (c != ' ' && c != '\t') { buffer[count] = c; count ++; space = 0; } else /* more than one space*/ { advance(f); break; } } } buffer[count] = '\0'; if(c == EOF) return -1; return count; } void fill_table(FILE *f,struct T_info *tab) { int line = 0, column = 0; fseek(f, 0, SEEK_SET); char buffer[MAX_LINE]; char c; int res; int i = 0; while((res = get_word(f, buffer)) != -999) { switch(column) { case 0: strcpy(tab[line].capitale, buffer); column++; break; case 1: strcpy(tab[line].pays, buffer); column++; break; default: strcpy(tab[line].commentaire, buffer); column++; break; } /*if I printf each one alone here, everything works ok*/ //last word in line if (res == -2) { if (column == 2) { strcpy(tab[line].commentaire, " "); } //wrong output here printf("%s -- %s -- %s\n", tab[line].capitale, tab[line].pays, tab[line].commentaire); column = 0; line++; continue; } column = column % 3; if (column == 0) { line++; } /*EOF reached*/ if(res == -1) return; } return ; } Edit : trying this printf("%s -- ", tab[line].capitale); printf("%s --", tab[line].pays); printf("%s --\n", tab[line].commentaire); gives me as result -- --abi -- Emirats arabes unis I expect to get Abu Dhabi -- Emirats arabes unis -- Am I missing something?

    Read the article

  • printf anomaly after "fork()"

    - by pechenie
    OS: Linux, Language: pure C I'm moving forward in learning C progpramming in general, and C programming under UNIX in a special case :D So, I detected a strange (as for me) behaviour of the printf() function after using a fork() call. Let's take a look at simple test program: #include <stdio.h> #include <system.h> int main() { int pid; printf( "Hello, my pid is %d", getpid() ); pid = fork(); if( pid == 0 ) { printf( "\nI was forked! :D" ); sleep( 3 ); } else { waitpid( pid, NULL, 0 ); printf( "\n%d was forked!", pid ); } return 0; } In this case the output looks like: Hello, my pid is 1111 I was forked! :DHello, my pid is 1111 2222 was forked! Why the second "Hello" string occured in the child's output? Yes, it is exactly what the parent printed on it's start, with the parent's pid. But! If we place '\n' character in the end of each string we got the expected output: #include <stdio.h> #include <system.h> int main() { int pid; printf( "Hello, my pid is %d\n", getpid() ); // SIC!! pid = fork(); if( pid == 0 ) { printf( "I was forked! :D" ); //removed the '\n', no matter sleep( 3 ); } else { waitpid( pid, NULL, 0 ); printf( "\n%d was forked!", pid ); } return 0; } And the output looks like: Hello, my pid is 1111 I was forked! :D 2222 was forked! Why does it happen? Is it ... ummm ... correct behaviour? Or it's a kind of the 'bug'?

    Read the article

  • Avoid trailing zeroes in printf()

    - by Gorpik
    I keep stumbling on the format specifiers for the printf() family of functions. What I want is to be able to print a double (or float) with a maximum given number of digits after the decimal point. If I use: printf("%1.3f", 359.01335); printf("%1.3f", 359.00999); I get 359.013 359.010 Instead of the desired 359.013 359.01 Can anybody help me?

    Read the article

  • When does printf("%s", char*) stop printing?

    - by remagen
    In my class we are writing our own copy of C's malloc() function. To test my code (which can currently allocate space fine) I was using: char* ptr = my_malloc(6*sizeof(char)); memcpy(ptr, "Hello\n", 6*sizeof(char)); printf("%s", ptr); The output would typically be this: Hello Unprintable character Some debugging figured that my code wasn't causing this per say, as ptr's memory is as follows: [24 bytes of meta info][Number of requested bytes][Padding] So I figured that printf was reaching into the padding, which is just garbage. So I ran a test of: printf("%s", "test\nd"); and got: test d Which makes me wonder, when DOES printf("%s", char*) stop printing chars?

    Read the article

  • C function changes behaviour depending on whether it has a call to printf in it

    - by Daniel
    I have a function that processes some data and finds the threshold that classifies the data with the lowest error. It looks like this: void find_threshold(FeatureVal* fvals, sampledata* data, unsigned int num_samples, double* thresh, double* err, int* pol) { //code to calculate minThresh, minErr, minPol omitted printf("minThresh: %f, minErr: %f, minPol: %d\n", minThresh, minErr, minPol); *thresh = minThresh; *err = minErr; *pol = minPol; } Then in my test file I have this: void test_find_threshold() { //code to set up test data omitted find_threshold(fvals, sdata, 6, &thresh, &err, &pol); printf("Expected 5 got %f\n", thresh); assert(eq(thresh, 5.0)); printf("Expected 1 got %d\n", pol); assert(pol == 1); printf("Expected 0 got %f\n", err); assert(eq(err, 0.0)); } This runs and the test passes with the following output: minThresh: 5.000000, minErr: 0.000000, minPol: 1 Expected 5 got 5.000000 Expected 1 got 1 Expected 0 got 0.000000 However if I remove the call to printf() from find_threshold, suddenly the test fails! Commenting out the asserts so that I can see what gets returned, the output is: Expected 5 got -15.000000 Expected 1 got -1 Expected 0 got 0.333333 I cannot make any sense of this whatsoever.

    Read the article

  • Centering strings with printf()

    - by Pieter
    By default, printf() seems to align strings to the right. printf("%10s %20s %20s\n", "col1", "col2", "col3"); /* col1 col2 col3 */ I can also align text to the left like this: printf("%-10s %-20s %-20s", "col1", "col2", "col3"); Is there a quick way to center text? Or do I have to write a function that turns a string like test into (space)(space)test(space)(space) if the text width for that column is 8?

    Read the article

  • Using stdint.h and ANSI printf?

    - by nn
    Hi, I'm writing a bignum library, and I want to use efficient data types to represent the digits. Particularly integer for the digit, and long (if strictly double the size of the integer) for intermediate representations when adding and multiplying. I will be using some C99 functionality, but trying to conform to ANSI C. Currently I have the following in my bignum library: #include <stdint.h> #if defined(__LP64__) || defined(__amd64) || defined(__x86_64) || defined(__amd64__) || defined(__amd64__) || defined(_LP64) typedef uint64_t u_w; typedef uint32_t u_hw; #define BIGNUM_DIGITS 2048 #define U_HW_BITS 16 #define U_W_BITS 32 #define U_HW_MAX UINT32_MAX #define U_HW_MIN UINT32_MIN #define U_W_MAX UINT64_MAX #define U_W_MIN UINT64_MIN #else typedef uint32_t u_w; typedef uint16_t u_hw; #define BIGNUM_DIGITS 4096 #define U_HW_BITS 16 #define U_W_BITS 32 #define U_HW_MAX UINT16_MAX #define U_HW_MIN UINT16_MIN #define U_W_MAX UINT32_MAX #define U_W_MIN UINT32_MIN #endif typedef struct bn { int sign; int n_digits; // #digits should exclude carry (digits = limbs) int carry; u_hw tab[BIGNUM_DIGITS]; } bn; As I haven't written a procedure to write the bignum in decimal, I have to analyze the intermediate array and printf the values of each digit. However I don't know which conversion specifier to use with printf. Preferably I would like to write to the terminal the digit encoded in hexadecimal. The underlying issue is, that I want two data types, one that is twice as long as the other, and further use them with printf using standard conversion specifiers. It would be ideal if int is 32bits and long is 64bits but I don't know how to guarantee this using a preprocessor, and when it comes time to use functions such as printf that solely rely on the standard types I no longer know what to use.

    Read the article

  • x86_64 printf segfault after brk call

    - by gmb11
    While i was trying do use brk (int 0x80 with 45 in %rax) to implement a simple memory manager program in assembly and print the blocks in order, i kept getting segfault. After a while i could only reproduce the error, but have no idea why is this happening: .section .data helloworld: .ascii "hello world" .section .text .globl _start _start: push %rbp mov %rsp, %rbp movq $45, %rax movq $0, %rbx #brk(0) should just return the current break of the programm int $0x80 #incq %rax #segfault #addq $1, %rax #segfault movq $0, %rax #works fine? #addq $1, %rax #segfault again? movq $helloworld, %rdi call printf movq $1, %rax #exit int $0x80 In the example here, if the commented lines are uncommented, i have a segfault, but some commands (like de movq $0, %rax) work just fine. In my other program, the first couple printf work, but the third crashes... Looking for other questions, i heard that printf sometimes allocates some memory, and that the brk shouldn't be used, because in this case it corrupts the heap or something... I'm very confused, does anyone know something about that? EDIT: I've just found out that for printf to work you need %rax=0.

    Read the article

  • No speed-up with useless printf's using OpenMP

    - by t2k32316
    I just wrote my first OpenMP program that parallelizes a simple for loop. I ran the code on my dual core machine and saw some speed up when going from 1 thread to 2 threads. However, I ran the same code on a school linux server and saw no speed-up. After trying different things, I finally realized that removing some useless printf statements caused the code to have significant speed-up. Below is the main part of the code that I parallelized: #pragma omp parallel for private(i) for(i = 2; i <= n; i++) { printf("useless statement"); prime[i-2] = is_prime(i); } I guess that the implementation of printf has significant overhead that OpenMP must be duplicating with each thread. What causes this overhead and why can OpenMP not overcome it?

    Read the article

  • Awk: how to have backclashes in printf?

    - by HH
    Works: awk '{print $$1"\t&\t"$$2"\t\\\\"}' .file > file.tex Does not work, why? awk '{printf %.2f"\t&\t"\.2f"\t\\\\",$$1,$$2}' .file > file.tex Error: awk: {printf %.2f"\t&\t"\.2f"\t\\\\",$1,$2} awk: ^ backslash not last character on line

    Read the article

  • Awk: error in printf but not in print

    - by HH
    Works: awk '{print $$1"\t&\t"$$2"\t\\\\"}' .file > file.tex Does not work, why? awk '{printf %.2f"\t&\t"\.2f"\t\\\\",$$1,$$2}' .file > file.tex Error: awk: {printf %.2f"\t&\t"\.2f"\t\\\\",$1,$2} awk: ^ backslash not last character on line

    Read the article

  • Python3k ctypes printf

    - by dsaccount1
    printf returns 1 instead of Hello World! which is the desired result. I googled it and think its because of the changes in the way sequences are treated. Python-3000 Possible py3k problem resolved. import ctypes msvcrt=ctypes.cdll.msvcrt string=b"Hello World!" msvcrt.printf("%s", string)

    Read the article

  • Unexpected result from printf

    - by Sandeep
    #include<stdio.h> int main() { printf("He %c llo",65); } Output: He A llo #include<stdio.h> int main() { printf("He %c llo",13); } Output: llo. It doesnt print He. I can understand that 65 is ascii value for A and hence A is printed in first case but why llo in second case. Thanks

    Read the article

  • Input/ Output alternatives for printf/scanf

    - by Nitish
    It may sound strange that knowing a lot about ios and haing some experience in .net, I am a newcomer to C. Somewhere I got this target to find average of n numbers without using printf and scanf. I don't want the code for the program but it will be really helpful if someone can help me with the alternatives to the mentioned functions. Please let me know if code with printf/scanf is required here. Also do let me know if my query stands invalid. Thanks and Regards, Nitish

    Read the article

  • How do I use multiple precisions in printf()?

    - by Tommy
    Looking at the information under the heading "Precision can be omitted or be any of:". The example: printf("%.*s", 3, "abcdef"); works, outputting:abc (truncating the rest of the string.) Now, I would like to have a string with multiple parameters formatted (truncated): printf("%.*s, %.*s", 3, 3, "abcdef", "xyz123"); but the program crashes. What is the correct syntax? Thank You.

    Read the article

  • Java printf using variable field size?

    - by paxdiablo
    I'm just trying to convert some C code over to Java and I'm having a little trouble with String.printf. In C, to get a specific width based on a variable, I can use: printf("Current index = %*d\n", sz, index); and it will format the integer to be a specific size based on sz. Trying: System.out.println(String.format("Current index = %*d\n", sz, index)); results in an error since it doesn't like the *. I currently have the following kludge: System.out.println(String.format("Current index = %" + sz + "d\n", index)); but I'm hoping there's a slightly better way, yes?

    Read the article

  • Formating with printf in using two functions

    - by user317203
    I am trying to output a document that looks like this. http://pastebin.com/dpBAY8Sb my issue is that I cannot find how to format the output I have to have a floating poing and format the distance between columns. My current code looks something like this. if (defined $longitude){ printf FILE ("%-8s %.6f","",$longitude); }else{ $longitude = ""; printf FILE ("%-20s ",$longitude); } but the extra "" throws off the whole column and it looks like this. pastebin.com/kcwHyNwb

    Read the article

1 2 3 4 5 6 7 8 9 10 11 12  | Next Page >