LinuxQuestions.org
Visit Jeremy's Blog.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 04-12-2018, 01:40 AM   #1
DRTGTBT
LQ Newbie
 
Registered: Apr 2018
Posts: 8

Rep: Reputation: Disabled
How to write c program


Hello
how to write c program in computer......
for adding two number...
 
Old 04-12-2018, 03:20 AM   #2
Michael Uplawski
Member
 
Registered: Dec 2015
Location: Normandy, France
Distribution: Debian buster/sid
Posts: 626
Blog Entries: 19

Rep: Reputation: 381Reputation: 381Reputation: 381Reputation: 381
ask teacher
 
2 members found this post helpful.
Old 04-12-2018, 04:01 AM   #3
DRTGTBT
LQ Newbie
 
Registered: Apr 2018
Posts: 8

Original Poster
Rep: Reputation: Disabled
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" ??
 
Old 04-12-2018, 04:24 AM   #4
Delcaran
LQ Newbie
 
Registered: Dec 2013
Location: ud.fvg.it
Distribution: Slackware64 14.2
Posts: 17

Rep: Reputation: 17
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.
 
1 members found this post helpful.
Old 04-12-2018, 09:25 AM   #5
BW-userx
LQ Guru
 
Registered: Sep 2013
Location: MID-SOUTH USA
Distribution: Slackware 14.2 / Slackware 14.2 current / Void Linux / Peppermint Eight
Posts: 5,932

Rep: Reputation: 1105Reputation: 1105Reputation: 1105Reputation: 1105Reputation: 1105Reputation: 1105Reputation: 1105Reputation: 1105Reputation: 1105
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.

Last edited by BW-userx; 04-12-2018 at 09:39 AM.
 
1 members found this post helpful.
Old 04-12-2018, 01:55 PM   #6
onebuck
Moderator
 
Registered: Jan 2005
Location: Midwest USA, Central Illinois
Distribution: Slackware®
Posts: 12,943
Blog Entries: 28

Rep: Reputation: 2234Reputation: 2234Reputation: 2234Reputation: 2234Reputation: 2234Reputation: 2234Reputation: 2234Reputation: 2234Reputation: 2234Reputation: 2234Reputation: 2234
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.
 
Old 04-12-2018, 02:17 PM   #7
dugan
LQ Guru
 
Registered: Nov 2003
Location: Canada
Distribution: Slackware
Posts: 8,033

Rep: Reputation: 3187Reputation: 3187Reputation: 3187Reputation: 3187Reputation: 3187Reputation: 3187Reputation: 3187Reputation: 3187Reputation: 3187Reputation: 3187Reputation: 3187
When you copy and paste code from somewhere, you need to link to the source.

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

Last edited by dugan; 04-12-2018 at 02:55 PM.
 
Old 04-13-2018, 03:27 AM   #8
Michael Uplawski
Member
 
Registered: Dec 2015
Location: Normandy, France
Distribution: Debian buster/sid
Posts: 626
Blog Entries: 19

Rep: Reputation: 381Reputation: 381Reputation: 381Reputation: 381
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.

Last edited by Michael Uplawski; 04-13-2018 at 03:50 AM. Reason: cosmetics. Actual call to ostream.
 
Old 04-13-2018, 07:44 AM   #9
rtmistler
Moderator
 
Registered: Mar 2011
Location: Sutton, MA. USA
Distribution: MINT Debian, Angstrom, SUSE, Ubuntu
Posts: 6,459
Blog Entries: 12

Rep: Reputation: 2220Reputation: 2220Reputation: 2220Reputation: 2220Reputation: 2220Reputation: 2220Reputation: 2220Reputation: 2220Reputation: 2220Reputation: 2220Reputation: 2220
Quote:
Originally Posted by DRTGTBT View Post
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.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Write a program about string! C program(in C++) Help!!!!!!!!!!!!!! Zoe Zhang Programming 2 04-06-2015 08:41 AM
C program(use C++) write a program about string Zoe Zhang Linux - Software 1 04-05-2015 11:40 AM
write a C++ program yewjingcho Programming 14 11-01-2007 06:05 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 09:23 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration