Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6717307
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T08:50:11+00:00 2026-05-26T08:50:11+00:00

This is my first C program. hello world! I’m sure this is no problem

  • 0

This is my first C program. hello world! I’m sure this is no problem for high school programmers these days, but they didn’t have programming when I was in high school. 🙂

I want to write to a serial port until the string I write is echoed back to me. Then do other stuff. My code below runs for a few seconds and then claims to see the string and ends, even when it could not have actually seen the string. It behaves the same no matter what so, I obviously have something very wrong.

Yes, the serial device /dev/kittens is real and, from a terminal, bash echoed strings to /dev/kittens are received(echoed) on the serial port when the port is looped.

I would be most appreciative to anyone who could correct my mistakes.

#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <fcntl.h>  
#include <errno.h> 
#include <termios.h> 


int fd;
char *buff;



int open_port(void)
{

 fd = open("/dev/kitens", O_RDWR | O_NOCTTY | O_NDELAY);
 if (fd == -1)
 {
  perror("open_port: Unable to open /dev/kittens ");
 }
  else
   fcntl(fd, F_SETFL, 0);

 return (fd);
}



int main()
{
int wr,rd;
open_port();

char msg[]="There are mice in the wire.\r";


do 
{
/* Read from the port */
fcntl(fd, F_SETFL, FNDELAY);
rd=read(fd,buff,sizeof(msg));

/* Write to the port */
wr = write(fd, msg, sizeof(msg));
printf("debugging - Wrote to port\n");   
usleep(10000);

if (wr < 0) {
    fputs("write() to port /dev/kittens failed!\n", stderr);
    break;
            }
 } while (buff != msg);

if (buff=msg)
printf(buff, "String Found! Now do the work.");
/* 
 system("dostuff.sh);
*/

/* Close the port on exit. */
close(fd);

return 0;
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T08:50:11+00:00Added an answer on May 26, 2026 at 8:50 am

    First,

    if (buff=msg)
    

    is assignment, not comparison 🙂 The latter is ==.

    Second,

    if (buff == msg)
    

    is actually pointer comparison, not string comparison. For string comparison, see strcmp() from C standard library.

    Third,

    char *buff;
    ...
    rd=read(fd,buff,sizeof(msg));
    

    buff is left uninitialized – there’s no memory allocated for it, so you’re happy enough it doesn’t crash at all.

    Well, there is more to check, but listed above is already enough to prevent the program from functioning correctly.

    As an advice: try to put a debugging printf below the read line to see what is actually read from the port. And remember, the data read from the port is not guaranteed to be zero-terminated (see zero-terminated strings for reference), so you have to watch for this also (either add a zero after the actual data, or somehow limit string operations on the buffer, like using strncmp() instead of strcmp()).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This is my first program I really created besides hello world. The code is
I have the following problem. Let's take the input (wikitext) ======hello((my first program)) world======
this is my very first android program, it's a modified hello world. I'm running
use this website a lot but first time posting. My program creates a number
problem euler #5 i found the solution but i don't know why this first
I'm learning AppleScript and my first program is a Hello World(of course!): display dialog
I have a compiling problem. I followed tutorial about Hello world program for Android
I have a problem running a simple Hello-world program in C++ on my Windows
I attempted to program my first app; Hello World and I already encountered a
Code first: '''this is main structure of my program''' from twisted.web import http from

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.