LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   How to write c program (https://www.linuxquestions.org/questions/programming-9/how-to-write-c-program-4175627494/)

DRTGTBT 04-12-2018 01:40 AM

How to write c program
 
Hello
how to write c program in computer......
for adding two number...

Michael Uplawski 04-12-2018 03:20 AM

ask teacher

DRTGTBT 04-12-2018 04:01 AM

Quote:

#include<stdio.h>

int main()
{
int a, b, c;

printf("Enter two numbers to add\n");
scanf("%d%d", &a, &b);

c = a + b;

printf("Sum of the numbers = %d\n", c);

return 0;
}
what is %d &a %d\n" ??

Delcaran 04-12-2018 04:24 AM

We will not do your homework.
Code:

man printf
will answer your question.

If you want to learn programming buy yourself a copy of Kernighan & Ritchie or go here.

FIRST study, THEN try, FINALLY ask questions.

BW-userx 04-12-2018 09:25 AM

1.
Code:

#include <stdio.h>

int main (void)
{
  printf("%d", 3+4);
  return 0;
}

2:
Code:

#include <stdio.h>
#include <stdlib.h>

int main (int argc, const char **argv)
{
 
  if ( argc < 3 )
  {
      printf("Nope, wrong number\n"
              "of arguments\n"
        "Enter two numbers\n");
      return EXIT_FAILURE;
  }

  printf ("%d\n", ( atoi(argv[1])+atoi(argv[2]) ) );

return 0;
}

compile.
Code:

gcc -Wall MyReallyCoolAndGrooveyAddsTwoNumbersProgram.c
//gets you
a.out
// to run it
./a.out

as you hopefully move further into this learning how to program. You'll find their is more than one way to skin a cat.

as @Delcaran stated, if this is homework, then I'd think the teacher would have covered on how to do this. The basics behind it anyways.

onebuck 04-12-2018 01:55 PM

Moderator response
 
Moved: This thread is more suitable in <Programming > and has been moved accordingly to help your thread/question get the exposure it deserves.

dugan 04-12-2018 02:17 PM

When you copy and paste code from somewhere, you need to link to the source.

https://www.programmingsimplified.co...dd-two-numbers

Michael Uplawski 04-13-2018 03:27 AM

As this all is completely useless, of course, here a c++ variant:
Code:

#include <iostream>
#include <stdlib.h>

using namespace std;
int main(int argc, char **argv) {
  system ("expr 1 + 1");
  cout << "\nokay then, here is something c++" << endl;
  return 0;
}

I am sorry. But I thought that all the mischief that I have been going through recently, must be a kind of punishment. As of now, at least, I am guilty of something. :p

rtmistler 04-13-2018 07:44 AM

Quote:

Originally Posted by DRTGTBT (Post 5842261)
what is %d &a %d\n" ??

https://en.wikipedia.org/wiki/Printf_format_string

Most of those are print format string modifiers.

The &a is the address of the variable "a" which you are giving to the scanf() function.

Sounds like you need to start with a hello world example, and get a full + complete description of every aspect of it before you try more complex examples.

Read this tutorial, https://www.codingunit.com/c-tutoria...am-hello-world it explains every aspect of each line and character in the hello world example.


All times are GMT -5. The time now is 05:01 AM.